示例#1
0
文件: tasks.py 项目: rdavydov/demozoo
def create_screenshot_from_production_link(production_link_id):
    try:
        prod_link = ProductionLink.objects.get(id=production_link_id)
        if prod_link.production.screenshots.count():
            return  # don't create a screenshot if there's one already

        production_id = prod_link.production_id
        url = prod_link.download_url
        download, file_content = fetch_url(url)
        buf = cStringIO.StringIO(file_content)

        if prod_link.is_zip_file():
            z = zipfile.ZipFile(buf, 'r')
            # catalogue the zipfile contents if we don't have them already
            if not download.archive_members.all():
                download.log_zip_contents(z)
            # select the archive member to extract a screenshot from, if we don't have
            # a candidate already
            if not prod_link.file_for_screenshot:
                file_for_screenshot = download.select_screenshot_file()
                if file_for_screenshot:
                    prod_link.file_for_screenshot = file_for_screenshot
                    prod_link.is_unresolved_for_screenshotting = False
                else:
                    prod_link.is_unresolved_for_screenshotting = True
                prod_link.save()

            image_extension = prod_link.file_for_screenshot.split(
                '.')[-1].lower()
            if image_extension in USABLE_IMAGE_FILE_EXTENSIONS:
                # we encode the filename as iso-8859-1 before retrieving it, because we
                # decoded it that way on insertion into the database to ensure that it had
                # a valid unicode string representation - see mirror/models.py
                member_buf = cStringIO.StringIO(
                    z.read(prod_link.file_for_screenshot.encode('iso-8859-1')))
                z.close()
                img = PILConvertibleImage(
                    member_buf, name_hint=prod_link.file_for_screenshot)
            else:  # image is not a usable format
                z.close()
                return
        else:
            img = PILConvertibleImage(buf, name_hint=url.split('/')[-1])

        screenshot = Screenshot(production_id=production_id,
                                source_download_id=download.id)
        u = download.sha1
        basename = u[0:2] + '/' + u[2:4] + '/' + u[4:8] + '.pl' + str(
            production_link_id) + '.'
        upload_original(img, screenshot, basename, reduced_redundancy=True)
        upload_standard(img, screenshot, basename)
        upload_thumb(img, screenshot, basename)
        screenshot.save()

    except ProductionLink.DoesNotExist:
        # guess it was deleted in the meantime, then.
        pass
示例#2
0
    def test_scr(self):
        with open(os.path.join(TEST_IMAGES_DIR, 'mr-scene.scr'), mode='rb') as f:
            img = PILConvertibleImage(f, name_hint='mr-scene.scr')
            output, size, format = img.create_thumbnail((200, 150))

        # with open(os.path.join(TEST_IMAGES_DIR, 'mr-scene.out.%s' % format), mode='wb') as f:
        #     f.write(output.getvalue())

        self.assertEqual(size, (200, 150))
        self.assertEqual(format, 'png')
        self.assertImagesSimilar(output, os.path.join(TEST_IMAGES_DIR, 'mr-scene.out.png'))
示例#3
0
    def test_gif_palette(self):
        # https://github.com/python-pillow/Pillow/issues/513
        with open(os.path.join(TEST_IMAGES_DIR, 'atari-metal-knight.gif'), mode='rb') as f:
            img = PILConvertibleImage(f, name_hint='atari-metal-knight.gif')
            output, size, format = img.create_thumbnail((200, 150))

        # with open(os.path.join(TEST_IMAGES_DIR, 'atari-metal-knight.out.%s' % format), mode='wb') as f:
        #    f.write(output.getvalue())

        self.assertEqual(size, (200, 125))
        self.assertEqual(format, 'png')
        self.assertImagesSimilar(output, os.path.join(TEST_IMAGES_DIR, 'atari-metal-knight.out.png'))
示例#4
0
def import_screenshot(production_id, janeway_id, url, suffix):
    blob = fetch_origin_url(url)
    sha1 = blob.sha1
    img = PILConvertibleImage(blob.as_io_buffer(), name_hint=blob.filename)
    basename = sha1[0:2] + '/' + sha1[2:4] + '/' + sha1[4:8] + '.jw' + str(
        janeway_id) + suffix + '.'

    screenshot = Screenshot(production_id=production_id,
                            data_source='janeway',
                            janeway_id=janeway_id,
                            janeway_suffix=suffix)
    upload_original(img, screenshot, basename, reduced_redundancy=False)
    upload_standard(img, screenshot, basename)
    upload_thumb(img, screenshot, basename)
    screenshot.save()
示例#5
0
def import_screenshot(production_id, janeway_id, url, suffix):
    blob = fetch_origin_url(url)
    sha1 = blob.sha1
    img = PILConvertibleImage(blob.as_io_buffer(), name_hint=blob.filename)
    basename = sha1[0:2] + '/' + sha1[2:4] + '/' + sha1[4:8] + '.jw' + str(
        janeway_id) + suffix + '.'

    screenshot = Screenshot(production_id=production_id,
                            data_source='janeway',
                            janeway_id=janeway_id,
                            janeway_suffix=suffix)
    upload_standard(img, screenshot, basename)
    upload_thumb(img, screenshot, basename)
    # leave upload_original until last to prevent things screwing up if the storage
    # closes the original file handle
    upload_original(img, screenshot, basename)
    screenshot.save()
示例#6
0
文件: tasks.py 项目: rdavydov/demozoo
def create_screenshot_versions_from_local_file(screenshot_id, filename):
    try:
        screenshot = Screenshot.objects.get(id=screenshot_id)
        f = open(filename, 'rb')
        img = PILConvertibleImage(f, name_hint=filename)

        basename = create_basename(screenshot_id)
        upload_original(img, screenshot, basename)
        upload_standard(img, screenshot, basename)
        upload_thumb(img, screenshot, basename)
        screenshot.save()

        f.close()

    except Screenshot.DoesNotExist:
        # guess it was deleted in the meantime, then.
        pass

    os.remove(filename)
示例#7
0
文件: tasks.py 项目: rdavydov/demozoo
def rebuild_screenshot(screenshot_id):
    try:
        screenshot = Screenshot.objects.get(id=screenshot_id)
        f = urllib2.urlopen(screenshot.original_url, None, 10)
        # read into a cStringIO buffer so that PIL can seek on it (which isn't possible for urllib2 responses) -
        # see http://mail.python.org/pipermail/image-sig/2004-April/002729.html
        buf = cStringIO.StringIO(f.read())
        img = PILConvertibleImage(buf, screenshot.original_url.split('/')[-1])

        basename = create_basename(screenshot_id)
        upload_original(img, screenshot, basename)
        upload_standard(img, screenshot, basename)
        upload_thumb(img, screenshot, basename)
        screenshot.save()

        f.close()

    except Screenshot.DoesNotExist:
        # guess it was deleted in the meantime, then.
        pass
示例#8
0
文件: tasks.py 项目: rdavydov/demozoo
def create_screenshot_from_remote_file(url, production_id):
    try:
        download, file_content = fetch_url(url)
        screenshot = Screenshot(production_id=production_id,
                                source_download_id=download.id)

        buf = cStringIO.StringIO(file_content)
        img = PILConvertibleImage(buf, name_hint=url.split('/')[-1])

        u = download.sha1
        basename = u[0:2] + '/' + u[2:4] + '/' + u[4:8] + '.p' + str(
            production_id) + '.'
        upload_original(img, screenshot, basename, reduced_redundancy=True)
        upload_standard(img, screenshot, basename)
        upload_thumb(img, screenshot, basename)
        screenshot.save()

    except (urllib2.URLError, FileTooBig):
        # oh well.
        pass
示例#9
0
def create_screenshot_versions_from_local_file(screenshot_id, filename):
    try:
        screenshot = Screenshot.objects.get(id=screenshot_id)
        f = open(filename, 'rb')
        img = PILConvertibleImage(f, name_hint=filename)

        basename = create_basename(screenshot_id)
        upload_standard(img, screenshot, basename)
        upload_thumb(img, screenshot, basename)
        # leave original until last, because if it's already a websafe format it'll just return
        # the original file handle, and the storage backend might close the file after uploading
        # which screws with PIL's ability to create resized versions...
        upload_original(img, screenshot, basename)
        screenshot.save()

        f.close()

    except Screenshot.DoesNotExist:
        # guess it was deleted in the meantime, then.
        pass

    os.remove(filename)
示例#10
0
def rebuild_screenshot(screenshot_id):
    try:
        screenshot = Screenshot.objects.get(id=screenshot_id)
        f = urllib.request.urlopen(screenshot.original_url, None, 10)
        # read into a BytesIO buffer so that PIL can seek on it (which isn't possible for urllib responses) -
        # see http://mail.python.org/pipermail/image-sig/2004-April/002729.html
        buf = io.BytesIO(f.read())
        img = PILConvertibleImage(buf, screenshot.original_url.split('/')[-1])

        basename = create_basename(screenshot_id)
        upload_standard(img, screenshot, basename)
        upload_thumb(img, screenshot, basename)
        # leave original until last, because if it's already a websafe format it'll just return
        # the original file handle, and the storage backend might close the file after uploading
        # which screws with PIL's ability to create resized versions...
        upload_original(img, screenshot, basename)
        screenshot.save()

        f.close()

    except Screenshot.DoesNotExist:
        # guess it was deleted in the meantime, then.
        pass
示例#11
0
def create_screenshot_from_production_link(production_link_id):
    try:
        prod_link = ProductionLink.objects.get(id=production_link_id)
    except ProductionLink.DoesNotExist:
        # guess it was deleted in the meantime, then.
        return

    if prod_link.production.screenshots.count():
        # don't create a screenshot if there's one already
        if prod_link.is_unresolved_for_screenshotting:
            prod_link.is_unresolved_for_screenshotting = False
            prod_link.save()
        return

    if prod_link.has_bad_image:
        return  # don't create a screenshot if a previous attempt has failed during image processing

    production_id = prod_link.production_id
    url = prod_link.download_url
    blob = fetch_link(prod_link)
    sha1 = blob.sha1

    if prod_link.is_zip_file():
        # select the archive member to extract a screenshot from, if we don't have
        # a candidate already
        archive_members = ArchiveMember.objects.filter(archive_sha1=sha1)
        if not prod_link.file_for_screenshot:
            file_for_screenshot = select_screenshot_file(archive_members)
            if file_for_screenshot:
                prod_link.file_for_screenshot = file_for_screenshot
                prod_link.is_unresolved_for_screenshotting = False
            else:
                prod_link.is_unresolved_for_screenshotting = True
            prod_link.save()

        image_extension = prod_link.file_for_screenshot.split('.')[-1].lower()
        if image_extension in USABLE_IMAGE_FILE_EXTENSIONS:
            z = None
            try:
                z = blob.as_zipfile()
                # decode the filename as stored in the db
                filename = unpack_db_zip_filename(
                    prod_link.file_for_screenshot)

                member_buf = io.BytesIO(z.read(filename))
            except zipfile.BadZipfile:
                prod_link.has_bad_image = True
                prod_link.save()
                if z:  # pragma: no cover
                    z.close()
                return

            z.close()
            try:
                img = PILConvertibleImage(
                    member_buf, name_hint=prod_link.file_for_screenshot)
            except IOError:
                prod_link.has_bad_image = True
                prod_link.save()
                return
        else:  # image is not a usable format
            return
    else:
        try:
            img = PILConvertibleImage(blob.as_io_buffer(),
                                      name_hint=url.split('/')[-1])
        except IOError:
            prod_link.has_bad_image = True
            prod_link.save()
            return

    screenshot = Screenshot(production_id=production_id)
    basename = sha1[0:2] + '/' + sha1[2:4] + '/' + sha1[4:8] + '.pl' + str(
        production_link_id) + '.'
    try:
        upload_standard(img, screenshot, basename)
        upload_thumb(img, screenshot, basename)
        # leave original until last, because if it's already a websafe format it'll just return
        # the original file handle, and the storage backend might close the file after uploading
        # which screws with PIL's ability to create resized versions...
        upload_original(img, screenshot, basename)
    except IOError:  # pragma: no cover
        prod_link.has_bad_image = True
        prod_link.save()
        return
    screenshot.save()
示例#12
0
 def test_wmf(self):
     # WMF is not in our PIL_READABLE_FORMATS list
     with open(os.path.join(TEST_IMAGES_DIR, 'clock.wmf'), mode='rb') as f:
         with self.assertRaises(IOError):
             PILConvertibleImage(f, name_hint='clock.wmf')
示例#13
0
def create_screenshot_from_production_link(production_link_id):
    try:
        prod_link = ProductionLink.objects.get(id=production_link_id)
    except ProductionLink.DoesNotExist:
        # guess it was deleted in the meantime, then.
        return

    if prod_link.production.screenshots.count():
        # don't create a screenshot if there's one already
        if prod_link.is_unresolved_for_screenshotting:
            prod_link.is_unresolved_for_screenshotting = False
            prod_link.save()
        return

    if prod_link.has_bad_image:
        return  # don't create a screenshot if a previous attempt has failed during image processing

    production_id = prod_link.production_id
    url = prod_link.download_url
    blob = fetch_link(prod_link)
    sha1 = blob.sha1

    if prod_link.is_zip_file():
        # select the archive member to extract a screenshot from, if we don't have
        # a candidate already
        archive_members = ArchiveMember.objects.filter(archive_sha1=sha1)
        if not prod_link.file_for_screenshot:
            file_for_screenshot = select_screenshot_file(archive_members)
            if file_for_screenshot:
                prod_link.file_for_screenshot = file_for_screenshot
                prod_link.is_unresolved_for_screenshotting = False
            else:
                prod_link.is_unresolved_for_screenshotting = True
            prod_link.save()

        image_extension = prod_link.file_for_screenshot.split('.')[-1].lower()
        if image_extension in USABLE_IMAGE_FILE_EXTENSIONS:
            z = blob.as_zipfile()
            # we encode the filename as iso-8859-1 before retrieving it, because we
            # decoded it that way on insertion into the database to ensure that it had
            # a valid unicode string representation - see mirror/models.py
            try:
                member_buf = cStringIO.StringIO(
                    z.read(prod_link.file_for_screenshot.encode('iso-8859-1')))
            except zipfile.BadZipfile:
                prod_link.has_bad_image = True
                prod_link.save()
                z.close()
                return

            z.close()
            try:
                img = PILConvertibleImage(
                    member_buf, name_hint=prod_link.file_for_screenshot)
            except IOError:
                prod_link.has_bad_image = True
                prod_link.save()
                return
        else:  # image is not a usable format
            return
    else:
        try:
            img = PILConvertibleImage(blob.as_io_buffer(),
                                      name_hint=url.split('/')[-1])
        except IOError:
            prod_link.has_bad_image = True
            prod_link.save()
            return

    screenshot = Screenshot(production_id=production_id)
    basename = sha1[0:2] + '/' + sha1[2:4] + '/' + sha1[4:8] + '.pl' + str(
        production_link_id) + '.'
    try:
        upload_original(img, screenshot, basename, reduced_redundancy=True)
        upload_standard(img, screenshot, basename)
        upload_thumb(img, screenshot, basename)
    except IOError:
        prod_link.has_bad_image = True
        prod_link.save()
        return
    screenshot.save()