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

[Issue #6414] Delete thumbnails using Django file storage API #6415

Merged
merged 2 commits into from
Sep 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions geonode/base/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import os
import re
import glob
import math
import uuid
import logging
Expand Down Expand Up @@ -1316,11 +1315,12 @@ def save_thumbnail(self, filename, image):
im = Image.open(content_data)
im.verify() # verify that it is, in fact an image

for _thumb in glob.glob(storage.path('thumbs/%s*' % os.path.splitext(filename)[0])):
try:
os.remove(_thumb)
except Exception:
pass
thumbnail_name, ext = os.path.splitext(filename)
_, _thumbs = storage.listdir("thumbs")
for _thumb in _thumbs:
if _thumb.startswith(thumbnail_name):
storage.delete(os.path.join("thumbs", _thumb))
logger.debug("Deleted existing thumbnail: " + _thumb)

if upload_path and image:
actual_name = storage.save(upload_path, ContentFile(image))
Expand Down
14 changes: 7 additions & 7 deletions geonode/documents/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@

import gisdata
from datetime import datetime
from django.conf import settings
from django.urls import reverse
from django.contrib.auth.models import Group
from django.contrib.auth import get_user_model
from django.contrib.contenttypes.models import ContentType
from django.core.files.uploadedfile import SimpleUploadedFile
from django.core.files.storage import default_storage as storage

from guardian.shortcuts import get_perms, get_anonymous_user

Expand Down Expand Up @@ -447,6 +449,9 @@ class DocumentModerationTestCase(GeoNodeBaseTestSupport):

def setUp(self):
super(DocumentModerationTestCase, self).setUp()
thumbs_dir = os.path.join(settings.MEDIA_ROOT, "thumbs")
if not os.path.exists(thumbs_dir):
os.mkdir(thumbs_dir)
self.user = 'admin'
self.passwd = 'admin'
create_models(type=b'document')
Expand Down Expand Up @@ -493,17 +498,12 @@ def test_moderated_upload(self):
from geonode.base.utils import delete_orphaned_thumbs
delete_orphaned_thumbs()

from django.conf import settings
documents_path = os.path.join(settings.MEDIA_ROOT, 'documents')
fn = os.path.join(documents_path, os.path.basename(input_path))
self.assertFalse(os.path.isfile(fn))

thumbs_path = os.path.join(settings.MEDIA_ROOT, 'thumbs')
_cnt = 0
for filename in os.listdir(thumbs_path):
fn = os.path.join(thumbs_path, filename)
if uuid in filename:
_cnt += 1
_, files = storage.listdir("thumbs")
_cnt = sum(1 for fn in files if uuid in fn)
self.assertTrue(_cnt == 0)

with self.settings(ADMIN_MODERATE_UPLOADS=True):
Expand Down