Added functionality to archive files from ingest
This commit is contained in:
parent
a544f1f016
commit
9010de6a14
12
adept.go
12
adept.go
@ -10,5 +10,15 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
config.LoadConfig()
|
config.LoadConfig()
|
||||||
|
|
||||||
transcoder.NewRepository(viper.GetString("transcoder.repository"))
|
r := transcoder.NewRepository(viper.GetString("transcoder.repository"))
|
||||||
|
|
||||||
|
ingestFiles := r.SearchIngest()
|
||||||
|
for _, i := range ingestFiles {
|
||||||
|
// archive file
|
||||||
|
r.ArchiveFile(i.Name())
|
||||||
|
// transcode file
|
||||||
|
// TODO - t.Transcode(i.Name())
|
||||||
|
// clean up source file
|
||||||
|
// TODO - r.CleanupFile(i.Name())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,37 +1,87 @@
|
|||||||
package transcoder
|
package transcoder
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
"io"
|
||||||
"path/filepath"
|
"log"
|
||||||
"os"
|
"path/filepath"
|
||||||
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Repository struct {
|
type Repository struct {
|
||||||
basePath string
|
basePath string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRepository(path string) *Repository {
|
func NewRepository(path string) *Repository {
|
||||||
// make sure repository base directory exists
|
// make sure repository base directory exists
|
||||||
create_repo_dir(path)
|
create_repo_dir(path)
|
||||||
|
|
||||||
// make sure the ingest, archive, and output directories exist
|
// make sure the ingest, archive, and output directories exist
|
||||||
subDirs := []string{"ingest", "archive", "output"}
|
subDirs := []string{"ingest", "archive", "output"}
|
||||||
for _, d := range subDirs {
|
for _, d := range subDirs {
|
||||||
subPath := filepath.Join(path, d)
|
subPath := filepath.Join(path, d)
|
||||||
create_repo_dir(subPath)
|
create_repo_dir(subPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
r := new(Repository)
|
r := new(Repository)
|
||||||
r.basePath = path
|
r.basePath = path
|
||||||
return r
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Repository) SearchIngest() []os.FileInfo {
|
||||||
|
ingestPath := filepath.Join(r.basePath, "ingest")
|
||||||
|
|
||||||
|
ingestDir, err := os.Open(ingestPath)
|
||||||
|
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
|
||||||
|
ingestPath := filepath.Join(r.basePath, "ingest", inFile)
|
||||||
|
archivePath := filepath.Join(r.basePath, "archive", inFile)
|
||||||
|
|
||||||
|
log.Printf("Copying %s to the archive.", ingestPath)
|
||||||
|
|
||||||
|
// open ingest file
|
||||||
|
source, err := os.Open(ingestPath)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Error opening file in ingest: %s.", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
defer source.Close()
|
||||||
|
|
||||||
|
// attempt to create destination file
|
||||||
|
destination, err := os.Create(archivePath)
|
||||||
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func create_repo_dir(path string) {
|
func create_repo_dir(path string) {
|
||||||
_, err := os.Stat(path)
|
_, err := os.Stat(path)
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
log.Printf("Creating directory %s.\n", path)
|
log.Printf("Creating directory %s.\n", path)
|
||||||
os.Mkdir(path, 0755)
|
os.Mkdir(path, 0755)
|
||||||
} else {
|
} else {
|
||||||
log.Printf("Directory %s already exists.\n", path)
|
log.Printf("Directory %s already exists.\n", path)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
10
util/file.go
Normal file
10
util/file.go
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
package util
|
||||||
|
|
||||||
|
import (
|
||||||
|
"path"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func FilenameWithoutExtension(filename string) string {
|
||||||
|
return strings.TrimSuffix(filename, path.Ext(filename))
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user