Skip to content

Commit

Permalink
Rework of the app file store to be scoped per item
Browse files Browse the repository at this point in the history
- fix a few issue

Co-authored-by: Benjamin Charmes <[email protected]>
  • Loading branch information
ml-evs and BenjaminCharmes committed Oct 26, 2024
1 parent fd69be0 commit 2ca2e5f
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 80 deletions.
30 changes: 10 additions & 20 deletions webapp/src/components/FileList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,39 @@
<label class="mr-2">Files</label>
<div class="card">
<div id="filearea" class="card-body overflow-auto">
<div v-for="file_id in file_ids" :key="file_id" class="file-group">
<div v-for="(file, file_id) in stored_files" :key="file_id" class="file-group">
<a @click="deleteFile($event, file_id)">
<font-awesome-icon icon="times" fixed-width class="delete-file-button" />
</a>
<a
class="filelink"
target="_blank"
:href="`${$API_URL}/files/${file_id}/${stored_files[file_id].name}`"
>
{{ stored_files[file_id].name }}
<a class="filelink" target="_blank" :href="`${$API_URL}/files/${file_id}/${file.name}`">
{{ file.name }}
</a>
<font-awesome-icon
v-if="stored_files[file_id].is_live == true"
v-if="file.is_live == true"
v-show="true"
class="link-icon"
:icon="['fa', 'link']"
/>
<font-awesome-icon
v-else-if="stored_files[file_id].source_server_name != null"
v-else-if="file.source_server_name != null"
v-show="true"
class="unlink-icon"
:icon="['fa', 'unlink']"
/>
<span v-if="stored_files[file_id].source_server_name != null">
<span v-if="file.source_server_name != null">
<span class="server-name">
<font-awesome-icon :icon="['fas', 'hdd']" class="toplevel-icon" />
{{ stored_files[file_id].source_server_name }}
{{ file.source_server_name }}
</span>
<span class="last-updated-text">
(updated
{{
formatDistance(new Date(stored_files[file_id].last_modified_remote), new Date(), {
formatDistance(new Date(file.last_modified_remote), new Date(), {
addSuffix: true,
})
}}, last synced
{{
formatDistance(new Date(stored_files[file_id].last_modified), new Date(), {
formatDistance(new Date(file.last_modified), new Date(), {
addSuffix: true,
})
}})
Expand All @@ -48,7 +44,7 @@
<span v-else class="last-updated-text">
(uploaded
{{
formatDistance(new Date(stored_files[file_id].last_modified), new Date(), {
formatDistance(new Date(file.last_modified), new Date(), {
addSuffix: true,
})
}})
Expand Down Expand Up @@ -81,10 +77,6 @@ export default {
type: String,
required: true,
},
file_ids: {
type: Array,
default: () => [],
},
stored_files: {
type: Object,
default: () => ({}),
Expand All @@ -98,8 +90,6 @@ export default {
methods: {
formatDistance,
deleteFile(event, file_id) {
console.log(`delete file button clicked!`);
console.log(event);
deleteFileFromSample(this.item_id, file_id);
return false;
},
Expand Down
11 changes: 7 additions & 4 deletions webapp/src/components/FileSelectDropdown.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<label class="mr-4"><b>Select a file:</b></label>
<select class="form-control file-select-dropdown" :value="modelValue" @input="handleInput">
<option v-for="file_id in available_file_ids" :key="file_id" :value="file_id">
{{ all_files[file_id].name }}
{{ all_files_name(file_id) }}
</option>
<option disabled>
Accepted filetypes:
Expand Down Expand Up @@ -43,14 +43,14 @@ export default {
emits: ["update:modelValue"],
computed: {
all_files() {
return this.$store.state.files;
return this.$store.state.all_item_data[this.item_id].files;
},
available_file_ids() {
let sample_files = this.$store.state.all_item_data[this.item_id].file_ObjectIds;
return sample_files.filter((file_id) => {
let filename = this.all_files[file_id].name;
let filename = this.all_files_name(file_id);
return this.extensions
.map((extension) => filename.endsWith(extension))
.map((extension) => filename?.endsWith(extension))
.some((element) => element); // check if the extension is any of the extensions
});
},
Expand All @@ -65,6 +65,9 @@ export default {
},
},
methods: {
all_files_name(file_id) {
return this.all_files.find((file) => file.immutable_id === file_id)?.name || "File not found";
},
handleInput(event) {
this.$emit("update:modelValue", event.target.value);
if (this.updateBlockOnChange) {
Expand Down
31 changes: 5 additions & 26 deletions webapp/src/file_upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import { API_URL, UPPY_MAX_NUMBER_OF_FILES, UPPY_MAX_TOTAL_FILE_SIZE } from "@/r
// file-upload loaded

export default function setupUppy(item_id, trigger_selector, reactive_file_list) {
console.log("setupUppy called with: " + trigger_selector);
var uppy = new Uppy({
restrictions: {
// Somewhat arbitrary restrictions that prevent numbers that would break the server in one go -- the API should also refuse files when 'full'
Expand All @@ -26,36 +25,21 @@ export default function setupUppy(item_id, trigger_selector, reactive_file_list)
inline: false,
trigger: trigger_selector,
close_after_finish: true,
metaFields: [
{
id: "blahblah",
name: "Blah",
placeholder: "Metadata can be added here",
},
],
})
.use(Webcam, { target: Dashboard })
.use(XHRUpload, {
//someday, try to upgrade this to Tus
//someday, try to upgrade this to Tus for resumable uploads
headers: headers,
endpoint: `${API_URL}/upload-file/`,
FormData: true, // send as form
fieldName: "files[]", // default, think about whether or not to change this
showProgressDetails: true,
withCredentials: true,
// getResponseData(responseText, response) {
// console.log("Uppy response text:")
// console.log(responseText)
// console.log("Uppy response:")
// console.log(response)
// }
});

uppy.on("file-added", (file) => {
console.log("searching for matching files for: " + file.name);
var matching_file_id = null;
for (const file_id in reactive_file_list) {
// console.log(`evaluating ${reactive_file_list[file_id].name} vs ${file.name}`)
if (reactive_file_list[file_id].name == file.name) {
alert(
"A file with this name already exists in the sample. If you upload this, it will be duplicated on the current item.",
Expand All @@ -71,21 +55,16 @@ export default function setupUppy(item_id, trigger_selector, reactive_file_list)
});

uppy.on("complete", function (result) {
console.log("Upload complete! We’ve uploaded these files:", result.successful);
console.log("There were errors with these files:", result.failed);
result.successful.forEach(function (f) {
console.log("file upload response:");
console.log(f.response);
var response_body = f.response.body;
store.commit("updateFiles", {
[response_body.file_id]: response_body.file_information,
});
console.log("Added file to store. Response_body:");
console.log(response_body);
if (!response_body.is_update) {
store.commit("addFileToSample", {
item_id: item_id,
file_id: response_body.file_id,
file_info: {
...response_body.file_information,
immutable_id: response_body.file_information.immutable_id.$oid,
},
});
}
});
Expand Down
21 changes: 6 additions & 15 deletions webapp/src/server_fetch_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,6 @@ export async function getItemData(item_id) {
child_items: response_json.child_items,
parent_items: response_json.parent_items,
});
store.commit("updateFiles", response_json.files_data);

return "success";
})
Expand All @@ -440,8 +439,6 @@ export async function getItemByRefcode(refcode) {
child_items: response_json.child_items,
parent_items: response_json.parent_items,
});
store.commit("updateFiles", response_json.files_data);

return "success";
})
.catch((error) => alert("Error getting item data: " + error));
Expand Down Expand Up @@ -602,19 +599,15 @@ export function deleteBlock(item_id, block_id) {
}

export function deleteFileFromSample(item_id, file_id) {
console.log("deleteFileFromSample called with item_id and file_id:");
console.log(item_id);
console.log(file_id);
fetch_post(`${API_URL}/delete-file-from-sample/`, {
item_id: item_id,
file_id: file_id,
})
.then(function (response_json) {
.then(function () {
store.commit("removeFileFromSample", {
item_id: item_id,
file_id: file_id,
});
store.commit("updateFiles", response_json.new_file_obj);
})
.catch((error) => alert(`Delete unsuccessful :(\n error: ${error}`));
}
Expand All @@ -641,22 +634,20 @@ export async function fetchRemoteTree(invalidate_cache) {
}

export async function addRemoteFileToSample(file_entry, item_id) {
console.log("loadSelectedRemoteFiles");
return fetch_post(`${API_URL}/add-remote-file-to-sample/`, {
file_entry: file_entry,
item_id: item_id,
})
.then(function (response_json) {
//handle response
console.log("received remote sample!");
console.log(response_json);
store.commit("updateFiles", {
[response_json.file_id]: response_json.file_information,
});
//store.commit("updateFiles", {
// [response_json.file_id]: response_json.file_information,
//});
if (!response_json.is_update) {
console.log("Adding file to sample", response_json.file_information);
store.commit("addFileToSample", {
item_id: item_id,
file_id: response_json.file_id,
file_info: response_json.file_information,
});
}
})
Expand Down
16 changes: 8 additions & 8 deletions webapp/src/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ export default createStore({
updatingDelayed: {},
remoteDirectoryTree: {},
remoteDirectoryTreeSecondsSinceLastUpdate: null,
files: {},
itemGraphData: null,
remoteDirectoryTreeIsLoading: false,
fileSelectModalIsOpen: false,
Expand Down Expand Up @@ -134,19 +133,20 @@ export default createStore({
setCollectionSampleList(state, payload) {
state.all_collection_children[payload.collection_id] = payload.child_items;
},
updateFiles(state, files_data) {
// payload should be an object with file ids as key and file data as values
// Note: this will overwrite any entries with the same file_ids
Object.assign(state.files, files_data);
},
addFileToSample(state, payload) {
state.all_item_data[payload.item_id].file_ObjectIds.push(payload.file_id);
console.log("adding file to sample with:", payload.file_info);
state.all_item_data[payload.item_id].files.push(payload.file_info);
},
removeFileFromSample(state, payload) {
var file_ids = state.all_item_data[payload.item_id].file_ObjectIds;
const index = file_ids.indexOf(payload.file_id);
const { item_id, file_id } = payload;
const files = state.all_item_data[item_id].files;
const file_ids = state.all_item_data[item_id].file_ObjectIds;
const index = files.findIndex((file) => file.immutable_id === file_id);

if (index > -1) {
file_ids.splice(index, 1);
files.splice(index, 1);
}
},
setRemoteDirectoryTree(state, remoteDirectoryTree) {
Expand Down
11 changes: 4 additions & 7 deletions webapp/src/views/EditPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
<div class="editor-body">
<component :is="itemTypeEntry?.itemInformationComponent" :item_id="item_id" />

<FileList :item_id="item_id" :file_ids="file_ids" :stored_files="stored_files" />
<FileList :item_id="item_id" :stored_files="stored_files" />

<div class="container">
<hr />
Expand Down Expand Up @@ -179,13 +179,10 @@ export default {
return allBlocksAreSaved && this.$store.state.saved_status_items[this.item_id];
},
files() {
return this.item_data.files;
},
file_ids() {
return this.item_data.file_ObjectIds;
return this.item_data.files || [];
},
stored_files() {
return this.$store.state.files;
return Object.fromEntries(this.files.map((file) => [file.immutable_id, file]));
},
blocksInfos() {
return this.$store.state.blocksInfos;
Expand Down Expand Up @@ -227,7 +224,7 @@ export default {
// this.updateRemoteTree()
// setup the uppy instsance
setupUppy(this.item_id, "#uppy-trigger", this.stored_files);
setupUppy(this.item_id, "#uppy-trigger", {});
},
beforeUnmount() {
document.removeEventListener("keydown", this._keyListener);
Expand Down

0 comments on commit 2ca2e5f

Please sign in to comment.