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 ...@@ -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 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): async def upload_news_qs(request):
"""附件上传""" """附件上传"""
files = request.files.getlist("file")
url_article, url_cover = "", "" url_article, url_cover = "", ""
file_name_lst = [file.name for file in files] news_file = request.files.getlist("file1")[0]
if len(file_name_lst) != len(set(file_name_lst)): cover_file = request.files.getlist("file2")[0]
if news_file.name == cover_file.name:
return False, url_article, url_cover return False, url_article, url_cover
for i, file in enumerate(files): news_upload, url_article = await upload_file_helper(news_file)
file_name = file.name if news_upload and url_article:
file_name_lst = file_name.rsplit(".", 1) log.info(f"上传文章成功, url {url_article}")
if len(file_name_lst) <= 1:
return False, url_article, url_cover cover_upload, url_cover = await upload_file_helper(cover_file)
if cover_upload and url_cover:
file_body = file.body log.info(f"上传文章封面, url {url_cover}")
if file_body:
now_dt = pendulum.now() if all([news_file, url_article, cover_upload, url_cover]):
yr, month = now_dt.year, now_dt.month log.info(f"上传文章和封面成功 {url_article} {url_cover}")
file_url = f"{OSS_NEWS}/{yr}/{month}/{uuid4()}.{file_name_lst[1]}" return True, url_article, url_cover
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
log.info(f"上传文章和封面成功 {url_article} {url_cover}") return False, url_article, url_cover
return True, url_article, url_cover
async def save_news_info(news_info, news_url, cover_url): async def save_news_info(news_info, news_url, cover_url):
......
...@@ -4,10 +4,10 @@ DATE:2024/3/5 16:19 ...@@ -4,10 +4,10 @@ DATE:2024/3/5 16:19
""" """
from pot_libs.sanic_api import summary 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.common.components.common_cps import SuccessRsp
from unify_api.modules.qkadmin.components.news_info_cps import ( from unify_api.modules.qkadmin.components.news_info_cps import (
PubNewsReq, NewsPagesReq, NewsPagesRsp, NewsInfo, ReviseNewsReq, NewsPagesReq, NewsPagesRsp, NewsInfo, ReviseNewsReq, ReviseNewsSeoReq
ReviseNewsSeoReq
) )
from unify_api.modules.qkadmin.service.news_mgr_srv import ( from unify_api.modules.qkadmin.service.news_mgr_srv import (
upload_news_qs, save_news_info, load_news_pages, update_news_info, 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 ( ...@@ -16,12 +16,16 @@ from unify_api.modules.qkadmin.service.news_mgr_srv import (
@summary("发布文章") @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) is_success, news_url, cover_url = await upload_news_qs(request)
if is_success: if is_success:
d_news_info = dict(title=body.title, author=body.author, form = request.form
pub_time=body.pub_time, d_news_info = dict(title=form.title, author=form.author,
news_type=int(body.news_type)) pub_time=form.pub_time,
news_type=int(form.news_type))
await save_news_info(d_news_info, news_url, cover_url) await save_news_info(d_news_info, news_url, cover_url)
return SuccessRsp(success=1, message="发布文章成功") 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