Use code to rename the uploaded User File before storing in Wix Media

Question:
I need advice on how to rename a user uploaded file before I store it in Wix Media. The collection also needs to be updated with the file name and file url.

Product:
Wix Editor: Front End and Back End coding

What are you trying to achieve:
Here is the workflow that I am trying to implement on a Wix website.

  1. User uploads a file
  2. The file is validated
  3. The file name is “randomized”
  4. The file s uploaded to Wix
  5. Corresponding collection is updated.

What have you already tried:
I am new to Wix environment. I have written front end code to secure and validate the uploaded file for size (less than 5MB, only PDF is allowed, single file is allowed). On the backend, I wrote an “uploadFile” function where I tried the upload function. This seems to be the problem.

let uploadResult = await mediaManager.upload(folderPath, fileBuffer, newFileName, {
“mediaOptions”: { mediaType: “” },
});

Additional information:
It seems to me that upload function needs “buffer” as one of the inputs. Again, I am not sure how to create a buffer from a user file.

Buffers are not unique to Wix so you can approach buffer creation how works best for you.

Here is the MDN documentation on ArrayBuffer() ArrayBuffer - JavaScript | MDN

While the SDK takes an ArrayBuffer as one of the options, I am not sure this is the case in the Velo version so you may need to convert further.

And here is more information in the introduction to the upload API