diff --git a/src/http/form_file.class.ts b/src/http/form_file.class.ts index 05d2a2a..9bda63a 100644 --- a/src/http/form_file.class.ts +++ b/src/http/form_file.class.ts @@ -25,8 +25,21 @@ export class FormFile { const content = new Uint8Array(buffer); const path = `${destination}/${fileName}`; + const pathParts = path.split('/'); + + pathParts.pop(); try { + try { + await Deno.mkdir(pathParts.join('/'), { + recursive: true, + }); + } catch (error) { + if (!(error instanceof Deno.errors.AlreadyExists)) { + throw error; + } + } + await Deno.writeFile(path, content); } catch { throw new Error(`Cannot upload file ${fileName} to ${destination}`); diff --git a/src/http/http_request.class.ts b/src/http/http_request.class.ts index 6f32cd2..bdd4830 100644 --- a/src/http/http_request.class.ts +++ b/src/http/http_request.class.ts @@ -75,10 +75,21 @@ export class HttpRequest { continue; } + if (name in files) { + files[name] = [ + ...(Array.isArray(files[name]) + ? files[name] as FormFile[] + : [files[name]] as FormFile[]), + new FormFile(field), + ]; + + continue; + } + if (name.endsWith('[]')) { const fieldName = name.slice(0, -2); - if (!files[fieldName]) { + if (!(fieldName in files)) { files[fieldName] = []; }