Exemplo n.º 1
0
    def avatar(self, image_data):
        if MIN_AVATAR_SIZE < len(image_data) < MAX_AVATAR_SIZE:
            ext = imghdr.what("", h=image_data)
            if ext in IMG_FORMATS and not is_xss_image(image_data):
                try:
                    if self._avatar is not None and os.path.exists(
                        options.avatar_dir + "/upload/" + self._avatar
                    ):
                        os.unlink(options.avatar_dir + "/upload/" + self._avatar)
                    file_path = str(
                        options.avatar_dir + "/upload/" + self.uuid + "." + ext
                    )
                    image = Image.open(io.BytesIO(image_data))
                    cover = resizeimage.resize_cover(image, [500, 250])
                    cover.save(file_path, image.format)
                    self._avatar = "upload/" + self.uuid + "." + ext
                except Exception as e:
                    raise ValidationError(e)

            else:
                raise ValidationError(
                    "Invalid image format, avatar must be: %s"
                    % (", ".join(IMG_FORMATS))
                )
        else:
            raise ValidationError(
                "The image is too large must be %d - %d bytes"
                % (MIN_AVATAR_SIZE, MAX_AVATAR_SIZE)
            )
Exemplo n.º 2
0
 def avatar(self, image_data):
     if self.uuid is None:
         self.uuid = str(uuid4())
     if len(image_data) < (1024 * 1024):
         ext = imghdr.what("", h=image_data)
         if ext in ["png", "jpeg", "gif", "bmp"
                    ] and not is_xss_image(image_data):
             try:
                 if self._avatar is not None and os.path.exists(
                         options.avatar_dir + "/upload/" + self._avatar):
                     os.unlink(options.avatar_dir + "/upload/" +
                               self._avatar)
                 file_path = str(options.avatar_dir + "/upload/" +
                                 self.uuid + "." + ext)
                 image = Image.open(io.BytesIO(image_data))
                 cover = resizeimage.resize_cover(image, [500, 250])
                 cover.save(file_path, image.format)
                 self._avatar = "upload/" + self.uuid + "." + ext
             except Exception as e:
                 raise ValidationError(e)
         else:
             raise ValidationError(
                 "Invalid image format, avatar must be: .png .jpeg .gif or .bmp"
             )
     else:
         raise ValidationError("The image is too large")
Exemplo n.º 3
0
def save_config_image(b64_data):
    image_data = bytearray(b64decode(b64_data))
    if len(image_data) < (2048 * 2048):
        ext = imghdr.what("", h=image_data)
        file_name = "story/%s.%s" % (hashlib.sha1(image_data).hexdigest(), ext)
        if ext in ["png", "jpeg", "gif", "bmp"
                   ] and not is_xss_image(image_data):
            with open("files/" + file_name, "wb") as fp:
                fp.write(image_data)
            return file_name
        else:
            raise ValidationError(
                "Invalid image format, avatar must be: .png .jpeg .gif or .bmp"
            )
    else:
        raise ValidationError("The image is too large")
Exemplo n.º 4
0
 def avatar(self, image_data):
     if self.uuid is None:
         self.uuid = str(uuid4())
     if len(image_data) < (1024 * 1024):
         ext = imghdr.what("", h=image_data)
         if ext in ['png', 'jpeg', 'gif', 'bmp'] and not is_xss_image(image_data):
             if self._avatar is not None and os.path.exists(options.avatar_dir + '/' + self._avatar):
                 os.unlink(options.avatar_dir + '/' + self._avatar)
             file_path = str(options.avatar_dir + '/' + self.uuid + '.' + ext)
             with open(file_path, 'wb') as fp:
                 fp.write(image_data)
             self._avatar = self.uuid + '.' + ext
         else:
             raise ValidationError(
                 "Invalid image format, avatar must be: .png .jpeg .gif or .bmp")
     else:
         raise ValidationError("The image is too large")
Exemplo n.º 5
0
 def avatar(self, image_data):
     if self.uuid is None:
         self.uuid = str(uuid4())
     if len(image_data) < (1024 * 1024):
         ext = imghdr.what("", h=image_data)
         if ext in ['png', 'jpeg', 'gif', 'bmp'] and not is_xss_image(image_data):
             if self._avatar is not None and os.path.exists(options.avatar_dir + '/' + self._avatar):
                 os.unlink(options.avatar_dir + '/' + self._avatar)
             file_path = str(options.avatar_dir + '/' + self.uuid + '.' + ext)
             with open(file_path, 'wb') as fp:
                 fp.write(image_data)
             self._avatar = self.uuid + '.' + ext
         else:
             raise ValidationError(
                 "Invalid image format, avatar must be: .png .jpeg .gif or .bmp")
     else:
         raise ValidationError("The image is too large")
Exemplo n.º 6
0
 def avatar(self, image_data):
     if self.uuid is None:
         self.uuid = str(uuid4())
     if len(image_data) < (1024 * 1024):
         ext = imghdr.what("", h=image_data)
         if ext in ['png', 'jpeg', 'gif', 'bmp'] and not is_xss_image(image_data):
             if self._avatar is not None and os.path.exists(options.avatar_dir + '/upload/' + self._avatar):
                 os.unlink(options.avatar_dir + '/upload/' + self._avatar)
             file_path = str(options.avatar_dir + '/upload/' + self.uuid + '.' + ext)
             image = Image.open(StringIO.StringIO(image_data))
             cover = resizeimage.resize_cover(image, [500, 250])
             cover.save(file_path, image.format)
             self._avatar = 'upload/' + self.uuid + '.' + ext
         else:
             raise ValidationError(
                 "Invalid image format, avatar must be: .png .jpeg .gif or .bmp")
     else:
         raise ValidationError("The image is too large")
Exemplo n.º 7
0
 def avatar(self, image_data):
     if MIN_AVATAR_SIZE < len(image_data) < MAX_AVATAR_SIZE:
         ext = imghdr.what("", h=image_data)
         if ext in IMG_FORMATS and not is_xss_image(image_data):
             if self._avatar is not None and os.path.exists(options.avatar_dir + '/' + self._avatar):
                 os.unlink(options.avatar_dir + '/' + self._avatar)
             file_path = str(options.avatar_dir + '/' + self.uuid + '.' + ext)
             with open(file_path, 'wb') as fp:
                 fp.write(image_data)
             self._avatar = self.uuid + '.' + ext
         else:
             raise ValidationError("Invalid image format, avatar must be: %s" % (
                 ' '.join(IMG_FORMATS)
             ))
     else:
         raise ValidationError("The image is too large must be %d - %d bytes"  % (
             MIN_AVATAR_SIZE, MAX_AVATAR_SIZE
         ))
Exemplo n.º 8
0
 def avatar(self, image_data):
     if self.uuid is None:
         self.uuid = str(uuid4())
     if len(image_data) < (1024 * 1024):
         ext = imghdr.what("", h=image_data)
         if ext in ['png', 'jpeg', 'gif', 'bmp'] and not is_xss_image(image_data):
             if self._avatar is not None and os.path.exists(options.avatar_dir + '/upload/' + self._avatar):
                 os.unlink(options.avatar_dir + '/upload/' + self._avatar)
             file_path = str(options.avatar_dir + '/upload/' + self.uuid + '.' + ext)
             image = Image.open(StringIO.StringIO(image_data))
             cover = resizeimage.resize_cover(image, [500, 250])
             cover.save(file_path, image.format)
             self._avatar = 'upload/' + self.uuid + '.' + ext
         else:
             raise ValidationError(
                 "Invalid image format, avatar must be: .png .jpeg .gif or .bmp")
     else:
         raise ValidationError("The image is too large")
Exemplo n.º 9
0
 def avatar(self, image_data):
     if MIN_AVATAR_SIZE < len(image_data) < MAX_AVATAR_SIZE:
         ext = imghdr.what("", h=image_data)
         if ext in IMG_FORMATS and not is_xss_image(image_data):
             if self._avatar is not None and os.path.exists(options.avatar_dir + '/upload/' + self._avatar):
                 os.unlink(options.avatar_dir + '/upload/' + self._avatar)
             file_path = str(options.avatar_dir + '/upload/' + self.uuid + '.' + ext)
             image = Image.open(StringIO.StringIO(image_data))
             cover = resizeimage.resize_cover(image, [500, 250])
             cover.save(file_path, image.format)
             self._avatar = 'upload/' + self.uuid + '.' + ext
         else:
             raise ValidationError("Invalid image format, avatar must be: %s" % (
                 ' '.join(IMG_FORMATS)
             ))
     else:
         raise ValidationError("The image is too large must be %d - %d bytes"  % (
             MIN_AVATAR_SIZE, MAX_AVATAR_SIZE
         ))
Exemplo n.º 10
0
 def avatar(self, image_data):
     if MIN_AVATAR_SIZE < len(image_data) < MAX_AVATAR_SIZE:
         ext = imghdr.what("", h=image_data)
         if ext in IMG_FORMATS and not is_xss_image(image_data):
             if self._avatar is not None and os.path.exists(
                     options.avatar_dir + '/' + self._avatar):
                 os.unlink(options.avatar_dir + '/' + self._avatar)
             file_path = str(options.avatar_dir + '/' + self.uuid + '.' +
                             ext)
             with open(file_path, 'wb') as fp:
                 fp.write(image_data)
             self._avatar = self.uuid + '.' + ext
         else:
             raise ValidationError(
                 "Invalid image format, avatar must be: %s" %
                 (' '.join(IMG_FORMATS)))
     else:
         raise ValidationError(
             "The image is too large must be %d - %d bytes" %
             (MIN_AVATAR_SIZE, MAX_AVATAR_SIZE))