0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-10-29 08:58:01 -04:00

feat(std/mime/multipart): Added multiple FormFile input (#6027)

This commit is contained in:
Henrique Emanoel Viana 2020-06-01 19:39:47 -03:00 committed by GitHub
parent b075f55d58
commit d8c681d37a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 14 deletions

View file

@ -252,11 +252,13 @@ function skipLWSPChar(u: Uint8Array): Uint8Array {
}
export interface MultipartFormData {
file(key: string): FormFile | undefined;
file(key: string): FormFile | FormFile[] | undefined;
value(key: string): string | undefined;
entries(): IterableIterator<[string, string | FormFile | undefined]>;
entries(): IterableIterator<
[string, string | FormFile | FormFile[] | undefined]
>;
[Symbol.iterator](): IterableIterator<
[string, string | FormFile | undefined]
[string, string | FormFile | FormFile[] | undefined]
>;
/** Remove all tempfiles */
removeAll(): Promise<void>;
@ -282,7 +284,7 @@ export class MultipartReader {
* @param maxMemory maximum memory size to store file in memory. bytes. @default 10485760 (10MB)
* */
async readForm(maxMemory = 10 << 20): Promise<MultipartFormData> {
const fileMap = new Map<string, FormFile>();
const fileMap = new Map<string, FormFile | FormFile[]>();
const valueMap = new Map<string, string>();
let maxValueBytes = maxMemory + (10 << 20);
const buf = new Buffer(new Uint8Array(maxValueBytes));
@ -307,7 +309,7 @@ export class MultipartReader {
continue;
}
// file
let formFile: FormFile | undefined;
let formFile: FormFile | FormFile[] | undefined;
const n = await copyN(p, buf, maxValueBytes);
const contentType = p.headers.get("content-type");
assert(contentType != null, "content-type must be set");
@ -343,7 +345,16 @@ export class MultipartReader {
maxValueBytes -= n;
}
if (formFile) {
fileMap.set(p.formName, formFile);
const mapVal = fileMap.get(p.formName);
if (mapVal !== undefined) {
if (Array.isArray(mapVal)) {
mapVal.push(formFile);
} else {
fileMap.set(p.formName, [mapVal, formFile]);
}
} else {
fileMap.set(p.formName, formFile);
}
}
}
return multipatFormData(fileMap, valueMap);
@ -411,17 +422,17 @@ export class MultipartReader {
}
function multipatFormData(
fileMap: Map<string, FormFile>,
fileMap: Map<string, FormFile | FormFile[]>,
valueMap: Map<string, string>
): MultipartFormData {
function file(key: string): FormFile | undefined {
function file(key: string): FormFile | FormFile[] | undefined {
return fileMap.get(key);
}
function value(key: string): string | undefined {
return valueMap.get(key);
}
function* entries(): IterableIterator<
[string, string | FormFile | undefined]
[string, string | FormFile | FormFile[] | undefined]
> {
yield* fileMap;
yield* valueMap;
@ -429,8 +440,15 @@ function multipatFormData(
async function removeAll(): Promise<void> {
const promises: Array<Promise<void>> = [];
for (const val of fileMap.values()) {
if (!val.tempfile) continue;
promises.push(Deno.remove(val.tempfile));
if (Array.isArray(val)) {
for (const subVal of val) {
if (!subVal.tempfile) continue;
promises.push(Deno.remove(subVal.tempfile));
}
} else {
if (!val.tempfile) continue;
promises.push(Deno.remove(val.tempfile));
}
}
await Promise.all(promises);
}
@ -440,7 +458,7 @@ function multipatFormData(
entries,
removeAll,
[Symbol.iterator](): IterableIterator<
[string, string | FormFile | undefined]
[string, string | FormFile | FormFile[] | undefined]
> {
return entries();
},

View file

@ -225,7 +225,10 @@ test({
try {
assertEquals(form.value("deno"), "land");
assertEquals(form.value("bar"), "bar");
const file = form.file("file");
let file = form.file("file");
if (Array.isArray(file)) {
file = file[0];
}
assert(file != null);
assert(file.tempfile != null);
assertEquals(file.size, size);
@ -249,7 +252,10 @@ test({
"--------------------------434049563556637648550474"
);
const form = await mr.readForm(20);
const file = form.file("file");
let file = form.file("file");
if (Array.isArray(file)) {
file = file[0];
}
assert(file != null);
const { tempfile, content } = file;
assert(tempfile != null);