Loading src/components/JamaUploadView.vue +3 −79 Original line number Diff line number Diff line <template> <v-container> <div ref="rootView"> <div v-if="!isUploading"> <template v-if="Object.keys(uncompletedUploads).length > 0"> <!-- <v-btn--> <!-- class="mr-2"--> <!-- small--> <!-- fab--> <!-- color="blue"--> <!-- title="Mettre à jour la liste"--> <!-- @click="updateUncompleteUploads()"--> <!-- >--> <!-- <v-icon>mdi-refresh</v-icon>--> <!-- </v-btn>--> <v-btn small fab color="warning" title="Vider la liste" @click="empty()" > <v-icon>mdi-delete</v-icon> </v-btn> </template> <span v-else>Aucun upload en cours. </span> </div> <v-container v-else> <v-container v-if="isUploading"> <h3> Envoi en cours | {{ done + 1 }}/{{ titleInfos }} </h3> Loading @@ -45,33 +21,7 @@ </v-col> </v-row> </v-container> <v-list v-if="!isUploading"> <v-list-item v-for="(upload, id) in uncompletedUploads" :key="'uncomplete-'+id" > <v-row> <v-col cols="6"> {{ upload.name }} : {{ Math.min(Math.round(Number(upload.available_chunks.length)/Number(upload.nb_chunks) * 100),100) }}% </v-col> <v-col cols="6"> <v-btn small class="ml-5" @click="resumeSelectFile('resume-input-'+id)" > Reprendre </v-btn> <input :id="'resume-input-'+id" type="file" style="display: none" @change="resume('resume-input-'+id, upload)" > </v-col> </v-row> </v-list-item> </v-list> <span v-else>Aucun upload en cours. </span> </div> </v-container> </template> Loading @@ -82,7 +32,6 @@ import {upload_events} from '../utils/JamaClient'; import {mapGetters} from "vuex"; import Misc from "../utils/Misc"; export default { name: "JamaUploadView", Loading @@ -106,17 +55,10 @@ export default { ...mapGetters(["currentProjectId"]), uncompletedUploads(){ return this.$store.getters['localStorageStore/uploads'] return [];//this.$store.getters['localStorageStore/uploads'] } }, // beforeMount() { // window.onbeforeunload = () => { // // //localStorage.setItem(JAMA_LSTORAGE_INCOMPLETED_KEY, JSON.stringify(this.uncompletedUploads)); // //this.updateUncompleteUploads(); // } // }, mounted() { //listening upload events Loading Loading @@ -151,14 +93,12 @@ export default { this.maxProgressValue = Number(e.detail.nb_chunks); let upload = e.detail; upload.available_chunks = [];//unknown here this.$store.commit('localStorageStore/updateUpload', upload); }, endFileUploadEvent(e) { this.done ++; if(e.detail.ok){ this.$alertInfo(e.detail.message); this.$store.commit('localStorageStore/removeUploadById', e.detail.id); } else { this.$alertError(e.detail.message); Loading @@ -171,28 +111,12 @@ export default { chunkUploadEvent(e) { this.progressMessage = e.detail.name; this.progressValue++; const chunkName = e.detail.nb_chunks + '-' + Misc.lpad(Number(e.detail.chunk) + 1, e.detail.nb_chunks.toString().length) + '.part'; this.$store.commit('localStorageStore/updateUploadChunks', {id : e.detail.id, chunk :chunkName}) }, resumeUploadEvent() { this.isUploading = true; }, resumeSelectFile(inputId) { document.getElementById(inputId).click(); }, resume(inputId, upload) { console.log(inputId, upload); this.progressValue = upload.available_chunks.length; this.maxProgressValue = Number(upload.nb_chunks); this.$emit('resume-resource-upload', upload, document.getElementById(inputId).files[0]) }, empty() { this.$store.commit('localStorageStore/clearUploads'); }, } } Loading src/store/localStorageStore.js +0 −32 Original line number Diff line number Diff line Loading @@ -13,9 +13,6 @@ const localStorageStore = { treeDrawer: localStorage.getItem(LOCAL_STORAGE_APP_ID + '.treeDrawer') ? localStorage.getItem(LOCAL_STORAGE_APP_ID + '.treeDrawer') === 'true' : true, uploads: localStorage.getItem(LOCAL_STORAGE_APP_ID + '.uploads') ? JSON.parse(localStorage.getItem(LOCAL_STORAGE_APP_ID + '.uploads')) : {} }), getters: { Loading @@ -37,35 +34,6 @@ const localStorageStore = { localStorage.setItem(LOCAL_STORAGE_APP_ID + '.treeDrawer', b); }, updateUpload: (state, upload) => { let uploads = state.uploads; uploads[upload.id] = {...uploads[upload.id], ...upload}; state.uploads = {...uploads}; localStorage.setItem(LOCAL_STORAGE_APP_ID + '.uploads', JSON.stringify(state.uploads)); }, updateUploadChunks: (state, {id, chunk}) => { let uploads = state.uploads; let upload = uploads[id]; upload.available_chunks.push(chunk); uploads[upload.id] = {...uploads[upload.id], ...upload}; state.uploads = {...uploads}; localStorage.setItem(LOCAL_STORAGE_APP_ID + '.uploads', JSON.stringify(state.uploads)); }, removeUploadById(state, uploadId) { console.log('removing upload because completed ', uploadId) delete state.uploads[uploadId]; // let uploads = state.uploads.filter((u) => u.id !== uploadId); // state.uploads = {...uploads}; localStorage.setItem(LOCAL_STORAGE_APP_ID + '.uploads', JSON.stringify(state.uploads)); }, clearUploads(state) { state.uploads = {}; localStorage.setItem(LOCAL_STORAGE_APP_ID + '.uploads', JSON.stringify(state.uploads)); }, }, actions: { resetView(context) { Loading src/utils/ChecksumService.js +2 −0 Original line number Diff line number Diff line Loading @@ -23,6 +23,7 @@ export default class ChecksumService { } async _sha256(file) { console.log("START SHA256") let hasher = new hash256(); const sliceSize = 8192; // 8ko let start = 0; Loading @@ -32,6 +33,7 @@ export default class ChecksumService { start += sliceSize; } hasher.finish(); console.log("END SHA256 - " + hasher.result) return toHexString(hasher.result); } Loading src/utils/JamaClient.js +18 −36 Original line number Diff line number Diff line import JamacRpcClient from './JamaRpcClient'; import ChecksumService from '../utils/ChecksumService'; import Misc from "./Misc"; Loading Loading @@ -68,14 +67,6 @@ class ChunksUploader { return this.id; } // setChecksum(cs) { // this.checksum = cs; // } // // getChecksum(){ // return this.checksum; // } getFile() { return this.file; } Loading @@ -102,7 +93,6 @@ class JamaClient extends JamacRpcClient { constructor(endPoint = '', apiKey = '', rpcMethodPrefix = '') { super(endPoint, apiKey, rpcMethodPrefix); this.checksumService = new ChecksumService(); this.listeners = []; //this.fileChunksUploader = null; } Loading @@ -118,6 +108,7 @@ class JamaClient extends JamacRpcClient { } async uploadChunk(fileChunksUploader, chunkNum, uploadId, path, projectId, attempts = 0) { console.info("Will upload chunk " + chunkNum + '/' + fileChunksUploader.getNbChunks()) try { let response = await fileChunksUploader.uploadChunk(chunkNum, path, projectId); this.dispatchEvent(new CustomEvent(upload_events.chunk_uploaded_event, { Loading Loading @@ -177,7 +168,6 @@ class JamaClient extends JamacRpcClient { async uploadOrResumeFile(file, path, projectId, uploadInfos) { let promises = []; uploadInfos = uploadInfos || {}; let resourceId = uploadInfos ? uploadInfos.resourceId : null; let uploadId = null; Loading @@ -185,11 +175,9 @@ class JamaClient extends JamacRpcClient { if (uploadInfos.id) {//resume file upload fileChunksUploader = new ChunksUploader(file, this.endPoint, this.apiKey, uploadInfos.id); uploadId = uploadInfos.id; promises.push(ChecksumService.sha256(file)); this.dispatchEvent(new CustomEvent(upload_events.resume_upload_event), {detail: {id: uploadId}}); } else {//start file upload - need a uniqid uploadId = Misc.makeid(64); promises.push(ChecksumService.sha256(file)); fileChunksUploader = new ChunksUploader(file, this.endPoint, this.apiKey, uploadId); this.dispatchEvent(new CustomEvent(upload_events.start_file_upload_event, { 'detail': { Loading Loading @@ -219,16 +207,10 @@ class JamaClient extends JamacRpcClient { } } } promises.push(this.resource(Number(resourceId)));//fetch resource to get its remote hash let promisesValues = await Promise.all(promises);//wait end of upload and end of local hash compute //must compare local and remote hash let localHash = promisesValues[0]; let remoteHash = promisesValues[1].hash; return { id: uploadId, resourceId: resourceId, ok: localHash === remoteHash } } Loading Loading @@ -260,8 +242,8 @@ class JamaClient extends JamacRpcClient { } async manageUploadFileResult(file, uploadResult) { let result = null; if (uploadResult.ok) { // let result = null; // if (uploadResult.ok) { let result = { ok: true, name: file.name, Loading @@ -273,20 +255,20 @@ class JamaClient extends JamacRpcClient { 'detail': result }) ); } else { let errorMessage = "Une erreur est survenue lors du versement du fichier " + file.name; let result = { ok: false, status: 'error', message: errorMessage, name: file.name, hash: uploadResult.hash } this.dispatchEvent(new CustomEvent(upload_events.end_file_upload_event, { 'detail': result }) ); } // } else { // let errorMessage = "Une erreur est survenue lors du versement du fichier " + file.name; // let result = { // ok: false, // status: 'error', // message: errorMessage, // name: file.name, // hash: uploadResult.hash // } // this.dispatchEvent(new CustomEvent(upload_events.end_file_upload_event, { // 'detail': result // }) // ); // } return result; } Loading Loading
src/components/JamaUploadView.vue +3 −79 Original line number Diff line number Diff line <template> <v-container> <div ref="rootView"> <div v-if="!isUploading"> <template v-if="Object.keys(uncompletedUploads).length > 0"> <!-- <v-btn--> <!-- class="mr-2"--> <!-- small--> <!-- fab--> <!-- color="blue"--> <!-- title="Mettre à jour la liste"--> <!-- @click="updateUncompleteUploads()"--> <!-- >--> <!-- <v-icon>mdi-refresh</v-icon>--> <!-- </v-btn>--> <v-btn small fab color="warning" title="Vider la liste" @click="empty()" > <v-icon>mdi-delete</v-icon> </v-btn> </template> <span v-else>Aucun upload en cours. </span> </div> <v-container v-else> <v-container v-if="isUploading"> <h3> Envoi en cours | {{ done + 1 }}/{{ titleInfos }} </h3> Loading @@ -45,33 +21,7 @@ </v-col> </v-row> </v-container> <v-list v-if="!isUploading"> <v-list-item v-for="(upload, id) in uncompletedUploads" :key="'uncomplete-'+id" > <v-row> <v-col cols="6"> {{ upload.name }} : {{ Math.min(Math.round(Number(upload.available_chunks.length)/Number(upload.nb_chunks) * 100),100) }}% </v-col> <v-col cols="6"> <v-btn small class="ml-5" @click="resumeSelectFile('resume-input-'+id)" > Reprendre </v-btn> <input :id="'resume-input-'+id" type="file" style="display: none" @change="resume('resume-input-'+id, upload)" > </v-col> </v-row> </v-list-item> </v-list> <span v-else>Aucun upload en cours. </span> </div> </v-container> </template> Loading @@ -82,7 +32,6 @@ import {upload_events} from '../utils/JamaClient'; import {mapGetters} from "vuex"; import Misc from "../utils/Misc"; export default { name: "JamaUploadView", Loading @@ -106,17 +55,10 @@ export default { ...mapGetters(["currentProjectId"]), uncompletedUploads(){ return this.$store.getters['localStorageStore/uploads'] return [];//this.$store.getters['localStorageStore/uploads'] } }, // beforeMount() { // window.onbeforeunload = () => { // // //localStorage.setItem(JAMA_LSTORAGE_INCOMPLETED_KEY, JSON.stringify(this.uncompletedUploads)); // //this.updateUncompleteUploads(); // } // }, mounted() { //listening upload events Loading Loading @@ -151,14 +93,12 @@ export default { this.maxProgressValue = Number(e.detail.nb_chunks); let upload = e.detail; upload.available_chunks = [];//unknown here this.$store.commit('localStorageStore/updateUpload', upload); }, endFileUploadEvent(e) { this.done ++; if(e.detail.ok){ this.$alertInfo(e.detail.message); this.$store.commit('localStorageStore/removeUploadById', e.detail.id); } else { this.$alertError(e.detail.message); Loading @@ -171,28 +111,12 @@ export default { chunkUploadEvent(e) { this.progressMessage = e.detail.name; this.progressValue++; const chunkName = e.detail.nb_chunks + '-' + Misc.lpad(Number(e.detail.chunk) + 1, e.detail.nb_chunks.toString().length) + '.part'; this.$store.commit('localStorageStore/updateUploadChunks', {id : e.detail.id, chunk :chunkName}) }, resumeUploadEvent() { this.isUploading = true; }, resumeSelectFile(inputId) { document.getElementById(inputId).click(); }, resume(inputId, upload) { console.log(inputId, upload); this.progressValue = upload.available_chunks.length; this.maxProgressValue = Number(upload.nb_chunks); this.$emit('resume-resource-upload', upload, document.getElementById(inputId).files[0]) }, empty() { this.$store.commit('localStorageStore/clearUploads'); }, } } Loading
src/store/localStorageStore.js +0 −32 Original line number Diff line number Diff line Loading @@ -13,9 +13,6 @@ const localStorageStore = { treeDrawer: localStorage.getItem(LOCAL_STORAGE_APP_ID + '.treeDrawer') ? localStorage.getItem(LOCAL_STORAGE_APP_ID + '.treeDrawer') === 'true' : true, uploads: localStorage.getItem(LOCAL_STORAGE_APP_ID + '.uploads') ? JSON.parse(localStorage.getItem(LOCAL_STORAGE_APP_ID + '.uploads')) : {} }), getters: { Loading @@ -37,35 +34,6 @@ const localStorageStore = { localStorage.setItem(LOCAL_STORAGE_APP_ID + '.treeDrawer', b); }, updateUpload: (state, upload) => { let uploads = state.uploads; uploads[upload.id] = {...uploads[upload.id], ...upload}; state.uploads = {...uploads}; localStorage.setItem(LOCAL_STORAGE_APP_ID + '.uploads', JSON.stringify(state.uploads)); }, updateUploadChunks: (state, {id, chunk}) => { let uploads = state.uploads; let upload = uploads[id]; upload.available_chunks.push(chunk); uploads[upload.id] = {...uploads[upload.id], ...upload}; state.uploads = {...uploads}; localStorage.setItem(LOCAL_STORAGE_APP_ID + '.uploads', JSON.stringify(state.uploads)); }, removeUploadById(state, uploadId) { console.log('removing upload because completed ', uploadId) delete state.uploads[uploadId]; // let uploads = state.uploads.filter((u) => u.id !== uploadId); // state.uploads = {...uploads}; localStorage.setItem(LOCAL_STORAGE_APP_ID + '.uploads', JSON.stringify(state.uploads)); }, clearUploads(state) { state.uploads = {}; localStorage.setItem(LOCAL_STORAGE_APP_ID + '.uploads', JSON.stringify(state.uploads)); }, }, actions: { resetView(context) { Loading
src/utils/ChecksumService.js +2 −0 Original line number Diff line number Diff line Loading @@ -23,6 +23,7 @@ export default class ChecksumService { } async _sha256(file) { console.log("START SHA256") let hasher = new hash256(); const sliceSize = 8192; // 8ko let start = 0; Loading @@ -32,6 +33,7 @@ export default class ChecksumService { start += sliceSize; } hasher.finish(); console.log("END SHA256 - " + hasher.result) return toHexString(hasher.result); } Loading
src/utils/JamaClient.js +18 −36 Original line number Diff line number Diff line import JamacRpcClient from './JamaRpcClient'; import ChecksumService from '../utils/ChecksumService'; import Misc from "./Misc"; Loading Loading @@ -68,14 +67,6 @@ class ChunksUploader { return this.id; } // setChecksum(cs) { // this.checksum = cs; // } // // getChecksum(){ // return this.checksum; // } getFile() { return this.file; } Loading @@ -102,7 +93,6 @@ class JamaClient extends JamacRpcClient { constructor(endPoint = '', apiKey = '', rpcMethodPrefix = '') { super(endPoint, apiKey, rpcMethodPrefix); this.checksumService = new ChecksumService(); this.listeners = []; //this.fileChunksUploader = null; } Loading @@ -118,6 +108,7 @@ class JamaClient extends JamacRpcClient { } async uploadChunk(fileChunksUploader, chunkNum, uploadId, path, projectId, attempts = 0) { console.info("Will upload chunk " + chunkNum + '/' + fileChunksUploader.getNbChunks()) try { let response = await fileChunksUploader.uploadChunk(chunkNum, path, projectId); this.dispatchEvent(new CustomEvent(upload_events.chunk_uploaded_event, { Loading Loading @@ -177,7 +168,6 @@ class JamaClient extends JamacRpcClient { async uploadOrResumeFile(file, path, projectId, uploadInfos) { let promises = []; uploadInfos = uploadInfos || {}; let resourceId = uploadInfos ? uploadInfos.resourceId : null; let uploadId = null; Loading @@ -185,11 +175,9 @@ class JamaClient extends JamacRpcClient { if (uploadInfos.id) {//resume file upload fileChunksUploader = new ChunksUploader(file, this.endPoint, this.apiKey, uploadInfos.id); uploadId = uploadInfos.id; promises.push(ChecksumService.sha256(file)); this.dispatchEvent(new CustomEvent(upload_events.resume_upload_event), {detail: {id: uploadId}}); } else {//start file upload - need a uniqid uploadId = Misc.makeid(64); promises.push(ChecksumService.sha256(file)); fileChunksUploader = new ChunksUploader(file, this.endPoint, this.apiKey, uploadId); this.dispatchEvent(new CustomEvent(upload_events.start_file_upload_event, { 'detail': { Loading Loading @@ -219,16 +207,10 @@ class JamaClient extends JamacRpcClient { } } } promises.push(this.resource(Number(resourceId)));//fetch resource to get its remote hash let promisesValues = await Promise.all(promises);//wait end of upload and end of local hash compute //must compare local and remote hash let localHash = promisesValues[0]; let remoteHash = promisesValues[1].hash; return { id: uploadId, resourceId: resourceId, ok: localHash === remoteHash } } Loading Loading @@ -260,8 +242,8 @@ class JamaClient extends JamacRpcClient { } async manageUploadFileResult(file, uploadResult) { let result = null; if (uploadResult.ok) { // let result = null; // if (uploadResult.ok) { let result = { ok: true, name: file.name, Loading @@ -273,20 +255,20 @@ class JamaClient extends JamacRpcClient { 'detail': result }) ); } else { let errorMessage = "Une erreur est survenue lors du versement du fichier " + file.name; let result = { ok: false, status: 'error', message: errorMessage, name: file.name, hash: uploadResult.hash } this.dispatchEvent(new CustomEvent(upload_events.end_file_upload_event, { 'detail': result }) ); } // } else { // let errorMessage = "Une erreur est survenue lors du versement du fichier " + file.name; // let result = { // ok: false, // status: 'error', // message: errorMessage, // name: file.name, // hash: uploadResult.hash // } // this.dispatchEvent(new CustomEvent(upload_events.end_file_upload_event, { // 'detail': result // }) // ); // } return result; } Loading