Skip to content

Commit

Permalink
Added support to upload UTF-8 file names to local network and added p…
Browse files Browse the repository at this point in the history
…rogress report to upload files
  • Loading branch information
joaomgcd committed Aug 7, 2020
1 parent dc895f2 commit 3da82eb
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
14 changes: 12 additions & 2 deletions v2/device/device.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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));
}
Expand Down Expand Up @@ -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;
}
Expand Down
19 changes: 18 additions & 1 deletion v2/fileupload.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { EventBus } from "./eventbus";

export class FileUploadProviderFactory{
static create(options = {devicesToSendTo}){
const canAllSend = options.devicesToSendTo.every(device=>device.canContactViaLocalNetwork);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
}

0 comments on commit 3da82eb

Please sign in to comment.