From bd57f243e394edaffe3582f00c0763612862865e Mon Sep 17 00:00:00 2001 From: Sonja Happ Date: Wed, 13 May 2020 15:20:32 +0200 Subject: [PATCH] downloaded files are stored in file-store, objectURL is created upon download #147 --- src/file/file-store.js | 46 ++++++++++++++++++++-------------- src/file/files-data-manager.js | 17 +++++++++++++ 2 files changed, 44 insertions(+), 19 deletions(-) diff --git a/src/file/file-store.js b/src/file/file-store.js index 7ab9f9b..cb0d167 100644 --- a/src/file/file-store.js +++ b/src/file/file-store.js @@ -25,21 +25,34 @@ class FileStore extends ArrayStore { saveFile(state, action){ - // save file data file - let fileID = parseInt(action.data.id) - console.log("Received file", action); - for (let f of state){ - if (f.id === fileID){ - f["data"] = action.data.data; - f.type = action.data.type; - console.log("Saving file data to file id", fileID); - } - } + let fileID = parseInt(action.id) + state.forEach((element, index, array) => { + if (element.id === fileID) { + // save file object + array[index]["data"] = new File([action.data.data], element.name, {type: action.data.type}); + // update file type + array[index]["type"] = action.data.type; + if (array[index]["objectURL"] !== ''){ + // free memory of previously generated object URL + URL.revokeObjectURL(array[index]["objectURL"]); + } + // create an object URL for the file + array[index]["objectURL"] = URL.createObjectURL(array[index]["data"]) + } + }); + + // announce change to listeners + this.__emitChange(); + return state } reduce(state, action) { switch (action.type) { + case 'files/start-download': + FilesDataManager.download(action) + return state + case 'files/start-upload': FilesDataManager.upload(action.data, action.token, action.progressCallback, action.finishedCallback, action.objectType, action.objectID); return state; @@ -51,15 +64,10 @@ class FileStore extends ArrayStore { case 'files/upload-error': console.log(action.error); return state; - case 'files/loaded': - if (Array.isArray(action.data)) { - return super.reduce(state, action) - } else { - // in this case a file is contained in the response (no JSON) - // TODO we have to extract and process the file here (=save it somewhere?) - this.saveFile(state, action) - return super.reduce(state, action) - } + + case 'files/downloaded': + // in this case a file is contained in the response (no JSON) + return this.saveFile(state, action) default: return super.reduce(state, action); diff --git a/src/file/files-data-manager.js b/src/file/files-data-manager.js index 5758b62..1a61033 100644 --- a/src/file/files-data-manager.js +++ b/src/file/files-data-manager.js @@ -48,6 +48,23 @@ class FilesDataManager extends RestDataManager { }); }); } + + download(action){ + RestAPI.download(this.makeURL(this.url), action.token, action.data).then(response => { + AppDispatcher.dispatch({ + type: 'files/downloaded', + data: response, + id: action.data, + token: action.token + }); + + }).catch(error => { + AppDispatcher.dispatch({ + type: 'files/load-error', + error: error + }); + }); + } } export default new FilesDataManager();