2022-08-31 19:56:06 -04:00
|
|
|
package transcoder
|
|
|
|
|
|
|
|
import (
|
2022-08-31 22:41:05 -04:00
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"path/filepath"
|
|
|
|
"os"
|
2022-08-31 19:56:06 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type Repository struct {
|
2022-09-03 19:51:37 -04:00
|
|
|
basePath string
|
|
|
|
ingestPath string
|
|
|
|
archivePath string
|
|
|
|
outputPath string
|
2022-08-31 19:56:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewRepository(path string) *Repository {
|
2022-08-31 22:41:05 -04:00
|
|
|
r := new(Repository)
|
|
|
|
r.basePath = path
|
2022-09-03 19:51:37 -04:00
|
|
|
r.ingestPath = filepath.Join(path, "ingest")
|
|
|
|
r.archivePath = filepath.Join(path, "archive")
|
|
|
|
r.outputPath = filepath.Join(path, "output")
|
|
|
|
|
|
|
|
// make sure repository base directory exists
|
|
|
|
create_repo_dir(r.basePath)
|
|
|
|
// make sure the folder structure is setup
|
|
|
|
create_repo_dir(r.ingestPath)
|
|
|
|
create_repo_dir(r.archivePath)
|
|
|
|
create_repo_dir(r.archivePath)
|
|
|
|
|
2022-08-31 22:41:05 -04:00
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2022-09-03 19:51:37 -04:00
|
|
|
// Repository getters
|
|
|
|
func (r *Repository) GetIngestPath() string {
|
|
|
|
return r.ingestPath
|
|
|
|
}
|
|
|
|
func (r *Repository) GetOutputPath() string {
|
|
|
|
return r.outputPath
|
|
|
|
}
|
2022-08-31 22:41:05 -04:00
|
|
|
|
2022-09-03 19:51:37 -04:00
|
|
|
func (r *Repository) SearchIngest() []os.FileInfo {
|
2022-09-01 00:17:29 -04:00
|
|
|
log.Printf("Searching ingest directory for files to transcode...")
|
|
|
|
|
2022-09-03 19:51:37 -04:00
|
|
|
ingestDir, err := os.Open(r.ingestPath)
|
2022-08-31 22:41:05 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error opening ingest directory: %s", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
files, err := ingestDir.Readdir(0)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error searching for files in ingest: %s", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
return files
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Repository) ArchiveFile(inFile string) {
|
|
|
|
// create ingest and archive paths
|
2022-09-03 19:51:37 -04:00
|
|
|
ingestFilePath := filepath.Join(r.ingestPath, inFile)
|
|
|
|
archiveFilePath := filepath.Join(r.archivePath, inFile)
|
2022-08-31 22:41:05 -04:00
|
|
|
|
2022-09-03 19:51:37 -04:00
|
|
|
log.Printf("Copying %s to the archive.", ingestFilePath)
|
2022-08-31 22:41:05 -04:00
|
|
|
|
|
|
|
// open ingest file
|
2022-09-03 19:51:37 -04:00
|
|
|
source, err := os.Open(ingestFilePath)
|
2022-08-31 22:41:05 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error opening file in ingest: %s.", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
defer source.Close()
|
|
|
|
|
|
|
|
// attempt to create destination file
|
2022-09-03 19:51:37 -04:00
|
|
|
destination, err := os.Create(archiveFilePath)
|
2022-08-31 22:41:05 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error opening archive file: %s.", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
defer destination.Close()
|
|
|
|
destination.Chmod(0755)
|
|
|
|
|
|
|
|
// perform the file copy
|
|
|
|
_, err = io.Copy(destination, source)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error while archiving file from ingest: %s.", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2022-08-31 19:56:06 -04:00
|
|
|
}
|
|
|
|
|
2022-08-31 22:50:42 -04:00
|
|
|
func (r *Repository) CleanupFile(inFile string) {
|
|
|
|
// create ingest path
|
2022-09-03 19:51:37 -04:00
|
|
|
ingestFilePath := filepath.Join(r.ingestPath, inFile)
|
2022-08-31 22:50:42 -04:00
|
|
|
|
|
|
|
// remove the file
|
2022-09-03 19:51:37 -04:00
|
|
|
os.Remove(ingestFilePath)
|
2022-08-31 22:50:42 -04:00
|
|
|
}
|
|
|
|
|
2022-08-31 19:56:06 -04:00
|
|
|
func create_repo_dir(path string) {
|
2022-08-31 22:41:05 -04:00
|
|
|
_, err := os.Stat(path)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
log.Printf("Creating directory %s.\n", path)
|
|
|
|
os.Mkdir(path, 0755)
|
|
|
|
} else {
|
|
|
|
log.Printf("Directory %s already exists.\n", path)
|
|
|
|
}
|
2022-08-31 19:56:06 -04:00
|
|
|
}
|