Skip to content

Commit

Permalink
fix: django-storage 1.14 complains about files being opened twice whe…
Browse files Browse the repository at this point in the history
…n copying (#1418)

* Fix #1377

* Fix: Do not open file twice when moving or copying

* Fix isort folder admin

* no message

* Catch exception raised by django-storage 1.14

* Update filemodels.py

* Update filemodels.py
  • Loading branch information
fsbraun authored Sep 8, 2023
1 parent ac3985d commit 68f6aaa
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
2 changes: 1 addition & 1 deletion filer/admin/folderadmin.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from django.core.exceptions import PermissionDenied, ValidationError
from django.core.paginator import EmptyPage, PageNotAnInteger, Paginator
from django.db import models, router
from django.db.models import F, OuterRef, Subquery, Case, When
from django.db.models import Case, F, OuterRef, Subquery, When
from django.db.models.functions import Coalesce, Lower
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import get_object_or_404, render
Expand Down
12 changes: 8 additions & 4 deletions filer/models/filemodels.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,9 @@ def _move_file(self):
self.is_public = not self.is_public
self.file.delete_thumbnails()
self.is_public = not self.is_public
src_file = src_storage.open(src_file_name)
# This is needed because most of the remote File Storage backend do not
# open the file.
src_file = src_storage.open(src_file_name)
# Context manager closes file after reading contents
with src_file.open() as f:
content_file = ContentFile(f.read())
Expand All @@ -254,10 +254,14 @@ def _copy_file(self, destination, overwrite=False):
src_file_name = self.file.name
storage = self.file.storages['public' if self.is_public else 'private']

# This is needed because most of the remote File Storage backend do not
# open the file.
src_file = storage.open(src_file_name)
src_file.open()
try:
# This is needed because most of the remote File Storage backend do not
# open the file. Re-opening would create a value error.
src_file.open()
except ValueError: # pragma: no cover
# If a validation error is raised, the file is already open.
pass
return storage.save(destination, ContentFile(src_file.read()))

def generate_sha1(self):
Expand Down

0 comments on commit 68f6aaa

Please sign in to comment.