Created
September 8, 2021 23:21
-
-
Save andreciornavei/098c7c4841d3817884c0c2c04deec9cd to your computer and use it in GitHub Desktop.
Strapi upload plugin from buffer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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