Skip to content

Instantly share code, notes, and snippets.

@andreciornavei
Created September 8, 2021 23:21
Show Gist options
  • Save andreciornavei/098c7c4841d3817884c0c2c04deec9cd to your computer and use it in GitHub Desktop.
Save andreciornavei/098c7c4841d3817884c0c2c04deec9cd to your computer and use it in GitHub Desktop.
Strapi upload plugin from buffer
// The returned ID can be passed on image field on model
const uploadFileBinary = async (binary, uuid) => {
const fileInfo = { name: uuid }
const metas = {}
const { optimize } = strapi.plugins.upload.services['image-manipulation'];
const { buffer, info } = await optimize(binary);
const formattedFile = await strapi.plugins.upload.services.upload.formatFileInfo(
{
filename: uuid + '.png',
type: 'image/png',
size: binary.toString().length,
},
fileInfo,
metas
);
const fileData = _.assign(formattedFile, info, { buffer });
const upload = await strapi.plugins.upload.services.upload.uploadFileAndPersist(fileData)
return upload.id;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment