r--w / pocketbase

PocketBase Go SDK
MIT License
81 stars 29 forks source link

(Binary) File Upload #42

Open slimcdk opened 1 year ago

slimcdk commented 1 year ago

Hello

How to possibly upload files together with other data ?

type Payload struct {
  File *os.File `json:"file"`
  Field string `json:"field"`
}
file, err := os.Open("somefile.bin")
res, err := pocketbase.Create("files", Payload{
  File: file
  Field: "blabla"
})

Resty either takes in file as raw bytes or parses them separately https://github.com/go-resty/resty#multipart-files-upload. Sending files a raw bytes doesn't work either.

type Payload struct {
  File []byte `json:"file"`
  Field string `json:"field"`
}
r--w commented 1 year ago

Thanks for this issue; this is a great question; the binary upload was not tested and developed with current releases. I need to rethink this problem and maybe introduce the Upload(id, []byte) method.

rubiojr commented 1 year ago

Would it work if we introduce a new method to create/update records with files, and avoid breaking API backwards compatibility?

_, err := client.CreateWithFiles(
      "bookmarks", // Collection name
      map[string]string{
          "title": "something",
          "url":   "http://foo.bar",
      }, // form data
      map[string]string{"snapshot": v["path-to-local-file"]} // files to upload. `snapshot` is pocketbase collection file column name
    )

I've been using that approach successfully in https://github.com/rubiojr/pocketbase-go/commit/666da01d379c024e24899b4f37c5d3aae0a7dbbf. Something similar could be done with updates.