wcms icon indicating copy to clipboard operation
wcms copied to clipboard

Media upload API

Open Mement0o opened this issue 2 years ago • 3 comments
trafficstars

It would be interesting to be able to post medias with the API.

edit by @vincent-peugnet:

  • [ ] Upload /upload/<path>
    • [x] get and store the file
    • [ ] create folders at the same time
    • [ ] check file integrity ?
    • [ ] use /tmp folder ?
  • [ ] Delete /delete/<path>
  • [ ] Move /move/
  • [ ] List folders paths (using Modelmedia::globlist())

Mement0o avatar Dec 28 '22 23:12 Mement0o

https://phil.tech/2016/http-rest-api-file-uploads/

According to this blog post, the multipart is not confortable from the client point of view. The most easy way is to separate the file from the metadatas in two requests.

In it's example, blog's author cite Youtube SDK which send metadatas before the video file.

In my case, I think of another strategy: Sending the file before.

The file is sent to an upload API, then W store it in the /tmp folder using a generated name. Then, the generated name is sent back in it's response. Now, the file sender can react and request another API point to move a file from the /tmp directory to it's final directory. It will also send the final name of the file. The API respond according to the success of the operation.

vincent-peugnet avatar Dec 31 '22 00:12 vincent-peugnet

I think you don't need to to something that complicated. You can just POST the content of the file in the body to /api/v0/media/<the/path/you/want> and then make the API return the real path it has chosen.

You can also add an endpoint to move files, but it is another feature.

But yes it is a good idea to mimic the classical PHP upload technic by first writing to /tmp but you can also do it in the upload route I described. This way if the upload fails during transfer, the media folder is left intact.

n-peugnet avatar Dec 31 '22 09:12 n-peugnet

Okay, I just saw that Symfony is using the other strategy:

Encoding the datas in BASE64 format so it can fit a JSON and be send through POST API.

https://symfonycasts.com/screencast/symfony-uploads/api-uploads#pure-api-endpoint-with-json-base64-decode

The problem is that the sender need to encode the BASE64 file.

vincent-peugnet avatar Jan 02 '23 12:01 vincent-peugnet