Exemplo n.º 1
0
    def generateThumbnail(original_resource):
        thumbnailDir = settings.MEDIA_ROOT + 'resource_thumbnail/tmp/'
        thumbnail = thumbnailDir + str(original_resource.id)

        (resource, resource_type) = ResourceThumbnail.get_resource_type(original_resource)

        import urllib

        if resource_type == "video":

            try:
                provider = VideoHelper.getVideoProvider(resource.url)
                video_tag = VideoHelper.getVideoID(resource.url, provider)

                thumbnailUrl = ResourceThumbnail.getThumbnailFromProvider(video_tag, provider)

                # Returns a 120x90 image
                urllib.urlretrieve(thumbnailUrl, thumbnail + "-tmp" + ResourceThumbnail.THUMBNAIL_EXT)

                call(
                    ["convert", "-size", str(ResourceThumbnail.THUMBNAIL_HEIGHT) + "x"
                        + str(ResourceThumbnail.THUMBNAIL_WIDTH), "xc:white", thumbnail + ResourceThumbnail.THUMBNAIL_EXT])

                call(
                    ["composite", "-geometry", "-30-15", thumbnail + "-tmp" + ResourceThumbnail.THUMBNAIL_EXT,
                        thumbnail + ResourceThumbnail.THUMBNAIL_EXT, thumbnail + ResourceThumbnail.THUMBNAIL_EXT])

                # Now delete the temporary retrived image thumbnail
                call(["rm", thumbnail + "-tmp" + ResourceThumbnail.THUMBNAIL_EXT])

            except:
                resource.type = 'url'
                ResourceThumbnail.generateURLThumbnail(resource, thumbnail)

        elif resource_type == "article":
            ResourceThumbnail.generate_thumbnail(resource)

        elif resource_type == "url":
            ResourceThumbnail.generateURLThumbnail(resource, thumbnail)

        elif resource_type == "attachment":

            # Figure out the extension of the attachment
            from os.path import splitext
            name, resource.extension = splitext(resource.file.name)

            ms = [".doc", ".docx", ".ppt", ".pptx", ".rtf", "odt", ".odp"]
            pdf = [".pdf"]
            image = [".jpg", ".jpeg", ".png", ".bmp", ".eps", ".ps", ".gif", ".tiff"]

            ext = str.lower(str(resource.extension))

            if ext in ms:
                thumbnailSrcName = "ms.jpg"
            elif ext in pdf:
                if not settings.DEBUG:
                    urllib.urlretrieve(settings.MEDIA_URL + resource.revision.content.file.name,
                        settings.MEDIA_ROOT + resource.revision.content.file.name)

                call(
                    ["convert", "-density", "300",
                    settings.MEDIA_ROOT + resource.revision.content.file.name + '[0]',
                    thumbnail + "-tmp" + ResourceThumbnail.THUMBNAIL_EXT])

                call(
                    ["convert", thumbnail + "-tmp" + ResourceThumbnail.THUMBNAIL_EXT,
                        '-resize', '30%', '-gravity', 'center', '-crop', '120x120+0+0',
                        thumbnail + ResourceThumbnail.THUMBNAIL_EXT])

                # Now delete the temporary retrived image thumbnail
                call(["rm", thumbnail + "-tmp" + ResourceThumbnail.THUMBNAIL_EXT])

                if not settings.DEBUG:
                    import os
                    os.remove(settings.MEDIA_ROOT + resource.revision.content.file.name)

                thumbnailSrcName = "pdf.jpg"
            elif ext in image:
                thumbnailSrcName = "image.jpg"
            else:
                thumbnailSrcName = "blank.jpg"

            if ext not in pdf:
                call(
                    ["cp", settings.MEDIA_ROOT + 'resource_thumbnail/defaults/' + thumbnailSrcName,
                        thumbnail + ResourceThumbnail.THUMBNAIL_EXT])

        from django.core.files.images import ImageFile
        image_file = open(thumbnail + ResourceThumbnail.THUMBNAIL_EXT)
        thumbnail_to_assign = ImageFile(image_file)

        # NOTE(Varun): It's really important to keep the save as false, to avoid
        #     a recursion here
        resource.image.save(
            thumbnail_to_assign.name, thumbnail_to_assign, save=False)

        image_file.close()

        # Now delete the temporary image thumbnail
        call(["rm", thumbnail + ResourceThumbnail.THUMBNAIL_EXT])