mirror of
https://github.com/Genshin-bots/gsuid_core.git
synced 2025-05-12 06:55:49 +08:00
✨ 支持发送图片时启用图片服务器
This commit is contained in:
parent
84b1d49598
commit
e77723cb99
@ -1,18 +1,22 @@
|
|||||||
import sys
|
import sys
|
||||||
import asyncio
|
import asyncio
|
||||||
|
from io import BytesIO
|
||||||
from typing import Dict
|
from typing import Dict
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
import uvicorn
|
import uvicorn
|
||||||
|
from PIL import Image
|
||||||
from msgspec import json as msgjson
|
from msgspec import json as msgjson
|
||||||
from starlette.requests import Request
|
from starlette.requests import Request
|
||||||
from fastapi import FastAPI, WebSocket, WebSocketDisconnect
|
from fastapi.responses import StreamingResponse
|
||||||
|
from fastapi import FastAPI, WebSocket, BackgroundTasks, WebSocketDisconnect
|
||||||
|
|
||||||
sys.path.append(str(Path(__file__).resolve().parents[1]))
|
sys.path.append(str(Path(__file__).resolve().parents[1]))
|
||||||
from gsuid_core.sv import SL # noqa: E402
|
from gsuid_core.sv import SL # noqa: E402
|
||||||
from gsuid_core.gss import gss # noqa: E402
|
from gsuid_core.gss import gss # noqa: E402
|
||||||
from gsuid_core.logger import logger # noqa: E402
|
from gsuid_core.logger import logger # noqa: E402
|
||||||
from gsuid_core.config import core_config # noqa: E402
|
from gsuid_core.config import core_config # noqa: E402
|
||||||
|
from gsuid_core.data_store import image_res # noqa: E402
|
||||||
from gsuid_core.handler import handle_event # noqa: E402
|
from gsuid_core.handler import handle_event # noqa: E402
|
||||||
from gsuid_core.models import MessageReceive # noqa: E402
|
from gsuid_core.models import MessageReceive # noqa: E402
|
||||||
from gsuid_core.webconsole.mount_app import site # noqa: E402
|
from gsuid_core.webconsole.mount_app import site # noqa: E402
|
||||||
@ -145,6 +149,20 @@ def main():
|
|||||||
retcode = -1
|
retcode = -1
|
||||||
return {'status': retcode, 'msg': '', 'data': {}}
|
return {'status': retcode, 'msg': '', 'data': {}}
|
||||||
|
|
||||||
|
def delete_image(image_path: Path):
|
||||||
|
image_path.unlink()
|
||||||
|
|
||||||
|
@app.get('/genshinuid/image/{image_id}.jpg')
|
||||||
|
async def get_image(image_id: str, background_tasks: BackgroundTasks):
|
||||||
|
path = image_res / f'{image_id}.jpg'
|
||||||
|
image = Image.open(path)
|
||||||
|
image_bytes = BytesIO()
|
||||||
|
image.save(image_bytes, format='JPEG')
|
||||||
|
image_bytes.seek(0)
|
||||||
|
response = StreamingResponse(image_bytes, media_type='image/png')
|
||||||
|
background_tasks.add_task(delete_image, path)
|
||||||
|
return response
|
||||||
|
|
||||||
site.mount_app(app)
|
site.mount_app(app)
|
||||||
|
|
||||||
uvicorn.run(
|
uvicorn.run(
|
||||||
|
@ -17,3 +17,6 @@ def get_res_path(_path: Optional[Union[str, List]] = None) -> Path:
|
|||||||
path.mkdir(parents=True)
|
path.mkdir(parents=True)
|
||||||
|
|
||||||
return path
|
return path
|
||||||
|
|
||||||
|
|
||||||
|
image_res = get_res_path('IMAGE_TEMP')
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
import uuid
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from base64 import b64encode
|
from base64 import b64encode
|
||||||
@ -6,6 +7,14 @@ from typing import List, Union, Literal
|
|||||||
from PIL import Image
|
from PIL import Image
|
||||||
|
|
||||||
from gsuid_core.models import Message
|
from gsuid_core.models import Message
|
||||||
|
from gsuid_core.config import core_config
|
||||||
|
from gsuid_core.data_store import image_res
|
||||||
|
from gsuid_core.utils.plugins_config.gs_config import core_plugins_config
|
||||||
|
|
||||||
|
pic_srv = core_plugins_config.get_config('EnablePicSrv').data
|
||||||
|
HOST = core_config.get_config('HOST')
|
||||||
|
PORT = int(core_config.get_config('PORT'))
|
||||||
|
_HOST = '127.0.0.1' if HOST == 'localhost' else HOST
|
||||||
|
|
||||||
|
|
||||||
class MessageSegment:
|
class MessageSegment:
|
||||||
@ -31,7 +40,16 @@ class MessageSegment:
|
|||||||
return Message(type='image', data=img)
|
return Message(type='image', data=img)
|
||||||
with open(img, 'rb') as fp:
|
with open(img, 'rb') as fp:
|
||||||
img = fp.read()
|
img = fp.read()
|
||||||
msg = Message(type='image', data=f'base64://{b64encode(img).decode()}')
|
|
||||||
|
if pic_srv:
|
||||||
|
name = f'{uuid.uuid1()}.jpg'
|
||||||
|
path = image_res / name
|
||||||
|
path.write_bytes(img)
|
||||||
|
data = f'{_HOST}:{PORT}/genshinuid/image/{name}'
|
||||||
|
else:
|
||||||
|
data = f'base64://{b64encode(img).decode()}'
|
||||||
|
|
||||||
|
msg = Message(type='image', data=data)
|
||||||
return msg
|
return msg
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
@ -59,4 +59,5 @@ CONIFG_DEFAULT: Dict[str, GSC] = {
|
|||||||
'EnableSpecificMsgId': GsBoolConfig('启用回复特殊ID', '如不知道请勿开启', False),
|
'EnableSpecificMsgId': GsBoolConfig('启用回复特殊ID', '如不知道请勿开启', False),
|
||||||
'SpecificMsgId': GsStrConfig('特殊返回消息ID', '如不知道请勿填写', ''),
|
'SpecificMsgId': GsStrConfig('特殊返回消息ID', '如不知道请勿填写', ''),
|
||||||
'AutoUpdateDep': GsBoolConfig('自动更新依赖', '更新插件时将会自动更新依赖', False),
|
'AutoUpdateDep': GsBoolConfig('自动更新依赖', '更新插件时将会自动更新依赖', False),
|
||||||
|
'EnablePicSrv': GsBoolConfig('将图片转链接发送(需公网)', '发送图片转链接', True),
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user