-
Notifications
You must be signed in to change notification settings - Fork 3
/
app_archives.go
84 lines (70 loc) · 1.77 KB
/
app_archives.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package app
import (
"errors"
"os"
"path/filepath"
"github.com/apex/log"
"github.com/tympanix/supper/extract"
"github.com/tympanix/supper/media"
"github.com/tympanix/supper/types"
)
// FindArchives searches through filepaths and finds media archives
func (a *Application) FindArchives(roots ...string) ([]types.MediaArchive, error) {
archives := make([]types.MediaArchive, 0)
for _, root := range roots {
if _, err := os.Stat(root); os.IsNotExist(err) {
return nil, err
}
err := filepath.Walk(root, func(filepath string, f os.FileInfo, err error) error {
if f == nil {
return errors.New("invalid file path")
}
if f.IsDir() {
return nil
}
archive, err := extract.OpenMediaArchive(filepath)
if extract.IsNotArchive(err) {
return nil
}
if err != nil {
return err
}
archives = append(archives, archive)
return nil
})
if err != nil {
return nil, err
}
}
return archives, nil
}
// ExtractMedia reads a media file from a stream performs renaming/copying to
// its new destination
func (a *Application) ExtractMedia(m types.MediaReadCloser) error {
ctx := log.WithField("media", m).WithField("action", "extract")
if a.Config().MediaFilter() != nil {
if !a.Config().MediaFilter()(m) {
return nil
}
}
dest, err := a.scrapeAndRenameMedia(m, m)
if err != nil {
return err
}
if err = ensurePath(dest, a.Config().Force()); err != nil {
if media.IsExistsErr(err) {
ctx.WithField("reason", "media already exists").Warn("Extraction skipped")
}
return err
}
if !a.Config().Dry() {
if err := copyMedia(m, dest); err != nil {
ctx.WithError(err).Error("Extraction failed")
} else {
ctx.Info("Media extracted")
}
} else {
ctx.WithField("reason", "dry-run").Info("Skip extraction")
}
return nil
}