Merge branch 'master' into butterify-file-upload
This commit is contained in:
commit
99b3791700
1 changed files with 45 additions and 24 deletions
|
@ -5,9 +5,6 @@ import { getEndpoint, getFileUrl, getThumbnailUrl } from 'utils/common/apiUtil';
|
|||
import { getFileExtension } from 'utils/common/utilFunctions';
|
||||
import CryptoWorker from 'utils/crypto/cryptoWorker';
|
||||
|
||||
const ENDPOINT = getEndpoint();
|
||||
|
||||
|
||||
const heic2any = typeof window !== 'undefined' && require('heic2any');
|
||||
const TYPE_HEIC = 'heic';
|
||||
|
||||
|
@ -15,7 +12,6 @@ class DownloadManager {
|
|||
private fileDownloads = new Map<number, Promise<string>>();
|
||||
private thumbnailDownloads = new Map<number, Promise<string>>();
|
||||
|
||||
constructor(private token) { }
|
||||
public async getPreview(file: file) {
|
||||
try {
|
||||
const cache = await caches.open('thumbs');
|
||||
|
@ -28,7 +24,7 @@ class DownloadManager {
|
|||
const resp = await HTTPService.get(
|
||||
getThumbnailUrl(file.id),
|
||||
null,
|
||||
{ 'X-Auth-Token': this.token },
|
||||
{ 'X-Auth-Token': getToken() },
|
||||
{ responseType: 'arraybuffer' }
|
||||
);
|
||||
const worker = await new CryptoWorker();
|
||||
|
@ -59,45 +55,51 @@ class DownloadManager {
|
|||
getFile = async (file: file) => {
|
||||
if (!this.fileDownloads.get(file.id)) {
|
||||
const download = (async () => {
|
||||
return await this.downloadFile(this.token, file);
|
||||
return await this.downloadFile(file);
|
||||
})();
|
||||
this.fileDownloads.set(file.id, download);
|
||||
}
|
||||
return await this.fileDownloads.get(file.id);
|
||||
};
|
||||
|
||||
private async downloadFile(token: string, file: file) {
|
||||
private async downloadFile(file: file) {
|
||||
const worker = await new CryptoWorker();
|
||||
if (file.metadata.fileType === 0) {
|
||||
const resp = await HTTPService.get(
|
||||
getFileUrl(file.id),
|
||||
null, { 'X-Auth-Token': token }, { responseType: 'arraybuffer' },
|
||||
null,
|
||||
{ 'X-Auth-Token': getToken() },
|
||||
{ responseType: 'arraybuffer' }
|
||||
);
|
||||
const decrypted: any = await worker.decryptFile(
|
||||
new Uint8Array(resp.data),
|
||||
await worker.fromB64(file.file.decryptionHeader),
|
||||
file.key,
|
||||
file.key
|
||||
);
|
||||
let decryptedBlob = new Blob([decrypted]);
|
||||
|
||||
if (getFileExtension(file.metadata.title) === TYPE_HEIC) {
|
||||
decryptedBlob = await this.convertHEIC2JPEG(
|
||||
decryptedBlob
|
||||
);
|
||||
decryptedBlob = await this.convertHEIC2JPEG(decryptedBlob);
|
||||
}
|
||||
return URL.createObjectURL(new Blob([decryptedBlob]));
|
||||
} else {
|
||||
const resp = await fetch(getFileUrl(file.id), {
|
||||
headers: {
|
||||
'X-Auth-Token': token,
|
||||
}
|
||||
'X-Auth-Token': getToken(),
|
||||
},
|
||||
});
|
||||
const reader = resp.body.getReader();
|
||||
const stream = new ReadableStream({
|
||||
async start(controller) {
|
||||
const decryptionHeader = await worker.fromB64(file.file.decryptionHeader);
|
||||
const decryptionHeader = await worker.fromB64(
|
||||
file.file.decryptionHeader
|
||||
);
|
||||
const fileKey = await worker.fromB64(file.key);
|
||||
let { pullState, decryptionChunkSize, tag } = await worker.initDecryption(decryptionHeader, fileKey);
|
||||
let {
|
||||
pullState,
|
||||
decryptionChunkSize,
|
||||
tag,
|
||||
} = await worker.initDecryption(decryptionHeader, fileKey);
|
||||
let data = new Uint8Array();
|
||||
// The following function handles each data chunk
|
||||
function push() {
|
||||
|
@ -105,12 +107,26 @@ class DownloadManager {
|
|||
reader.read().then(async ({ done, value }) => {
|
||||
// Is there more data to read?
|
||||
if (!done) {
|
||||
const buffer = new Uint8Array(data.byteLength + value.byteLength);
|
||||
const buffer = new Uint8Array(
|
||||
data.byteLength + value.byteLength
|
||||
);
|
||||
buffer.set(new Uint8Array(data), 0);
|
||||
buffer.set(new Uint8Array(value), data.byteLength);
|
||||
buffer.set(
|
||||
new Uint8Array(value),
|
||||
data.byteLength
|
||||
);
|
||||
if (buffer.length > decryptionChunkSize) {
|
||||
const fileData = buffer.slice(0, decryptionChunkSize);
|
||||
const { decryptedData, newTag } = await worker.decryptChunk(fileData, pullState);
|
||||
const fileData = buffer.slice(
|
||||
0,
|
||||
decryptionChunkSize
|
||||
);
|
||||
const {
|
||||
decryptedData,
|
||||
newTag,
|
||||
} = await worker.decryptChunk(
|
||||
fileData,
|
||||
pullState
|
||||
);
|
||||
controller.enqueue(decryptedData);
|
||||
tag = newTag;
|
||||
data = buffer.slice(decryptionChunkSize);
|
||||
|
@ -120,17 +136,22 @@ class DownloadManager {
|
|||
push();
|
||||
} else {
|
||||
if (data) {
|
||||
const { decryptedData } = await worker.decryptChunk(data, pullState);
|
||||
const {
|
||||
decryptedData,
|
||||
} = await worker.decryptChunk(
|
||||
data,
|
||||
pullState
|
||||
);
|
||||
controller.enqueue(decryptedData);
|
||||
data = null;
|
||||
}
|
||||
controller.close();
|
||||
}
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
push();
|
||||
}
|
||||
},
|
||||
});
|
||||
return URL.createObjectURL(await new Response(stream).blob());
|
||||
}
|
||||
|
@ -145,4 +166,4 @@ class DownloadManager {
|
|||
}
|
||||
}
|
||||
|
||||
export default new DownloadManager(getToken());
|
||||
export default new DownloadManager();
|
||||
|
|
Loading…
Reference in a new issue