Commit 5c00c181 authored by nanahira's avatar nanahira

fix small file upload

parent 1f39562e
Pipeline #14476 failed with stages
in 1 minute and 42 seconds
...@@ -25,6 +25,7 @@ export const UploadImage = async (ctx: Context) => { ...@@ -25,6 +25,7 @@ export const UploadImage = async (ctx: Context) => {
const uploadFiles: { fieldname: string, file: Readable, fname: string, encoding: string, mimeType: string }[] = [] const uploadFiles: { fieldname: string, file: Readable, fname: string, encoding: string, mimeType: string }[] = []
const results: any[] = []; const results: any[] = [];
const waitForUploads: Promise<any>[] = []; const waitForUploads: Promise<any>[] = [];
const filesToUpload: Promise<Readable>[] = [];
await busboy(ctx.req, { await busboy(ctx.req, {
onFile: async(fieldname, _file, fname, encoding, mimeType) => { onFile: async(fieldname, _file, fname, encoding, mimeType) => {
const file = new Readable().wrap(_file); const file = new Readable().wrap(_file);
...@@ -35,7 +36,7 @@ export const UploadImage = async (ctx: Context) => { ...@@ -35,7 +36,7 @@ export const UploadImage = async (ctx: Context) => {
if (['png', 'jpg', 'jpeg', 'gif', 'webp'].indexOf(ext) === -1) { if (['png', 'jpg', 'jpeg', 'gif', 'webp'].indexOf(ext) === -1) {
throw new Error('Unsupported image type'); throw new Error('Unsupported image type');
} }
file.pause();
const filename = `avatars/${uuid.v1()}.${ext}`; const filename = `avatars/${uuid.v1()}.${ext}`;
console.log(`uploading ${fname} to ${filename}`) console.log(`uploading ${fname} to ${filename}`)
const upload = new Upload({ const upload = new Upload({
...@@ -52,6 +53,7 @@ export const UploadImage = async (ctx: Context) => { ...@@ -52,6 +53,7 @@ export const UploadImage = async (ctx: Context) => {
} }
}); });
console.log(`waiting`); console.log(`waiting`);
filesToUpload.forEach((file) => file.resume());
await Promise.all(waitForUploads); await Promise.all(waitForUploads);
console.log(`finish`); console.log(`finish`);
ctx.body = results; ctx.body = results;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment