Skip to content

Uploading the same image for multiple parts results in multiple images being safed #5532

Closed

Description

@30350n

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

  1. upload the same image for two different parts
  2. observe to files being created in the media/part_images directory

Expected behaviour

Files should only be stored if they contain unique data.

Deployment Method

  • Docker
  • Bare metal

Version Information

Version Information:

InvenTree-Version: 0.13.0 dev
Django Version: 3.2.20
Commit Hash: cd35744
Commit Date: 2023-09-12

Database: postgresql
Debug-Mode: False
Deployed using Docker: False
Platform: Linux-3.10.0-1160.83.1.el7.x86_64-x86_64-with-glibc2.17
Installer: GIT

Active plugins: [{'name': 'InvenTreeBarcode', 'slug': 'inventreebarcode', 'version': '2.0.0'}, {'name': 'InvenTreeCoreNotificationsPlugin', 'slug': 'inventreecorenotificationsplugin', 'version': '1.0.0'}, {'name': 'InvenTreeLabel', 'slug': 'inventreelabel', 'version': '1.0.0'}, {'name': 'DigiKeyPlugin', 'slug': 'digikeyplugin', 'version': '1.0.0'}, {'name': 'LCSCPlugin', 'slug': 'lcscplugin', 'version': '1.0.0'}, {'name': 'MouserPlugin', 'slug': 'mouserplugin', 'version': '1.0.0'}]

Please verify if you can reproduce this bug on the demo site.

  • I can reproduce this bug on the demo site.

Relevant log output

No response

Activity

  1. added
    bug
    question
    triage:not-checked
    on Sep 12, 2023
  2. 30350n commented on Sep 12, 2023

    @30350n
    ContributorAuthor

    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:

    1. just hash every image after uploading and save it with the hash as its name (simplest, but doesn't allow for keeping the file name)
    2. basically 1. but keep some kind of dictionary to map from image name to hash

    If this would be desirable behavior, I'd be happy to look into implementing this :)

  3. SchrodingersGat commented on Sep 13, 2023

    @SchrodingersGat
    Member

    Currently the way to have multiple parts use the same image is to use the "select existing image" process - have you been using this?

    1. Create a part A
    2. Upload an image for the part
    3. Create a part B
    4. Select the existing image (from part A) to use for part B

    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?

  4. removed
    bug
    triage:not-checked
    on Sep 13, 2023
  5. 30350n commented on Sep 13, 2023

    @30350n
    ContributorAuthor

    Perhaps you can hook into the existing "select existing image" functionality to achieve what you want here?

    Well there's two problems with that.

    1. I'm adding the images automatically via a script, so that's just not possible I think.
    2. This is how the Select Part Image dialog looks like for me, for whatever reason:
      image

    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).

  6. SchrodingersGat commented on Sep 14, 2023

    @SchrodingersGat
    Member

    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.

    I'm hoping there is a simpler way :)

  7. simonkuehling commented on Sep 21, 2023

    @simonkuehling
    Contributor

    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...?

  8. SchrodingersGat commented on Sep 21, 2023

    @SchrodingersGat
    Member

    @simonkuehling we use the @cleanup.ignore decorator to protect part and company models from being incorrectly "cleaned" by the django_cleanup librayr

  9. simonkuehling commented on Sep 21, 2023

    @simonkuehling
    Contributor

    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)

  10. 30350n commented on Sep 21, 2023

    @30350n
    ContributorAuthor

    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.
    image
    I just hadn't time to look at the exact implemenation yet.

  11. wolflu05 commented on Sep 21, 2023

    @wolflu05
    Member

    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

  12. wolflu05 commented on Sep 21, 2023

    @wolflu05
    Member

    The thing with hashes is also, that there are 3 types of images stored for different previewing sizes.

  13. 30350n commented on Sep 21, 2023

    @30350n
    ContributorAuthor

    The thing with hashes is also, that there are 3 types of images stored for different previewing sizes.

    That is not an issue though.

  14. SchrodingersGat commented on Sep 26, 2023

    @SchrodingersGat
    Member

    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.

  15. 30350n commented on Sep 26, 2023

    @30350n
    ContributorAuthor

    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 ^^

  16. 30350n commented on Sep 26, 2023

    @30350n
    ContributorAuthor

    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.

  17. SchrodingersGat commented on Sep 26, 2023

    @SchrodingersGat
    Member

    I would not suggest touching any existing files - only run this "hash based naming" scheme for newly uploaded files.

  18. github-actions commented on Nov 25, 2023

    @github-actions
    Contributor

    This issue seems stale. Please react to show this is still important.

  19. added
    inactive
    on Nov 25, 2023
  20. 30350n commented on Nov 29, 2023

    @30350n
    ContributorAuthor

    Not stale, being worked on in #5703.

  21. SchrodingersGat commented on Nov 29, 2023

    @SchrodingersGat
    Member

    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.

  22. 30350n commented on Nov 29, 2023

    @30350n
    ContributorAuthor

    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).

  23. SchrodingersGat commented on Nov 29, 2023

    @SchrodingersGat
    Member

    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

  24. removed
    inactive
    on Nov 30, 2023
  25. SchrodingersGat commented on Dec 2, 2023

    @SchrodingersGat
    Member

    @30350n see #6021 which implements this feature

  26. 30350n commented on Dec 4, 2023

    @30350n
    ContributorAuthor

    Thanks for the work on implementing this! I'll see how I can integrate this with my tooling.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    question

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions