# 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

        dir_path = path.join(settings.MEDIA_ROOT, upload_to_directory)

        if path.isdir(dir_path):
            for (index, filename) in enumerate(listdir(dir_path)):
                # Just info.
                if show_progress:
                    print('%s - %s' % (index, filename))

                full_path = path.join(dir_path, filename)

                # Check if this is a file or a directory.
                if path.isfile(full_path):
                    # Remove image.
                    if not check_in_db_function(path.join(upload_to_directory, filename)):
                        try:
                            remove(full_path)
                            files_deleted += 1

                        except:
                            pass

        return files_deleted


    def image_exists_in_db(self, upload_to_path):
        return Image.objects.filter(image=upload_to_path).exists()


    def thumbnail_exists_in_db(self, upload_to_path):

# 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


        dir_path = path.join(settings.MEDIA_ROOT, upload_to_directory)

        if path.isdir(dir_path):
            for (index, filename) in enumerate(listdir(dir_path)):
                # Just info.
                if show_progress:
                    print('%s - %s' % (index, filename))

                full_path = path.join(dir_path, filename)

                # Check if this is a file or a directory.
                if path.isfile(full_path):
                    # Remove image.
                    if not check_in_db_function(path.join(upload_to_directory, filename)):
                        try:
                            remove(full_path)
                            files_deleted += 1

                        except:
                            pass

        return files_deleted


    def image_exists_in_db(self, upload_to_path):
        return Image.objects.filter(image=upload_to_path).exists()


    def thumbnail_exists_in_db(self, upload_to_path):

        return Image.objects.filter(thumbnail=upload_to_path).exists()