From a3c68c84f70f8c5be458802d24dd39e1469685e4 Mon Sep 17 00:00:00 2001 From: n0vad3v Date: Sat, 4 Nov 2023 22:17:26 +0800 Subject: [PATCH 1/2] Fix bug that could remove original image --- encoder/encoder.go | 15 ++++++++++----- encoder/jpgconvert.go | 11 ++++++----- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/encoder/encoder.go b/encoder/encoder.go index 969faed47..976f03496 100644 --- a/encoder/encoder.go +++ b/encoder/encoder.go @@ -2,6 +2,7 @@ package encoder import ( "errors" + "fmt" "os" "path" "runtime" @@ -112,9 +113,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 +125,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 { + fmt.Println("delete jpgRaw", convertedRaw) + err := os.Remove(convertedRaw) if err != nil { log.Warnln("failed to delete converted file", err) } diff --git a/encoder/jpgconvert.go b/encoder/jpgconvert.go index 355c44d76..b4fae7bc2 100644 --- a/encoder/jpgconvert.go +++ b/encoder/jpgconvert.go @@ -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 } From 08f5dfa301ebb1937f3a3c51edcddcf80d267d8d Mon Sep 17 00:00:00 2001 From: n0vad3v Date: Sat, 4 Nov 2023 22:21:52 +0800 Subject: [PATCH 2/2] Refine notice --- encoder/encoder.go | 3 +-- handler/router.go | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/encoder/encoder.go b/encoder/encoder.go index 976f03496..cf9e15035 100644 --- a/encoder/encoder.go +++ b/encoder/encoder.go @@ -2,7 +2,6 @@ package encoder import ( "errors" - "fmt" "os" "path" "runtime" @@ -127,7 +126,7 @@ func convertImage(raw, optimized, imageType string, extraParams config.ExtraPara } // Remove converted file after convertion if converted { - fmt.Println("delete jpgRaw", convertedRaw) + log.Infoln("Removing intermediate conversion file:", convertedRaw) err := os.Remove(convertedRaw) if err != nil { log.Warnln("failed to delete converted file", err) diff --git a/handler/router.go b/handler/router.go index 7d86bd8f6..bbc431f4d 100644 --- a/handler/router.go +++ b/handler/router.go @@ -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)