From ba0da1981001cb57e9381753ab1bff1a4992ad07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20H=C3=BCttel?= Date: Fri, 5 Aug 2022 20:59:51 +0200 Subject: [PATCH 1/2] Rework file upload structure to use subfolders for better file performance --- shimatta_kenkyusho/shimatta_modules/RandomFileName.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/shimatta_kenkyusho/shimatta_modules/RandomFileName.py b/shimatta_kenkyusho/shimatta_modules/RandomFileName.py index 279fe27..d457715 100644 --- a/shimatta_kenkyusho/shimatta_modules/RandomFileName.py +++ b/shimatta_kenkyusho/shimatta_modules/RandomFileName.py @@ -6,8 +6,13 @@ from django.utils.deconstruct import deconstructible @deconstructible class RandomFileName(object): def __init__(self, path): - self.path = os.path.join(path, "%s%s") + self.path = os.path.join(path, "%s/%s/%s%s") def __call__(self, _, filename): extension = os.path.splitext(filename)[1] - return self.path % (uuid.uuid4(), extension) \ No newline at end of file + file_uuid = uuid.uuid4() + uuid_str = str(file_uuid) + first_char = uuid_str[0] + second_char = uuid_str[1] + + return self.path % (first_char, second_char, file_uuid, extension) From 191705c6a69be6c1950bb76ee66d70478dc004ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20H=C3=BCttel?= Date: Fri, 5 Aug 2022 21:47:55 +0200 Subject: [PATCH 2/2] Add migration command for old file name structure --- .../management/commands/migrate_images.py | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 shimatta_kenkyusho/parts/management/commands/migrate_images.py diff --git a/shimatta_kenkyusho/parts/management/commands/migrate_images.py b/shimatta_kenkyusho/parts/management/commands/migrate_images.py new file mode 100644 index 0000000..4e6e740 --- /dev/null +++ b/shimatta_kenkyusho/parts/management/commands/migrate_images.py @@ -0,0 +1,59 @@ +from django.core.management.base import BaseCommand +from parts.models import Component, Package, Distributor, Manufacturer +import os +import shutil + +class Command(BaseCommand): + help = 'Migrate all media files to the current folder structure' + + def move_files_of_model(self, queryset): + for comp in queryset: + img_path = comp.image.name + img_path = os.path.normpath(img_path) + path_components = img_path.split(os.sep) + + if len(path_components) <= 2: + self.stdout.write(f'Legacy path found: {img_path}. Will be moved') + full_path_components = os.path.normpath(comp.image.path).split(os.sep) + fname = full_path_components[-1] + path_elem_count = len(full_path_components) + full_path_components.insert(path_elem_count-1, str(fname[1])) + full_path_components.insert(path_elem_count-1, str(fname[0])) + dest_path = os.sep.join(full_path_components) + + # Move file + os.makedirs(os.path.dirname(dest_path), exist_ok=True) + shutil.move(comp.image.path, dest_path) + + # Update model + new_rel_path_comps = path_components + l = len(new_rel_path_comps) + new_rel_path_comps.insert(l-1, str(fname[1])) + new_rel_path_comps.insert(l-1, str(fname[0])) + new_name = os.sep.join(new_rel_path_comps) + self.stdout.write(f'New location: {dest_path}, new name: {new_name}') + comp.image.name = new_name + comp.save() + + def handle(self, *args, **kwargs): + + self.stdout.write('Querying components...') + components = Component.objects.exclude(image='') + self.stdout.write(f'Count of components with images: {components.count()}'); + self.move_files_of_model(components) + + self.stdout.write('Querying packages...') + pkgs = Package.objects.exclude(image='') + self.stdout.write(f'Count of components with images: {pkgs.count()}'); + self.move_files_of_model(pkgs) + + self.stdout.write('Querying manufacturers...') + manufacturers = Manufacturer.objects.exclude(image='') + self.stdout.write(f'Count of components with images: {manufacturers.count()}'); + self.move_files_of_model(manufacturers) + + self.stdout.write('Querying distributors...') + distris = Distributor.objects.exclude(image='') + self.stdout.write(f'Count of components with images: {distris.count()}'); + self.move_files_of_model(distris) +