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
|
2024-11-15 02:12:21 +08:00
|
|
|
from protobuf_files import niming_pb2
|
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:
|
2024-12-17 01:08:35 +08:00
|
|
|
return abort(404)
|
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': # 文章
|
2024-12-12 11:30:51 +08:00
|
|
|
resfn, code = dbhelper.solo_article_fetcher("owner", key=key, hash=sha256)
|
2024-12-09 03:24:22 +08:00
|
|
|
elif type == 'c': # 留言
|
2024-12-12 11:30:51 +08:00
|
|
|
resfn, code = dbhelper.solo_comment_fetcher("owner", key=key, hash=sha256)
|
2024-11-26 09:17:44 +08:00
|
|
|
if code == 200:
|
2024-12-17 01:08:35 +08:00
|
|
|
return internal_json2protobuf(role="owner", otype=type, original=[resfn]), code
|
|
|
|
return abort(code)
|
2024-12-12 11:30:51 +08:00
|
|
|
# 刪除指定文章/留言
|
2024-11-19 21:22:01 +08:00
|
|
|
elif request.method == "DELETE":
|
2024-12-09 03:24:22 +08:00
|
|
|
if type == 'a':
|
2024-12-12 11:30:51 +08:00
|
|
|
rtype = niming_pb2.FetchPostResponse
|
2024-12-09 03:24:22 +08:00
|
|
|
result, code = dbhelper.solo_article_remover("owner", hash=sha256, id=key)
|
|
|
|
elif type == 'c':
|
2024-12-12 11:30:51 +08:00
|
|
|
rtype = niming_pb2.FetchCommentResponse
|
2024-12-09 03:24:22 +08:00
|
|
|
result, code = dbhelper.solo_comment_remover("owner", hash=sha256, sha1=key)
|
2024-12-12 11:30:51 +08:00
|
|
|
|
2024-12-17 01:08:35 +08:00
|
|
|
if not code == 200: # Exception
|
|
|
|
return abort(code)
|
2024-12-09 03:24:22 +08:00
|
|
|
|
2024-12-12 11:30:51 +08:00
|
|
|
if type == 'a':
|
|
|
|
ret = rtype(posts=[ rtype.Message(id=result["id"]) ])
|
|
|
|
elif type == 'c':
|
|
|
|
ret = rtype(posts=[ rtype.Message(sha1=result["sha1"]) ])
|
2024-12-09 03:24:22 +08:00
|
|
|
|
2024-12-12 11:30:51 +08:00
|
|
|
return ret.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:
|
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
|
|
|
|
|
|
|
|
|
|
|
# 獲取指定文章的留言
|
|
|
|
@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:
|
2024-12-17 01:08:35 +08:00
|
|
|
return internal_json2protobuf(role="general", otype='c', original=[resfn]), code
|
|
|
|
return abort(code)
|
2024-11-26 09:17:44 +08:00
|
|
|
|
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()
|
2024-11-15 02:12:21 +08:00
|
|
|
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-11-15 02:12:21 +08:00
|
|
|
|
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
|