Example #1
0
    def save_image(
            self,
            binary,
            application_code,
            code_key=None,
            filename=None,
            title=None,
            crop_left=None,
            crop_upper=None,
            crop_right=None,
            crop_lower=None,
            image_validation=None,
            type_key=None,
        ):

        im = self.verify_image(binary)
        if image_validation:
            image_validation(im)

        if crop_left or crop_upper or crop_right or crop_lower:
            crop_left = int(crop_left or 0)
            crop_upper = int(crop_upper or 0)
            crop_right = int(crop_right or 0)
            crop_lower = int(crop_lower or 0)
            width = int(im.size[0])
            height = int(im.size[1])

            if (crop_left + crop_right) >= width:
                raise Error('crop_left+crop_right', u('Left and right crop bigger then image width'))
            elif (crop_upper + crop_lower) >= height:
                raise Error('crop_upper+crop_lower', u('Upper and lower crop bigger then image height'))

            im = im.crop((int(crop_left), int(crop_upper), int(width - crop_right), int(height - crop_lower)))

        temporary_path = save_temporary_image(im)
        binary = get_open_file(temporary_path)

        f = self.save_file(
            binary,
            application_code,
            code_key,
            type_key=type_key,
            filename=filename,
            title=title,
        )

        if asbool(self.settings.get('thumb.create_on_add')):
            self.create_image_resizes.run_job()
        if asbool(self.settings.get('compress_on_add')):
            self.compress_images.run_job()

        if temporary_path:
            remove_file_quietly(temporary_path)

        return f
Example #2
0
    def read(self, size=-1):
        if size == 0:
            return b''

        try:
            open_block = self.blocks[self.block_position]
        except IndexError:
            return b''

        if isinstance(open_block, string_types):
            open_block = self.blocks[self.block_position] = get_open_file(join_paths(self.storage_path, open_block))

        binary = open_block.read(size)
        if size > 0:
            size -= len(binary)
            if size <= 0:
                return binary

        self.block_position += 1
        binary += self.read(size)
        return binary
Example #3
0
def create_temporary_file(mode='wb'):
    temporary_path = join_paths(FILES_TEMPORARY_DIR, make_unique_hash(64))
    open_file = get_open_file(temporary_path, mode=mode)
    return temporary_path, open_file
Example #4
0
    def resize_image(self, fid, application_code, resize_name):
        if application_code not in self.api_session_manager.resizes:
            raise Error('application_code', u('Invalid application code: %s') % application_code)

        resize = self.api_session_manager.resizes[application_code].get(resize_name)
        if not resize:
            raise Error('resize_name', u('Invalid resize name: %s') % resize_name)
        resize_width = maybe_integer(resize.get('width'))
        resize_height = maybe_integer(resize.get('height'))
        if not resize_width and not resize_height:
            raise Error('resize', 'Invalid resize options')

        file_info = self.get_file(
            id=fid,
            application_code=application_code,
            attributes=['open_file', 'key', 'filename', 'title', 'code_key'])
        if not file_info:
            raise Error('file', 'File ID not found')

        temporary_path = None
        type_key = u('resize-%s') % resize_name
        filename = None
        if file_info.filename:
            filename = u('%s-%s') % (resize_name, file_info.filename)

        lock_key = 'create image resize %s %s' % (fid, resize_name)
        self.cache.lock(lock_key)
        try:
            existing = (
                self.session
                .query(File)
                .filter(File.parent_id == fid)
                .filter(File.application_code == application_code)
                .filter(File.type_key == type_key)
                .first())

            if not existing:
                original_temporary_path, original_file = create_temporary_file(mode='wb')
                original_file.write(file_info.open_file.read())
                original_file.close()

                original_file = get_open_file(original_temporary_path)
                im = self.api_session_manager.image_cls.open(original_file)

                width = int(im.size[0])
                height = int(im.size[1])

                if not resize_width:
                    resize_width = ceil((float(width) * resize_height) / height)

                elif not resize_height:
                    resize_height = ceil((float(resize_width) * height) / width)

                else:
                    resize_racio = resize_width / float(resize_height)
                    image_racio = width / float(height)

                    if image_racio < resize_racio:
                        # Crop image on height
                        crop_size = ceil(round(height - (width / resize_racio)) / 2)
                        lower_position = int(height - int(crop_size))
                        # Crop as left, upper, right, and lower pixel
                        im = im.crop((0, int(crop_size), width, lower_position))

                    elif image_racio > resize_racio:
                        crop_size = ceil(round(width - (height * resize_racio)) / 2)
                        right_position = int(width - int(crop_size))
                        # Crop as left, upper, right, and lower pixel
                        im = im.crop((int(crop_size), 0, right_position, height))

                # Resize image
                im = im.resize((int(resize_width), int(resize_height)), self.api_session_manager.resize_quality)
                temporary_path = save_temporary_image(im)
                resized = get_open_file(temporary_path)

                original_file.close()
                remove_file_quietly(original_temporary_path)

                self.save_file(
                    resized,
                    application_code=application_code,
                    code_key=file_info.code_key,
                    type_key=type_key,
                    filename=filename,
                    title=file_info.title,
                    parent_id=fid)

        finally:
            self.cache.unlock(lock_key)

            if temporary_path:
                remove_file_quietly(temporary_path)

        return True