Example #1
0
    def post(self, category):
        if not self.has_permission:
            return
        if not self.request.files or 'myfile' not in self.request.files:
            self.write({"status": "error", "message": "对不起,请选择文件"})
            return

        file_type_list = []
        if category == 'music':
            file_type_list = ['audio/mpeg', 'audio/x-wav', 'audio/mp3']
        if not file_type_list:
            return
        send_file = self.request.files['myfile'][0]
        if send_file['content_type'] not in file_type_list:
            if category == 'music':
                self.write({
                    "status": "error",
                    "message": "对不起,仅支持 mp3, wav 格式的音乐文件"
                })
                return

        if category == 'music':
            if len(send_file['body']) > 20 * 1024 * 1024:
                self.write({"status": "error", "message": "对不起,请上传20M以下的音乐文件"})
                return

        user = self.current_user
        if category == 'music':
            upload_path = os.path.join(config.upload_path, 'music', get_year(),
                                       get_month())
        else:
            return
        mkdir_p(upload_path)

        timestamp = str(int(time.time())) + gen_random_str() + '_' + str(
            user.id)
        image_format = send_file['filename'].split('.').pop().lower()
        filename = timestamp + '.' + image_format
        file_path = os.path.join(upload_path, filename)
        with open(file_path, 'wb') as f:
            f.write(send_file['body'])

        path = '/' + get_relative_path(file_path)

        if not self.is_ajax:
            return

        return self.write({
            'path': path,
            'status': "success",
            'message': '上传成功',
            'category': category,
            'content_type': send_file['content_type'],
        })
Example #2
0
    def post(self, category):
        if not self.has_permission:
            return
        if not self.request.files or 'myfile' not in self.request.files:
            self.write({"status": "error",
                        "message": "对不起,请选择文件"})
            return

        file_type_list = []
        if category == 'music':
            file_type_list = ['audio/mpeg', 'audio/x-wav', 'audio/mp3']
        if not file_type_list:
            return
        send_file = self.request.files['myfile'][0]
        if send_file['content_type'] not in file_type_list:
            if category == 'music':
                self.write({"status": "error",
                            "message": "对不起,仅支持 mp3, wav 格式的音乐文件"})
                return

        if category == 'music':
            if len(send_file['body']) > 20 * 1024 * 1024:
                self.write({"status": "error",
                            "message": "对不起,请上传20M以下的音乐文件"})
                return

        user = self.current_user
        if category == 'music':
            upload_path = os.path.join(config.upload_path, 'music', get_year(), get_month())
        else:
            return
        mkdir_p(upload_path)

        timestamp = str(int(time.time())) + gen_random_str() + '_' + str(user.id)
        image_format = send_file['filename'].split('.').pop().lower()
        filename = timestamp + '.' + image_format
        file_path = os.path.join(upload_path, filename)
        with open(file_path, 'wb') as f:
            f.write(send_file['body'])

        path = '/' + get_relative_path(file_path)

        if not self.is_ajax:
            return

        return self.write({
            'path': path,
            'status': "success",
            'message': '上传成功',
            'category': category,
            'content_type': send_file['content_type'],
        })
Example #3
0
 def post(self):
     if not self.has_permission:
         return
     user = self.current_user
     if not user:
         return self.redirect_next_url()
     if self.request.files == {} or 'myimage' not in self.request.files:
         self.write({"status": "error", "message": "对不起,请选择图片"})
         return
     image_type_list = [
         'image/gif', 'image/jpeg', 'image/pjpeg', 'image/png', 'image/bmp',
         'image/x-png'
     ]
     send_file = self.request.files['myimage'][0]
     if send_file['content_type'] not in image_type_list:
         self.write({
             "status":
             "error",
             "message":
             "对不起,仅支持 jpg, jpeg, bmp, gif, png\
                     格式的图片"
         })
         return
     if len(send_file['body']) > 6 * 1024 * 1024:
         self.write({"status": "error", "message": "对不起,请上传6M以下的图片"})
         return
     tmp_file = tempfile.NamedTemporaryFile(delete=True)
     tmp_file.write(send_file['body'])
     tmp_file.seek(0)
     try:
         image_one = Image.open(tmp_file.name)
     except IOError as error:
         logging.info(error)
         logging.info('+' * 30 + '\n')
         logging.info(self.request.headers)
         tmp_file.close()
         self.write({"status": "error", "message": "对不起,此文件不是图片"})
         return
     width = image_one.size[0]
     height = image_one.size[1]
     if width < 80 or height < 80 or width > 30000 or height > 30000:
         tmp_file.close()
         self.write({
             "status": "error",
             "message": "对不起,请上传长宽在80px~30000px之间的图片!"
         })
         return
     user = self.current_user
     upload_path = os.path.join(config.upload_path, get_year(), get_month())
     mkdir_p(upload_path)
     timestamp = str(int(time.time())) + gen_random_str() + '_' + str(
         user.id)
     image_format = send_file['filename'].split('.').pop().lower()
     filename = timestamp + '.' + image_format
     tmp_path = os.path.join(upload_path, filename)
     image_one.save(tmp_path)
     tmp_file.close()
     path = '/' + get_relative_path(tmp_path)
     category = self.get_argument('category', None)
     del_path = None
     if category == 'head':
         del_path = user.head_img
         user.head_img = path
         data = {'path': path, 'category': 'head'}
     elif category == 'background':
         del_path = user.background_img
         user.background_img = path
         data = {'path': path, 'category': 'background'}
     else:
         data = {'path': path, 'category': 'other'}
     if del_path:
         remove_file(get_asset_path(del_path))
     return self.send_success_result(**data)
Example #4
0
 def post(self):
     if self.request.files == {} or 'myavatar' not in self.request.files:
         self.write({"status": "error", "message": "请选择图片!"})
         return
     image_type_list = [
         'image/gif', 'image/jpeg', 'image/pjpeg', 'image/png', 'image/bmp',
         'image/x-png'
     ]
     send_file = self.request.files['myavatar'][0]
     if send_file['content_type'] not in image_type_list:
         self.write({
             "status":
             "error",
             "message":
             "仅支持 jpg, jpeg, bmp, gif, png\
                     格式的图片!"
         })
         return
     if len(send_file['body']) > 100 * 1024 * 1024:
         self.write({"status": "error", "message": "请上传100M以下的图片!"})
         return
     tmp_file = tempfile.NamedTemporaryFile(delete=True)
     tmp_file.write(send_file['body'])
     tmp_file.seek(0)
     try:
         image_one = Image.open(tmp_file.name)
     except IOError as error:
         logging.info(error)
         logging.info('+' * 30 + '\n')
         logging.info(self.request.headers)
         tmp_file.close()
         self.write({"status": "error", "message": "图片不合法!"})
         return
     width = image_one.size[0]
     height = image_one.size[1]
     if width < 24 or height < 24 or width > 20000 or height > 20000:
         tmp_file.close()
         self.write({"status": "error", "message": "图片长宽在24px~20000px之间!"})
         return
     timestamp = str(int(time.time()))
     user = self.current_user
     upload_path = os.path.join(config.upload_path, 'avatar')
     mkdir_p(upload_path)
     if user:
         timestamp += '_' + str(user.id)
     else:
         timestamp += '_' + gen_random_str()
     image_format = send_file['filename'].split('.').pop().lower()
     filename = timestamp + '.' + image_format
     tmp_path = os.path.join(upload_path, filename)
     if os.path.exists(tmp_path):
         while True:
             if os.path.exists(tmp_path):
                 timestamp += '_' + gen_random_str()
                 filename = timestamp + '.' + image_format
                 tmp_path = os.path.join(upload_path, filename)
             else:
                 break
     image_one.save(tmp_path)
     tmp_file.close()
     src = '/' + get_relative_path(tmp_path)
     if user:
         user.avatar_tmp = src
     data = {"src": src, "height": height, "width": width}
     return self.send_success_result(msg=u'成功上传头像', **data)
Example #5
0
    def post(self):
        if not self.request.files or 'myimage' not in self.request.files:
            self.write({"status": "error",
                        "message": "对不起,请选择图片"})
            return
        image_type_list = ['image/gif', 'image/jpeg', 'image/pjpeg',
                           'image/png', 'image/bmp', 'image/x-png']
        send_file = self.request.files['myimage'][0]
        if send_file['content_type'] not in image_type_list:
            self.write({"status": "error",
                        "message": "对不起,仅支持 jpg, jpeg, bmp, gif, png\
                        格式的图片"})
            return
        if len(send_file['body']) > 100 * 1024 * 1024:
            self.write({"status": "error",
                        "message": "对不起,请上传100M以下的图片"})
            return

        tmp_file = tempfile.NamedTemporaryFile(delete=True)
        tmp_file.write(send_file['body'])
        tmp_file.seek(0)
        try:
            img = Img.open(tmp_file.name)
        except IOError as error:
            logging.info(error)
            logging.info('+' * 30 + '\n')
            logging.info(self.request.headers)
            tmp_file.close()
            self.write({"status": "error",
                        "message": "对不起,此文件不是图片"})
            return

        width, height = img.size
        if width < 80 or height < 80 or width > 30000 or height > 30000:
            tmp_file.close()
            self.write({"status": "error",
                        "message": "对不起,请上传长宽在80px~30000px之间的图片!"})
            return

        user = self.current_user
        suffix = img.format.lower()
        upload_path = gen_upload_path(suffix=suffix)

        img.save(upload_path, img.format or 'JPEG')
        tmp_file.close()

        path = '/%s' % get_relative_path(upload_path).lstrip('/')
        album_id = self.get_argument('album_id', '')
        if not album_id:
            album = user.default_album
        else:
            album = Album.get(id=album_id)
            if not (album and album.user_id != user.id):
                album = user.default_album
        image = Image(user_id=user.id,
                      album_id=album.id,
                      path=path,
                      width=width,
                      height=height).save()
        image.crop()
        return self.send_success_result(msg='上传成功', **image.to_dict())
Example #6
0
 def post(self):
     if not self.has_permission:
         return
     user = self.current_user
     if not user:
         return self.redirect_next_url()
     if self.request.files == {} or 'myimage' not in self.request.files:
         self.write({"status": "error",
                     "message": "对不起,请选择图片"})
         return
     image_type_list = ['image/gif', 'image/jpeg', 'image/pjpeg',
                        'image/png', 'image/bmp', 'image/x-png']
     send_file = self.request.files['myimage'][0]
     if send_file['content_type'] not in image_type_list:
         self.write({"status": "error",
                     "message": "对不起,仅支持 jpg, jpeg, bmp, gif, png\
                     格式的图片"})
         return
     if len(send_file['body']) > 6 * 1024 * 1024:
         self.write({"status": "error",
                     "message": "对不起,请上传6M以下的图片"})
         return
     tmp_file = tempfile.NamedTemporaryFile(delete=True)
     tmp_file.write(send_file['body'])
     tmp_file.seek(0)
     try:
         image_one = Image.open(tmp_file.name)
     except IOError as error:
         logging.info(error)
         logging.info('+' * 30 + '\n')
         logging.info(self.request.headers)
         tmp_file.close()
         self.write({"status": "error",
                     "message": "对不起,此文件不是图片"})
         return
     width = image_one.size[0]
     height = image_one.size[1]
     if width < 80 or height < 80 or width > 30000 or height > 30000:
         tmp_file.close()
         self.write({"status": "error",
                     "message": "对不起,请上传长宽在80px~30000px之间的图片!"})
         return
     user = self.current_user
     upload_path = os.path.join(config.upload_path, get_year(), get_month())
     mkdir_p(upload_path)
     timestamp = str(int(time.time())) + gen_random_str() + '_' + str(user.id)
     image_format = send_file['filename'].split('.').pop().lower()
     filename = timestamp + '.' + image_format
     tmp_path = os.path.join(upload_path, filename)
     image_one.save(tmp_path)
     tmp_file.close()
     path = '/' + get_relative_path(tmp_path)
     category = self.get_argument('category', None)
     del_path = None
     if category == 'head':
         del_path = user.head_img
         user.head_img = path
         data = {'path': path, 'category': 'head'}
     elif category == 'background':
         del_path = user.background_img
         user.background_img = path
         data = {'path': path, 'category': 'background'}
     else:
         data = {'path': path, 'category': 'other'}
     if del_path:
         remove_file(get_asset_path(del_path))
     return self.send_success_result(**data)
Example #7
0
 def post(self):
     if self.request.files == {} or 'myavatar' not in self.request.files:
         self.write({"status": "error",
                     "message": "请选择图片!"})
         return
     image_type_list = ['image/gif', 'image/jpeg', 'image/pjpeg',
                        'image/png', 'image/bmp', 'image/x-png']
     send_file = self.request.files['myavatar'][0]
     if send_file['content_type'] not in image_type_list:
         self.write({"status": "error",
                     "message": "仅支持 jpg, jpeg, bmp, gif, png\
                     格式的图片!"})
         return
     if len(send_file['body']) > 100 * 1024 * 1024:
         self.write({"status": "error",
                     "message": "请上传100M以下的图片!"})
         return
     tmp_file = tempfile.NamedTemporaryFile(delete=True)
     tmp_file.write(send_file['body'])
     tmp_file.seek(0)
     try:
         image_one = Image.open(tmp_file.name)
     except IOError as error:
         logging.info(error)
         logging.info('+' * 30 + '\n')
         logging.info(self.request.headers)
         tmp_file.close()
         self.write({"status": "error",
                     "message": "图片不合法!"})
         return
     width = image_one.size[0]
     height = image_one.size[1]
     if width < 24 or height < 24 or width > 20000 or height > 20000:
         tmp_file.close()
         self.write({"status": "error",
                     "message": "图片长宽在24px~20000px之间!"})
         return
     timestamp = str(int(time.time()))
     user = self.current_user
     upload_path = os.path.join(config.upload_path, 'avatar')
     mkdir_p(upload_path)
     if user:
         timestamp += '_' + str(user.id)
     else:
         timestamp += '_' + gen_random_str()
     image_format = send_file['filename'].split('.').pop().lower()
     filename = timestamp + '.' + image_format
     tmp_path = os.path.join(upload_path, filename)
     if os.path.exists(tmp_path):
         while True:
             if os.path.exists(tmp_path):
                 timestamp += '_' + gen_random_str()
                 filename = timestamp + '.' + image_format
                 tmp_path = os.path.join(upload_path, filename)
             else:
                 break
     image_one.save(tmp_path)
     tmp_file.close()
     src = '/' + get_relative_path(tmp_path)
     if user:
         user.avatar_tmp = src
     data = {"src": src, "height": height, "width": width}
     return self.send_success_result(msg=u'成功上传头像', **data)
Example #8
0
class UploadHandler(BaseHandler):
    @orm.db_session
    @tornado.web.authenticated
    @require_permission
    def post(self):
        if not self.request.files or 'myimage' not in self.request.files:
            self.write({"status": "error", "message": "对不起,请选择图片"})
            return
        image_type_list = [
            'image/gif', 'image/jpeg', 'image/pjpeg', 'image/png', 'image/bmp',
            'image/x-png'
        ]
        send_file = self.request.files['myimage'][0]
        if send_file['content_type'] not in image_type_list:
            self.write({
                "status":
                "error",
                "message":
                "对不起,仅支持 jpg, jpeg, bmp, gif, png\
                        格式的图片"
            })
            return
        if len(send_file['body']) > 100 * 1024 * 1024:
            self.write({"status": "error", "message": "对不起,请上传100M以下的图片"})
            return

        tmp_file = tempfile.NamedTemporaryFile(delete=True)
        tmp_file.write(send_file['body'])
        tmp_file.seek(0)
        try:
            img = Img.open(tmp_file.name)
        except IOError, error:
            logging.info(error)
            logging.info('+' * 30 + '\n')
            logging.info(self.request.headers)
            tmp_file.close()
            self.write({"status": "error", "message": "对不起,此文件不是图片"})
            return

        width, height = img.size
        if width < 80 or height < 80 or width > 30000 or height > 30000:
            tmp_file.close()
            self.write({
                "status": "error",
                "message": "对不起,请上传长宽在80px~30000px之间的图片!"
            })
            return

        user = self.current_user
        suffix = img.format.lower()
        upload_path = gen_upload_path(suffix=suffix)

        img.save(upload_path, img.format or 'JPEG')
        tmp_file.close()

        path = '/%s' % get_relative_path(upload_path).lstrip('/')
        album_id = self.get_argument('album_id', '')
        if not album_id:
            album = user.default_album
        else:
            album = Album.get(id=album_id)
            if not (album and album.user_id != user.id):
                album = user.default_album
        image = Image(user_id=user.id,
                      album_id=album.id,
                      path=path,
                      width=width,
                      height=height).save()
        image.crop()
        return self.send_success_result(msg=u'上传成功', **image.to_dict())