mirror of
https://github.com/Genshin-bots/gsuid_core.git
synced 2025-05-29 03:30:28 +08:00
✨ 新增custom
方式图片上传, 修复BUG
This commit is contained in:
parent
eaae8528b7
commit
4a8afaf352
@ -236,7 +236,6 @@ class Bot:
|
|||||||
while self.mutiply_resp == []:
|
while self.mutiply_resp == []:
|
||||||
await asyncio.wait_for(self.mutiply_event.wait(), timeout)
|
await asyncio.wait_for(self.mutiply_event.wait(), timeout)
|
||||||
|
|
||||||
self.mutiply_tag = False
|
|
||||||
self.mutiply_event = asyncio.Event()
|
self.mutiply_event = asyncio.Event()
|
||||||
return self.mutiply_resp.pop(0)
|
return self.mutiply_resp.pop(0)
|
||||||
elif is_recive:
|
elif is_recive:
|
||||||
|
@ -41,7 +41,7 @@ class GsClient:
|
|||||||
while True:
|
while True:
|
||||||
intent = await self._input()
|
intent = await self._input()
|
||||||
content = Message(type='text', data=intent)
|
content = Message(type='text', data=intent)
|
||||||
group_id = random.choice(['555', '666', '777'])
|
group_id = random.choice(['555'])
|
||||||
user_id = random.choice(['1', '2'])
|
user_id = random.choice(['1', '2'])
|
||||||
msg = MessageReceive(
|
msg = MessageReceive(
|
||||||
bot_id='console',
|
bot_id='console',
|
||||||
|
@ -40,6 +40,10 @@ if IS_UPLOAD:
|
|||||||
from gsuid_core.utils.upload.s3 import S3
|
from gsuid_core.utils.upload.s3 import S3
|
||||||
|
|
||||||
pclient = S3()
|
pclient = S3()
|
||||||
|
elif SERVER == 'custom':
|
||||||
|
from gsuid_core.utils.upload.custom import CUSTOM
|
||||||
|
|
||||||
|
pclient = CUSTOM()
|
||||||
|
|
||||||
|
|
||||||
URL_MAP = {}
|
URL_MAP = {}
|
||||||
|
@ -4,11 +4,14 @@ from .models import GSC, GsStrConfig, GsBoolConfig
|
|||||||
|
|
||||||
PIC_UPLOAD_CONIFG: Dict[str, GSC] = {
|
PIC_UPLOAD_CONIFG: Dict[str, GSC] = {
|
||||||
'PicUpload': GsBoolConfig('自动上传图片', '发送图片时将会自动上传', False),
|
'PicUpload': GsBoolConfig('自动上传图片', '发送图片时将会自动上传', False),
|
||||||
'PicUploadServer': GsStrConfig('上传图片方式', '可选s3或smms', 'smms'),
|
'PicUploadServer': GsStrConfig('上传图片方式', '可选s3或smms或custom', 'smms'),
|
||||||
|
'AutoDelete': GsBoolConfig('上传完后自动删除', '是否自动删除图片', True),
|
||||||
'smms_token': GsStrConfig('sm.ms_token', 'sm.ms的token', ''),
|
'smms_token': GsStrConfig('sm.ms_token', 'sm.ms的token', ''),
|
||||||
's3_endpoint': GsStrConfig('s3_endpoint', '终结点url', ''),
|
's3_endpoint': GsStrConfig('s3_endpoint', '终结点url', ''),
|
||||||
's3_access_key': GsStrConfig('s3_access_key', 'AK', ''),
|
's3_access_key': GsStrConfig('s3_access_key', 'AK', ''),
|
||||||
's3_secret_key': GsStrConfig('s3_secret_key', 'SK', ''),
|
's3_secret_key': GsStrConfig('s3_secret_key', 'SK', ''),
|
||||||
's3_bucket': GsStrConfig('s3_bucket', 'Bucket', ''),
|
's3_bucket': GsStrConfig('s3_bucket', 'Bucket', ''),
|
||||||
's3_region': GsStrConfig('s3_region', 'Region', ''),
|
's3_region': GsStrConfig('s3_region', 'Region', ''),
|
||||||
|
'custom_url': GsStrConfig('自定义上传图片API', '填入上传图片API', ''),
|
||||||
|
'custom_header': GsStrConfig('自定义上传图片Header', '填入上传图片hader', ''),
|
||||||
}
|
}
|
||||||
|
41
gsuid_core/utils/upload/custom.py
Normal file
41
gsuid_core/utils/upload/custom.py
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
import json
|
||||||
|
import asyncio
|
||||||
|
from io import BytesIO
|
||||||
|
|
||||||
|
from aiohttp.client import ClientSession
|
||||||
|
|
||||||
|
from gsuid_core.logger import logger
|
||||||
|
from gsuid_core.utils.plugins_config.gs_config import pic_upload_config
|
||||||
|
|
||||||
|
from .utils import is_auto_delete
|
||||||
|
|
||||||
|
URL: str = pic_upload_config.get_config('custom_url').data
|
||||||
|
_header: str = pic_upload_config.get_config('custom_header').data
|
||||||
|
|
||||||
|
|
||||||
|
class CUSTOM:
|
||||||
|
def __init__(self, _header: str = _header) -> None:
|
||||||
|
self.header = json.dumps(_header)
|
||||||
|
|
||||||
|
async def delete(self):
|
||||||
|
logger.warning('[custom / upload] 未实现delete...')
|
||||||
|
|
||||||
|
async def upload(self, file_name: str, files: BytesIO):
|
||||||
|
async with ClientSession() as client:
|
||||||
|
async with client.request(
|
||||||
|
'POST',
|
||||||
|
url=URL,
|
||||||
|
headers=self.header,
|
||||||
|
data={'file': files.getvalue()},
|
||||||
|
timeout=300,
|
||||||
|
) as resp:
|
||||||
|
logger.info('[custom / upload] 开始上传...')
|
||||||
|
raw_data = await resp.json()
|
||||||
|
logger.debug(f'[custom / upload] {raw_data}')
|
||||||
|
if raw_data and 'image_info_array' in raw_data[0]:
|
||||||
|
data = raw_data[0]['image_info_array']
|
||||||
|
if is_auto_delete:
|
||||||
|
asyncio.create_task(self.delete())
|
||||||
|
return data['url']
|
||||||
|
else:
|
||||||
|
logger.info('[custom / upload] 上传失败!')
|
@ -6,6 +6,8 @@ import aioboto3
|
|||||||
from gsuid_core.logger import logger
|
from gsuid_core.logger import logger
|
||||||
from gsuid_core.utils.plugins_config.gs_config import pic_upload_config
|
from gsuid_core.utils.plugins_config.gs_config import pic_upload_config
|
||||||
|
|
||||||
|
from .utils import is_auto_delete
|
||||||
|
|
||||||
SERVER = pic_upload_config.get_config('PicUploadServer').data
|
SERVER = pic_upload_config.get_config('PicUploadServer').data
|
||||||
END_POINT = pic_upload_config.get_config('s3_endpoint').data
|
END_POINT = pic_upload_config.get_config('s3_endpoint').data
|
||||||
ACCESS_KEY = pic_upload_config.get_config('s3_access_key').data
|
ACCESS_KEY = pic_upload_config.get_config('s3_access_key').data
|
||||||
@ -32,7 +34,8 @@ class S3:
|
|||||||
logger.info('[S3 / upload] 开始上传...')
|
logger.info('[S3 / upload] 开始上传...')
|
||||||
await s3.upload_fileobj(files, self.bucket_id, key)
|
await s3.upload_fileobj(files, self.bucket_id, key)
|
||||||
logger.info('[S3 / upload] 上传成功!')
|
logger.info('[S3 / upload] 上传成功!')
|
||||||
asyncio.create_task(self.delete(key))
|
if is_auto_delete:
|
||||||
|
asyncio.create_task(self.delete(key))
|
||||||
|
|
||||||
return f'{END_POINT}/{self.bucket_id}/{key}'
|
return f'{END_POINT}/{self.bucket_id}/{key}'
|
||||||
|
|
||||||
|
@ -6,6 +6,8 @@ from aiohttp.client import ClientSession
|
|||||||
from gsuid_core.logger import logger
|
from gsuid_core.logger import logger
|
||||||
from gsuid_core.utils.plugins_config.gs_config import pic_upload_config
|
from gsuid_core.utils.plugins_config.gs_config import pic_upload_config
|
||||||
|
|
||||||
|
from .utils import is_auto_delete
|
||||||
|
|
||||||
SERVER = pic_upload_config.get_config('PicUploadServer').data
|
SERVER = pic_upload_config.get_config('PicUploadServer').data
|
||||||
TOKEN = pic_upload_config.get_config('smms_token').data
|
TOKEN = pic_upload_config.get_config('smms_token').data
|
||||||
|
|
||||||
@ -44,7 +46,8 @@ class SMMS:
|
|||||||
logger.debug(f'[sm.ms / upload] {raw_data}')
|
logger.debug(f'[sm.ms / upload] {raw_data}')
|
||||||
if raw_data['success']:
|
if raw_data['success']:
|
||||||
data = raw_data['data']
|
data = raw_data['data']
|
||||||
asyncio.create_task(self.delete(data['hash']))
|
if is_auto_delete:
|
||||||
|
asyncio.create_task(self.delete(data['hash']))
|
||||||
return data['url']
|
return data['url']
|
||||||
else:
|
else:
|
||||||
logger.info('[sm.ms / upload] 上传失败!')
|
logger.info('[sm.ms / upload] 上传失败!')
|
||||||
|
3
gsuid_core/utils/upload/utils.py
Normal file
3
gsuid_core/utils/upload/utils.py
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
from gsuid_core.utils.plugins_config.gs_config import pic_upload_config
|
||||||
|
|
||||||
|
is_auto_delete: bool = pic_upload_config.get_config('AutoDelete').data
|
Loading…
x
Reference in New Issue
Block a user