Skip to content

Commit

Permalink
Fix bug that could remove original image (#287)
Browse files Browse the repository at this point in the history
* Fix bug that could remove original image

* Refine notice
  • Loading branch information
n0vad3v authored Nov 4, 2023
1 parent d98740c commit 4a90b18
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
14 changes: 9 additions & 5 deletions encoder/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,18 +112,22 @@ 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":
err = webpEncoder(raw, optimized, extraParams)
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)
}
Expand Down
11 changes: 6 additions & 5 deletions encoder/jpgconvert.go
Original file line number Diff line number Diff line change
@@ -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{
Expand All @@ -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
}
2 changes: 1 addition & 1 deletion handler/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 4a90b18

Please sign in to comment.