Ejemplo n.º 1
0
 def post(self):
     remove_file(get_asset_path(self.current_user.background_img))
     self.current_user.background_img = ''
     try:
         orm.commit()
     except:
         pass
     result = {"status": "success", "message": "已成功重置背景图片"}
     self.send_result(result)
Ejemplo n.º 2
0
 def post(self):
     remove_file(get_asset_path(self.current_user.background_img))
     self.current_user.background_img = ''
     try:
         orm.commit()
     except:
         pass
     result = {"status": "success", "message": "已成功重置背景图片"}
     self.send_result(result)
Ejemplo n.º 3
0
 def reset_img(self, category):
     if category == 'background':
         path = self.background_img
         self.background_img = ''
     elif category == 'head':
         path = self.head_img
         self.head_img = ''
     else:
         return False
     try:
         remove_file(get_asset_path(path))
         return True
     except:
         return False
Ejemplo n.º 4
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)
Ejemplo n.º 5
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)