def get(self): """get attachment by key""" if self.request.get("key", None): attachment = Attachment.get(self.request.get("key")) if attachment: self.response.headers['Content-Type'] = attachment.content_type self.response.set_status(200) else: self.error(404) else: self.response.out.write("Paperclip storage engine")
def api_delete_attachment(attachment_id): u'删除附件API' check_admin() attachment = Attachment.get(attachment_id) if attachment is None: raise APIResourceNotFoundError('Attachment') # 删除本地文件 local_path = get_local_file_path(attachment.local_name) if os.path.exists(local_path): os.remove(local_path) # 删除数据库记录 attachment.delete() return dict(id=attachment_id)
def attachment(attachment_id): u'下载附件' attachment = Attachment.get(attachment_id) if attachment is None: raise notfound() local_path = get_local_file_path(attachment.local_name) if not os.path.exists(local_path): raise notfound() if not os.path.isfile(local_path): raise notfound() file_name_encode = quote(attachment.file_name.encode('utf-8')) ctx.response.set_header('Content-Length', os.path.getsize(local_path)) ctx.response.set_header('Content-Type', attachment.file_type) ctx.response.set_header( 'Content-Disposition', 'attachment;filename="%s";filename*=UTF-8\'\'%s' % (file_name_encode, file_name_encode)) return static_file_generator(local_path)
def get(self, key): a = Attachment.get(key) self.send_blob(a.content, content_type=a.content_type)