niming_backend/blueprints/article.py

173 lines
5.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
"""
TODO:
2024-12-09 03:24:22 +08:00
- 測試 rebuild 完成的功能
2024-11-19 02:19:25 +08:00
- IG post ( Po文刪文只PO本體文章 )
2024-12-09 03:24:22 +08:00
- 檔案傳輸加低畫質版本(縮圖)
2024-11-14 13:03:00 +08:00
2024-11-18 02:47:25 +08:00
- log 的方式之後要重新設計 > 正規化
2024-11-14 13:03:00 +08:00
- IP Record (deploy之前配合rev proxy)
2024-11-19 02:19:25 +08:00
- gunicorn
- 檔案完成但是再看看要不要讓發文者持sha256存取自己發的文的檔案
2024-11-13 03:23:11 +08:00
"""
article = Blueprint('article', __name__)
# 匿名文列表
@article.route('/list', methods = ["GET"])
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-11-19 21:22:01 +08:00
# 獲取匿名文附檔
2024-12-09 03:24:22 +08:00
@article.route("/file/<fnhash>", methods=["GET"])
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-19 21:22:01 +08:00
# 只有發文者可以看到的獲取指定文章
# 只有發文者可以做到的刪除文章
2024-12-09 03:24:22 +08:00
@article.route("/own/<type>/<key>", methods = ["GET", "DELETE"])
def owner_getarticle(type:str, key:str):
2024-11-26 09:17:44 +08:00
# arguments
sha256 = request.args.get("hash", None)
if not sha256:
2024-12-09 03:24:22 +08:00
return abort(400)
2024-11-26 09:17:44 +08:00
sha256 = str(sha256)
2024-12-09 03:24:22 +08:00
if type == 'a':
if not (len(key) > 0 and key.isdigit()):
return abort(400)
key = int(key) # id
elif type == 'c':
if not (len(key) > 0):
return abort(400)
key = str(key) # sha1
else:
return abort(400)
2024-11-19 21:22:01 +08:00
2024-12-09 03:24:22 +08:00
# 獲取指定文章/留言
2024-11-19 21:22:01 +08:00
if request.method == "GET":
2024-12-09 03:24:22 +08:00
if type == 'a': # 文章
resfn, code = dbhelper.solo_article_fetcher("owner", key=(sha256, key))
elif type == 'c': # 留言
resfn, code = dbhelper.solo_comment_fetcher("owner", key=(sha256, key))
2024-11-26 09:17:44 +08:00
if code == 200:
return internal_json2protobuf(resfn), code
return resfn, code
2024-11-19 21:22:01 +08:00
# 刪除指定文章跟他們的留言、檔案
elif request.method == "DELETE":
2024-12-09 03:24:22 +08:00
if type == 'a':
result, code = dbhelper.solo_article_remover("owner", hash=sha256, id=key)
elif type == 'c':
result, code = dbhelper.solo_comment_remover("owner", hash=sha256, sha1=key)
2024-11-26 09:17:44 +08:00
if not code == 200:
return result, code
2024-12-09 03:24:22 +08:00
one = niming_pb2.FetchResponse.Message()
if "id" in result: one.id = result["id"]
return niming_pb2.FetchResponse(posts=[one]).SerializeToString(), 200
2024-11-13 03:23:11 +08:00
# 獲取指定文章
2024-12-09 03:24:22 +08:00
@article.route("/a/<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:
return internal_json2protobuf(resfn), code
return resfn, code
# 獲取指定文章的留言
@article.route("/c/<sha1>", methods = ["GET"])
def getcomment(sha1:str):
resfn, code = dbhelper.solo_comment_fetcher("general", key=sha1)
2024-11-26 09:17:44 +08:00
if code == 200:
return internal_json2protobuf(resfn), code
return resfn, code
2024-11-13 03:23:11 +08:00
2024-11-14 13:03:00 +08:00
# 上傳文章 / 留言
2024-11-19 21:22:01 +08:00
@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:
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
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:
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:
if len(f) <= 0 or len(f) > sizelimit: return error_proto("Empty file or file too big."), 400
# check - mimetype
allowed_mime = opt["Allowed_MIME"]
fmimes = []
for f in files:
mime = magic.Magic(mime=True)
type = mime.from_buffer(f)
if not(type in allowed_mime): return error_proto("File type not allowed."), 400
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