Reputation: 3108
I am currently developing on a UWSGI/NGINX server running on Ubuntu Linux 16.04. This is nearly the exact setup that we have on the server (it is 18.04). We're using Wagtail 2.4 and Python 3.6 in both environments. I have deployed a site and copied over all images from the media folder (from both the /media/images and the media/original_images folder). On the dev server I could edit every image just fine, and everything is working correctly on the production server (both with the site and with the admin) except that, on a handful of the images, when I'm in the admin and click on one of these to view/edit, these few generate an error. They are not in any particular collection and are of different types (jpg, png). The error that is generated is:
[Errno 13] Permission denied:
/sites/site_name/media/images/image_name.original.jpg
Following the flow from uploading an image in the Wagtail admin to then editing it, I see that, when images are first uploaded, three renditions are created in /media/images:
Then, when an image is clicked on in order to edit it, the xxxxxx.original.xxx version is placed in /media/images. On the dev server, I had not clicked on this handful of images to edit them and so the xxxxxx.original.xxx version had not been placed in /media/images. Therefore, I did this and then uploaded these .original versions to the production server to, I thought, fix the above error. However, now when I click on an image to edit it, I get the same error except it's now looking for an original with a randomly generated string as part of the name:
[Errno 13] Permission denied:
/sites/site_name/media/images/image_name.original_RANDOM-STRING.jpg
Each of the original images IS in the /media/original_images folder, so I actually would have thought that it would pull from there OK and create the xxxxxxx.original.xxx version in /media/images when trying to edit. But why, then, doesn't placing the xxxxxx.original.xxx version in the /media/images folder fix this error and why does it then look for a version with a randomly generated string instead of simply finding the xxxxxx.original.xxx version?
Upvotes: 2
Views: 1385
Reputation: 25272
Whenever a particular-sized rendition of an image is requested (usually through the {% image %}
template tag), Wagtail checks the wagtailimages.Rendition
model (or the custom Rendition model, if your project defines one) to see if there's an existing rendition record for that source image and size specification. If there is, the URL to that existing image file is served; if not, it will generate a new image file and a corresponding database entry in wagtailimages.Rendition
.
Uploading a file into /media/images/
without creating a corresponding wagtailimages.Rendition
entry will have no effect, as Wagtail will see that there's no database entry and conclude that it has to create a new image file. Then, at the point where it comes to save that file, Django's logic for assigning unique filenames will take effect, and give the new file a randomised filename to avoid overwriting the one you placed there.
The direct fix for this, then, is to add a database entry to wagtailimages.Rendition
whenever you upload a file into /media/images/
. However, a better idea is to fix the underlying permission error. I suspect Django is failing to create the file because it doesn't have write permission to that directory - for the fix to this, see:
Django [Errno 13] Permission denied: '/var/www/media/animals/user_uploads' and https://www.adamerispaha.com/2016/12/14/file-permissions-for-django-media-uploads/
Upvotes: 3