Skip to content

Commit

Permalink
fix: 解决在docker容器内使用os.Rename发invalid cross-device link
Browse files Browse the repository at this point in the history
  • Loading branch information
yqchilde committed Mar 1, 2023
1 parent 4def064 commit 86888df
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions plugins/memepicture/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func init() {
}

func featureImage(url, b64, cacheDir string) (original, thumbnail string, err error) {
tmpFile := "/tmp/" + time.Now().Local().Format("20060102150405") + ".png"
tmpFile := fmt.Sprintf("%s/tmp_%s%s", cacheDir, time.Now().Local().Format("20060102150405"), ".png")
if url == "" && b64 == "" {
return "", "", errors.New("url和b64不能同时为空")
}
Expand Down Expand Up @@ -102,15 +102,18 @@ func featureImage(url, b64, cacheDir string) (original, thumbnail string, err er
mime, err := mimetype.DetectFile(tmpFile)
if err != nil {
os.Remove(tmpFile)
return
return "", "", err
}
original = fmt.Sprintf("%s/origin_%s%s", cacheDir, time.Now().Local().Format("20060102150405"), mime.Extension())
if err := os.Rename(tmpFile, original); err != nil {
os.Remove(tmpFile)
return "", "", err
}
original = fmt.Sprintf("%s/%s%s", cacheDir, time.Now().Local().Format("20060102150405"), mime.Extension())
os.Rename(tmpFile, original)

// 生成缩略图
if mime.Extension() == ".gif" { // gif
thumbnail = strings.ReplaceAll(original, "origin", "thumb")
thumbnail = strings.ReplaceAll(original, ".gif", ".png")
thumbnail = strings.ReplaceAll(thumbnail, ".gif", ".png")
return original, thumbnail, gif2Png(original, thumbnail)
} else {
thumbnail = original
Expand Down

0 comments on commit 86888df

Please sign in to comment.