Skip to content

Commit

Permalink
fix: download non image resource (#230)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoxiang authored Nov 27, 2024
1 parent 7798752 commit bd21955
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
25 changes: 23 additions & 2 deletions internal/markdown/markdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ func findAllImages(md string) (images []string) {
for _, matches := range imgRegexp.FindAllStringSubmatch(md, -1) {
if len(matches) == 3 {
s := matches[2]
_, err := url.ParseRequestURI(s)
if err == nil {
isImg, err := isImageURL(s)
if err == nil && isImg {
images = append(images, s)
}
// sometime exists broken image url, just ignore
Expand Down Expand Up @@ -138,3 +138,24 @@ func writeImageFile(ctx context.Context,
}
return nil
}

func isImageURL(urlStr string) (bool, error) {
// 解析 URL
parsedURL, err := url.ParseRequestURI(urlStr)
if err != nil {
return false, err
}
// 提取路径部分
filePath := parsedURL.Path

// 获取文件扩展名并转换为小写
ext := strings.ToLower(path.Ext(filePath))

// 检查扩展名是否属于图片类型
switch ext {
case ".jpg", ".jpeg", ".png", ".gif", ".webp", ".bmp", ".tiff":
return true, nil
default:
return false, nil
}
}
Loading

0 comments on commit bd21955

Please sign in to comment.