Skip to content

Commit e4784a5

Browse files
committed
Remove files when resource is deleted #851
1 parent 082b838 commit e4784a5

File tree

3 files changed

+63
-40
lines changed

3 files changed

+63
-40
lines changed

browser/data-browser/src/locales/en.po

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,17 +1158,14 @@ msgid "Set {0} as current drive"
11581158
msgstr "Set {0} as current drive"
11591159

11601160
#: src/views/Drive/DrivePage.tsx
1161-
#: src/views/DrivePage.tsx
11621161
msgid "Set as current drive"
11631162
msgstr "Set as current drive"
11641163

11651164
#: src/views/Drive/DrivePage.tsx
1166-
#: src/views/DrivePage.tsx
11671165
msgid "Default Ontology"
11681166
msgstr "Default Ontology"
11691167

11701168
#: src/views/Drive/DrivePage.tsx
1171-
#: src/views/DrivePage.tsx
11721169
msgid ""
11731170
"You are running Atomic-Server on `localhost`, which means that it\n"
11741171
"will not be available from any other machine than your current local\n"
@@ -3180,23 +3177,11 @@ msgstr "Plugins"
31803177
msgid "Add Plugin"
31813178
msgstr "Add Plugin"
31823179

3183-
#~ msgid "<0/> Add Plugin"
3184-
#~ msgstr "<0/> Add Plugin"
3185-
3186-
#~ msgid "New Plugin"
3187-
#~ msgstr "New Plugin"
3188-
31893180
#: src/views/Drive/NewPluginButton.tsx
31903181
#: src/views/Drive/NewPluginButton.tsx
31913182
msgid "<0/> Upload Plugin"
31923183
msgstr "<0/> Upload Plugin"
31933184

3194-
#~ msgid "Invalid plugin zip file. It must contain plugin.wasm and plugin.json at the root, and optionally an assets folder."
3195-
#~ msgstr "Invalid plugin zip file. It must contain plugin.wasm and plugin.json at the root, and optionally an assets folder."
3196-
3197-
#~ msgid "Invalid plugin zip file."
3198-
#~ msgstr "Invalid plugin zip file."
3199-
32003185
#. placeholder {0}: metadata.version
32013186
#. placeholder {0}: resource.props.version
32023187
#: src/views/Drive/NewPluginButton.tsx
@@ -3211,9 +3196,6 @@ msgstr "v{0}"
32113196
msgid "by {0}"
32123197
msgstr "by {0}"
32133198

3214-
#~ msgid "Configure"
3215-
#~ msgstr "Configure"
3216-
32173199
#: src/routes/SettingsAgent.tsx
32183200
msgid "Cannot fill subject and privatekey fields."
32193201
msgstr "Cannot fill subject and privatekey fields."
@@ -3315,9 +3297,6 @@ msgstr "Invalid URI"
33153297
msgid "Content saved"
33163298
msgstr "Content saved"
33173299

3318-
#~ msgid "Config <0/>"
3319-
#~ msgstr "Config <0/>"
3320-
33213300
#: src/views/Drive/NewPluginButton.tsx
33223301
#: src/views/Plugin/PluginPage.tsx
33233302
msgid "Config"
@@ -3343,33 +3322,15 @@ msgstr "Install"
33433322
msgid "Please fill in all fields"
33443323
msgstr "Please fill in all fields"
33453324

3346-
#~ msgid "Failed to create plugin resource, error: {0}"
3347-
#~ msgstr "Failed to create plugin resource, error: {0}"
3348-
33493325
#. placeholder {0}: err.message
33503326
#: src/views/Drive/NewPluginButton.tsx
33513327
msgid "Failed to install plugin, error: {0}"
33523328
msgstr "Failed to install plugin, error: {0}"
33533329

3354-
#~ msgid "Go to plugin"
3355-
#~ msgstr "Go to plugin"
3356-
3357-
#~ msgid "<0/>Save"
3358-
#~ msgstr "<0/>Save"
3359-
3360-
#~ msgid "Update"
3361-
#~ msgstr "Update"
3362-
33633330
#: src/views/Plugin/PluginPage.tsx
33643331
msgid "Uninstall"
33653332
msgstr "Uninstall"
33663333

3367-
#~ msgid "<0/>Update"
3368-
#~ msgstr "<0/>Update"
3369-
3370-
#~ msgid "<0/>Uninstall"
3371-
#~ msgstr "<0/>Uninstall"
3372-
33733334
#: src/views/Plugin/PluginPage.tsx
33743335
msgid "<0/> Update"
33753336
msgstr "<0/> Update"

server/src/appstate.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ impl AppState {
6262
store.add_class_extender(plugins::plugin::build_plugin_extender(
6363
config.plugin_path.clone(),
6464
))?;
65+
store.add_class_extender(plugins::files::build_file_extender(
66+
config.uploads_path.clone(),
67+
))?;
6568

6669
// Register all built-in endpoints
6770
store.add_endpoint(plugins::versioning::version_endpoint())?;

server/src/plugins/files.rs

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
use atomic_lib::{endpoints::Endpoint, urls};
1+
use std::path::PathBuf;
2+
3+
use atomic_lib::{
4+
class_extender::{BoxFuture, ClassExtender, ClassExtenderScope, CommitExtenderContext},
5+
endpoints::Endpoint,
6+
errors::AtomicResult,
7+
urls, AtomicError, Storelike, Value,
8+
};
29

310
pub fn upload_endpoint() -> Endpoint {
411
Endpoint {
@@ -33,3 +40,55 @@ The following query parameters are available:
3340
handle_post: None,
3441
}
3542
}
43+
44+
// Removes the file from the filesystem after the resource has been deleted.
45+
fn on_after_commit(
46+
context: CommitExtenderContext,
47+
uploads_dir: PathBuf,
48+
) -> BoxFuture<AtomicResult<()>> {
49+
Box::pin(async move {
50+
let CommitExtenderContext {
51+
store,
52+
commit,
53+
resource,
54+
} = context;
55+
56+
if commit.destroy != Some(true) {
57+
return Ok(());
58+
}
59+
60+
let Ok(Value::String(file_name)) = resource.get(urls::INTERNAL_ID) else {
61+
return Ok(());
62+
};
63+
64+
let correct_subject = format!("{}/files/{}", store.get_server_url()?, file_name);
65+
66+
if resource.get_subject() != &correct_subject {
67+
return Err(AtomicError::from(format!(
68+
"Internal ID {} does not match resource subject {}",
69+
file_name,
70+
resource.get_subject()
71+
)));
72+
}
73+
74+
let file_path = uploads_dir.join(file_name);
75+
if file_path.exists() {
76+
std::fs::remove_file(file_path)?;
77+
}
78+
79+
Ok(())
80+
})
81+
}
82+
83+
pub fn build_file_extender(uploads_dir: PathBuf) -> ClassExtender {
84+
ClassExtender {
85+
id: Some("file".to_string()),
86+
classes: vec![urls::FILE.to_string()],
87+
on_resource_get: None,
88+
before_commit: None,
89+
after_commit: Some(ClassExtender::wrap_commit_handler(move |context| {
90+
on_after_commit(context, uploads_dir.clone())
91+
})),
92+
scope: ClassExtenderScope::Global,
93+
}
94+
}

0 commit comments

Comments
 (0)