Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Folder created for each file uploded that is completely insane #150

Open
fborgherodbsagency opened this issue Nov 11, 2022 · 19 comments
Open
Assignees
Labels
bug Something isn't working

Comments

@fborgherodbsagency
Copy link

System is creating one folder for each image that is completely insane also in terms of management.

An image mouse.jpeg with the hash mouse_2cab11f170 gets stored under /mouse_2cab11f170/mouse_2cab11f170.jpeg.

I've seen multiple issues related to this but all of them were closed as MERGED, but we are still experiencing the issue

    **Still experiencing the issue with the latest version is that merged in the latest version?**

Originally posted by @fborgherodbsagency in #89 (comment)

@tudor0
Copy link

tudor0 commented Dec 6, 2022

Hi, I am having this issue as well, did you find a way to circumvent this? @fborgherodbsagency

@gentaliti
Copy link

Same issue here, did you manage to resolve it?

@tudor0
Copy link

tudor0 commented Dec 13, 2022

@gentaliti I couldn't find a way to fix this, but my front end works well even with the folders. Please let me know if you end up finding a solution.

@metaforx
Copy link

Can confirm this. It's a bit unpractical. For example: nuxt-image will not work when used with strapi as provider, correct image name but missing the folder.

@Lith Lith added the bug Something isn't working label Jan 19, 2023
@Lith
Copy link
Collaborator

Lith commented Jan 19, 2023

Hi,

Yes, it's insane.
But I'm not sure this is a bug of the provider, but from Strapi himself.
Some years ago, when a new image feature was add about image resizing system (thumbnails, small, medium, large size), this as produce this bug and I've try to "fix" it.

Do you have this problem from the BackOffice of Strapi, or when you upload your files from your own client ?

Thanks,

@tudor0
Copy link

tudor0 commented Jan 19, 2023

Me, personally, I have a nextjs application, where i use this package. When uploading pictures from the client to the google cloud, I find a separate file. Is there any more info I can provide so we can pinpoint this more accurately?

@Lith Lith self-assigned this Feb 10, 2023
@sulagnacc
Copy link

Hello,
We are having trouble previewing the images at the Strapi backend.

As Strapi is creating a size folder for every image, if we only allow the domain in i.e 'storage.googleapis.com' in the middleware we are getting the following error
Refused to load the image 'https://storage.googleapis.com/<bucket-name>/user_thumbnail_b1483e17fe/user_thumbnail_b1483e17fe.png?width=120&height=120' because it violates the following Content Security Policy directive: "img-src 'self' data: blob: storage.googleapis.com".

Please let me know if anyone has a solution for this.

@ianyoung
Copy link

ianyoung commented Apr 3, 2023

I get the same behaviour (a folder created for each image size of each image uploaded). This isn't the case with the official S3 plugin so I think it is the plugin which is doing it rather than Strapi(?).

@sulagnacc
Copy link

sulagnacc commented Apr 3, 2023

I have got a solution of this problem. This problem is related to Strapi. I have added some code in the plugin.ts.
Reference is taken from Strapi documentation https://docs-v3.strapi.io/developer-docs/latest/development/plugins/upload.html#configuration
I am sharing my code snippet here
image

Output - No more individual folders created for each image. Size-wise images are being saved.
image

@ianyoung
Copy link

ianyoung commented Apr 3, 2023

That could work. Thanks for the tip! :)

Have you been able to get thumbnails to display in the Media library though? So far in addition to the folder issue I don't see any thumbnail image with publicFiles: true, uniform: false, and security added to middlewares.js.

@sulagnacc
Copy link

In my case I keep uniform: true because I was getting the error "Error uploading file to Google Cloud Storage: Cannot insert legacy ACL for an object when uniform bucket-level access is enabled".

my middlewares.ts
image

and yes, uploaded image thumbnails are displaying in the Media Library section 💯

@ianyoung
Copy link

ianyoung commented Apr 3, 2023

Thanks for confirming @sulagnacc. Can you copy in the above snippets as code for ease of copy? I'll give things another try.

Did you try creating your bucket with Fine-grained access instead of Uniform?

@sulagnacc
Copy link

No, I have tried uniform access only.

Here is the code:
plugin.ts

generateUploadFileName: (file) => {
              let folder='';
              if(file.width>=1920){
                folder='xlarge'
              }else if(file.width>=1000){
                folder='large'
              }else if(file.width>=750){
                folder='medium'
              }else if(file.width>=500){
                folder='small'
              }else if(file.width>=64){
                folder='xsmall'
              }
              return `${folder}/${file.name}`;
            }

middlewares.ts

'strapi::errors', { name: 'strapi::security', config: { contentSecurityPolicy: { useDefaults: true, directives: { 'connect-src': ["'self'", 'https:'], 'img-src': ["'self'", 'data:', 'blob:', 'storage.googleapis.com/<bucket_name>/xlarge/','storage.googleapis.com/<bucket_name>/large/','storage.googleapis.com/<bucket_name>/medium/','storage.googleapis.com/<bucket_name>/small/','storage.googleapis.com/<bucket_name>/xsmall/'], 'media-src': ["'self'", 'data:', 'blob:', 'storage.googleapis.com/<bucket_name>/xlarge/','storage.googleapis.com/<bucket_name>/large/','storage.googleapis.com/<bucket_name>/medium/','storage.googleapis.com/<bucket_name>/small/','storage.googleapis.com/<bucket_name>/xsmall/'], upgradeInsecureRequests: null, }, }, }, },

@ianyoung
Copy link

ianyoung commented Apr 3, 2023

Thanks @sulagnacc this solution works really well.

You don't need the extra declarations in middleware.ts though. The subdomain of storage.googleapis.com is enough to satisfy everything on that domain.

Also, if you create your bucket with Fine-grained access and set uniform: false in plugins.js you shouldn't receive that error message.

Much appreciated.

@gilvent
Copy link

gilvent commented Apr 26, 2023

hi @ianyoung , do you manage to make it work while still using Fine grained access bucket (thumbnails displaying in media library)?

@ianyoung
Copy link

@gilvent I did. Which bit are you having problems with?

@gilvent
Copy link

gilvent commented May 1, 2023

hi @ianyoung , when I use Fine grained bucket, images can be uploaded but thumbnails are not showing in the admin panel (uploaded images are set to non public in the bucket). Is this an expected behavior?

// Edit
turns out I haven't made the bucket public, it is working now. Thanks!

@ianyoung
Copy link

ianyoung commented May 1, 2023

It should work with fine-grained access control. My bucket is set to use that. Usually if thumbnails aren't showing in the dashboard it's because of the directive. Make sure you have the following set in middlewares.js:

module.exports = [
  "strapi::errors",
  {
    name: "strapi::security",
    config: {
      contentSecurityPolicy: {
        useDefaults: true,
        directives: {
          "connect-src": ["'self'", "https:"],
          "img-src": ["'self'", "data:", "blob:", "storage.googleapis.com"],
          "media-src": ["'self'", "data:", "blob:", "storage.googleapis.com"],
          upgradeInsecureRequests: null,
        },
      },
    },
  },
  "strapi::cors",
  "strapi::poweredBy",
  "strapi::logger",
  "strapi::query",
  "strapi::body",
  "strapi::session",
  "strapi::favicon",
  "strapi::public",
];

@alvseven
Copy link

alvseven commented Oct 7, 2023

This solution worked for me, now the files aren't being sent to a folder anymore, however, I suggest adding a bit more code to prevent duplicate files

generateUploadFileName: (file) => file.name

generateUploadFileName: (file) => `${someHash}-${file.name}`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

9 participants