file-store: use pathlib2 to determine complex filenames with double extensions, e.g exmaple.tar.gz

This commit is contained in:
Marcin Kuzminski 2019-06-06 14:26:01 +02:00
parent be45ea4459
commit db360afc78
2 changed files with 12 additions and 6 deletions

View file

@ -38,19 +38,18 @@ class LocalFileStorage(object):
"""
Resolves a unique name and the correct path. If a filename
for that path already exists then a numeric prefix with values > 0 will be
added, for example test.jpg -> test-1.jpg etc. initially file would have 0 prefix.
added, for example test.jpg -> 1-test.jpg etc. initially file would have 0 prefix.
:param name: base name of file
:param directory: absolute directory path
"""
basename, ext = os.path.splitext(name)
counter = 0
while True:
name = '%s-%d%s' % (basename, counter, ext)
name = '%d-%s' % (counter, name)
# sub_store prefix to optimize disk usage, e.g some_path/ab/final_file
sub_store = cls._sub_store_from_filename(basename)
sub_store = cls._sub_store_from_filename(name)
sub_store_path = os.path.join(directory, sub_store)
if not os.path.exists(sub_store_path):
os.makedirs(sub_store_path)

View file

@ -19,9 +19,10 @@
# and proprietary license terms, please see https://rhodecode.com/licenses/
import os
import uuid
import pathlib2
def get_file_storage(settings):
from rhodecode.apps.file_store.local_store import LocalFileStorage
@ -30,6 +31,11 @@ def get_file_storage(settings):
return LocalFileStorage(base_path=store_path)
def splitext(filename):
ext = ''.join(pathlib2.Path(filename).suffixes)
return filename, ext
def uid_filename(filename, randomized=True):
"""
Generates a randomized or stable (uuid) filename,
@ -38,7 +44,8 @@ def uid_filename(filename, randomized=True):
:param filename: the original filename
:param randomized: define if filename should be stable (sha1 based) or randomized
"""
_, ext = os.path.splitext(filename)
_, ext = splitext(filename)
if randomized:
uid = uuid.uuid4()
else: