How do I share media between items?

Hmmm maybe this snippet might help. In a nutshell, it uploads a png QR code image saved on disk and associates it with a QR code collection type in the Image field.

const setUpQRCodeUploadData = (qrCodeId) => ({ data: { refId: qrCodeId, ref: 'api::qr-code.qr-code', field: 'Image' } })
const setUpQRCodeUploadFiles = (filePath, slug) => ({ files: { path: filePath, name: url.slugCreator(slug) + '.png', type: 'image/png', size: fs.statSync(filePath).size } })

const uploadImageToStrapi = async (data, files) => {
    try {
        await strapi.service('plugin::upload.upload').upload({ ...data, ...files })
    } catch (e) {
        throw e
    }
}

const uploadQRCodeImageToStrapi = async (qrCodeId, slug, filePath) => {
    const uploadData = setUpQRCodeUploadData(qrCodeId)
    const uploadFiles = setUpQRCodeUploadFiles(filePath, slug)
    await uploadImageToStrapi(uploadData, uploadFiles)
}

It took me a while to figure out that the ref in the data object has to be prefixed with api::. Would this be, more or less, what you are trying to accomplish?

1 Like