I’ve created a site API to upload images to the Media Manager. I used this documentation:
https://support.wix.com/en/article/wix-code-exposing-a-site-api-with-http-functions
https://www.wix.com/code/reference/draft/wix-media-backend.html
It is kind of strange to see ‘draft’ in the second URL. Brings me to my first question: is this API ready to be used and did someone has already managed to use the upload function?
Anyhow, in my http-functions.js I have
import {uploadImage} from 'backend/media';
...
export function post_image(request) {
let options = {
"headers": {
"Content-Type": "application/json"
}
};
return request.body.text()
.then((body) => {
let record = JSON.parse(body);
let image = CryptoJS.enc.Base64.parse(record.image);
...
return uploadImage(image, record.filename, mime)
.then((fileInfo) =>
...
.catch((error) => {
options.body = {
"error": error.message
};
return serverError(options);
});
The image comes in as a base64 String. Is this the right way to convert it to a Buffer variable?
In media.js there is:
export function uploadImage(buffer, filename, mime) {
return wixMedia.upload(
"/Galleries/",
buffer,
filename,
{
"mediaOptions": {
"mimeType": mime,
"mediaType": "image"
},
"metadataOptions": {
"isPrivate": false,
"isVisitorUpload": false,
"context": {
"someKey1": "someValue1",
"someKey2": "someValue2"
}
}
}
);
}
Result: upload() throws an error, resulting in returning a ServerError response
“error”:“Cannot read property ‘mediaType’ of undefined”
The folder ‘Galleries’ defined in the function, does get created… So I guess that either the upload isn’t working yet because of the draft notion, or that I just didn’t converted my image to the right Buffer type.
Does anyone can help me with this?