Skip to content

Commit

Permalink
Merge pull request #13 from ITCraftDevelopmentTeam/update-checker
Browse files Browse the repository at this point in the history
在启动时检查更新
  • Loading branch information
github-actions[bot] authored Jan 4, 2024
2 parents fdcc78e + 6007ab8 commit bc5d607
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 11 deletions.
18 changes: 9 additions & 9 deletions actions/v12/file.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from utils.logger import get_logger
from utils.config import config
from actions import register_action
import utils.checker as checker
import utils.type_checker as type_checker
import os.path
import os
import traceback
Expand Down Expand Up @@ -128,15 +128,15 @@ async def get_file_fragmented(

match stage:
case "prepare":
checker.check_aruments(file_id)
type_checker.check_aruments(file_id)
return return_object.get(
0,
name=file_name,
total_size=len(content),
sha256=hashlib.sha256(content).hexdigest()
)
case "transfer":
checker.check_aruments(file_id, offset, size)
type_checker.check_aruments(file_id, offset, size)
return return_object.get(
0,
data=base64.b64encode(content[offset:offset + size]).decode("utf-8") # type: ignore
Expand All @@ -159,7 +159,7 @@ async def upload_file_fragmented(
match stage:

case "prepare":
checker.check_aruments(name, total_size)
type_checker.check_aruments(name, total_size)
uploading_files[file_id := str(uuid.uuid1())] = {
"name": name,
"content": [0] * total_size
Expand All @@ -169,15 +169,15 @@ async def upload_file_fragmented(
)

case "transfer":
checker.check_aruments(file_id, offset, data)
type_checker.check_aruments(file_id, offset, data)
byte_offset = 0
for byte in list(base64.b64decode(data)):
uploading_files[file_id]["content"][offset + byte_offset] = byte
byte_offset += 1
return return_object.get()

case "finish":
checker.check_aruments(file_id, offset, sha256)
type_checker.check_aruments(file_id, offset, sha256)
file_name = uploading_files[file_id]['name']
with open(f".cache/files/{file_name}", "wb") as f:
f.write(bytes(uploading_files.pop(file_id)["content"]))
Expand All @@ -202,7 +202,7 @@ async def upload_file(
match type:

case "url":
checker.check_aruments(name, url)
type_checker.check_aruments(name, url)
is_successful = await upload_file_from_url(
url,
headers,
Expand All @@ -218,14 +218,14 @@ async def upload_file(
)

case "name":
checker.check_aruments(name, path)
type_checker.check_aruments(name, path)
is_successful = upload_file_from_path(name, path)
if not is_successful[0]:
return return_object.get(32001, is_successful[1])
return return_object.get(0, file_id=register_saved_file(name))

case "data":
checker.check_aruments(name, data)
type_checker.check_aruments(name, data)
is_successful = upload_file_from_data(name, data)
if not is_successful[0]:
return return_object.get(32001, is_successful[1])
Expand Down
4 changes: 2 additions & 2 deletions call_action.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from actions import action_list
from typing import Callable
from utils.checker import check_request_params
from utils.type_checker import check_request_params
import utils.return_object as return_object
from utils.checker import BadParam
from utils.type_checker import BadParam
from utils.logger import get_logger
import traceback
from utils.message.v12.parser import UnsupportedSegment, BadSegmentData
Expand Down
8 changes: 8 additions & 0 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,14 @@ OneBot V11 中,私聊消息事件(`message.private`)的 `sub_type` 字段

此项为 `false` 时,将删除缓存库的索引


### 在启动时检查更新(`check_update`

| 类型 | 必须 | 默认值 |
|:---------:|:----:|:----------------:|
| 布尔 || `true` |


### 是否忽略不支持的消息段(`ignore_unsupported_segment`

| 类型 | 必须 | 默认值 |
Expand Down
3 changes: 3 additions & 0 deletions utils/event/discord_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import discord
import asyncio

from utils.update_checker import check_update
from actions.v12.basic import get_status
from actions.v11.basic import get_role

Expand All @@ -30,6 +31,8 @@ async def on_ready() -> None:
"status_update",
status=(await get_status())["data"]
)
if config["system"].get("check_update", True):
asyncio.create_task(check_update())


@client.event
Expand Down
File renamed without changes.
28 changes: 28 additions & 0 deletions utils/update_checker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from typing import Literal
from .logger import get_logger
from .config import config
import version
import httpx

logger = get_logger()


async def get_latest_version() -> dict[str, str]:
async with httpx.AsyncClient(proxies=config["system"].get("proxy")) as client:
response = await client.get("https://onedisc.itcdt.top/version.json")
return response.json()

def get_version_type() -> Literal["beta", "stable"]:
return "stable" if version.SUB_VER == 0 else "beta"

def parse_version_number(version_number: str) -> tuple[str, int, str]:
ver = version_number.split(".")
return ".".join(ver[:3]), int(ver[3]), version_number

async def check_update() -> None:
latest_version = parse_version_number(
(await get_latest_version())[get_version_type()]
)
if latest_version[0] == version.VERSION and latest_version[1] <= version.SUB_VER:
return
logger.info(f"发现新版本: {latest_version[2]}")

0 comments on commit bc5d607

Please sign in to comment.