# Path: archive_chan/models.py
# class Image(models.Model):
#     original_name = models.CharField(max_length=255)
#     post = models.OneToOneField('Post')
#     image = models.FileField(upload_to = "post_images", storage=fs) # It is impossible to use ImageField to store webm.
#     thumbnail = models.FileField(upload_to = "post_thumbnails", storage=fs)
# 
#     def get_extension(self):
#         name, extension = os.path.splitext(self.image.name)
#         return extension


from os import path, listdir, remove
from optparse import make_option
from django.core.management.base import BaseCommand
from archive_chan.models import Image
from django.conf import settings
import datetime


class Command(BaseCommand):
    args = ''
    help = 'Remove orphaned files - files without a corresponding database entry. In theory they should be deleted automatically but who knows. WARNING: this is a dumb function which will simply iterate and query the database once for each file. It can really strain your server.' 
    option_list = BaseCommand.option_list + (
        make_option(
            '--progress',
            action="store_true",
            dest='progress',
            help='Display progress.',
        ),
    )


    def __init__(self, *args, **kwargs):
        super(Command, self).__init__(*args, **kwargs)
        self.files_deleted = 0
        self.thumbnails_deleted = 0


    def handle(self, *args, **options):
        processing_start = datetime.datetime.now()

        self.files_deleted = self.process_directory('post_images', self.image_exists_in_db, options['progress'])
        self.thumbnails_deleted = self.process_directory('post_thumbnails', self.thumbnail_exists_in_db, options['progress'])


# Path: archive_chan/models.py
# class Image(models.Model):
#     original_name = models.CharField(max_length=255)
#     post = models.OneToOneField('Post')
#     image = models.FileField(upload_to = "post_images", storage=fs) # It is impossible to use ImageField to store webm.
#     thumbnail = models.FileField(upload_to = "post_thumbnails", storage=fs)
# 
#     def get_extension(self):
#         name, extension = os.path.splitext(self.image.name)
#         return extension


# Path: archive_chan/management/commands/archive_chan_remove_orphaned_files.py
from os import path, listdir, remove
from optparse import make_option
from django.core.management.base import BaseCommand
from archive_chan.models import Image
from django.conf import settings
import datetime



class Command(BaseCommand):
    args = ''
    help = 'Remove orphaned files - files without a corresponding database entry. In theory they should be deleted automatically but who knows. WARNING: this is a dumb function which will simply iterate and query the database once for each file. It can really strain your server.' 
    option_list = BaseCommand.option_list + (
        make_option(
            '--progress',
            action="store_true",
            dest='progress',
            help='Display progress.',
        ),
    )


    def __init__(self, *args, **kwargs):
        super(Command, self).__init__(*args, **kwargs)
        self.files_deleted = 0
        self.thumbnails_deleted = 0


    def handle(self, *args, **options):
        processing_start = datetime.datetime.now()

        self.files_deleted = self.process_directory('post_images', self.image_exists_in_db, options['progress'])
        self.thumbnails_deleted = self.process_directory('post_thumbnails', self.thumbnail_exists_in_db, options['progress'])


        processing_time = datetime.datetime.now() - processing_start