floCloudAPI v2.4.5 : file support
- uploadFile: upload a file to the cloud - downloadFile: download a file from cloud - upload/download file supports encryption of file. use options.encrypt in uploadFile and options.decrypt in downloadFile
This commit is contained in:
parent
5be35c28ce
commit
098a62047c
@ -1,4 +1,4 @@
|
|||||||
(function (EXPORTS) { //floCloudAPI v2.4.4
|
(function (EXPORTS) { //floCloudAPI v2.4.5
|
||||||
/* FLO Cloud operations to send/request application data*/
|
/* FLO Cloud operations to send/request application data*/
|
||||||
'use strict';
|
'use strict';
|
||||||
const floCloudAPI = EXPORTS;
|
const floCloudAPI = EXPORTS;
|
||||||
@ -801,6 +801,67 @@
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//upload file
|
||||||
|
floCloudAPI.uploadFile = function (fileBlob, type, options = {}) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
if (!(fileBlob instanceof File) && !(fileBlob instanceof Blob))
|
||||||
|
return reject("file must be instance of File/Blob");
|
||||||
|
fileBlob.arrayBuffer().then(arraybuf => {
|
||||||
|
let file_data = { type: fileBlob.type, name: fileBlob.name };
|
||||||
|
file_data.content = Crypto.util.bytesToBase64(new Uint8Array(arraybuf));
|
||||||
|
if (options.encrypt) {
|
||||||
|
let encryptionKey = options.encrypt === true ?
|
||||||
|
floGlobals.settings.encryptionKey : options.encrypt
|
||||||
|
file_data = floCrypto.encryptData(JSON.stringify(file_data), encryptionKey)
|
||||||
|
}
|
||||||
|
sendApplicationData(file_data, type, options)
|
||||||
|
.then(({ vectorClock, receiverID, type, application }) => resolve({ vectorClock, receiverID, type, application }))
|
||||||
|
.catch(error => reject(error))
|
||||||
|
}).catch(error => reject(error))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
//download file
|
||||||
|
floCloudAPI.downloadFile = function (vectorClock, options = {}) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
options.atVectorClock = vectorClock;
|
||||||
|
requestApplicationData(options.type, options).then(result => {
|
||||||
|
if (!result.length)
|
||||||
|
return reject("File not found");
|
||||||
|
result = result[0];
|
||||||
|
try {
|
||||||
|
let file_data = decodeMessage(result.message);
|
||||||
|
//file is encrypted: decryption required
|
||||||
|
if (file_data instanceof Object && "secret" in file_data) {
|
||||||
|
if (!options.decrypt)
|
||||||
|
return reject("Data is encrypted");
|
||||||
|
let decryptionKey = (options.decrypt === true) ? Crypto.AES.decrypt(user_private, aes_key) : options.decrypt;
|
||||||
|
if (!Array.isArray(decryptionKey))
|
||||||
|
decryptionKey = [decryptionKey];
|
||||||
|
let flag = false;
|
||||||
|
for (let key of decryptionKey) {
|
||||||
|
try {
|
||||||
|
let tmp = floCrypto.decryptData(file_data, key);
|
||||||
|
file_data = JSON.parse(tmp);
|
||||||
|
flag = true;
|
||||||
|
break;
|
||||||
|
} catch (error) { }
|
||||||
|
}
|
||||||
|
if (!flag)
|
||||||
|
return reject("Unable to decrypt file: Invalid private key");
|
||||||
|
}
|
||||||
|
//reconstruct the file
|
||||||
|
let arraybuf = new Uint8Array(Crypto.util.base64ToBytes(file_data.content))
|
||||||
|
result.file = new File([arraybuf], file_data.name, { type: file_data.type });
|
||||||
|
resolve(result)
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
reject("Data is not a file");
|
||||||
|
}
|
||||||
|
}).catch(error => reject(error))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Functions:
|
Functions:
|
||||||
findDiff(original, updatedObj) returns an object with the added, deleted and updated differences
|
findDiff(original, updatedObj) returns an object with the added, deleted and updated differences
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user