-
Notifications
You must be signed in to change notification settings - Fork 8
fix: get url auth #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
# v1.2.1 | ||
- 处理多媒体签名问题 | ||
|
||
# v1.2.0 | ||
- 支持文件上传至七牛 Bucket | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
import logging | ||
|
||
import qiniu | ||
from mcp import types | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unable to import 'mcp' (import-error) Detailslint 解释这个lint结果表明在代码中尝试导入一个名为 错误用法# 错误的导入方式
import mcp 正确用法
通过以上步骤,你应该能够解决
|
||
|
||
from . import utils | ||
from .processing import MediaProcessingService | ||
from ...config import config | ||
from ...consts import consts | ||
from ...tools import tools | ||
|
||
|
@@ -12,7 +15,8 @@ | |
|
||
|
||
class _ToolImpl: | ||
def __init__(self, cli: MediaProcessingService): | ||
def __init__(self, cfg: config.Config, cli: MediaProcessingService): | ||
self.auth = qiniu.Auth(cfg.access_key, cfg.secret_key) | ||
self.client = cli | ||
|
||
@tools.tool_meta( | ||
|
@@ -55,8 +59,8 @@ def image_scale_by_percent( | |
types.TextContent(type="text", text="percent must be between 1 and 999") | ||
] | ||
|
||
fop = f"imageMogr2/thumbnail/!{percent}p" | ||
object_url = utils.url_add_processing_func(object_url, fop) | ||
func = f"imageMogr2/thumbnail/!{percent}p" | ||
object_url = utils.url_add_processing_func(auth=self.auth, url=object_url, func=func) | ||
return [ | ||
types.TextContent( | ||
type="text", | ||
|
@@ -111,16 +115,16 @@ def image_scale_by_size( | |
if object_url is None or len(object_url) == 0: | ||
return [types.TextContent(type="text", text="object_url is required")] | ||
|
||
fop = f"{width}x{height}" | ||
if len(fop) == 1: | ||
func = f"{width}x{height}" | ||
if len(func) == 1: | ||
return [ | ||
types.TextContent( | ||
type="text", text="At least one width or height must be set" | ||
) | ||
] | ||
|
||
fop = f"imageMogr2/thumbnail/{fop}" | ||
object_url = utils.url_add_processing_func(object_url, fop) | ||
func = f"imageMogr2/thumbnail/{func}" | ||
object_url = utils.url_add_processing_func(auth=self.auth, url=object_url, func=func) | ||
return [ | ||
types.TextContent( | ||
type="text", | ||
|
@@ -191,7 +195,7 @@ def image_round_corner(self, **kwargs) -> list[types.TextContent]: | |
radius_y = radius_x | ||
|
||
func = f"roundPic/radiusx/{radius_x}/radiusy/{radius_y}" | ||
object_url = utils.url_add_processing_func(object_url, func) | ||
object_url = utils.url_add_processing_func(auth=self.auth, url=object_url, func=func) | ||
return [ | ||
types.TextContent( | ||
type="text", | ||
|
@@ -228,7 +232,7 @@ def image_info(self, **kwargs) -> list[types.TextContent]: | |
] | ||
|
||
func = "imageInfo" | ||
object_url = utils.url_add_processing_func(object_url, func) | ||
object_url = utils.url_add_processing_func(auth=self.auth, url=object_url, func=func) | ||
return [ | ||
types.TextContent( | ||
type="text", | ||
|
@@ -259,8 +263,8 @@ def get_fop_status(self, **kwargs) -> list[types.TextContent]: | |
return [types.TextContent(type="text", text=str(status))] | ||
|
||
|
||
def register_tools(cli: MediaProcessingService): | ||
tool_impl = _ToolImpl(cli) | ||
def register_tools(cfg: config.Config, cli: MediaProcessingService): | ||
tool_impl = _ToolImpl(cfg, cli) | ||
tools.auto_register_tools( | ||
[ | ||
tool_impl.image_scale_by_percent, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,16 @@ | ||
import time | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing module docstring (missing-module-docstring) Detailslint 解释
错误用法以下是一个缺少模块文档字符串的示例: # src/mcp_server/core/media_processing/utils.py
def add(a, b):
return a + b 在这个例子中, 正确用法以下是添加了模块文档字符串的正确示例: # src/mcp_server/core/media_processing/utils.py
"""
This module contains utility functions for media processing.
"""
def add(a, b):
"""
Adds two numbers and returns the result.
Args:
a (int): The first number.
b (int): The second number.
Returns:
int: The sum of `a` and `b`.
"""
return a + b 在这个例子中,模块顶部添加了一个文档字符串,描述了该模块的功能。每个函数也都有自己的文档字符串,详细说明了函数的参数和返回值。
|
||
from urllib import parse | ||
|
||
import qiniu | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unable to import 'qiniu' (import-error) Detailslint 解释这个lint结果表明在代码中尝试导入 错误用法from qiniu import Auth, put_file 在这个示例中,代码试图从 正确用法要解决这个问题,你需要确保 pip install qiniu 安装完成后,代码应该可以正常运行: from qiniu import Auth, put_file 这样,Python解释器就能找到并加载
|
||
|
||
from mcp_server.core.storage.tools import logger | ||
|
||
FUNC_POSITION_NONE = "none" | ||
FUNC_POSITION_PREFIX = "prefix" | ||
FUNC_POSITION_SUFFIX = "suffix" | ||
|
||
|
||
def url_add_processing_func(url: str, func: str) -> str: | ||
def url_add_processing_func(auth: qiniu.auth, url: str, func: str) -> str: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing function or method docstring (missing-function-docstring) Detailslint 解释
错误用法以下是一个缺少函数文档字符串的示例: def calculate_area(length, width):
return length * width 在这个例子中, 正确用法以下是添加了文档字符串的正确示例: def calculate_area(length, width):
"""
计算矩形面积
参数:
length (float): 矩形的长度
width (float): 矩形的宽度
返回:
float: 矩形的面积
"""
return length * width 在这个正确的示例中,
|
||
func_items = func.split("/") | ||
func_prefix = func_items[0] | ||
|
||
|
@@ -14,7 +19,8 @@ def url_add_processing_func(url: str, func: str) -> str: | |
new_query = parse.quote(new_query, safe='&=') | ||
url_info = url_info._replace(query=new_query) | ||
new_url = parse.urlunparse(url_info) | ||
return str(new_url) | ||
new_url = _sign_url(str(new_url), auth) | ||
return new_url | ||
|
||
|
||
def _query_add_processing_func(query: str, func: str, func_prefix: str) -> str: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Too many return statements (7/6) (too-many-return-statements) Detailslint 解释
错误用法以下是一个示例代码,展示了错误的用法: def check_number(num):
if num > 0:
return "Positive"
elif num < 0:
return "Negative"
else:
return "Zero" 在这个例子中,函数 正确用法以下是一个示例代码,展示了正确的用法: def check_number(num):
if num > 0:
return "Positive"
elif num < 0:
return "Negative"
return "Zero" 在这个例子中,函数
|
||
|
@@ -70,3 +76,33 @@ def _query_add_processing_func(query: str, func: str, func_prefix: str) -> str: | |
func = first_query + func.removeprefix(func_prefix) | ||
queries.insert(0, func) | ||
return "&".join(queries) | ||
|
||
|
||
def _sign_url(url: str, auth: qiniu.auth) -> str: | ||
url_info = parse.urlparse(url) | ||
query = url_info.query | ||
if ('e=' not in query) or ('token=' not in query): | ||
return url | ||
|
||
queries = query.split("&") | ||
if '' in queries: | ||
queries.remove('') | ||
|
||
# 移除之前的签名信息,但顺序不可变 | ||
expires = 3600 | ||
new_queries = [] | ||
for query_item in queries: | ||
if query_item.startswith('e='): | ||
try: | ||
deadline = int(query_item.removeprefix('e=')) | ||
expires = deadline - int(time.time()) | ||
except Exception as e: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Catching too general exception Exception (broad-exception-caught) Detailslint 解释这个lint结果表明代码捕获了一个过于通用的异常 错误用法try:
# 一些可能抛出异常的代码
pass
except Exception as e:
print(f"An error occurred: {e}") 在这个例子中, 正确用法try:
# 一些可能抛出异常的代码
pass
except SpecificException as e:
print(f"A specific error occurred: {e}") 在这个例子中,
|
||
logger.warning(f"expires parse fail for url:{url} err:{str(e)}") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use lazy % formatting in logging functions (logging-fstring-interpolation) Detailslint 解释在日志记录函数中使用惰性格式化(lazy % formatting)可以提高性能,因为它避免了在每次调用时进行字符串格式化。然而,lint 工具提示你使用 f-string 进行格式化,因为 f-string 在某些情况下可能更高效且更易读。 错误用法import logging
logger = logging.getLogger(__name__)
def log_message():
user_name = "Alice"
logger.info("User %s logged in" % user_name) 在这个例子中,使用了 正确用法import logging
logger = logging.getLogger(__name__)
def log_message():
user_name = "Alice"
logger.info(f"User {user_name} logged in") 在这个例子中,使用了 f-string 进行字符串格式化。
|
||
expires = 3600 | ||
elif not query_item.startswith('token='): | ||
new_queries.append(query_item) | ||
|
||
new_query = "&".join(new_queries) | ||
url_info = url_info._replace(query=new_query) | ||
new_url = parse.urlunparse(url_info) | ||
return auth.private_download_url(new_url, expires=expires) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing module docstring (missing-module-docstring) Detailslint 解释
错误用法以下是一个缺少模块文档字符串的示例: # version.py
def get_version():
return "1.0.0" 在这个例子中, 正确用法以下是添加了模块文档字符串的正确示例: """
This module provides functions to manage and retrieve the version information of the application.
"""
def get_version():
return "1.0.0" 在这个例子中,
|
||
VERSION = '1.2.0' | ||
VERSION = '1.2.1' |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing module docstring (missing-module-docstring)
Details
lint 解释
这个lint结果表明在文件
src/mcp_server/core/media_processing/tools.py
中缺少模块文档字符串(module docstring)。模块文档字符串是用于描述整个模块的注释,通常放在模块的最开始部分。错误用法
在这个例子中,
tools.py
文件没有包含任何文档字符串。正确用法
在这个例子中,
tools.py
文件包含了一个模块文档字符串,并且每个函数也包含了相应的文档字符串。