chladog / payload-webp

payloadcms/payload plugin for automatic image conversion to webp format.
https://www.npmjs.com/package/payload-webp
GNU General Public License v3.0
44 stars 5 forks source link

Cannot upload more than one image asset #11

Closed STR1234 closed 1 year ago

STR1234 commented 1 year ago

After installing the plugin I get a "validationError: The following field is invalid: webp.filename" upon uploading a second asset.

I would love to use the plugin - hopefully you can fix this. The error persists after removing the plugin from the payload config as it is seemingly triggered by the entry in mongoDB and I suspect that the unique value has something to do with it.

Screenshot 2023-03-04 at 20 23 20

Running Payload version: 1.6.16 Resolutions sharp version: 0.31.3

And the newest version of your plugin.

STR1234 commented 1 year ago

Update: After I dropped the Index from the DB new assets could successfully be uploaded again. With your plugin installed and added to the payload config there were empty webp objects created inside the assets mongodb entries but no webp conversion ever took place so those objects were not populated with a URL or other props.

I installed the plugin and did not give it any options so it was running all default values. Hopefully you can figure this out (it may as well be something I missed configuring the plugin :P)

Thanks for your work @chladog

Grandnainconnu commented 1 year ago

Got the same problem inserting many medias from the API, fixed it by patching temporarily with patch-package. Like this:

diff --git a/node_modules/payload-webp/lib/getFileMetadataFields.js b/node_modules/payload-webp/lib/getFileMetadataFields.js
index c622c62..1935cc1 100644
--- a/node_modules/payload-webp/lib/getFileMetadataFields.js
+++ b/node_modules/payload-webp/lib/getFileMetadataFields.js
@@ -1,7 +1,7 @@
 "use strict";
 Object.defineProperty(exports, "__esModule", { value: true });
 var getFileMetadataFields = function (afterReadHook) { return [
-    { name: 'filename', type: 'text', unique: true },
+    { name: 'filename', type: 'text' },
     {
         name: 'filesize',
         label: 'File Size',
glettl commented 1 year ago

The problem is, that for some kind of media, webp cannot be generated which results in webp.filename not set. As there is a unique index on the webp filename fields, which is not sparse, bc payload does not support this kind of index, there cannot be multiple undefined values.

Would be if this would be fixed. In my opinion, the unique definition has to be removed in the field config. ( as @Grandnainconnu figured out)

chladog commented 1 year ago

I see, I took the field definition from payload's core, where the filename is unique: true however I didn't realize that there can non-image uploads in which case there would be webp.filename undefined.

The fix present in version 1.1.2.