Skip to content

Commit 68f6aaa

Browse files
authored
fix: django-storage 1.14 complains about files being opened twice when 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
1 parent ac3985d commit 68f6aaa

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

filer/admin/folderadmin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from django.core.exceptions import PermissionDenied, ValidationError
1414
from django.core.paginator import EmptyPage, PageNotAnInteger, Paginator
1515
from django.db import models, router
16-
from django.db.models import F, OuterRef, Subquery, Case, When
16+
from django.db.models import Case, F, OuterRef, Subquery, When
1717
from django.db.models.functions import Coalesce, Lower
1818
from django.http import HttpResponse, HttpResponseRedirect
1919
from django.shortcuts import get_object_or_404, render

filer/models/filemodels.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -229,9 +229,9 @@ def _move_file(self):
229229
self.is_public = not self.is_public
230230
self.file.delete_thumbnails()
231231
self.is_public = not self.is_public
232+
src_file = src_storage.open(src_file_name)
232233
# This is needed because most of the remote File Storage backend do not
233234
# open the file.
234-
src_file = src_storage.open(src_file_name)
235235
# Context manager closes file after reading contents
236236
with src_file.open() as f:
237237
content_file = ContentFile(f.read())
@@ -254,10 +254,14 @@ def _copy_file(self, destination, overwrite=False):
254254
src_file_name = self.file.name
255255
storage = self.file.storages['public' if self.is_public else 'private']
256256

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

263267
def generate_sha1(self):

0 commit comments

Comments
 (0)