Skip to content

Commit

Permalink
fix: file uploading with multiple assets
Browse files Browse the repository at this point in the history
  • Loading branch information
dominiq007 committed Nov 27, 2023
1 parent 40efc47 commit fd52476
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
13 changes: 13 additions & 0 deletions src/http/form_file.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
Expand Down
13 changes: 12 additions & 1 deletion src/http/http_request.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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] = [];
}

Expand Down

0 comments on commit fd52476

Please sign in to comment.