|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +from __future__ import unicode_literals |
| 3 | + |
| 4 | +import os |
| 5 | + |
| 6 | +from django.core.files.storage import DefaultStorage |
| 7 | +from django.core.management.base import BaseCommand |
| 8 | +from django.utils.module_loading import import_string |
| 9 | +from django.utils.six.moves import input |
| 10 | + |
| 11 | +from filer import settings as filer_settings |
| 12 | + |
| 13 | + |
| 14 | +class Command(BaseCommand): |
| 15 | + help = "Look for orphaned files in media folders." |
| 16 | + storage = DefaultStorage() |
| 17 | + prefix = filer_settings.FILER_STORAGES['public']['main']['UPLOAD_TO_PREFIX'] |
| 18 | + |
| 19 | + def add_arguments(self, parser): |
| 20 | + parser.add_argument( |
| 21 | + '--orphans', |
| 22 | + action='store_true', |
| 23 | + dest='orphans', |
| 24 | + default=False, |
| 25 | + help="Walk through the media folders and look for orphaned files.", |
| 26 | + ) |
| 27 | + parser.add_argument( |
| 28 | + '--delete-orphans', |
| 29 | + action='store_true', |
| 30 | + dest='delete_orphans', |
| 31 | + default=False, |
| 32 | + help="Delete orphaned files from their media folders.", |
| 33 | + ) |
| 34 | + parser.add_argument( |
| 35 | + '--missing', |
| 36 | + action='store_true', |
| 37 | + dest='missing', |
| 38 | + default=False, |
| 39 | + help="Verify media folders and report about missing files.", |
| 40 | + ) |
| 41 | + parser.add_argument( |
| 42 | + '--delete-missing', |
| 43 | + action='store_true', |
| 44 | + dest='delete_missing', |
| 45 | + default=False, |
| 46 | + help="Delete references in database if files are missing in media folder.", |
| 47 | + ) |
| 48 | + parser.add_argument( |
| 49 | + '--noinput', |
| 50 | + '--no-input', |
| 51 | + action='store_false', |
| 52 | + dest='interactive', |
| 53 | + default=True, |
| 54 | + help="Do NOT prompt the user for input of any kind." |
| 55 | + ) |
| 56 | + |
| 57 | + def handle(self, *args, **options): |
| 58 | + if options['missing']: |
| 59 | + self.verify_references(options) |
| 60 | + if options['delete_missing']: |
| 61 | + if options['interactive']: |
| 62 | + msg = "\nThis will delete entries from your database. Are you sure you want to do this?\n\n" \ |
| 63 | + "Type 'yes' to continue, or 'no' to cancel: " |
| 64 | + if input(msg) != 'yes': |
| 65 | + self.stdout.write("Aborted: Delete missing file entries from database.") |
| 66 | + return |
| 67 | + self.verify_references(options) |
| 68 | + |
| 69 | + if options['orphans']: |
| 70 | + self.verify_storages(options) |
| 71 | + if options['delete_orphans']: |
| 72 | + if options['interactive']: |
| 73 | + msg = "\nThis will delete orphaned files from your storage. Are you sure you want to do this?\n\n" \ |
| 74 | + "Type 'yes' to continue, or 'no' to cancel: " |
| 75 | + if input(msg) != 'yes': |
| 76 | + self.stdout.write("Aborted: Delete orphaned files from storage.") |
| 77 | + return |
| 78 | + self.verify_storages(options) |
| 79 | + |
| 80 | + def verify_references(self, options): |
| 81 | + from filer.models.filemodels import File |
| 82 | + |
| 83 | + for file in File.objects.all(): |
| 84 | + if not file.file.storage.exists(file.file.name): |
| 85 | + if options['delete_missing']: |
| 86 | + file.delete() |
| 87 | + msg = "Delete missing file reference '{}/{}' from database." |
| 88 | + else: |
| 89 | + msg = "Referenced file '{}/{}' is missing in media folder." |
| 90 | + if options['verbosity'] > 2: |
| 91 | + self.stdout.write(msg.format(str(file.folder), str(file))) |
| 92 | + elif options['verbosity']: |
| 93 | + self.stdout.write(os.path.join(str(file.folder), str(file))) |
| 94 | + |
| 95 | + def verify_storages(self, options): |
| 96 | + from filer.models.filemodels import File |
| 97 | + |
| 98 | + def walk(prefix): |
| 99 | + child_dirs, files = storage.listdir(prefix) |
| 100 | + for filename in files: |
| 101 | + relfilename = os.path.join(prefix, filename) |
| 102 | + if not File.objects.filter(file=relfilename).exists(): |
| 103 | + if options['delete_orphans']: |
| 104 | + storage.delete(relfilename) |
| 105 | + msg = "Deleted orphaned file '{}'" |
| 106 | + else: |
| 107 | + msg = "Found orphaned file '{}'" |
| 108 | + if options['verbosity'] > 2: |
| 109 | + self.stdout.write(msg.format(relfilename)) |
| 110 | + elif options['verbosity']: |
| 111 | + self.stdout.write(relfilename) |
| 112 | + |
| 113 | + for child in child_dirs: |
| 114 | + walk(os.path.join(prefix, child)) |
| 115 | + |
| 116 | + filer_public = filer_settings.FILER_STORAGES['public']['main'] |
| 117 | + storage = import_string(filer_public['ENGINE'])() |
| 118 | + walk(filer_public['UPLOAD_TO_PREFIX']) |
0 commit comments