diff options
Diffstat (limited to 'app/lib/filebrowser/management')
4 files changed, 216 insertions, 0 deletions
diff --git a/app/lib/filebrowser/management/__init__.py b/app/lib/filebrowser/management/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/app/lib/filebrowser/management/__init__.py diff --git a/app/lib/filebrowser/management/commands/__init__.py b/app/lib/filebrowser/management/commands/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/app/lib/filebrowser/management/commands/__init__.py diff --git a/app/lib/filebrowser/management/commands/fb_version_generate.py b/app/lib/filebrowser/management/commands/fb_version_generate.py new file mode 100644 index 0000000..be33f60 --- /dev/null +++ b/app/lib/filebrowser/management/commands/fb_version_generate.py @@ -0,0 +1,84 @@ +# coding: utf-8 + +# Python +import os, re + +# Django +from django.core.management.base import BaseCommand, CommandError + +# filebrowser +from filebrowser.settings import EXTENSION_LIST, EXCLUDE, MEDIA_ROOT, DIRECTORY, VERSIONS, EXTENSIONS +from filebrowser.functions import version_generator + +class Command(BaseCommand): + args = '<media_path>' + help = "(Re)Generate versions of Images within the FILEBROWSER_DIRECTORY or a " + + def handle(self, *args, **options): + media_path = "" + + if len(args): + media_path = args[0] + path = os.path.join(MEDIA_ROOT, media_path) + else: + path = os.path.join(MEDIA_ROOT, DIRECTORY) + + if not os.path.isdir(path): + raise CommandError('<media_path> must be a directory in MEDIA_ROOT (If you don\'t add a media_path the default path is FILEBROWSER_DIRECTORY).\n"%s" is no directory.' % path); + + # get version name + while 1: + self.stdout.write('\nSelect a version you whant to generate:\n') + for version in VERSIONS: + self.stdout.write(' * %s\n' % version) + + version_name = raw_input('(leave blank to generate all versions): ') + + if version_name == "": + selected_version = None + break + else: + try: + tmp = VERSIONS[version_name] + selected_version = version_name + break + except: + self.stderr.write('Error: Version "%s" doesn\'t exist.\n' % version_name) + version_name = None + continue + + # Precompile regular expressions + filter_re = [] + for exp in EXCLUDE: + filter_re.append(re.compile(exp)) + for k,v in VERSIONS.iteritems(): + exp = (r'_%s.(%s)') % (k, '|'.join(EXTENSION_LIST)) + filter_re.append(re.compile(exp)) + + # walkt throu the filebrowser directory + # for all/new files (except file versions itself and excludes) + for dirpath,dirnames,filenames in os.walk(path): + for filename in filenames: + filtered = False + # no "hidden" files (stating with ".") + if filename.startswith('.'): + continue + # check the exclude list + for re_prefix in filter_re: + if re_prefix.search(filename): + filtered = True + if filtered: + continue + (tmp, extension) = os.path.splitext(filename) + if extension in EXTENSIONS["Image"]: + self.createVersions(os.path.join(dirpath, filename), selected_version) + + + def createVersions(self, path, selected_version): + if selected_version: + self.stdout.write('generating version "%s" for: %s\n' % (selected_version, path)) + version_generator(path, selected_version, True) + else: + self.stdout.write('generating all versions for: %s\n' % path) + for version in VERSIONS: + version_generator(path, version, True) diff --git a/app/lib/filebrowser/management/commands/fb_version_remove.py b/app/lib/filebrowser/management/commands/fb_version_remove.py new file mode 100644 index 0000000..4e4767a --- /dev/null +++ b/app/lib/filebrowser/management/commands/fb_version_remove.py @@ -0,0 +1,132 @@ +# coding: utf-8 + +# Python +import os, re + +# Django +from django.core.management.base import BaseCommand, CommandError + +# Filebrowser +from filebrowser.settings import EXTENSION_LIST, EXCLUDE, MEDIA_ROOT, DIRECTORY, VERSIONS, EXTENSIONS + + +class Command(BaseCommand): + args = '<media_path>' + help = "Remove version files of a specific version in MEDIA_ROOT or subdirectory of MEDIA_ROOT" + + def handle(self, *args, **options): + + media_path = "" + + if len(args): + media_path = args[0] + + path = os.path.join(MEDIA_ROOT, media_path) + + if not os.path.isdir(path): + raise CommandError('<media_path> must be a directory in MEDIA_ROOT. "%s" is no directory.' % path); + + self.stdout.write("\n%s\n" % self.help) + self.stdout.write("in this case: %s\n" % path) + + # get suffix or prefix + default_prefix_or_suffix = "s" + while 1: + self.stdout.write('\nOlder versions of django-filebrowser used to prefix the filename with the version name.\n') + self.stdout.write('Current version of django-filebrowser adds the version name as suffix.\n') + prefix_or_suffix = raw_input('"p" for prefix or "s" for suffix (leave blank for "%s"): ' % default_prefix_or_suffix) + + if default_prefix_or_suffix and prefix_or_suffix == '': + prefix_or_suffix = default_prefix_or_suffix + if prefix_or_suffix != "s" and prefix_or_suffix != "p": + sys.stderr.write('Error: "p" and "s" are the only valid inputs.\n') + prefix_or_suffix = None + continue + break + + # get version name + while 1: + version_name = raw_input('\nversion name ("thumb", "big",...): ') + + if version_name == "": + self.stderr.write('Error: You have to enter a version name like "thumb" or "big".\n') + version_name = None + continue + else: + break + + # get list of all matching files + files = self.get_files(path, version_name, (prefix_or_suffix == "p")) + + # output (short version) of files to be deleted + if len(files) > 15: + self.stdout.write('\nFirst/Last 5 files to remove:\n') + for current_file in files[:5]: + self.stdout.write('%s\n' % current_file) + self.stdout.write('...\n') + self.stdout.write('...\n') + for current_file in files[len(files)-5:]: + self.stdout.write('%s\n' % current_file) + else: + self.stdout.write('\nFiles to remove:\n') + for current_file in files: + self.stdout.write('%s\n' % current_file) + + # no files...done + if len(files) == 0: + self.stdout.write('0 files removed.\n\n') + return + else: + self.stdout.write('%d file(s) will be removed.\n\n' % len(files)) + + # ask to make sure + do_remove = "" + self.stdout.write('Sure you want to delete these files?\n') + do_remove = raw_input('"y" for Yes or "n" for No (leave blank for No): ') + + # if "yes" we delete. any different case we finish without removing anything + if do_remove == "y": + for current_file in files: + os.remove(current_file) + self.stdout.write('%d file(s) removed.\n\n' % len(files)) + else: + self.stdout.write('No files removed.\n\n') + return + + + # get files mathing: + # path: search recoursive in this path (os.walk) + # version_name: string is pre/suffix of filename + # search_for_prefix: if true we match against the start of the filename (default is the end) + def get_files(self, path, version_name, search_for_prefix): + file_list = [] + # Precompile regular expressions + filter_re = [] + for exp in EXCLUDE: + filter_re.append(re.compile(exp)) + + # walkt throu the filebrowser directory + # for all/new files (except file versions itself and excludes) + for dirpath,dirnames,filenames in os.walk(path): + for filename in filenames: + filtered = False + # no "hidden" files (stating with ".") + if filename.startswith('.'): + continue + # check the exclude list + for re_prefix in filter_re: + if re_prefix.search(filename): + filtered = True + if filtered: + continue + (filename_noext, extension) = os.path.splitext(filename) + # images only + if extension in EXTENSIONS["Image"]: + # if image matches with version_name we add it to the file_list + if search_for_prefix: + if filename_noext.startswith(version_name + "_"): + file_list.append(os.path.join(dirpath, filename)) + elif filename_noext.endswith("_" + version_name): + file_list.append(os.path.join(dirpath, filename)) + + return file_list |