2023-08-10 12:27:18 +08:00
|
|
|
|
import socket
|
2023-08-10 12:40:31 +08:00
|
|
|
|
import os
|
2023-08-10 12:27:18 +08:00
|
|
|
|
import platform
|
2023-10-24 17:46:51 +08:00
|
|
|
|
import time
|
|
|
|
|
|
2023-08-10 12:27:18 +08:00
|
|
|
|
import psutil
|
|
|
|
|
|
|
|
|
|
from apiflask import APIBlueprint
|
2023-08-16 14:15:15 +08:00
|
|
|
|
from flask import session
|
|
|
|
|
|
|
|
|
|
from app import rpc
|
2023-09-15 18:00:17 +08:00
|
|
|
|
from app.api.v1.exception.api import ImageUploadError, UserInfoError, ImageNotFound, DietCreationError
|
|
|
|
|
from app.api.v1.schema.api import ImageIn, ImagePreSignUrlOut, ImagePreSignUrlIn, DietImageIn
|
2023-08-16 14:15:15 +08:00
|
|
|
|
from app.util.auth import login_required
|
2023-08-10 12:27:18 +08:00
|
|
|
|
|
|
|
|
|
api = APIBlueprint('api', __name__)
|
|
|
|
|
|
|
|
|
|
|
2023-08-16 14:15:15 +08:00
|
|
|
|
@api.post('/images')
|
2023-09-15 18:00:17 +08:00
|
|
|
|
@api.doc(summary='上传头像图片', description='上传头像图片')
|
2023-08-16 14:15:15 +08:00
|
|
|
|
@api.input(ImageIn, location='files')
|
2023-08-18 21:07:10 +08:00
|
|
|
|
@api.output(ImagePreSignUrlOut)
|
2023-08-16 14:15:15 +08:00
|
|
|
|
@login_required
|
2023-09-15 18:00:17 +08:00
|
|
|
|
def upload_avatar_image(files_data):
|
2023-08-16 14:25:51 +08:00
|
|
|
|
f = files_data['image']
|
2023-08-16 14:15:15 +08:00
|
|
|
|
try:
|
2023-09-15 18:00:17 +08:00
|
|
|
|
result = rpc.storage.upload(file_name=str(session['user_id'] + f'_{f.filename}'),
|
2023-08-16 14:47:39 +08:00
|
|
|
|
file_binary=f.read(),
|
2023-08-16 15:40:33 +08:00
|
|
|
|
bucket='bodyrecord',
|
2023-08-16 14:15:15 +08:00
|
|
|
|
folder='avatar',
|
|
|
|
|
app='bodyrecord')
|
|
|
|
|
except Exception as e:
|
|
|
|
|
raise ImageUploadError(extra_data={'error_docs': str(e)})
|
|
|
|
|
|
|
|
|
|
if result.get('status') and result['status'] == 'UPLOADED':
|
|
|
|
|
try:
|
|
|
|
|
rpc.admin.set_user_info(session['user_id'],
|
|
|
|
|
avatar_id=result['_id'])
|
|
|
|
|
except Exception as e:
|
|
|
|
|
raise UserInfoError(extra_data={'error_docs': str(e)})
|
|
|
|
|
|
2023-08-18 21:07:10 +08:00
|
|
|
|
try:
|
|
|
|
|
presign_url = rpc.storage.get_presign_url(result['_id'],
|
|
|
|
|
'bodyrecord',
|
|
|
|
|
bucket='bodyrecord')
|
|
|
|
|
except Exception as e:
|
|
|
|
|
raise ImageNotFound(extra_data={'error_docs': str(e)})
|
|
|
|
|
|
|
|
|
|
return {'image_presign_url': presign_url}
|
2023-08-16 14:15:15 +08:00
|
|
|
|
else:
|
2023-08-18 21:07:10 +08:00
|
|
|
|
raise ImageUploadError()
|
2023-08-16 14:15:15 +08:00
|
|
|
|
|
|
|
|
|
|
2023-09-15 18:00:17 +08:00
|
|
|
|
@api.post('/images/diet')
|
|
|
|
|
@api.doc(summary='上传食物图片', description='上传食物图片')
|
|
|
|
|
@api.input(DietImageIn, location='files')
|
|
|
|
|
@api.output(ImagePreSignUrlOut)
|
|
|
|
|
@login_required
|
|
|
|
|
def upload_diet_image(files_data):
|
|
|
|
|
f = files_data['image']
|
2023-09-20 16:09:05 +08:00
|
|
|
|
category_dict = {
|
|
|
|
|
0: '早餐',
|
|
|
|
|
1: '午餐',
|
|
|
|
|
2: '晚餐',
|
|
|
|
|
3: '零食',
|
|
|
|
|
4: '下午茶',
|
|
|
|
|
5: '夜宵',
|
|
|
|
|
}
|
|
|
|
|
category = category_dict[files_data['category']] # 食物类别
|
2023-09-15 18:00:17 +08:00
|
|
|
|
try:
|
|
|
|
|
result = rpc.storage.upload(file_name=str(session['user_id'] + f'_{f.filename}'),
|
|
|
|
|
file_binary=f.read(),
|
|
|
|
|
bucket='bodyrecord',
|
|
|
|
|
folder='diet',
|
|
|
|
|
app='bodyrecord')
|
|
|
|
|
except Exception as e:
|
|
|
|
|
raise ImageUploadError(extra_data={'error_docs': str(e)})
|
|
|
|
|
|
|
|
|
|
if result.get('status') and result['status'] == 'UPLOADED':
|
|
|
|
|
# 图片上传成功,处理业务逻辑
|
|
|
|
|
try:
|
2023-09-15 18:23:36 +08:00
|
|
|
|
rpc.diet.add(**{
|
|
|
|
|
'user_id': session['user_id'],
|
|
|
|
|
'category': category,
|
|
|
|
|
'diet_image_id': result['_id'],
|
|
|
|
|
})
|
2023-09-15 18:00:17 +08:00
|
|
|
|
except Exception as e:
|
|
|
|
|
raise DietCreationError(extra_data={'error_docs': str(e)})
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
presign_url = rpc.storage.get_presign_url(result['_id'],
|
|
|
|
|
'bodyrecord',
|
|
|
|
|
bucket='bodyrecord')
|
|
|
|
|
except Exception as e:
|
|
|
|
|
raise ImageNotFound(extra_data={'error_docs': str(e)})
|
|
|
|
|
|
|
|
|
|
return {'image_presign_url': presign_url}
|
|
|
|
|
else:
|
|
|
|
|
raise ImageUploadError()
|
|
|
|
|
|
|
|
|
|
|
2023-08-16 17:13:52 +08:00
|
|
|
|
@api.get('/images/presign_url')
|
|
|
|
|
@api.input(ImagePreSignUrlIn, location='query')
|
|
|
|
|
@api.output(ImagePreSignUrlOut)
|
|
|
|
|
@api.doc(summary='获取图片预签名链接', description='获取图片预签名链接')
|
|
|
|
|
def get_image_presign_url(query_data):
|
|
|
|
|
try:
|
2023-08-16 17:21:50 +08:00
|
|
|
|
presign_url = rpc.storage.get_presign_url(query_data['avatar_id'],
|
2023-09-15 18:00:17 +08:00
|
|
|
|
'bodyrecord',
|
|
|
|
|
bucket='bodyrecord',
|
|
|
|
|
expire_time=query_data.get('expire_time', 3600))
|
2023-08-16 17:13:52 +08:00
|
|
|
|
except Exception as e:
|
|
|
|
|
raise ImageNotFound(extra_data={'error_docs': str(e)})
|
2023-08-16 17:21:50 +08:00
|
|
|
|
return {'image_presign_url': presign_url}
|
2023-08-16 17:13:52 +08:00
|
|
|
|
|
|
|
|
|
|
2023-08-10 12:27:18 +08:00
|
|
|
|
@api.get('/hello')
|
|
|
|
|
def hello():
|
2023-10-24 17:46:51 +08:00
|
|
|
|
time.sleep(2)
|
2023-08-10 13:08:06 +08:00
|
|
|
|
result_one = f'我是您的专属接口提供服务器:'
|
2023-08-10 12:27:18 +08:00
|
|
|
|
result_two = f'我的机器参数如下'
|
2023-08-10 13:01:49 +08:00
|
|
|
|
result_three = f'操作系统:{platform.system()}'
|
2023-08-10 12:27:18 +08:00
|
|
|
|
text = """
|
|
|
|
|
<style type="text/css">
|
|
|
|
|
* {
|
|
|
|
|
padding: 0;
|
|
|
|
|
margin: 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
div {
|
|
|
|
|
padding: 4px 48px;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
a {
|
|
|
|
|
color: black;
|
|
|
|
|
cursor: pointer;
|
|
|
|
|
text-decoration: none
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
a:hover {
|
|
|
|
|
text-decoration: None;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
body {
|
|
|
|
|
background: #fff;
|
|
|
|
|
font-family:
|
|
|
|
|
"Century Gothic", "Microsoft yahei";
|
|
|
|
|
color: #333;
|
|
|
|
|
font-size: 18px;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
h1 {
|
|
|
|
|
font-size: 100px;
|
|
|
|
|
font-weight: normal;
|
|
|
|
|
margin-bottom: 12px;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
p {
|
|
|
|
|
line-height: 1.6em;
|
|
|
|
|
font-size: 42px
|
|
|
|
|
}
|
|
|
|
|
</style>
|
|
|
|
|
<div style="padding: 24px 48px;">
|
|
|
|
|
<p>
|
|
|
|
|
<a href="" target="_Blank">您好,PZX 大人!</a>
|
|
|
|
|
<br />
|
|
|
|
|
<span style="font-size:30px">
|
|
|
|
|
<a href=""> """ + result_one + """</a>
|
2023-08-10 13:08:06 +08:00
|
|
|
|
<br />
|
|
|
|
|
<a href=""> """ + f'{os.environ["HOSTNAME"]} ' + f'进程 {socket.gethostname()}' + """</a>
|
2023-08-10 12:27:18 +08:00
|
|
|
|
</span>
|
|
|
|
|
<br />
|
|
|
|
|
<span style="font-size:25px">
|
|
|
|
|
<a href=""> """ + result_two + """</a>
|
|
|
|
|
</span>
|
|
|
|
|
<br />
|
|
|
|
|
<span style="font-size:20px">
|
|
|
|
|
<a href=""> """ + result_three + """</a>
|
|
|
|
|
<br />
|
2023-08-10 13:01:49 +08:00
|
|
|
|
<a href=""> """ + f'CPU 核数:{psutil.cpu_count()},目前 CPU 占用率: {psutil.cpu_percent()}' + """</a>
|
2023-08-10 12:27:18 +08:00
|
|
|
|
<br />
|
2023-08-10 13:02:47 +08:00
|
|
|
|
<a href=""> """ + f'总内存 {round(psutil.virtual_memory().total / 1024 / 1024, 2)} MB,使用中内存:{round(psutil.virtual_memory().used / 1024 / 1024, 2)} MB' + """</a>
|
2023-08-10 13:01:49 +08:00
|
|
|
|
<br />
|
|
|
|
|
<a href=""> """ + f'磁盘总空间 {round(psutil.disk_usage("/").total / 1024 / 1024 / 1024, 2)} G,磁盘使用情况:{round(psutil.disk_usage("/").used / 1024 / 1024 / 1024, 2)} G' + """</a>
|
2023-08-10 12:27:18 +08:00
|
|
|
|
</span>
|
|
|
|
|
</p>
|
|
|
|
|
</div>
|
|
|
|
|
"""
|
|
|
|
|
return text
|