Skip to content

Commit

Permalink
fix(tasks): use smaller dropbox upload when available
Browse files Browse the repository at this point in the history
  • Loading branch information
Snazzah committed Feb 3, 2024
1 parent 67422f1 commit 2375b70
Showing 1 changed file with 46 additions and 53 deletions.
99 changes: 46 additions & 53 deletions apps/tasks/src/queries/driveUpload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ export async function driveUpload({
case 'dropbox': {
const driveUser = await prisma.dropboxUser.findFirst({ where: { id: userId } });
if (!driveUser) return { error: 'data_not_found', notify: false };
const DROPBOX_UPLOAD_FILE_SIZE_LIMIT = 150 * 1024 * 1024;

const auth = new DropboxAuth({
clientId: dropboxConfig.clientId,
Expand All @@ -368,8 +369,8 @@ export async function driveUpload({
try {
await dbx.usersGetCurrentAccount();
} catch (e) {
const err: DropboxError<never> = e as any;
logger.error(`Error in uploading recording ${recordingId} for user ${userId} due to dropbox: ${err?.error_summary || String(e)}`);
const err: DropboxError<{ error_summary: string }> = e as any;
logger.error(`Error in uploading recording ${recordingId} for user ${userId} due to dropbox`, err.error);
await prisma.dropboxUser.delete({ where: { id: userId } });
return { error: 'dropbox_token_invalid', notify: true };
}
Expand All @@ -384,66 +385,58 @@ export async function driveUpload({

const fileSize = (await fs.stat(tempFile)).size;
const readStream = createReadStream(tempFile);
const file: DropboxResponse<files.FileMetadata> = await new Promise((resolve, reject) => {
let sessionId = '';
let uploadedBytes = 0;
let chunksToUploadSize = 0;
let chunks: Buffer[] = [];

readStream.on('data', async (chunk) => {
chunks.push(chunk as Buffer);
chunksToUploadSize += chunk.length;

const finished = chunksToUploadSize + uploadedBytes === fileSize;

// upload only if we've specified number of chunks in memory OR we're uploading the final chunk
if (chunks.length === CHUNKS_PER_DRIVE_UPLOAD || finished) {
readStream.pause();
const chunkBuffer = Buffer.concat(chunks, chunksToUploadSize);

try {
if (uploadedBytes === 0) {
const response = await dbx.filesUploadSessionStart({ close: false, contents: chunkBuffer });
sessionId = response.result.session_id;

// Small files could be finished quickly
if (finished) {
const file: DropboxResponse<files.FileMetadata> = fileSize < DROPBOX_UPLOAD_FILE_SIZE_LIMIT

Check warning on line 388 in apps/tasks/src/queries/driveUpload.ts

View workflow job for this annotation

GitHub Actions / Lint source code

Insert `⏎·········`
? await dbx.filesUpload({path: '/' + fileName, autorename: true, contents: readStream })

Check warning on line 389 in apps/tasks/src/queries/driveUpload.ts

View workflow job for this annotation

GitHub Actions / Lint source code

Replace `········?·await·dbx.filesUpload({` with `············?·await·dbx.filesUpload({·`
: await new Promise((resolve, reject) => {

Check warning on line 390 in apps/tasks/src/queries/driveUpload.ts

View workflow job for this annotation

GitHub Actions / Lint source code

Replace `········` with `············`
let sessionId = '';

Check warning on line 391 in apps/tasks/src/queries/driveUpload.ts

View workflow job for this annotation

GitHub Actions / Lint source code

Insert `····`
let uploadedBytes = 0;

Check warning on line 392 in apps/tasks/src/queries/driveUpload.ts

View workflow job for this annotation

GitHub Actions / Lint source code

Replace `············` with `················`
let chunksToUploadSize = 0;

Check warning on line 393 in apps/tasks/src/queries/driveUpload.ts

View workflow job for this annotation

GitHub Actions / Lint source code

Replace `············` with `················`
let chunks: Buffer[] = [];

readStream.on('data', async (chunk) => {
chunks.push(chunk as Buffer);
chunksToUploadSize += chunk.length;

const finished = chunksToUploadSize + uploadedBytes === fileSize;

// upload only if we've specified number of chunks in memory OR we're uploading the final chunk
if (chunks.length === CHUNKS_PER_DRIVE_UPLOAD || finished) {
readStream.pause();
const chunkBuffer = Buffer.concat(chunks, chunksToUploadSize);

try {
if (uploadedBytes === 0) {
const response = await dbx.filesUploadSessionStart({ close: false, contents: chunkBuffer });
sessionId = response.result.session_id;
} else if (finished) {
const file = await dbx.filesUploadSessionFinish({
cursor: { session_id: sessionId, offset: uploadedBytes },
commit: { path: '/' + fileName, autorename: true },
contents: chunkBuffer
});
return resolve(file);
} else {
await dbx.filesUploadSessionAppendV2({
cursor: { session_id: sessionId, offset: uploadedBytes },
close: false,
contents: chunkBuffer
});
}
} else if (finished) {
const file = await dbx.filesUploadSessionFinish({
cursor: { session_id: sessionId, offset: uploadedBytes },
commit: { path: '/' + fileName, autorename: true },
contents: chunkBuffer
});
return resolve(file);
} else {
await dbx.filesUploadSessionAppendV2({
cursor: { session_id: sessionId, offset: uploadedBytes },
close: false,
contents: chunkBuffer
});
} catch (e) {
return reject(e);
}
} catch (e) {
return reject(e);
}

// update uploaded bytes
uploadedBytes += chunksToUploadSize;
// update uploaded bytes
uploadedBytes += chunksToUploadSize;

// reset for next chunks
chunks = [];
chunksToUploadSize = 0;
// reset for next chunks
chunks = [];
chunksToUploadSize = 0;

readStream.resume();
}
readStream.resume();
}
});
});
});

await fs.unlink(tempFile).catch(() => {});

Expand Down Expand Up @@ -480,9 +473,9 @@ export async function driveUpload({
else if (request) logger.error(`AxiosError <unknown response> ${request.method} ${request.host}${request.path}`);
else console.error(e);
} else if ((e as Error).name === 'DropboxResponseError') {
const err: DropboxError<never> = e as any;
logger.error(`DropboxError [${err.error_summary}]`, err.user_message?.text || err);
return { error: `DropboxError [${err.error_summary}]`, notify: true };
const err: DropboxError<{ error_summary: string }> = e as any;
logger.error(`DropboxError [${err.error.error_summary}]`, err.error);
return { error: `DropboxError [${err.error.error_summary}]`, notify: true };
}
return { error: (e as any).toString() || 'unknown_error', notify: true };
}
Expand Down

0 comments on commit 2375b70

Please sign in to comment.