about summary refs log blame commit diff stats
path: root/cpp/022call_ingredient
blob: 3736177a8394f2b2556353223539f8789d4ae549 (plain) (tree)
>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") return WebFile( isfile: true, path: path, file: file, ctype: DefaultGuess.guessContentType(path), webkitRelativePath: webkitRelativePath ) type BlobPropertyBag = object of JSDict `type`: string #TODO endings FilePropertyBag = object of BlobPropertyBag #TODO lastModified: int64 proc newWebFile(ctx: JSContext; fileBits: seq[string]; fileName: string; options = FilePropertyBag()): WebFile {.jsctor.} = let file = WebFile( isfile: false, path: fileName, ) var len = 0 for blobPart in fileBits: len += blobPart.len let buffer = alloc(len) file.buffer = buffer file.deallocFun = proc() = dealloc(buffer) var buf = cast[ptr UncheckedArray[uint8]](file.buffer) var i = 0 for blobPart in fileBits: if blobPart.len > 0: copyMem(addr buf[i], unsafeAddr blobPart[0], blobPart.len) i += blobPart.len file.size = uint64(len) block ctype: for c in options.`type`: if c notin char(0x20)..char(0x7E): break ctype file.ctype &= c.toLowerAscii() return file #TODO File, Blob constructors proc getSize*(this: Blob): uint64 = if this.isfile: return uint64(WebFile(this).path.getFileSize()) 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) = let blobCID = ctx.registerType(Blob) ctx.registerType(WebFile, parent = blobCID, name = "File")