octet-stream / form-data

Spec-compliant FormData implementation for Node.js
https://www.npmjs.com/package/formdata-node
MIT License
142 stars 17 forks source link

Blobs backed up by filesystem #25

Closed jimmywarting closed 3 years ago

jimmywarting commented 4 years ago

I have just released a feature in fetch-blob to create blobs backed up by the filesystem. (but it dosen't contain a File class)

Here is what i have been doing in my own project

import Blob from 'fetch-blob'
import blobFrom from 'fetch-blob/from.js'

class File extends Blob {
  constructor(blobParts, fileName, options = {}) {
    const { lastModified = Date.now(), ...blobPropertyBag } = options
    super(blobParts, blobPropertyBag)
    this.name = String(fileName).replace(/\//g, '\u003A')
    this.lastModified = lastModified
    this.lastModifiedDate = new Date(lastModified)
  }

  get [Symbol.toStringTag]() {
    return 'File'
  }
}

const path = './package.json'
const blob = blobFrom(path)
const { mtime } = await fs.stat(path)
const file = new File([blob], 'package.json', {
  lastModified: mtime,
  type: 'text/json'
})

Was thinking maybe you would be in any intresset of this or mention it in the readme somewhere. (now the File class is not necessary but it's maybe something you would like to adapt?)

Here is how without the File class npm install formdata-node fetch-blob domexception

import FormData from 'formdata-node'
import blobFrom from 'fetch-blob/from.js'

const fd = new FormData()
const blob = blobFrom('package.json')
fd.append('data', blob, 'package.json')
octet-stream commented 4 years ago

Not sure how could I adopt this. Personally I'd prefer to have File class for that, but since we have no such thing in Node (as far as I know), I decided to make bare-minimal implementation, just to have spec compatibility. It would be really nice to have your solution in some 3rd party File class implementation.

Anyway, maybe I need to add examples how to use formdata-node to upload files. I totally will mention your solution as well. For more clarification. And since my library supports not only Blob, File, ReadStream and Buffer as field's content.

jimmywarting commented 4 years ago

replace your file class with the one i provided. then you can construct your own spec compatibility file instance.

if you like to create a file backed up by the filesystem first create a blob using blobFrom and then wrap it around the file class like this:

const parts = [ blobFrom('./package.json') ]
new File(parts, 'filename', {...})
octet-stream commented 3 years ago

Sorry for the late response. Your solution will be included once I finish to port the project over TypeScript and release the 3.x version. Actually, it is included already.

octet-stream commented 3 years ago

And the File class now inherits fetch-blob: https://github.com/octet-stream/form-data/blob/ef791f018e4da293fdef40181765c84b82670b5e/lib/File.ts#L8

octet-stream commented 3 years ago

So, this change available in https://github.com/octet-stream/form-data/releases/tag/v3.0.0. I believe this issue is now resolved.