Skip to content

Commit

Permalink
Implements mp3 copy when source is mp3
Browse files Browse the repository at this point in the history
There's no need to transcode to mp3 if the source file is already an MP3
file. This copies the file to the destination.
  • Loading branch information
topfunky committed May 2, 2024
1 parent 1028d47 commit 397e961
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 14 deletions.
50 changes: 43 additions & 7 deletions find_and_transcode_files.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"io"
"os"
"path/filepath"
"strings"
Expand All @@ -19,27 +20,61 @@ type FileToRender struct {
// findFiles traverses the specified directory and transcodes all .m4a files to .mp3 format.
func findFiles(sourceDir, destinationDir string) {
fmt.Printf("🔍 Finding files in source directory %s\n", sourceDir)
// TODO: Needs to either look for existence of .m4a or compareDirectories() should be rewritten to return source file name
// TODO: But if .mp3 exists as source, then it should be copied to the destination as-is
filesThatNeedToBeRendered, err := compareDirectories(sourceDir, destinationDir)

if err != nil {
fmt.Println("Error:", err)
}

for _, file := range filesThatNeedToBeRendered {
if strings.HasSuffix(file.sourcePath, ".m4a") {
sourcePath := filepath.Join(sourceDir, file.sourcePath)

if strings.HasSuffix(sourcePath, ".m4a") {
// TODO: Extract to transcodeFileAtPath with sourcePath and destinationDir
sourcePath := filepath.Join(sourceDir, file.sourcePath)
destinationPath := filepath.Join(destinationDir, strings.TrimSuffix(file.sourcePath, filepath.Ext(file.sourcePath))+".mp3")

err := transcodeFileAtPath(sourcePath, destinationPath)
if err != nil {
fmt.Println("Error:", err)
}
} else {
// Copy mp3 from source to destination
destinationPath := filepath.Join(destinationDir, file.sourcePath)
fmt.Printf("📂 Copy MP3: %s\n", destinationPath)
if err := copyFile(sourcePath, destinationPath); err != nil {
fmt.Println("Error:", err)
}
}
}
}

func copyFile(source, destination string) error {
if err := os.MkdirAll(filepath.Dir(destination), 0755); err != nil {
return fmt.Errorf("❗️Failed to create directories: %v", err)
}

// Open the source file for reading
sourceFile, err := os.Open(source)
if err != nil {
return err
}
defer sourceFile.Close()

// Create the destination file
destinationFile, err := os.Create(destination)
if err != nil {
return err
}
defer destinationFile.Close()

// Copy the contents of the source file into the destination file
_, err = io.Copy(destinationFile, sourceFile)
if err != nil {
return err
}

// Call Sync to flush writes to stable storage
destinationFile.Sync()

return nil
}

// transcodeFileAtPath transcodes the file at the specified path from .m4a to .mp3 format.
Expand Down Expand Up @@ -118,11 +153,12 @@ func getExclusiveFiles(filesA, filesB []string) []FileToRender {
// Generate destination filenames so they can be compared to rendered output filenames
var sourceFileOutputNameList []FileToRender
for _, file := range filesA {
// TODO: This needs to be a struct with source and destination filenames (so that they can be rendered properly)

destinationFilename := ""
if strings.HasSuffix(file, ".mp3") {
destinationFilename = file
} else {
// TODO: Use func to get file ext instead of hard coding .m4a
destinationFilename = strings.TrimSuffix(file, ".m4a") + ".mp3"
}
fileToRender := FileToRender{
Expand Down
17 changes: 10 additions & 7 deletions sync_and_transcode_music_files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,12 @@ func setupFixtureFilesInDirectory(tempDir string, numberOfFiles int) error {
"source/file4.m4a",
"source/a-band/file5.m4a",
"source/Whitespace Band/file6.m4a",
"source/the-band/file7.mp3",
}
for _, file := range testFiles[0:numberOfFiles] {
filePath := filepath.Join(tempDir, file)
if err := generateM4aFixtureFileAtPath(filePath); err != nil {
return fmt.Errorf("failed to create test file: %v", err)
return fmt.Errorf("Failed to create test file: %v", err)
}
}

Expand Down Expand Up @@ -87,23 +88,25 @@ func setup(t *testing.T, numberOfFiles int) (string, error) {
}

func TestFindFiles(t *testing.T) {
tempDir, err := setup(t, 5)
defer os.RemoveAll(tempDir)

findFiles(filepath.Join(tempDir, "source"), filepath.Join(tempDir, "destination"))

transcodedFiles := []string{
"destination/file1.mp3",
"destination/file2.mp3",
"destination/file4.mp3",
"destination/a-band/file5.mp3",
"destination/Whitespace Band/file6.mp3",
"destination/the-band/file7.mp3",
}

tempDir, err := setup(t, len(transcodedFiles))
defer os.RemoveAll(tempDir)

findFiles(filepath.Join(tempDir, "source"), filepath.Join(tempDir, "destination"))

for _, file := range transcodedFiles {
t.Run(fmt.Sprintf("File %s should be rendered", file), func(t *testing.T) {
filePath := filepath.Join(tempDir, file)
_, err = os.Stat(filePath)
assert.False(t, os.IsNotExist(err), fmt.Sprintf("transcoded file not found: %s", file))
assert.False(t, os.IsNotExist(err), fmt.Sprintf("Transcoded file not found: %s", file))
})
}

Expand Down

0 comments on commit 397e961

Please sign in to comment.