Example #1
0
    def generate(cls, manga):
        """
        This method is locked for up to 30 seconds (per manga) until it returns.
        This prevents the server from falling over due to stampede effect.
        """

        if not cls.acquire_lock(manga): return

        try:
            manga_archive = MangaArchive.objects.get(manga=manga)
            DeletedFile.objects.create(path=manga_archive.file.path)
        except MangaArchive.DoesNotExist:
            manga_archive = MangaArchive(manga=manga)

        manga_zip_file = BytesIO()
        manga_zip = zipfile.ZipFile(manga_zip_file, 'w')

        # write manga pages into zip file
        for page in MangaPage.objects.filter(manga=manga).order_by('page'):
            if not page.image: continue
            extension = get_image_extension(page.image)
            manga_zip.write(page.image.path, '{:03d}.{}'.format(page.page, extension))

        # write info.txt into zip file
        info_text = manga.info_text
        manga_zip.writestr('info.txt', info_text)
        manga_zip.close()

        manga_archive.name = manga.archive_name
        manga_archive.file = UploadedFile(manga_zip_file, 'archive.zip')
        manga_archive.save()

        manga_zip_file.close()

        cls.release_lock(manga)

        return manga_archive
Example #2
0
def manga_page_upload_to(instance, filename):
    filename = '{}-{}.{}'.format(instance.page, get_timestamp(), get_image_extension(instance.image))
    dirs = int_to_base36(instance.manga.id)
    return os.sep.join(['manga-page'] + list(dirs) + [filename])
Example #3
0
def tag_cover_upload_to(instance, filename):
    filename = '{}-{}.{}'.format(int_to_base36(instance.id), get_timestamp(), get_image_extension(instance.cover))
    dirs = int_to_base36(instance.id)[:-1]
    return os.sep.join(['tag-cover'] + list(dirs) + [filename])
Example #4
0
def user_avatar_upload_to(instance, filename):
    filename = '{}.{}'.format(int_to_base36(instance.id), get_image_extension(instance.avatar))
    dirs = int_to_base36(instance.id)[:-1]
    return os.sep.join(['user-avatar'] + list(dirs) + [filename])