-
Notifications
You must be signed in to change notification settings - Fork 28
/
storage.go
59 lines (52 loc) · 1.43 KB
/
storage.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
package file
import (
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"github.com/ajdnik/decrypo/decryptor"
"github.com/kennygrant/sanitize"
)
var (
// ErrNil defines an error sent when an unexpected value is nil
ErrNil = errors.New("value is nil")
unknown = ""
)
// Storage implements the video clip storage interface which stores clips to the filesystem
type Storage struct {
Path string
}
func pathFriendlyTitle(title string) string {
return sanitize.BaseName(title)
}
// generatePath generates a filesystem path where the clip can be saved
func (s *Storage) generatePath(mod *decryptor.Module) (string, error) {
if mod == nil {
return unknown, ErrNil
}
if mod.Course == nil {
return unknown, ErrNil
}
path := filepath.Join(s.Path, pathFriendlyTitle(mod.Course.Title))
path = filepath.Join(path, pathFriendlyTitle(fmt.Sprintf("%v - %v", mod.Order, mod.Title)))
err := os.MkdirAll(path, os.ModePerm)
if err != nil {
return path, err
}
return path, nil
}
// Save stores the decrypted video clip to teh filesystem
func (s *Storage) Save(c decryptor.Clip, r io.Reader) (string, error) {
path, err := s.generatePath(c.Module)
if err != nil {
return unknown, err
}
filename := filepath.Join(path, fmt.Sprintf("%v.mp4", pathFriendlyTitle(fmt.Sprintf("%v %v", c.Order, c.Title))))
buf, err := ioutil.ReadAll(r)
if err != nil {
return unknown, err
}
return filename, ioutil.WriteFile(filename, buf, os.ModePerm)
}