-
Notifications
You must be signed in to change notification settings - Fork 0
/
local.go
84 lines (68 loc) · 1.6 KB
/
local.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 storage
import (
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"github.com/itgcloud/gobackup/helper"
"github.com/itgcloud/gobackup/logger"
)
// Local storage
//
// type: local
// path: /data/backups
type Local struct {
Base
path string
}
func (s *Local) open() error {
s.path = s.viper.GetString("path")
return helper.MkdirP(s.path)
}
func (s *Local) close() {}
func (s *Local) upload(fileKey string) (err error) {
logger := logger.Tag("Local")
// Related path
if !path.IsAbs(s.path) {
s.path = path.Join(s.model.WorkDir, s.path)
}
targetPath := path.Join(s.path, fileKey)
targetDir := path.Dir(targetPath)
if err := helper.MkdirP(targetDir); err != nil {
logger.Errorf("failed to mkdir %q, %v", targetDir, err)
}
_, err = helper.Exec("cp", "-a", s.archivePath, targetPath)
if err != nil {
return err
}
logger.Info("Store succeeded", targetPath)
return nil
}
func (s *Local) delete(fileKey string) (err error) {
targetPath := filepath.Join(s.path, fileKey)
logger.Info("Deleting", targetPath)
return os.Remove(targetPath)
}
// List all files
func (s *Local) list(parent string) ([]FileItem, error) {
remotePath := filepath.Join(s.path, parent)
var items = []FileItem{}
files, err := ioutil.ReadDir(remotePath)
if err != nil {
return nil, err
}
for _, file := range files {
if !file.IsDir() {
items = append(items, FileItem{
Filename: file.Name(),
Size: file.Size(),
LastModified: file.ModTime(),
})
}
}
return items, nil
}
func (s *Local) download(fileKey string) (string, error) {
return "", fmt.Errorf("Local is not support download")
}