niming_backend/blueprints/general/article.py

114 lines
3.9 KiB
Python
Raw Normal View History

2024-11-19 21:22:01 +08:00
import magic
2024-11-26 17:38:28 +08:00
from flask import Blueprint, request, abort
2024-11-19 21:22:01 +08:00
from google.protobuf.message import DecodeError
2024-12-09 03:24:22 +08:00
from utils import pgclass, setting_loader, dbhelper
from utils.misc import internal_json2protobuf, error_proto
from protobuf_files import niming_pb2
2024-11-13 03:23:11 +08:00
2024-12-23 02:03:55 +08:00
# prefix: /article
bl_article = Blueprint('article', __name__)
2024-11-13 03:23:11 +08:00
2024-12-23 02:03:55 +08:00
#########################
# Article #
#########################
2024-11-13 03:23:11 +08:00
# 匿名文列表
2024-12-23 02:03:55 +08:00
@bl_article.route('/list', methods = ["GET"])
2024-11-13 03:23:11 +08:00
def listing():
2024-12-09 03:24:22 +08:00
res, code = dbhelper.multi_article_fetcher("general", request.args.get("page"), 30)
2024-11-19 21:22:01 +08:00
return res, code
2024-11-26 09:17:44 +08:00
2024-12-23 02:03:55 +08:00
# 這裡要改
2024-11-19 21:22:01 +08:00
# 獲取匿名文附檔
2024-12-23 02:03:55 +08:00
@bl_article.route("/file/<fnhash>", methods=["GET"])
2024-12-09 03:24:22 +08:00
def getfile(fnhash:str):
resp, code = dbhelper.solo_file_fetcher("general", fnhash)
2024-11-19 21:22:01 +08:00
return resp, code
2024-11-13 03:23:11 +08:00
2024-11-26 09:17:44 +08:00
2024-11-13 03:23:11 +08:00
# 獲取指定文章
2024-12-23 02:03:55 +08:00
@bl_article.route("/<int:id>", methods = ["GET"])
2024-11-13 03:23:11 +08:00
def getarticle(id:int):
2024-12-09 03:24:22 +08:00
resfn, code = dbhelper.solo_article_fetcher("general", key=id)
if code == 200:
2024-12-17 01:08:35 +08:00
return internal_json2protobuf(role="general", otype='a', original=[resfn]), code
return abort(code)
2024-12-09 03:24:22 +08:00
2024-12-23 02:03:55 +08:00
#########################
# Post(Article/Comment) #
#########################
@bl_article.route("/", methods = ["POST"])
2024-11-13 03:23:11 +08:00
def posting():
2024-12-09 03:24:22 +08:00
"""
Work Flow:
ctx -> reference -> file -> post( hash -> IP -> IG -> mark ) | -> log
"""
2024-11-14 13:03:00 +08:00
# loadset
opt = setting_loader.loadset()
maxword = opt["Niming_Max_Word"]
2024-11-19 21:22:01 +08:00
# protobuf parse
2024-11-25 21:51:50 +08:00
recv = niming_pb2.Post()
2024-11-19 02:19:25 +08:00
try: recv.ParseFromString(request.data)
2024-12-09 03:24:22 +08:00
except DecodeError:
2024-12-12 11:30:51 +08:00
return error_proto("Failed to parse data"), 400
2024-11-13 03:23:11 +08:00
2024-11-26 09:17:44 +08:00
# content and check
2024-12-09 03:24:22 +08:00
content = str(recv.content)
if len(content) == 0 or len(content) > maxword: # length check
2024-12-12 11:30:51 +08:00
return error_proto("No content or too many words"), 400
2024-11-13 03:23:11 +08:00
2024-11-26 09:17:44 +08:00
# reference and check
ref = int(recv.ref)
if ref != 0:
2024-12-09 03:24:22 +08:00
# 檢查指向的文章是否存在 且 可訪問
with dbhelper.db.getsession() as session:
article = pgclass.SQLarticle
article_mark = pgclass.SQLmark
tpid = session.query(article.id).join(article_mark, article.hash==article_mark.hash) \
.filter(article.id==ref, article_mark.mark=="visible").first()
if not tpid:
2024-12-12 11:30:51 +08:00
return error_proto("Invalid Reference"), 400
2024-11-26 09:17:44 +08:00
else:
ref = None
2024-12-09 03:24:22 +08:00
result_id, sha1, hash = 0, "", ""
if ref is None: # only article (comment dont have files)
# file processing & check
files = recv.files
# check - size
atts = opt["Attachment_Count"]
sizelimit = opt["Attachment_Size"]
if len(files) > atts: return error_proto("Too many files"), 400
for f in files:
2024-12-12 11:30:51 +08:00
if len(f) <= 0 or len(f) > sizelimit: return error_proto("Empty file or file too big"), 400
2024-12-09 03:24:22 +08:00
# check - mimetype
allowed_mime = opt["Allowed_MIME"]
fmimes = []
for f in files:
mime = magic.Magic(mime=True)
type = mime.from_buffer(f)
2024-12-12 11:30:51 +08:00
if not(type in allowed_mime): return error_proto("File type not allowed"), 400
2024-12-09 03:24:22 +08:00
fmimes.append(type)
# posting
result_id, hash = dbhelper.solo_article_uploader(content=content,
file_list=files,
fmimes=fmimes)
if not result_id:
return error_proto("Failed to Post"), 400
else: # comments
sha1, hash = dbhelper.solo_comment_uploader(content=content,
ref=ref)
if not sha1:
return error_proto("Failed to Post"), 400
2024-11-25 21:51:50 +08:00
2024-11-26 09:17:44 +08:00
# to protobuf & return
proto_stres = niming_pb2.PostResponse(
2024-12-09 03:24:22 +08:00
status = niming_pb2.Status.Success,
2024-11-26 09:17:44 +08:00
hash = hash,
id = int(result_id)
).SerializeToString()
return proto_stres, 200