Example #1
0
    def process_content(self, content, filename=None, content_type=None):
        orig_content = content
        content = utils.file_from_content(content)
        __, filename, content_type = FileStorage.fileinfo(orig_content)

        uploaded_image = Image.open(content)
        if max(uploaded_image.size) >= self.max_size:
            uploaded_image.thumbnail((self.max_size, self.max_size),
                                     Image.BILINEAR)
            content = SpooledTemporaryFile(INMEMORY_FILESIZE)
            uploaded_image.save(content, uploaded_image.format)

        content.seek(0)
        super(UploadedImageWithThumb,
              self).process_content(content, filename, content_type)

        thumbnail = uploaded_image.copy()
        thumbnail.thumbnail(self.thumbnail_size, Image.ANTIALIAS)
        thumbnail = thumbnail.convert('RGBA')
        thumbnail.format = self.thumbnail_format

        output = SpooledTemporaryFile(INMEMORY_FILESIZE)
        thumbnail.save(output, self.thumbnail_format)
        output.seek(0)

        thumb_path, thumb_id = self.store_content(
            output, 'thumb.%s' % self.thumbnail_format.lower())
        self['thumb_id'] = thumb_id
        self['thumb_path'] = thumb_path

        thumbnail_file = self.thumb_file
        self['_thumb_public_url'] = thumbnail_file.public_url
Example #2
0
    def generate_thumbnail(self, content):
        content = file_from_content(content)
        uploaded_image = Image.open(content)
        if max(uploaded_image.size) >= self.max_size:
            uploaded_image.thumbnail((self.max_size, self.max_size), Image.BILINEAR)
            content = SpooledTemporaryFile(INMEMORY_FILESIZE)
            uploaded_image.save(content, uploaded_image.format)

        content.seek(0)

        thumbnail = uploaded_image.copy()
        thumbnail.thumbnail(self.thumbnail_size, Image.ANTIALIAS)
        thumbnail = thumbnail.convert('RGBA')
        thumbnail.format = self.thumbnail_format

        output = SpooledTemporaryFile(INMEMORY_FILESIZE)
        thumbnail.save(output, self.thumbnail_format)
        output.seek(0)

        thumb_path, thumb_id = self.store_content(output,
                                                  'thumb.%s' % self.thumbnail_format.lower())
        self['thumb_id'] = thumb_id
        self['thumb_path'] = thumb_path

        thumbnail_file = self.thumb_file
        self['_thumb_public_url'] = thumbnail_file.public_url
        content.close()
Example #3
0
    def generate_thumbnail(self, content):
        content = file_from_content(content)
        try:
            uploaded_image = Image.open(content)
        except Exception:
            flask.abort(400, ValidationError(
                {
                    'message': 'File Format',
                    'object': {"error": "Format Incorrect"},
                }
            ))
        if max(uploaded_image.size) >= self.max_size:
            uploaded_image.thumbnail((self.max_size, self.max_size), Image.BILINEAR)
            content = SpooledTemporaryFile(INMEMORY_FILESIZE)
            uploaded_image.save(content, uploaded_image.format)

        content.seek(0)

        thumbnail = uploaded_image.copy()
        thumbnail.thumbnail(self.thumbnail_size, Image.ANTIALIAS)
        thumbnail = thumbnail.convert('RGBA')
        thumbnail.format = self.thumbnail_format

        output = SpooledTemporaryFile(INMEMORY_FILESIZE)
        thumbnail.save(output, self.thumbnail_format)
        output.seek(0)

        thumb_path, thumb_id = self.store_content(output,
                                                  'thumb.%s' % self.thumbnail_format.lower())
        self['thumb_id'] = thumb_id
        self['thumb_path'] = thumb_path

        thumbnail_file = self.thumb_file
        self['_thumb_public_url'] = thumbnail_file.public_url
        content.close()
Example #4
0
    def process_content(self, content, filename=None, content_type=None):
        orig_content = content
        content = utils.file_from_content(content)
        __, filename, content_type = FileStorage.fileinfo(orig_content)

        uploaded_image = Image.open(content)
        if max(uploaded_image.size) >= self.max_size:
            uploaded_image.thumbnail((self.max_size, self.max_size), Image.BILINEAR)
            content = SpooledTemporaryFile(INMEMORY_FILESIZE)
            uploaded_image.save(content, uploaded_image.format)

        content.seek(0)
        super(UploadedImageWithThumb, self).process_content(content, filename, content_type)

        thumbnail = uploaded_image.copy()
        thumbnail.thumbnail(self.thumbnail_size, Image.ANTIALIAS)
        thumbnail = thumbnail.convert('RGBA')
        thumbnail.format = self.thumbnail_format

        output = SpooledTemporaryFile(INMEMORY_FILESIZE)
        thumbnail.save(output, self.thumbnail_format)
        output.seek(0)

        thumb_path, thumb_id = self.store_content(output,
                                                  'thumb.%s' % self.thumbnail_format.lower())
        self['thumb_id'] = thumb_id
        self['thumb_path'] = thumb_path

        thumbnail_file = self.thumb_file
        self['_thumb_public_url'] = thumbnail_file.public_url
Example #5
0
    def generate_thumbnail(self, content):
        content = file_from_content(content)
        uploaded_image = Image.open(content)
        if max(uploaded_image.size) >= self.max_size:
            uploaded_image.thumbnail((self.max_size, self.max_size),
                                     Image.BILINEAR)
            content = SpooledTemporaryFile(INMEMORY_FILESIZE)
            uploaded_image.save(content, uploaded_image.format)

        content.seek(0)

        thumbnail = uploaded_image.copy()
        thumbnail.thumbnail(self.thumbnail_size, Image.ANTIALIAS)
        thumbnail = thumbnail.convert('RGBA')
        thumbnail.format = self.thumbnail_format

        output = SpooledTemporaryFile(INMEMORY_FILESIZE)
        thumbnail.save(output, self.thumbnail_format)
        output.seek(0)

        thumb_path, thumb_id = self.store_content(
            output, 'thumb.%s' % self.thumbnail_format.lower())
        self['thumb_id'] = thumb_id
        self['thumb_path'] = thumb_path

        thumbnail_file = self.thumb_file
        self['_thumb_public_url'] = thumbnail_file.public_url
        content.close()
Example #6
0
 def process_content(self, content, *args, **kwargs):
     mod = ModuleType('uploaded_module')
     content = file_from_content(content)
     content = content.read()
     exec(content.decode('utf-8'), mod.__dict__)
     self.ensure_module(mod)
     super().process_content(content, *args, **kwargs)
     UploadedPythonModule.module_cache[self['file_id']] = mod
Example #7
0
 def process_content(self, content, filename=None, content_type=None):
     file = file_from_content(content)
     FileTypeCheckFilter(
         filetypes=["raster-image", "raw-image", "vector-image"
                    ]).validate_filetype(file)
     __, filename, content_type = FileStorage.fileinfo(content)
     for name, size in self.thumbnail_sizes.items():
         self.store_thumbnail(file, filename, name, size)
     content.seek(0)
     super().process_content(content, filename, content_type)
Example #8
0
    def process_content(self, content, filename=None, content_type=None):
        """process_content"""
        __, filename, content_type = FileStorage.fileinfo(content)

        # Get a file object even if content was bytes
        content = utils.file_from_content(content)
        content.seek(0, 2)
        if content.tell() >= self.max_size:
            raise ValueError(
                _(f"Yüklenen resim boyutu {self.max_size/1024} KB büyük olamaz."
                  ))
        content.seek(0)
        super(LimitedSizeUploadedFile,
              self).process_content(content, filename, content_type)
Example #9
0
    def process_content(self, content, filename=None, content_type=None):
        filename, content_type = FileStorage.fileinfo(content)[1:]
        content = utils.file_from_content(content)

        try:
            for processor in self.processors:
                content = processor(self, content, content_type) or content
                content.seek(0)
        except Image.DecompressionBombError:
            # not a real content type - but useful for us to be able to rely
            # on anything uploaded having a content type, even though that
            # content is discarded soon afterwards
            content = b''
            content_type = 'application/malicious'

        super().process_content(content, filename, content_type)
Example #10
0
    def on_save(self, uploaded_file):
        content = utils.file_from_content(uploaded_file.original_content)

        thumbnail = Image.open(content)
        thumbnail.thumbnail(self.thumbnail_size, Image.BILINEAR)
        thumbnail = thumbnail.convert('RGBA')
        thumbnail.format = self.thumbnail_format

        output = BytesIO()
        thumbnail.save(output, self.thumbnail_format)
        output.seek(0)

        thumb_file_name = 'thumb.%s' % self.thumbnail_format.lower()
        thumb_path, thumb_id = uploaded_file.store_content(output, thumb_file_name)
        uploaded_file['thumb_id'] = thumb_id
        uploaded_file['thumb_path'] = thumb_path
        uploaded_file['thumb_url'] = DepotManager.get_middleware().url_for(thumb_path)
Example #11
0
    def on_save(self, uploaded_file):
        content = utils.file_from_content(uploaded_file.original_content)

        thumbnail = Image.open(content)
        thumbnail.thumbnail(self.thumbnail_size, Image.BILINEAR)
        thumbnail = thumbnail.convert('RGBA')
        thumbnail.format = self.thumbnail_format

        output = BytesIO()
        thumbnail.save(output, self.thumbnail_format)
        output.seek(0)

        thumb_name = 'thumb_%sx%s' % self.thumbnail_size
        thumb_file_name = '%s.%s' % (thumb_name, self.thumbnail_format.lower())
        thumb_path, thumb_id = uploaded_file.store_content(output, thumb_file_name)
        uploaded_file[thumb_name + '_id'] = thumb_id
        uploaded_file[thumb_name + '_path'] = thumb_path
        uploaded_file[thumb_name + '_url'] = DepotManager.get_middleware().url_for(thumb_path)
Example #12
0
    def process_content(self, content, filename=None, content_type=None):
        """process_content"""
        __, filename, content_type = FileStorage.fileinfo(content)

        # Get a file object even if content was bytes
        content = utils.file_from_content(content)

        uploaded_image = Image.open(content)
        width, height = uploaded_image.size
        if content_type not in self.accepted_file_types:
            raise ValueError(
                _("Yüklenen resim bu dosya türlerinden biri olmalıdır : {}".
                  format(', '.join(
                      [i.split('/')[1] for i in self.accepted_file_types]))))
        if width > self.max_width or height > self.max_height:
            raise ValueError(
                _(f"Yüklenen resim boyutu {self.max_width}x{self.max_height}"
                  f" pikselden büyük olamaz."))

        content.seek(0)
        super(UploadedImageWithMaxDimensions,
              self).process_content(content, filename, content_type)
Example #13
0
    def by_content(self, content):
        """ Returns a query that matches the given content (may be more than
        one record).

        """
        file = file_from_content(content)

        # we need to look up two checksums, the one of the file stored and
        # possibly the one it had before signing
        #
        # XXX maybe it makes sense to combine those into a data structure
        # that holds all kinds of digests a file is known under

        # checksum
        md5 = digest(file, 'md5')

        # old_digest of signature_metadata
        sha = digest(file, 'sha256')

        return self.query().filter(or_(
            File.checksum == md5,
            File.signature_metadata['old_digest'].astext == sha
        ))
Example #14
0
 def on_save(self, uploaded_file):
     fp = file_from_content(uploaded_file.original_content)
     self.validate_filetype(fp)
Example #15
0
 def on_save(self, uploaded_file):
     fp = file_from_content(uploaded_file.original_content)
     self.store_thumbnail(uploaded_file, self.generate_thumbnail(fp))