Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug that could remove original image #287

Merged
merged 2 commits into from
Nov 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading