From 3da82ebd9a4ac8a900920a1241d7bcfdf2ea23c8 Mon Sep 17 00:00:00 2001 From: joaomgcd Date: Fri, 7 Aug 2020 16:38:17 +0100 Subject: [PATCH] Added support to upload UTF-8 file names to local network and added progress report to upload files --- v2/device/device.js | 14 ++++++++++++-- v2/fileupload.js | 19 ++++++++++++++++++- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/v2/device/device.js b/v2/device/device.js index 6a631e8..09838fe 100644 --- a/v2/device/device.js +++ b/v2/device/device.js @@ -148,7 +148,7 @@ export class Devices extends Array{ return result.payload[0].path; }catch(error){ failedDevices.push(device); - device.canContactViaLocalNetwork = false; + await device.setToRemoteNetwork(true); console.log(`Couldn't upload via local network for `,device.deviceName); return null; } @@ -190,23 +190,27 @@ export class Device{ method: 'POST', body: file, headers: { - "Content-Disposition": `filename="${file.name}"` + "Content-Disposition": `filename*=UTF-8''${encodeURIComponent(file.name)}` } } const serverAddress = this.localNetworkServerAddress; const url = `${serverAddress}files?token=${token}`; + StatusReport.report(`Uploading ${file.name} via Local Network...`); console.log(`Uploading ${file.name} to ${serverAddress}...`); const result = await fetch(url,options); + StatusReport.clear(); console.log(`Uploading ${file.name} to ${serverAddress} done!`); return result.json(); } async uploadFilesGoogleDrive({files,token}){ const googleDrive = new GoogleDrive(()=>token); + StatusReport.report("Uploading files via Google Drive..."); const uploadedFiles = await googleDrive.uploadFiles({ folderName: GoogleDrive.getBaseFolderForMyDevice(), accountToShareTo:this.userAccount, notify: false }, files); + StatusReport.clear(); return uploadedFiles.map(uploadedFile=>GoogleDrive.getDownloadUrlFromFileId(uploadedFile)); } @@ -1138,6 +1142,12 @@ export class DeviceBrowser extends Device{ } class StatusReport{ + static report(message){ + EventBus.post(new StatusReport(message)) + } + static clear(){ + StatusReport.report(null); + } constructor(message){ this.message = message; } diff --git a/v2/fileupload.js b/v2/fileupload.js index e71af26..8300b58 100644 --- a/v2/fileupload.js +++ b/v2/fileupload.js @@ -1,3 +1,5 @@ +import { EventBus } from "./eventbus"; + export class FileUploadProviderFactory{ static create(options = {devicesToSendTo}){ const canAllSend = options.devicesToSendTo.every(device=>device.canContactViaLocalNetwork); @@ -27,16 +29,18 @@ export class FileUploadProviderFactory{ const FileUploadProviderLocalNetwork = function({files,deviceIdsToSendTo}){ const uploadFile = async (file,serverAddress) => { + StatusReport.report(`Uploading ${file.name} via Local Network...`); const options = { method: 'POST', body: file, headers: { - "Content-Disposition": `filename="${file.name}"` + "Content-Disposition": `filename*=UTF-8''${file.name}` } } const url = `${serverAddress}files?token=${await back.getAuthTokenPromise()}`; console.log(`Uploading ${file.name} to ${serverAddress}...`); const result = await fetch(url,options); + StatusReport.clear(); return result.json(); } //returns final file URL and deviceIds @@ -74,13 +78,26 @@ const FileUploadProviderGoogleDrive = function({files,deviceIdsToSendTo}){ if(device){ accountToShareTo = device.userAccount; } + StatusReport.report("Uploading files via Google Drive..."); const result = await googleDriveManager.uploadFiles({ folderName: GoogleDriveManager.getBaseFolderForMyDevice(), accountToShareTo:accountToShareTo, notify: back.getShowInfoNotifications() }, filesToUpload); + StatusReport.clear(); return {"deviceId":deviceId,"files":result}; }); return Promise.all(result); } +} +class StatusReport{ + static report(message){ + EventBus.post(new StatusReport(message)) + } + static clear(){ + StatusReport.report(null); + } + constructor(message){ + this.message = message; + } } \ No newline at end of file