aboutsummaryrefslogtreecommitdiffstats
path: root/src/types
diff options
context:
space:
mode:
Diffstat (limited to 'src/types')
-rw-r--r--src/types/blob.nim44
-rw-r--r--src/types/formdata.nim32
2 files changed, 35 insertions, 41 deletions
diff --git a/src/types/blob.nim b/src/types/blob.nim
index ebf5aedf..dd812c02 100644
--- a/src/types/blob.nim
+++ b/src/types/blob.nim
@@ -1,27 +1,25 @@
import std/options
-import std/os
+import std/posix
import std/strutils
import js/fromjs
import js/javascript
import js/jstypes
import utils/mimeguess
-import utils/twtstr
type
DeallocFun = proc() {.closure, raises: [].}
Blob* = ref object of RootObj
- isfile*: bool
size* {.jsget.}: uint64
ctype* {.jsget: "type".}: string
buffer*: pointer
deallocFun*: DeallocFun
+ fd*: Option[FileHandle]
WebFile* = ref object of Blob
webkitRelativePath {.jsget.}: string
- path*: string
- file: File #TODO maybe use fd?
+ name* {.jsget.}: string
jsDestructor(Blob)
jsDestructor(WebFile)
@@ -36,25 +34,20 @@ proc newBlob*(buffer: pointer; size: int; ctype: string;
)
proc finalize(blob: Blob) {.jsfin.} =
+ if blob.fd.isSome:
+ discard close(blob.fd.get)
if blob.deallocFun != nil and blob.buffer != nil:
blob.deallocFun()
blob.buffer = nil
proc finalize(file: WebFile) {.jsfin.} =
- if file.deallocFun != nil and file.buffer != nil:
- file.deallocFun()
- file.buffer = nil
-
-proc newWebFile*(path: string; webkitRelativePath = ""): WebFile =
- var file: File
- if not open(file, path, fmRead):
- raise newException(IOError, "Failed to open file")
+ Blob(file).finalize()
+
+proc newWebFile*(name: string; fd: FileHandle): WebFile =
return WebFile(
- isfile: true,
- path: path,
- file: file,
- ctype: DefaultGuess.guessContentType(path),
- webkitRelativePath: webkitRelativePath
+ name: name,
+ fd: some(fd),
+ ctype: DefaultGuess.guessContentType(name)
)
type
@@ -68,8 +61,7 @@ type
proc newWebFile(ctx: JSContext; fileBits: seq[string]; fileName: string;
options = FilePropertyBag()): WebFile {.jsctor.} =
let file = WebFile(
- isfile: false,
- path: fileName,
+ name: fileName
)
var len = 0
for blobPart in fileBits:
@@ -94,18 +86,16 @@ proc newWebFile(ctx: JSContext; fileBits: seq[string]; fileName: string;
#TODO File, Blob constructors
proc getSize*(this: Blob): uint64 =
- if this.isfile:
- return uint64(WebFile(this).path.getFileSize())
+ if this.fd.isSome:
+ var statbuf: Stat
+ if fstat(this.fd.get, statbuf) < 0:
+ return 0
+ return uint64(statbuf.st_size)
return this.size
proc size*(this: WebFile): uint64 {.jsfget.} =
return this.getSize()
-func name*(this: WebFile): string {.jsfget.} =
- if this.path.len > 0 and this.path[^1] != '/':
- return this.path.afterLast('/')
- return this.path.afterLast('/', 2)
-
#TODO lastModified
proc addBlobModule*(ctx: JSContext) =
diff --git a/src/types/formdata.nim b/src/types/formdata.nim
index b353b814..ec3d8bbf 100644
--- a/src/types/formdata.nim
+++ b/src/types/formdata.nim
@@ -1,7 +1,7 @@
import std/strutils
import io/dynstream
-import io/filestream
+import io/posixstream
import js/javascript
import types/blob
import utils/twtstr
@@ -47,12 +47,11 @@ proc calcLength*(this: FormData): int =
# content type
result += "Content-Type: \r\n".len
result += entry.value.ctype.len
- if entry.value.isfile:
- result += int(WebFile(entry.value).getSize())
- else:
- result += int(entry.value.size)
+ result += int(entry.value.getSize())
result += "\r\n".len # header is always followed by \r\n
result += "\r\n".len # value is always followed by \r\n
+ result += "--".len + this.boundary.len + "--\r\n".len
+ result += "\r\n".len
proc getContentType*(this: FormData): string =
return "multipart/form-data; boundary=" & this.boundary
@@ -61,25 +60,27 @@ proc writeEntry*(stream: DynStream; entry: FormDataEntry; boundary: string) =
stream.write("--" & boundary & "\r\n")
let name = percentEncode(entry.name, {'"', '\r', '\n'})
if entry.isstr:
- stream.write("Content-Disposition: form-data; name=\"" & name & "\"\r\n")
- stream.write("\r\n")
+ stream.write("Content-Disposition: form-data; name=\"" & name &
+ "\"\r\n\r\n")
stream.write(entry.svalue)
+ stream.write("\r\n")
else:
- stream.write("Content-Disposition: form-data; name=\"" & name & "\";")
+ var buf = "Content-Disposition: form-data; name=\"" & name & "\";"
let filename = percentEncode(entry.filename, {'"', '\r', '\n'})
- stream.write(" filename=\"" & filename & "\"\r\n")
+ buf &= " filename=\"" & filename & "\"\r\n"
let blob = entry.value
let ctype = if blob.ctype == "":
"application/octet-stream"
else:
blob.ctype
- stream.write("Content-Type: " & ctype & "\r\n")
- if blob.isfile:
- let fs = newDynFileStream(WebFile(blob).path)
- if fs != nil:
+ buf &= "Content-Type: " & ctype & "\r\n\r\n"
+ stream.write(buf)
+ if blob.fd.isSome:
+ let ps = newPosixStream(blob.fd.get)
+ if ps != nil:
var buf {.noinit.}: array[4096, uint8]
while true:
- let n = fs.recvData(addr buf[0], 4096)
+ let n = ps.recvData(addr buf[0], 4096)
if n == 0:
break
stream.sendDataLoop(addr buf[0], n)
@@ -88,4 +89,7 @@ proc writeEntry*(stream: DynStream; entry: FormDataEntry; boundary: string) =
else:
stream.sendDataLoop(blob.buffer, int(blob.size))
stream.write("\r\n")
+
+proc writeEnd*(stream: DynStream; boundary: string) =
+ stream.write("--" & boundary & "--\r\n")
stream.write("\r\n")