🐛 Fix big file (with multiple chunks) download (#653)

This commit is contained in:
Montassar Ghanmy
2024-09-19 17:39:57 +01:00
committed by GitHub
parent 9adc0ec646
commit ac9700f9b5
2 changed files with 6 additions and 10 deletions
@@ -137,21 +137,19 @@ export default class StorageService extends TdriveService<StorageAPI> implements
try { try {
// eslint-disable-next-line @typescript-eslint/no-this-alias // eslint-disable-next-line @typescript-eslint/no-this-alias
const self = this; const self = this;
const chunks = options?.totalChunks || 1; const chunks = options?.totalChunks || 1;
let count = 1; let count = 1;
//check that the file a really exists // Check if the first chunk or file exists
await self._read(options?.totalChunks ? `${path}/chunk${count}` : path); await self._read(options?.totalChunks ? `${path}/chunk${count}` : path);
let stream: any;
async function factory(callback: (err?: Error, stream?: Stream) => unknown) { async function factory(callback: (err?: Error, stream?: Stream) => unknown) {
if (count > chunks) { if (count > chunks) {
callback(); callback();
return; return;
} }
let decipher: Decipher; let decipher: Decipher | undefined;
if (options?.encryptionKey) { if (options?.encryptionKey) {
const [key, iv] = options.encryptionKey.split("."); const [key, iv] = options.encryptionKey.split(".");
decipher = createDecipheriv(options.encryptionAlgo || this.algorithm, key, iv); decipher = createDecipheriv(options.encryptionAlgo || this.algorithm, key, iv);
@@ -161,17 +159,15 @@ export default class StorageService extends TdriveService<StorageAPI> implements
count += 1; count += 1;
try { try {
stream = await self._read(chunk); let stream = await self._read(chunk); // Read the chunk
if (decipher) { if (decipher) {
stream = stream.pipe(decipher); stream = stream.pipe(decipher); // Apply decipher if necessary
} }
callback(null, stream); callback(null, stream); // Return the stream only once
} catch (err) { } catch (err) {
logger.error(err); logger.error(err);
callback(err, null); callback(err, null);
} }
callback(null, stream);
return;
} }
return new Multistream(factory); return new Multistream(factory);
@@ -303,7 +303,7 @@ class FileUploadService {
return new Resumable({ return new Resumable({
target, target,
headers, headers,
chunkSize: chunkSize || 50000000, chunkSize: chunkSize || 5000000,
testChunks: testChunks || false, testChunks: testChunks || false,
simultaneousUploads: simultaneousUploads || 5, simultaneousUploads: simultaneousUploads || 5,
maxChunkRetries: maxChunkRetries || 2, maxChunkRetries: maxChunkRetries || 2,