Skip to content

Commit

Permalink
feat(app):
Browse files Browse the repository at this point in the history
- vditor image fetch ignore cos domain
  • Loading branch information
MorvanZhou committed Jul 25, 2024
1 parent 032dc59 commit d4240c6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/retk/controllers/files/upload_files.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from typing import List
from urllib.parse import urlparse

from fastapi import UploadFile

from retk import const, core
from retk.config import get_settings
from retk.controllers import schemas
from retk.controllers.utils import (
AuthedUser,
Expand Down Expand Up @@ -94,11 +96,14 @@ async def fetch_image_vditor(
url=req.url,
),
)

if len(req.url) <= 2048:
new_url, code = await core.files.fetch_image_vditor(au=au, url=req.url)
domain = urlparse(req.url).netloc
if get_settings().COS_DOMAIN != "" and domain == get_settings().COS_DOMAIN:
new_url, code = req.url, const.CodeEnum.OK
else:
new_url, code = "", const.CodeEnum.URL_TOO_LONG
if len(req.url) <= 2048:
new_url, code = await core.files.fetch_image_vditor(au=au, url=req.url)
else:
new_url, code = "", const.CodeEnum.URL_TOO_LONG
maybe_raise_json_exception(au=au, code=code)
return schemas.files.VditorImagesResponse(
msg=const.get_msg_by_code(code, au.language),
Expand Down
11 changes: 11 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -901,6 +901,17 @@ def test_upload_invalid_file(self):
shutil.rmtree("temp", ignore_errors=True)

def test_fetch_image(self):
config.get_settings().COS_DOMAIN = "example.com"
img = f"https://{config.get_settings().COS_DOMAIN}/example.png"
resp = self.client.post(
"/api/files/vditor/images",
json={"url": img},
headers=self.default_headers,
)
rj = self.check_ok_response(resp, 200)
self.assertEqual(img, rj["data"]["url"])
config.get_settings().COS_DOMAIN = ""

img = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/" \
"w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=="
resp = self.client.post(
Expand Down

0 comments on commit d4240c6

Please sign in to comment.