diff options
Diffstat (limited to 'std/mime')
-rw-r--r-- | std/mime/multipart.ts | 21 | ||||
-rw-r--r-- | std/mime/multipart_test.ts | 7 |
2 files changed, 18 insertions, 10 deletions
diff --git a/std/mime/multipart.ts b/std/mime/multipart.ts index d88ff3d13..7113af012 100644 --- a/std/mime/multipart.ts +++ b/std/mime/multipart.ts @@ -12,7 +12,7 @@ import { extname } from "../path/mod.ts"; import { tempFile } from "../io/util.ts"; import { BufReader, BufWriter, UnexpectedEOFError } from "../io/bufio.ts"; import { encoder } from "../strings/mod.ts"; -import { assertStrictEq } from "../testing/asserts.ts"; +import { assertStrictEq, assert } from "../testing/asserts.ts"; import { TextProtoReader } from "../textproto/mod.ts"; import { hasOwnProperty } from "../util/has_own_property.ts"; @@ -205,7 +205,8 @@ class PartReader implements Reader, Closer { if (this.contentDispositionParams) return this.contentDispositionParams; const cd = this.headers.get("content-disposition"); const params: { [key: string]: string } = {}; - const comps = cd!.split(";"); + assert(cd != null, "content-disposition must be set"); + const comps = cd.split(";"); this.contentDisposition = comps[0]; comps .slice(1) @@ -265,10 +266,12 @@ export class MultipartReader { /** Read all form data from stream. * If total size of stored data in memory exceed maxMemory, * overflowed file data will be written to temporal files. - * String field values are never written to files */ + * String field values are never written to files. + * null value means parsing or writing to file was failed in some reason. + * */ async readForm( maxMemory: number - ): Promise<{ [key: string]: string | FormFile }> { + ): Promise<{ [key: string]: null | string | FormFile }> { const result = Object.create(null); let maxValueBytes = maxMemory + (10 << 20); const buf = new Buffer(new Uint8Array(maxValueBytes)); @@ -293,8 +296,10 @@ export class MultipartReader { continue; } // file - let formFile: FormFile; + let formFile: FormFile | null = null; const n = await copy(buf, p); + const contentType = p.headers.get("content-type"); + assert(contentType != null, "content-type must be set"); if (n > maxMemory) { // too big, write to disk and flush buffer const ext = extname(p.fileName); @@ -311,7 +316,7 @@ export class MultipartReader { file.close(); formFile = { filename: p.fileName, - type: p.headers.get("content-type")!, + type: contentType, tempfile: filepath, size }; @@ -321,14 +326,14 @@ export class MultipartReader { } else { formFile = { filename: p.fileName, - type: p.headers.get("content-type")!, + type: contentType, content: buf.bytes(), size: buf.length }; maxMemory -= n; maxValueBytes -= n; } - result[p.formName] = formFile!; + result[p.formName] = formFile; } return result; } diff --git a/std/mime/multipart_test.ts b/std/mime/multipart_test.ts index 869932fbf..d3e49aa4d 100644 --- a/std/mime/multipart_test.ts +++ b/std/mime/multipart_test.ts @@ -199,7 +199,8 @@ test(async function multipartMultipartReader2(): Promise<void> { assertEquals(form["bar"], "bar"); const file = form["file"] as FormFile; assertEquals(file.type, "application/octet-stream"); - const f = await open(file.tempfile!); + assert(file.tempfile != null); + const f = await open(file.tempfile); const w = new StringWriter(); await copy(w, f); const json = JSON.parse(w.toString()); @@ -207,7 +208,9 @@ test(async function multipartMultipartReader2(): Promise<void> { f.close(); } finally { const file = form["file"] as FormFile; - await remove(file.tempfile!); + if (file.tempfile) { + await remove(file.tempfile); + } } }); |