def clear_imagekit_cache(): cache = get_cache() cache.clear() # Clear IMAGEKIT_CACHEFILE_DIR cache_dir = os.path.join(settings.MEDIA_ROOT, settings.IMAGEKIT_CACHEFILE_DIR) if os.path.exists(cache_dir): shutil.rmtree(cache_dir)
def auto_delete_file_on_change(sender, instance, **kwargs): """ Deletes old file from filesystem when corresponding Delegate object is updated with new file. """ if not instance.pk: return False try: old_file = Delegate.objects.get(pk=instance.pk).photo except Delegate.DoesNotExist: return False # Check if photo changed new_file = instance.photo if not old_file == new_file: old_instance = Delegate.objects.get(pk=instance.pk) # Delete thumbnails for field in ['photo_thumbnail', 'photo_print']: field = getattr(old_instance, field) try: file = field.file except FileNotFoundError: pass else: cache_backend = field.cachefile_backend cache_backend.cache.delete(cache_backend.get_key(file)) field.storage.delete(file.name) # Delete original photo old_instance.photo.delete(save=False) # Clear cache # necessary because the name of the thumbnails change # and the cache still points to the old (and now deleted) photos get_cache().clear()
def delete_image_kit_image_field(image_kit_field): # ImageKit has a bug where files are cached and not deleted right away # https://github.com/matthewwithanm/django-imagekit/issues/229#issuecomment-315690575 if not image_kit_field: return try: file = image_kit_field.file except FileNotFoundError: pass else: cache = get_cache() cache.delete(cache.get(file)) image_kit_field.storage.delete(file.name) image_kit_field.delete()
def clear_imagekit_cache_img_profile(user, pk): # def clear_imagekit_cache_img_profile(pk): print('resize.py 왔다.') SETTINGS_MODULE = os.environ.get('DJANGO_SETTINGS_MODULE') if SETTINGS_MODULE == 'config.settings': cache = get_cache() cache.clear() # Clear IMAGEKIT_CACHEFILE_DIR cache_dir = os.path.join(settings.MEDIA_ROOT, settings.IMAGEKIT_CACHEFILE_DIR) img_profile_dir = os.path.join(cache_dir, f'user/user_{pk}/img_profile') if os.path.exists(img_profile_dir): shutil.rmtree(img_profile_dir) else: # 1) User = get_user_model() # user = User.objects.get(pk=pk) # -> "AUTH_USER_MODEL refers to model '%s' that has not been installed" % settings.AUTH_USER_MODEL # django.core.exceptions.ImproperlyConfigured: AUTH_USER_MODEL refers to model 'members.User' # that has not been installed # 2) AUTH_USER_MODEL # user = get_object_or_404(settings.AUTH_USER_MODEL, pk=pk) # -> { "detail": "찾을 수 없습니다." } # 404 Not Found # 3) def clear_imagekit_cache_img_profile(user, pk): # -> user를 parameter로 전달받음. s3 = boto3.resource('s3') if user.images.img_profile_28: file_path_1 = 'media/' + user.images.img_profile_28.name print(f'file_path_1: {file_path_1}') print(type(file_path_1)) # response = s3.Object('s3-finn-project', file_path_1).delete() # print(response) if user.images.img_profile_225: file_path_2 = 'media/' + user.images.img_profile_225.name print(f'file_path_2: {file_path_2}') print(type(file_path_2))
def delete_file_field(filefield): if not filefield: return try: file = filefield.file except FileNotFoundError: pass else: if isinstance(filefield.field, ProcessedImageField): # ImageKit has a bug where files are cached and not deleted right away # https://github.com/matthewwithanm/django-imagekit/issues/229#issuecomment-315690575 cache = get_cache() cache.delete(cache.get(file)) filefield.storage.delete(file.name)
def handle(self, *args, **options): from imagekit.utils import get_cache cache = get_cache() cache.clear()