You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Custom field types: let addFile() replace an existing file instead of adding -1
#1074
addFile() (#961) lets a custom control generate files in the browser and commit them together with the entry. Many of these files have a fixed name per entry, because the control regenerates them whenever the entry changes. A new version should then replace the old file, not be saved next to it.
Some examples:
Generated thumbnails and preview images. A control renders a preview of the entry to a canvas and stores it as thumbnails/<slug>.webp. The collection uses this field as thumbnail, so the grid view of the entry list shows a picture of each entry. This is what we do.
Social or Open Graph images. A control draws a share card from the title, date and cover image of a post and stores it as og/<slug>.png. Each edit of the title should update the card.
Files that a framework expects under a fixed name. For example, a Hugo page bundle with featured.jpg or cover.webp next to index.md. A custom image control (crop, focal point, format conversion) must write exactly that name, or the site doesn't find the image.
Edit an existing image in place. A crop or annotation control that saves the edited image back to its own path keeps all references to that path valid, for example in other entries or in the Markdown body.
Generated attachments. An event entry with an .ics file, a contact entry with a .vcf file, or a QR code for the entry URL, all generated from the entry data.
Problem
When an asset with the given name already exists, the save gives the new file a unique name (thumb-1.webp, thumb-2.webp, …), and the old file stays in the repository. Each change therefore leaves one orphaned file behind. The media library fills up with old versions, and a fixed name (like featured.jpg) is lost after the first change. The control can't overwrite the previous file, and the public API can't delete it either.
This is the same -N behaviour as in #753. There, a confirmation dialog for the user solved it. A custom control that adds files from code has no such dialog, so it needs to choose the behaviour itself.
With replace: true, the save keeps the given name. If an asset with that name exists in the target folder, the save updates it in the same commit (action: 'update') instead of saving thumb-1.webp. If no such asset exists, the save creates the file as usual. Without the option, nothing changes.
Implementation notes (v0.239.1)
The overwrite already exists internally for the Replace choice of the duplicate-file dialog:
processResource() (fields/file/process.js) reads replace from the resource and stores it on the draft file (draft.files[blobURL].replace). For a replacing file, it also skips filename_template.
replaceBlobURL() (draft/save/assets.js) keeps the name of a replacing file and saves it as action: 'update' when the name exists in the folder. Only the first file with a given name in one save replaces it, so two replacing files can't overwrite each other.
addFileToDraft() (fields/custom/files.js) calls processResource({ draft, resource: { file: namedFile, folder }, … }), so a custom control can't set the flag.
So the change seems small: pass replace: options.replace into the resource in addFileToDraft(), add replace to CustomFieldAddFileOptions, and document it under "Uploading files". pickFile() (#965) doesn't help here: its replace path needs the user to pick a file in the dialog.
Alternatives we considered
A preSave hook can change the entry data, but not the list of files to commit.
Deleting old files later (for example in CI) needs a separate commit by a bot with write access, which we'd like to avoid.
Thank you for addFile() and pickFile(). They made features like this possible in the first place!
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Use case
addFile()(#961) lets a custom control generate files in the browser and commit them together with the entry. Many of these files have a fixed name per entry, because the control regenerates them whenever the entry changes. A new version should then replace the old file, not be saved next to it.Some examples:
thumbnails/<slug>.webp. The collection uses this field asthumbnail, so the grid view of the entry list shows a picture of each entry. This is what we do.og/<slug>.png. Each edit of the title should update the card.{ original, tiny, optimized }. When the editor replaces the original, the control generates new variants. With fixed names such as<slug>-tiny.webp, the new variants would update the old ones in place.featured.jpgorcover.webpnext toindex.md. A custom image control (crop, focal point, format conversion) must write exactly that name, or the site doesn't find the image..icsfile, a contact entry with a.vcffile, or a QR code for the entry URL, all generated from the entry data.Problem
When an asset with the given name already exists, the save gives the new file a unique name (
thumb-1.webp,thumb-2.webp, …), and the old file stays in the repository. Each change therefore leaves one orphaned file behind. The media library fills up with old versions, and a fixed name (likefeatured.jpg) is lost after the first change. The control can't overwrite the previous file, and the public API can't delete it either.This is the same
-Nbehaviour as in #753. There, a confirmation dialog for the user solved it. A custom control that adds files from code has no such dialog, so it needs to choose the behaviour itself.Proposal
An option for
addFile():With
replace: true, the save keeps the given name. If an asset with that name exists in the target folder, the save updates it in the same commit (action: 'update') instead of savingthumb-1.webp. If no such asset exists, the save creates the file as usual. Without the option, nothing changes.Implementation notes (v0.239.1)
The overwrite already exists internally for the Replace choice of the duplicate-file dialog:
processResource()(fields/file/process.js) readsreplacefrom the resource and stores it on the draft file (draft.files[blobURL].replace). For a replacing file, it also skipsfilename_template.replaceBlobURL()(draft/save/assets.js) keeps the name of a replacing file and saves it asaction: 'update'when the name exists in the folder. Only the first file with a given name in one save replaces it, so two replacing files can't overwrite each other.addFileToDraft()(fields/custom/files.js) callsprocessResource({ draft, resource: { file: namedFile, folder }, … }), so a custom control can't set the flag.So the change seems small: pass
replace: options.replaceinto the resource inaddFileToDraft(), addreplacetoCustomFieldAddFileOptions, and document it under "Uploading files".pickFile()(#965) doesn't help here: its replace path needs the user to pick a file in the dialog.Alternatives we considered
preSavehook can change the entry data, but not the list of files to commit.Thank you for
addFile()andpickFile(). They made features like this possible in the first place!All reactions