Example #1
0
def get_files_browse_urls(user=None):
    """
    Recursively walks all dirs under upload dir and generates a list of
    thumbnail and full image URL's for each file found.
    """
    files = []
    for filename in get_image_files(user=user):
        src = utils.get_media_url(filename)
        if getattr(settings, 'CKEDITOR_IMAGE_BACKEND', None):
            if is_image(src):
                thumb = utils.get_media_url(utils.get_thumb_filename(filename))
            else:
                thumb = utils.get_icon_filename(filename)
            visible_filename = os.path.split(filename)[1]
            if len(visible_filename) > 20:
                visible_filename = visible_filename[0:19] + '...'
        else:
            thumb = src
            visible_filename = os.path.split(filename)[1]
        files.append({
            'thumb': thumb,
            'src': src,
            'is_image': is_image(src),
            'visible_filename': visible_filename,
        })

    return files
Example #2
0
def get_files_browse_urls(user=None):
    """
    Recursively walks all dirs under upload dir and generates a list of
    thumbnail and full image URL's for each file found.
    """
    files = []
    for filename in get_image_files(user=user):
        src = utils.get_media_url(filename)
        if getattr(settings, 'CKEDITOR_IMAGE_BACKEND', None):
            if is_image(src):
                thumb = utils.get_media_url(utils.get_thumb_filename(filename))
            else:
                thumb = utils.get_icon_filename(filename)
            visible_filename = os.path.split(filename)[1]
            if len(visible_filename) > 20:
                visible_filename = visible_filename[0:19] + '...'
        else:
            thumb = src
            visible_filename = os.path.split(filename)[1]
        files.append({
            'thumb': thumb,
            'src': src,
            'is_image': is_image(src),
            'visible_filename': visible_filename,
        })

    return files
 def create_thumbnail(self, file_object, file_path):
     thumbnail_filename = utils.get_thumb_filename(file_path)
     thumbnail_io = BytesIO()
     image = Image.open(file_object).convert('RGB')
     image.thumbnail(THUMBNAIL_SIZE, Image.ANTIALIAS)
     image.save(thumbnail_io, format='JPEG', optimize=True)
     return self.storage_engine.save(thumbnail_filename, thumbnail_io)
Example #4
0
def create_thumbnail(file_path):
    thumbnail_filename = utils.get_thumb_filename(file_path)
    thumbnail_format = utils.get_image_format(os.path.splitext(file_path)[1])

    image = storage.open(file_path)
    image = Image.open(image)
    file_format = image.format

    # Convert to RGB if necessary
    # Thanks to Limodou on DjangoSnippets.org
    # http://www.djangosnippets.org/snippets/20/
    if image.mode not in ('L', 'RGB'):
        image = image.convert('RGB')

    # scale and crop to thumbnail
    imagefit = ImageOps.fit(image, THUMBNAIL_SIZE, Image.ANTIALIAS)
    thumbnail_io = BytesIO()
    imagefit.save(thumbnail_io, format=file_format)

    thumbnail = InMemoryUploadedFile(thumbnail_io, None, thumbnail_filename,
                                     thumbnail_format,
                                     len(thumbnail_io.getvalue()), None)
    thumbnail.seek(0)

    return storage.save(thumbnail_filename, thumbnail)
def create_thumbnail(file_path):
    thumbnail_filename = utils.get_thumb_filename(os.path.basename(file_path))
    thumbnail_format = utils.get_image_format(os.path.splitext(file_path)[1])
    file_format = thumbnail_format.split('/')[1]

    image_from_url = cStringIO.StringIO(urllib.urlopen(file_path).read())
    image = Image.open(image_from_url)

    # Convert to RGB if necessary
    # Thanks to Limodou on DjangoSnippets.org
    # http://www.djangosnippets.org/snippets/20/
    if image.mode not in ('L', 'RGB'):
        image = image.convert('RGB')

    # scale and crop to thumbnail
    imagefit = ImageOps.fit(image, THUMBNAIL_SIZE, Image.ANTIALIAS)
    thumbnail_io = BytesIO()
    imagefit.save(thumbnail_io, format=file_format)

    thumbnail = InMemoryUploadedFile(
        thumbnail_io,
        None,
        thumbnail_filename,
        thumbnail_format,
        len(thumbnail_io.getvalue()),
        None)
    thumbnail.seek(0)

    cc = CloudContainer('mediaplan-images')
    data = thumbnail.read()
    cc.upload_data(filename=thumbnail_filename, data=data)
    return thumbnail_filename
def create_thumbnail(file_path):
    thumbnail_filename = utils.get_thumb_filename(file_path)
    thumbnail_format = utils.get_image_format(os.path.splitext(file_path)[1])

    image = default_storage.open(file_path)
    image = Image.open(image)
    file_format = image.format

    # Convert to RGB if necessary
    # Thanks to Limodou on DjangoSnippets.org
    # http://www.djangosnippets.org/snippets/20/
    if image.mode not in ('L', 'RGB'):
        image = image.convert('RGB')

    # scale and crop to thumbnail
    imagefit = ImageOps.fit(image, THUMBNAIL_SIZE, Image.ANTIALIAS)
    thumbnail_io = BytesIO()
    imagefit.save(thumbnail_io, format=file_format)

    thumbnail = InMemoryUploadedFile(
        thumbnail_io,
        None,
        thumbnail_filename,
        thumbnail_format,
        len(thumbnail_io.getvalue()),
        None)
    thumbnail.seek(0)

    return default_storage.save(thumbnail_filename, thumbnail)
Example #7
0
def get_files_browse_urls(user=None):
    """
    Recursively walks all dirs under upload dir and generates a list of
    thumbnail and full image URL's for each file found.
    """
    files = []
    for filename in get_image_files(user=user):
        src = utils.get_media_url(filename)
        if getattr(settings, "CKEDITOR_IMAGE_BACKEND", None):
            if is_valid_image_extension(src):
                thumb = utils.get_media_url(utils.get_thumb_filename(filename))
            else:
                thumb = utils.get_icon_filename(filename)
            visible_filename = os.path.split(filename)[1]
            if len(visible_filename) > 20:
                visible_filename = visible_filename[0:19] + "..."
        else:
            thumb = src
            visible_filename = os.path.split(filename)[1]
        files.append(
            {
                "thumb": thumb,
                "src": src,
                "is_image": is_valid_image_extension(src),
                "visible_filename": visible_filename,
            }
        )

    return files
 def create_thumbnail(self, file_object, file_path):
     thumbnail_filename = utils.get_thumb_filename(file_path)
     thumbnail_io = BytesIO()
     image = Image.open(file_object).convert('RGB')
     image.thumbnail(THUMBNAIL_SIZE, Image.ANTIALIAS)
     image.save(thumbnail_io, format='JPEG', optimize=True)
     return self.storage_engine.save(thumbnail_filename, thumbnail_io)
Example #9
0
    def delete(self, request, **kwargs):
        file_to_de_deleted = request.GET['path']

        if is_image(file_to_de_deleted):
            thumbnail_filename_path = utils.get_thumb_filename(
                file_to_de_deleted)
            default_storage.delete(thumbnail_filename_path)

        default_storage.delete(file_to_de_deleted)
        return JsonResponse({'success': 1})
 def create_thumbnail(self, file_object, file_path):
     thumbnail_filename = utils.get_thumb_filename(file_path)
     thumbnail_io = BytesIO()
     # File object after saving e.g. to S3 can be closed.
     try:
         image = Image.open(file_object).convert('RGB')
     except ValueError:
         file_object = self.storage_engine.open(file_path)
         image = Image.open(file_object).convert('RGB')
     image.thumbnail(THUMBNAIL_SIZE, Image.ANTIALIAS)
     image.save(thumbnail_io, format='JPEG', optimize=True)
     return self.storage_engine.save(thumbnail_filename, thumbnail_io)
    def create_thumbnail(self, file_object, file_path):
        thumbnail_filename = utils.get_thumb_filename(file_path)
        thumbnail_io = BytesIO()
        # File object after saving e.g. to S3 can be closed.
        try:
            image = Image.open(self.file_object)
        except ValueError:
            file_object = self.storage_engine.open(file_path)
            image = Image.open(self.file_object)

        for orientation in ExifTags.TAGS.keys():
            if ExifTags.TAGS[orientation] == "Orientation":
                break

        if not hasattr(image, "_getexif"):
            image.thumbnail(THUMBNAIL_SIZE, Image.ANTIALIAS)
            image = image.convert("RGB")
            image.save(thumbnail_io, format="JPEG", optimize=True)
            return self.storage_engine.save(thumbnail_filename, thumbnail_io)

        exif = image._getexif()

        if exif is None:
            image.thumbnail(THUMBNAIL_SIZE, Image.ANTIALIAS)
            image = image.convert("RGB")
            image.save(thumbnail_io, format="JPEG", optimize=True)
            return self.storage_engine.save(thumbnail_filename, thumbnail_io)

        exif = dict(exif.items())

        try:
            if exif[orientation] == 3:
                image = image.rotate(180, expand=True)
            elif exif[orientation] == 6:
                image = image.rotate(270, expand=True)
            elif exif[orientation] == 8:
                image = image.rotate(90, expand=True)
        except KeyError:
            pass

        image.thumbnail(THUMBNAIL_SIZE, Image.ANTIALIAS)
        image = image.convert("RGB")
        image.save(thumbnail_io, format="JPEG", optimize=True)
        return self.storage_engine.save(thumbnail_filename, thumbnail_io)
Example #12
0
def append_file_images(filename,files,user):
    src = utils.get_media_url(filename)
    if getattr(settings, 'CKEDITOR_IMAGE_BACKEND', None):
        if is_image(src):
            thumb = utils.get_media_url(utils.get_thumb_filename(filename))
        else:
            thumb = utils.get_icon_filename(filename)
        visible_filename = os.path.split(filename)[1]
        if len(visible_filename) > 20:
            visible_filename = visible_filename[0:19] + '...'
    else:
        thumb = src
        visible_filename = os.path.split(filename)[1]
    files.append({
        'thumb': thumb,
        'src': src,
        'is_image': is_image(src),
        'visible_filename': visible_filename,
    })    
Example #13
0
    def delete(self, request, **kwargs):
        try:
            # Check if user is authenticated
            if request.user and request.user.is_authenticated:
                file_to_be_deleted = request.GET['path']
                full_user_path = os.path.join(settings.CKEDITOR_UPLOAD_PATH,
                                              _get_user_path(
                                                  request.user)) + '/'
                logger.info('User want to delete %s with user path %s' %
                            (file_to_be_deleted, full_user_path))

                # Secutiry: check if the file is owned by user.
                if full_user_path and full_user_path in file_to_be_deleted:
                    if is_valid_image_extension(file_to_be_deleted):
                        thumbnail_filename_path = utils.get_thumb_filename(
                            file_to_be_deleted)
                        storage.delete(thumbnail_filename_path)

                    storage.delete(file_to_be_deleted)
                    return JsonResponse({'success': 1})
        except Exception as error:
            pass

        return JsonResponse(data={'success': 0}, status=403)
Example #14
0
 def _thumbnail_exists(self, image_path):
     thumb_path = self._to_absolute_path(get_thumb_filename(image_path))
     return os.path.isfile(thumb_path)
 def _thumbnail_exists(self, image_path):
     thumb_path = self._to_absolute_path(
         get_thumb_filename(image_path)
     )
     return os.path.isfile(thumb_path)