Skip to content

Commit

Permalink
Fix bug that could remove original image
Browse files Browse the repository at this point in the history
  • Loading branch information
n0vad3v committed Nov 4, 2023
1 parent d98740c commit a3c68c8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
15 changes: 10 additions & 5 deletions encoder/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package encoder

import (
"errors"
"fmt"
"os"
"path"
"runtime"
Expand Down Expand Up @@ -112,18 +113,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 {
fmt.Println("delete jpgRaw", 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
}

0 comments on commit a3c68c8

Please sign in to comment.