def put(self, image_id): image_id = int(image_id) image = Image.get(id=image_id) if not image: raise tornado.web.HTTPError(404) album_id = self.get_int('album_id', None) if not album_id: return self.send_error_result(msg=u'没有指定专辑哦') album = Album.get(id=album_id) if not album: return self.send_error_result(msg=u'专辑不存在') if album.user_id != self.current_user.id: return self.send_error_result(msg=u'此专辑不是您的专辑') if image.album_id != album_id: image.album_id = album_id return self.send_success_result()
def get(self, album_id): album_id = int(album_id) album = Album.get(id=album_id) if not album: raise tornado.web.HTTPError(404) return self.render("album/index.html", album=album, page=self.page)
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())
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())