🐛 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 {
// eslint-disable-next-line @typescript-eslint/no-this-alias
const self = this;
const chunks = options?.totalChunks || 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);
let stream: any;
async function factory(callback: (err?: Error, stream?: Stream) => unknown) {
if (count > chunks) {
callback();
return;
}
let decipher: Decipher;
let decipher: Decipher | undefined;
if (options?.encryptionKey) {
const [key, iv] = options.encryptionKey.split(".");
decipher = createDecipheriv(options.encryptionAlgo || this.algorithm, key, iv);
@@ -161,17 +159,15 @@ export default class StorageService extends TdriveService<StorageAPI> implements
count += 1;
try {
stream = await self._read(chunk);
let stream = await self._read(chunk); // Read the chunk
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) {
logger.error(err);
callback(err, null);
}
callback(null, stream);
return;
}
return new Multistream(factory);
@@ -303,7 +303,7 @@ class FileUploadService {
return new Resumable({
target,
headers,
chunkSize: chunkSize || 50000000,
chunkSize: chunkSize || 5000000,
testChunks: testChunks || false,
simultaneousUploads: simultaneousUploads || 5,
maxChunkRetries: maxChunkRetries || 2,