mirror of
https://github.com/woodchen-ink/webp_server_go.git
synced 2025-07-18 13:42:02 +08:00
Fix bug that could remove original image (#287)
* Fix bug that could remove original image * Refine notice
This commit is contained in:
parent
d98740cfea
commit
4a90b18a63
@ -112,9 +112,11 @@ func convertImage(raw, optimized, imageType string, extraParams config.ExtraPara
|
||||
if err != nil {
|
||||
log.Error(err.Error())
|
||||
}
|
||||
var jpgRaw = ConvertRawToJPG(raw, optimized)
|
||||
if jpgRaw != raw {
|
||||
raw = jpgRaw
|
||||
// Convert NEF image to JPG first
|
||||
var convertedRaw, converted = ConvertRawToJPG(raw, optimized)
|
||||
// If converted, use converted file as raw
|
||||
if converted {
|
||||
raw = convertedRaw
|
||||
}
|
||||
switch imageType {
|
||||
case "webp":
|
||||
@ -122,8 +124,10 @@ func convertImage(raw, optimized, imageType string, extraParams config.ExtraPara
|
||||
case "avif":
|
||||
err = avifEncoder(raw, optimized, extraParams)
|
||||
}
|
||||
if jpgRaw == raw {
|
||||
err := os.Remove(jpgRaw)
|
||||
// Remove converted file after convertion
|
||||
if converted {
|
||||
log.Infoln("Removing intermediate conversion file:", convertedRaw)
|
||||
err := os.Remove(convertedRaw)
|
||||
if err != nil {
|
||||
log.Warnln("failed to delete converted file", err)
|
||||
}
|
||||
|
@ -1,15 +1,16 @@
|
||||
package encoder
|
||||
|
||||
import (
|
||||
"github.com/jeremytorres/rawparser"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/jeremytorres/rawparser"
|
||||
)
|
||||
|
||||
func ConvertRawToJPG(rawPath, optimizedPath string) string {
|
||||
func ConvertRawToJPG(rawPath, optimizedPath string) (string, bool) {
|
||||
if !strings.HasSuffix(strings.ToLower(rawPath), ".nef") {
|
||||
// Maybe can use rawParser to convert other raw files to jpg, but I haven't tested it
|
||||
return rawPath
|
||||
return rawPath, false
|
||||
}
|
||||
parser, _ := rawparser.NewNefParser(true)
|
||||
info := &rawparser.RawFileInfo{
|
||||
@ -20,7 +21,7 @@ func ConvertRawToJPG(rawPath, optimizedPath string) string {
|
||||
_, err := parser.ProcessFile(info)
|
||||
if err == nil {
|
||||
_, file := filepath.Split(rawPath)
|
||||
return optimizedPath + file + "_extracted.jpg"
|
||||
return optimizedPath + file + "_extracted.jpg", true
|
||||
}
|
||||
return rawPath
|
||||
return rawPath, false
|
||||
}
|
||||
|
@ -145,7 +145,7 @@ func Convert(c *fiber.Ctx) error {
|
||||
// Check the original image for existence,
|
||||
if !helper.ImageExists(rawImageAbs) {
|
||||
helper.DeleteMetadata(reqURIwithQuery, targetHostName)
|
||||
msg := "image not found"
|
||||
msg := "Image not found!"
|
||||
_ = c.Send([]byte(msg))
|
||||
log.Warn(msg)
|
||||
_ = c.SendStatus(404)
|
||||
|
Loading…
x
Reference in New Issue
Block a user