veliovgroup / Meteor-Files

🚀 Upload files via DDP or HTTP to ☄️ Meteor server FS, AWS, GridFS, DropBox or Google Drive. Fast, secure and robust.
https://packosphere.com/ostrio/files
BSD 3-Clause "New" or "Revised" License
1.11k stars 166 forks source link

Updating Files #849

Closed chrschae closed 1 year ago

chrschae commented 2 years ago

Hello,

im wondering what is the current optimal way to update files with this package?

Im currently using this in the onAfterUpload hook

export function removeOldFiles(filter) {
    let currentFile = Files.findOne(filter)
    // console.log("remove old files ", filter, currentFile)
    if (currentFile) {
        let duplicateDbEntry = Files.findOne({_id: {$ne: currentFile._id}, path: currentFile.path})
        if(duplicateDbEntry) {
            return Files.collection.remove({ _id: currentFile._id })
        } else {
            return Files.remove({ _id: currentFile._id })
        }
    } else return false;
}

Here i had to use Files.collection.remove(), because otherwise the newly inserted file (that replaced the old file in the filesystem) would be removed. The Problem i had is that on new insert, a file might have been replaced with a file with the same name, but the old mongoDB entry was still there - so in the end i had 2 entries in the DB for one file.

Thanks in advance for your feedback

dr-dimitru commented 2 years ago

@chrschae please rephrase what are you trying to achieve. Why do you think that inserting the same file twice should result in having only single file?

For more details please follow our ISSUE_TEMPLATE

chrschae commented 2 years ago

@dr-dimitru Ok no problem. So my setup is as follows: For Example, my fileDir could look like this : root-> userdir-> userid -> userProfileImage.png The name and path of the file is controlled by the namingFunction. So when a user is uploading a profileImage, it will be saved with the same name always. I use one FileCollection for different types of files and save relevant info for me in the metadata of the file. I want to keep the directory structured similar to my example.

On Insert of another picture for the same user, my file in the filedirectory gets replaced (by the package because of same naming) and in the end ill have a dead entry in the DB (for that file that got replaced). Because of that circumstance, i try to remove the old database entry after the new file was uploaded, so i dont have duplicated DB entries for one file.

An optimal case would be to just call an update function that updates my DB entry instead of inserting new one, or even better an upsert function. In general, i think the insert should deny overwriting files, if such a update/upsert function would exist. So as you see with my approach, i am inserting the same "kind" of file (userProfileImage for same user) and get multiple db entries for one file. In my case, an upsert function would be really helpful. It could check if the file already exists at specific path and just update the db entry instead of creating a new one.

I hope this is more helpful to you

dr-dimitru commented 2 years ago

The only proper (your case only) way is to .remove() file and upload a new one or remove after upload and rename in order to replace old file

dr-dimitru commented 1 year ago

Feel free to close it in case if the issue is solved on your end.