[Ask] Link an existed image to model using plugin

System Information
  • Strapi Version: 4.1.12
  • Operating System: Windows
  • Database: MySQL
  • Node Version: 14.18.0
  • NPM Version: 6.14.15
  • Yarn Version: 1.22.18

Hello everyone, I’m creating a plugin that uploads an image to AWS S3 using the Strapi upload plugin.
I have done the upload part and linked the uploaded image to the model:

        const formattedFile = await uploadService.formatFileInfo(
            {
                filename: file.name,
                type: file.mimeType,
                size: buffer.length,
            },
            fileInfo,
            metas,
        );

        formattedFile.getStream = () => Readable.from(buffer);
        await this.getServiceUpload('provider').upload(formattedFile);

        const fileValues = { ...formattedFile };
        await strapi.query('plugin::upload.file').create({ data: fileValues });

The problem is, I want to check that if the file has been uploaded before, then I don’t upload anymore, just link the existed image to the related model. But I don’t know how to do that, because:

  • I don’t know how to access table: files_related_morphs. Then I can’t add a relationship as a value here.

  • I don’t know how to link the existed image without uploading it and linking it with ref, refId.

Please anyone could help me with this problem? Thanks.