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
Uploading the same image for multiple parts results in multiple images being safed #5532
Please verify that this bug has NOT been raised before.
I checked and didn't find a similar issue
Describe the bug*
I've added a whole bunch of parts to my instance (via an automatic script that fetches the parts from digikey) and noticed a lot of media files accumulating. Thus I wondered if uploading the same image twice would result in two media files being saved.
I just did a quick check by running a python script over all part_images, hashing their content and confirmed that out of the 360 files I've got, only 248 are unique.
Steps to Reproduce
upload the same image for two different parts
observe to files being created in the media/part_images directory
Expected behaviour
Files should only be stored if they contain unique data.
Perhaps you can hook into the existing "select existing image" functionality to achieve what you want here?
Well there's two problems with that.
I'm adding the images automatically via a script, so that's just not possible I think.
This is how the Select Part Image dialog looks like for me, for whatever reason:
What would be tricky/time consuming about hashing the images exactly? Hashing the 360 part images I got took less than a second, so I kinda doubt that be an issue? Edge cases are what I'd probably be most concerned about, but I can't think of any really. (Except for the obvious issue that the image names would be messed up, but the way it is now they already get changed so yeah).
What would be tricky/time consuming about hashing the images exactly?
We would need to create new database schema for storing the image hashes and then comparing on upload. It seems like a lot of complication for (IMO) minimal benefit? You might be able to convince me elsewise.
maybe a ScheduleMixin plugin could do the job here, periodically running a cleanup-task to consolidate duplicates of images?
@SchrodingersGat I can see references to django_cleanup in the code, which could lead to unexpected results since this does not check for multiple models referencing the same media file if I understood the docs correctly...?
ah, ok. So, if a plugin would modify the image url of some parts to eliminate duplicates - that would not automatically do anything to the media files folder, right? (The duplicate images would still be in the media dir)
(only of interest if such a plugin solution would be suitable for @30350n in this case, of course)
Could probably work, but I still think just naming the images by image hash would be the better and easier to implement solution. The image names already get alterered anyways, all you'd have to do really is just replace the whole file name with an actual file hash.
I just hadn't time to look at the exact implemenation yet.
In my opinion the best and scalable solution would be to name them by a uuid and create a db table images which is polymorphic related to parts, notes images, ... (everywhere where images are used) and then instead of messing with the filesystem on our self, we use the Django solution which also supports other storage types like s3, then there could also be implemented direct serving if the storage provider supports it. Same could be done for other attachments like files.
Maybe on upload a hash is calculated and saved to the db image model, if one upload a file with a hash that's already in db that could be reused, but then security aspects need to be considered.
We could potentially rework the existing upload function to use the file content hash as the image name, and then check if an image with that filename exists.
(We should then also double check that the hash does actually match).
In such a case, we can simply point to that image, rather than saving the duplicate.
This could be done without requiring any new tables, etc.
Yep, that is pretty much exactly what I wanted to do. I just hadn't had much time to look into this yet and with the little time I spent, I honestly have struggled to even find the function responsible for the renaming/writing 😅 .
If you could guide me to it, I can probably throw together a quick PR the next few days ^^
We could add a write only field to the API which allows an existing part image path to be specified. If the specific image does not exist, or is outside the part image directory it throws an error.
Issue here would be obtaining said image reference in the first place though. At least for my use case, I'd have to search the image "by data" still (which could more or less be done by naming the image after it's hash on the client side but yeah).
Activity
30350n commentedon Sep 12, 2023
Opening this issue because I'm not sure if there's a solid reason for this behavior.
I can see two possible solutions to fix this:
If this would be desirable behavior, I'd be happy to look into implementing this :)
SchrodingersGat commentedon Sep 13, 2023
Currently the way to have multiple parts use the same image is to use the "select existing image" process - have you been using this?
Hashing each image and checking all existing images would be potentially:
a) tricky
b) time consuming
c) Introduce extra failure modes / edge cases
Perhaps you can hook into the existing "select existing image" functionality to achieve what you want here?
30350n commentedon Sep 13, 2023
Well there's two problems with that.
What would be tricky/time consuming about hashing the images exactly? Hashing the 360 part images I got took less than a second, so I kinda doubt that be an issue? Edge cases are what I'd probably be most concerned about, but I can't think of any really. (Except for the obvious issue that the image names would be messed up, but the way it is now they already get changed so yeah).
SchrodingersGat commentedon Sep 14, 2023
We would need to create new database schema for storing the image hashes and then comparing on upload. It seems like a lot of complication for (IMO) minimal benefit? You might be able to convince me elsewise.
I'm hoping there is a simpler way :)
simonkuehling commentedon Sep 21, 2023
maybe a
ScheduleMixinplugin could do the job here, periodically running a cleanup-task to consolidate duplicates of images?@SchrodingersGat I can see references to
django_cleanupin the code, which could lead to unexpected results since this does not check for multiple models referencing the same media file if I understood the docs correctly...?SchrodingersGat commentedon Sep 21, 2023
@simonkuehling we use the
@cleanup.ignoredecorator to protect part and company models from being incorrectly "cleaned" by thedjango_cleanuplibrayrsimonkuehling commentedon Sep 21, 2023
ah, ok. So, if a plugin would modify the image url of some parts to eliminate duplicates - that would not automatically do anything to the media files folder, right? (The duplicate images would still be in the media dir)
(only of interest if such a plugin solution would be suitable for @30350n in this case, of course)
30350n commentedon Sep 21, 2023
Could probably work, but I still think just naming the images by image hash would be the better and easier to implement solution. The image names already get alterered anyways, all you'd have to do really is just replace the whole file name with an actual file hash.

I just hadn't time to look at the exact implemenation yet.
wolflu05 commentedon Sep 21, 2023
In my opinion the best and scalable solution would be to name them by a uuid and create a db table images which is polymorphic related to parts, notes images, ... (everywhere where images are used) and then instead of messing with the filesystem on our self, we use the Django solution which also supports other storage types like s3, then there could also be implemented direct serving if the storage provider supports it. Same could be done for other attachments like files.
Maybe on upload a hash is calculated and saved to the db image model, if one upload a file with a hash that's already in db that could be reused, but then security aspects need to be considered.
This would provably also fix #3029, #2585
wolflu05 commentedon Sep 21, 2023
The thing with hashes is also, that there are 3 types of images stored for different previewing sizes.
30350n commentedon Sep 21, 2023
That is not an issue though.
SchrodingersGat commentedon Sep 26, 2023
We could potentially rework the existing upload function to use the file content hash as the image name, and then check if an image with that filename exists.
(We should then also double check that the hash does actually match).
In such a case, we can simply point to that image, rather than saving the duplicate.
This could be done without requiring any new tables, etc.
30350n commentedon Sep 26, 2023
Yep, that is pretty much exactly what I wanted to do. I just hadn't had much time to look into this yet and with the little time I spent, I honestly have struggled to even find the function responsible for the renaming/writing 😅 .
If you could guide me to it, I can probably throw together a quick PR the next few days ^^
30350n commentedon Sep 26, 2023
Also, I wonder if you can do something like a migration on media files or if that's not possible/wouldn't be a good idea.
SchrodingersGat commentedon Sep 26, 2023
I would not suggest touching any existing files - only run this "hash based naming" scheme for newly uploaded files.
github-actions commentedon Nov 25, 2023
This issue seems stale. Please react to show this is still important.
30350n commentedon Nov 29, 2023
Not stale, being worked on in #5703.
SchrodingersGat commentedon Nov 29, 2023
We could add a write only field to the API which allows an existing part image path to be specified. If the specific image does not exist, or is outside the part image directory it throws an error.
30350n commentedon Nov 29, 2023
Issue here would be obtaining said image reference in the first place though. At least for my use case, I'd have to search the image "by data" still (which could more or less be done by naming the image after it's hash on the client side but yeah).
SchrodingersGat commentedon Nov 29, 2023
If you have an existing part that you query via the API, the path to the image related to the part will be available in the API
SchrodingersGat commentedon Dec 2, 2023
@30350n see #6021 which implements this feature
30350n commentedon Dec 4, 2023
Thanks for the work on implementing this! I'll see how I can integrate this with my tooling.