misc: fix mkdir race condition in thumbnails (#58852)
gitea-wip/wcs/pipeline/head Build started... Details

This commit is contained in:
Lauréline Guérin 2021-11-22 18:16:56 +01:00
parent 782ec1b8ba
commit 92d7530f83
No known key found for this signature in database
GPG Key ID: 1FAB9B9B4F93D473
2 changed files with 7 additions and 1 deletions

View File

@ -273,10 +273,14 @@ def test_thumbnail_caching(pub):
thumb_filepath = os.path.join(
thumbs_dir, hashlib.sha256(force_bytes(data_file.get_fs_filename())).hexdigest()
)
assert os.path.exists(thumbs_dir) is False
assert os.path.exists(thumb_filepath) is False
admin_app = login(get_app(pub), username='admin', password='admin')
admin_app.get('/backoffice/management/test/1/download?f=0&thumbnail=1').follow()
assert os.path.exists(thumbs_dir) is True
assert os.path.exists(thumb_filepath) is True
# again, thumbs_dir already exists
admin_app.get('/backoffice/management/test/1/download?f=0&thumbnail=1').follow()
@mock.patch('wcs.wscalls.call_webservice')

View File

@ -681,8 +681,10 @@ def get_thumbnail(filepath, content_type=None):
# check if thumbnail already exists
thumbs_dir = os.path.join(get_publisher().app_dir, 'thumbs')
if not os.path.exists(thumbs_dir):
try:
os.mkdir(thumbs_dir)
except FileExistsError:
pass
thumb_filepath = os.path.join(thumbs_dir, hashlib.sha256(force_bytes(filepath)).hexdigest())
if os.path.exists(thumb_filepath):
with open(thumb_filepath, 'rb') as f: