Commit 961562b0 authored by ZZH's avatar ZZH

fix file upload 2024-03-8

parent 46d95b91
......@@ -11,40 +11,45 @@ from unify_api.modules.common.procedures.file_operate_pds import upload_file
from pot_libs.mysql_util.mysql_util import MysqlUtil
async def upload_file_helper(file):
is_upload, file_url = False, ""
file_name = file.name
file_name_lst = file_name.rsplit(".", 1)
if len(file_name_lst) <= 1:
return False, file_url
if file.body:
now_dt = pendulum.now()
yr, month = now_dt.year, now_dt.month
file_url = f"{OSS_NEWS}/{yr}/{month}/{uuid4()}.{file_name_lst[1]}"
log.info(f"准备上传文件 {file_name} {file_url}")
is_upload = await upload_file(file, file_url)
return is_upload, file_url
async def upload_news_qs(request):
"""附件上传"""
files = request.files.getlist("file")
url_article, url_cover = "", ""
file_name_lst = [file.name for file in files]
if len(file_name_lst) != len(set(file_name_lst)):
news_file = request.files.getlist("file1")[0]
cover_file = request.files.getlist("file2")[0]
if news_file.name == cover_file.name:
return False, url_article, url_cover
for i, file in enumerate(files):
file_name = file.name
file_name_lst = file_name.rsplit(".", 1)
if len(file_name_lst) <= 1:
return False, url_article, url_cover
file_body = file.body
if file_body:
now_dt = pendulum.now()
yr, month = now_dt.year, now_dt.month
file_url = f"{OSS_NEWS}/{yr}/{month}/{uuid4()}.{file_name_lst[1]}"
log.info(f"准备上传文件 {file_name} {file_url}")
is_upload = await upload_file(file, file_url)
if not is_upload:
return False, url_article, url_cover
if i == 0:
url_article = file_url
else:
url_cover = file_url
else:
return False, url_article, url_cover
news_upload, url_article = await upload_file_helper(news_file)
if news_upload and url_article:
log.info(f"上传文章成功, url {url_article}")
cover_upload, url_cover = await upload_file_helper(cover_file)
if cover_upload and url_cover:
log.info(f"上传文章封面, url {url_cover}")
if all([news_file, url_article, cover_upload, url_cover]):
log.info(f"上传文章和封面成功 {url_article} {url_cover}")
return True, url_article, url_cover
log.info(f"上传文章和封面成功 {url_article} {url_cover}")
return True, url_article, url_cover
return False, url_article, url_cover
async def save_news_info(news_info, news_url, cover_url):
......
......@@ -4,10 +4,10 @@ DATE:2024/3/5 16:19
"""
from pot_libs.sanic_api import summary
from pot_libs.logger import log
from unify_api.modules.common.components.common_cps import SuccessRsp
from unify_api.modules.qkadmin.components.news_info_cps import (
PubNewsReq, NewsPagesReq, NewsPagesRsp, NewsInfo, ReviseNewsReq,
ReviseNewsSeoReq
NewsPagesReq, NewsPagesRsp, NewsInfo, ReviseNewsReq, ReviseNewsSeoReq
)
from unify_api.modules.qkadmin.service.news_mgr_srv import (
upload_news_qs, save_news_info, load_news_pages, update_news_info,
......@@ -16,12 +16,16 @@ from unify_api.modules.qkadmin.service.news_mgr_srv import (
@summary("发布文章")
async def post_publish_news(request, body: PubNewsReq) -> SuccessRsp:
async def post_publish_news(request, *args, **kwargs) -> SuccessRsp:
log.info(f"post_publish_news:{request.form}")
log.info(f"post_publish_news:{len(request.files.getlist('file1'))}")
log.info(f"post_publish_news:{len(request.files.getlist('file2'))}")
is_success, news_url, cover_url = await upload_news_qs(request)
if is_success:
d_news_info = dict(title=body.title, author=body.author,
pub_time=body.pub_time,
news_type=int(body.news_type))
form = request.form
d_news_info = dict(title=form.title, author=form.author,
pub_time=form.pub_time,
news_type=int(form.news_type))
await save_news_info(d_news_info, news_url, cover_url)
return SuccessRsp(success=1, message="发布文章成功")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment