I’m building a CSV importer for my Strapi application and one of its task is to read an image URL from a cell and download it from the URL and save it to media library.
The code looks like this:
const request = require('request').defaults({ encoding: null });
request.get(src, function (err, res, body) {
const fileName = src.split('/').pop();
strapi.plugins.upload.services.upload.upload({
files: {
path: body,
name: fileName,
type: res.headers['content-type'],
size: Number(res.headers['content-length']),
},
data: {
ref: 'products',
refId: data.itemID,
field: 'images'
}
});
});
The download works and I get a Buffer in the body
variable of the request.get
callback. But passing this to strapi.plugins.upload.services.upload.upload
give me the following error:
UnhandledPromiseRejectionWarning: TypeError [ERR_INVALID_ARG_VALUE]: The argument 'path' must be a string or Uint8Array without null bytes. Received <Buffer ff d8 ff e1 10 c1 45 78 69 66 00 00 49 49 2a 00 08 00 00 00 0f 00 00 01 03 00 01 00 00 00 6c 05 00 00 01 01 03 00 01 00 ...
I already tried to replace path: body,
with path: new Uint8Array(body),
but without no luck.
Any idea what I do wrong here?
Thanks for your help!