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
Feature Request: more secure way to get image URL and deleteFile callback #482
I love the image block feature and the uploadFile method in the editor that allows you to define a custom function for uploading images to your server. To improve on this, I wish there would be an equivalent deleteFile callback invoked when the image block is deleted so we can delete the image from our database to save resources. I also think that defining the image as just a public URL is not the most secure, and we could also define a getFile method that will allow developers to define a custom function to retrieve a signed URL that expires after a certain duration.
I recently used the library and felt the same blocker. Requirement of deleteFile is ➕ ➕ . @matthewlipski I'm interested in contributing to it. Let me know if I can take this.
Thanks. Both deleteFile and getFile are good suggestions.
What's tricky with deleteFile this is that a user could delete a block and then re-add it using cmd/ctrl+z. How would you go about this? Happy to accept contributions ofc!
Not at the moment unfortunately:/ the updates to file blocks were done as part of a client request so most likely deleteFile will be added also from client feedback or when we have time to implement it after some higher prio issues.
I think the most direct approach to the undo problem is to provide a message to the user if a file is not successfully loaded indicating that the file is not available, and that the block should be removed and a new one added.
A more sophisticated approach would be to provide a reload option, which would replace an existing file or supply a missing file. This would require a fileDelete trigger for any replaced file.
Here's what I'm doing in the meantime (using firebase):
My editor is modal ('display', 'edit').
When uploading a file, I record the filename in an array, saved to the same firestore document as the editor content
async function uploadFile(file:File) {
if (editorUploadedFiles.includes(file.name)) { // avoid invalidation of the first
alert(file.name + ' has already been uploaded') // placeholder
return null
}
const fileRef = ref(storage, workboxHandler.editRecord.profile.workbox.id + '/document/' + file.name)
try {
await uploadBytes(fileRef, file)
} catch (error) {
console.log('An error occured uploading file.name', file.name)
alert (error.message) // placeholder
return null
}
editorUploadedFiles.push(file.name) // <<== save uploaded file name
const url = await getDownloadURL(fileRef)
return url
}
Deletions in edit mode before save make no change to the file list, or the storage files (which takes care of the Ctrl-Z problem).
On save, I do this (function tucked away in a class)
// deletes storage files not in editor files, before firestore save of edited document
reconcileUploadedFiles = (uploadedFiles, editorFiles) => {
// avoid duplicates
const uploadedFileSet = new Set(uploadedFiles)
const editorFileSet = new Set(editorFiles)
this.removeOrphans(uploadedFileSet, editorFileSet, this)
// refreshes firestore document files list to match editor files, in place
uploadedFiles.length = 0
editorFileSet.forEach((filename) => {
uploadedFiles.push(filename)
})
}
On cancel I do something similar, but with the cancelled uploadedFiles list compared to the previous media editor blocks
// takes edited file list and compares with pre-edit editor.content
revertUploadedFiles = (uploadedFiles, editorFiles) => {
// avoid duplicates
const uploadedFileSet = new Set(uploadedFiles)
const editorFileSet = new Set(editorFiles)
this.removeOrphans(uploadedFileSet, editorFileSet, this)
}
There's a wrinkle to this. It turns out that an image can be saved to the editor using "url":"data:image/jpeg;base64...", which is to say not saved to a file at all. (Drag an image from another web page to the editor directly).
I plan to search for these on editor save and save the image to a separate file, naming it by microsecond time.
Ideally we should still expose something like onDeleteBlock or similar.
A user can then decide what he wants to do with that information (e.g. set a timeout to delete the image if a user hasn't pressed ctrl+z after a few seconds)
+1 to the getFile method for signed urls.
How are you currently dealing with the signed url issue? The only workarounds that I can currently think of are to use a proxy service that returns the signed url or to add a custom image block that handles this.
Activity
i-am-chitti commentedon Dec 23, 2023
I recently used the library and felt the same blocker. Requirement of
deleteFileis ➕ ➕ .@matthewlipski I'm interested in contributing to it. Let me know if I can take this.
YousefED commentedon Dec 29, 2023
Thanks. Both
deleteFileandgetFileare good suggestions.What's tricky with
deleteFilethis is that a user could delete a block and then re-add it using cmd/ctrl+z. How would you go about this? Happy to accept contributions ofc!ahmedbna commentedon Jun 9, 2024
Hey, any updates on
deleteFilefunctionality?matthewlipski commentedon Jun 11, 2024
Not at the moment unfortunately:/ the updates to file blocks were done as part of a client request so most likely
deleteFilewill be added also from client feedback or when we have time to implement it after some higher prio issues.HenrikBechmann commentedon Sep 22, 2024
I think the most direct approach to the undo problem is to provide a message to the user if a file is not successfully loaded indicating that the file is not available, and that the block should be removed and a new one added.
A more sophisticated approach would be to provide a reload option, which would replace an existing file or supply a missing file. This would require a fileDelete trigger for any replaced file.
How do you handle duplicate file names?
HenrikBechmann commentedon Sep 22, 2024
In the meantime, is there a way to extend the editor specs for useCreateBlockNote to include a
onDeleteFileproperty?HenrikBechmann commentedon Sep 23, 2024
Here's what I'm doing in the meantime (using firebase):
My editor is modal ('display', 'edit').
When uploading a file, I record the filename in an array, saved to the same firestore document as the editor content
Deletions in edit mode before save make no change to the file list, or the storage files (which takes care of the Ctrl-Z problem).
On save, I do this (function tucked away in a class)
On cancel I do something similar, but with the cancelled uploadedFiles list compared to the previous media editor blocks
Here is the common
removeOrphansStill needs a recovery routine in case an internet break scrambles things, but I think this will do.
HenrikBechmann commentedon Sep 23, 2024
Getting the editor media file names is of course trivial
HenrikBechmann commentedon Oct 8, 2024
There's a wrinkle to this. It turns out that an image can be saved to the editor using
"url":"data:image/jpeg;base64...", which is to say not saved to a file at all. (Drag an image from another web page to the editor directly).I plan to search for these on editor save and save the image to a separate file, naming it by microsecond time.
HenrikBechmann commentedon Oct 12, 2024
FWIW here is the code for saving data urls to files. The idea is to save database space (firestore "documents" have a hard limit of ~1MB)
Comments are welcome.
So before doing other save processing, I check for data urls with
and here's the function...
pumuckelo commentedon Feb 2, 2025
Hey @YousefED , is there any update on this?
Ideally we should still expose something like onDeleteBlock or similar.
A user can then decide what he wants to do with that information (e.g. set a timeout to delete the image if a user hasn't pressed ctrl+z after a few seconds)
+1 to the getFile method for signed urls.
How are you currently dealing with the signed url issue? The only workarounds that I can currently think of are to use a proxy service that returns the signed url or to add a custom image block that handles this.
hunterkiernan commentedon Oct 28, 2025
Hi everyone. I too would love a
getFilelike solution for signed urls. Thanks!