Skip to content

Commit

Permalink
Merge pull request #189 from ConnectAI-E/hotfix-issue-image-upload
Browse files Browse the repository at this point in the history
hotfix-issue-image-upload
  • Loading branch information
lloydzhou authored Feb 1, 2024
2 parents e5f108b + f42fce0 commit e4aaef8
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
2 changes: 1 addition & 1 deletion server/tasks/lark/issue.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def replace_images_with_keys(text, bot):
lambda match: f"![]({upload_image(match.group(1), bot)})",
text,
)
return replaced_text
return replaced_text.replace("![]()", "(请确认图片是否上传成功)")


def send_issue_url_message(
Expand Down
6 changes: 4 additions & 2 deletions server/utils/redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ def func():
real_time_delay = random.randrange(time_delay, max_time_delay)
get_client(False).expire(name, stale + real_time_delay + time_lock)
# 创建一个 asyncio 任务来执行 func
asyncio.create_task(asyncio.sleep(real_time_delay, func()))
loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.sleep(real_time_delay, func()))

return v

Expand Down Expand Up @@ -101,7 +102,8 @@ async def func():
# set expire to get a "lock", and delay to run the task
real_time_delay = random.randrange(time_delay, max_time_delay)
get_client(False).expire(name, stale + real_time_delay + time_lock)
asyncio.create_task(asyncio.sleep(real_time_delay, func()))
loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.sleep(real_time_delay, func()))

return v

Expand Down
6 changes: 4 additions & 2 deletions server/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@


# 使用 stalecache 装饰器,以 url 作为缓存键
@stalecache(expire=300, stale=600)
@stalecache(expire=3600, stale=600)
def upload_image(url, bot):
logging.info("upload image: %s", url)
if not url or not url.startswith("http"):
return ""
response = httpx.get(url, follow_redirects=True)
if response.status_code == 200:
# 函数返回值: iamg_key 存到缓存中
img_key = upload_image_binary(response.content, bot)
return img_key
else:
return None
return ""


def upload_image_binary(img_bin, bot):
Expand Down

0 comments on commit e4aaef8

Please sign in to comment.