Skip to content

Commit

Permalink
update(converter): re-use existing temporary directory
Browse files Browse the repository at this point in the history
Signed-off-by: Thanh Thai <[email protected]>
  • Loading branch information
FuLygon committed Jun 5, 2024
1 parent 55334d4 commit 6d1940f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 17 deletions.
16 changes: 2 additions & 14 deletions internal/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,28 +58,16 @@ func ConvertStatic(inputPath, outputDir, outputExt string, jpegQuality int) erro
return nil
}

func ConvertDynamic(inputPath, outputDir, outputExt string) error {
func ConvertDynamic(inputPath, outputDir, outputExt, tmpDir string) error {
var ffmpegInput = inputPath

// convert gif to avi if input is gif
if filepath.Ext(inputPath) == ".gif" {
// create temporary directory for storing avi file
tmpDir, err := os.MkdirTemp("", "sr65-app-*")
if err != nil {
return fmt.Errorf("error creating temporary directory for gif conversion: %w", err)
}
defer func(path string) {
err = os.RemoveAll(path)
if err != nil {
logger.Error("error removing temporary directory", err)
}
}(tmpDir)

// generate output avi path
outputFileAvi := generateOutput(inputPath, tmpDir, "avi")

// convert gif to avi
err = ffmpeg.Input(inputPath).Output(outputFileAvi,
err := ffmpeg.Input(inputPath).Output(outputFileAvi,
ffmpeg.KwArgs{
"f": "gif",
"vf": "fps=31,scale=128:128:flags=lanczos",
Expand Down
28 changes: 26 additions & 2 deletions internal/converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,18 @@ func TestConvertDynamicGif(t *testing.T) {
}
}

// prepare temporary directory
tmpDir, err := os.MkdirTemp("", "sr65-app-test-*")
if err != nil {
panic(err)
}
defer func(path string) {
err = os.RemoveAll(path)
if err != nil {
t.Logf("error removing temporary directory: %v", err)
}
}(tmpDir)

// create test file
testFile, err := os.Create(testGif)
if err != nil {
Expand Down Expand Up @@ -140,7 +152,7 @@ func TestConvertDynamicGif(t *testing.T) {
}

// convert gif
err = ConvertDynamic(testFile.Name(), outputDir, outputDynamicExt)
err = ConvertDynamic(testFile.Name(), outputDir, outputDynamicExt, tmpDir)
if err != nil {
t.Errorf("error converting gif: %v", err)
t.FailNow()
Expand Down Expand Up @@ -203,6 +215,18 @@ func TestConvertDynamicVideo(t *testing.T) {
}
}(testVideo)

// prepare temporary directory
tmpDir, err := os.MkdirTemp("", "sr65-app-test-*")
if err != nil {
panic(err)
}
defer func(path string) {
err = os.RemoveAll(path)
if err != nil {
t.Logf("error removing temporary directory: %v", err)
}
}(tmpDir)

// get test file path
testVideoPath, err := filepath.Abs(testVideo)
if err != nil {
Expand All @@ -216,7 +240,7 @@ func TestConvertDynamicVideo(t *testing.T) {
}

// convert mp4
err = ConvertDynamic(testVideoPath, outputDir, outputDynamicExt)
err = ConvertDynamic(testVideoPath, outputDir, outputDynamicExt, tmpDir)
if err != nil {
t.Errorf("error converting video: %v", err)
t.FailNow()
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func main() {
logger.Error("error converting static media", err)
}
case ".gif", ".mp4":
err = internal.ConvertDynamic(inputPath, outputDir, outputExtDynamic)
err = internal.ConvertDynamic(inputPath, outputDir, outputExtDynamic, tmpDir)
if err != nil {
logger.Error("error converting dynamic media", err)
}
Expand Down

0 comments on commit 6d1940f

Please sign in to comment.