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 {
|
if err != nil {
|
||||||
log.Error(err.Error())
|
log.Error(err.Error())
|
||||||
}
|
}
|
||||||
var jpgRaw = ConvertRawToJPG(raw, optimized)
|
// Convert NEF image to JPG first
|
||||||
if jpgRaw != raw {
|
var convertedRaw, converted = ConvertRawToJPG(raw, optimized)
|
||||||
raw = jpgRaw
|
// If converted, use converted file as raw
|
||||||
|
if converted {
|
||||||
|
raw = convertedRaw
|
||||||
}
|
}
|
||||||
switch imageType {
|
switch imageType {
|
||||||
case "webp":
|
case "webp":
|
||||||
@ -122,8 +124,10 @@ func convertImage(raw, optimized, imageType string, extraParams config.ExtraPara
|
|||||||
case "avif":
|
case "avif":
|
||||||
err = avifEncoder(raw, optimized, extraParams)
|
err = avifEncoder(raw, optimized, extraParams)
|
||||||
}
|
}
|
||||||
if jpgRaw == raw {
|
// Remove converted file after convertion
|
||||||
err := os.Remove(jpgRaw)
|
if converted {
|
||||||
|
log.Infoln("Removing intermediate conversion file:", convertedRaw)
|
||||||
|
err := os.Remove(convertedRaw)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warnln("failed to delete converted file", err)
|
log.Warnln("failed to delete converted file", err)
|
||||||
}
|
}
|
||||||
|
@ -1,15 +1,16 @@
|
|||||||
package encoder
|
package encoder
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/jeremytorres/rawparser"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"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") {
|
if !strings.HasSuffix(strings.ToLower(rawPath), ".nef") {
|
||||||
// Maybe can use rawParser to convert other raw files to jpg, but I haven't tested it
|
// 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)
|
parser, _ := rawparser.NewNefParser(true)
|
||||||
info := &rawparser.RawFileInfo{
|
info := &rawparser.RawFileInfo{
|
||||||
@ -20,7 +21,7 @@ func ConvertRawToJPG(rawPath, optimizedPath string) string {
|
|||||||
_, err := parser.ProcessFile(info)
|
_, err := parser.ProcessFile(info)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
_, file := filepath.Split(rawPath)
|
_, 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,
|
// Check the original image for existence,
|
||||||
if !helper.ImageExists(rawImageAbs) {
|
if !helper.ImageExists(rawImageAbs) {
|
||||||
helper.DeleteMetadata(reqURIwithQuery, targetHostName)
|
helper.DeleteMetadata(reqURIwithQuery, targetHostName)
|
||||||
msg := "image not found"
|
msg := "Image not found!"
|
||||||
_ = c.Send([]byte(msg))
|
_ = c.Send([]byte(msg))
|
||||||
log.Warn(msg)
|
log.Warn(msg)
|
||||||
_ = c.SendStatus(404)
|
_ = c.SendStatus(404)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user