-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
147 lines (133 loc) · 3.4 KB
/
main.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"sync"
"gopkg.in/yaml.v2"
)
type Config struct {
AccountID string `yaml:"account_id"`
AppKey string `yaml:"app_key"`
ConnConnections int `yaml:"con_connections"`
API APIConfig `yaml:"api"`
LogDir string `yaml:"log_dir"`
Folders []*Folders `yaml:"folders"`
}
type Folders struct {
BucketID string `yaml:"bucket_id"`
B2Folder string `yaml:"b2_folder"`
RootFolder string `yaml:"root_folder"`
Monitor bool `yaml:"monitor"`
Hour int `yaml:"hour"`
Minute int `yaml:"minute"`
DeleteFile bool `yaml:"delete_after_upload"`
b2Files B2ListFiles
}
type FileQueue struct {
sync.Mutex
Files []File
}
type File struct {
FilePath string
B2Path string
SHA string
RootPath string
FileSize int64
FileID string `json:"fileId"`
BucketID string `json:"bucketId"`
Parts []FilePart
UploadURL B2UploadURL
}
type FilePart struct {
Path string
ParentFileID string
Number int64
SHA string
ChunkSize int64
Complete bool
URL string
AuthToken string
FileName string
}
var configFilePath *string
var config Config
func main() {
getFlags()
config.parseConfig()
instance.b2Authorize()
for _, folder := range config.Folders {
folder.b2Files.b2GetCurrentFiles(*folder)
}
go config.initialScan()
daemon()
}
func getFiles(rootPath string) []string {
var fileList []string
if _, err := os.Stat(rootPath); os.IsNotExist(err) {
log.Fatal("Bad Folder location, please check config for ", rootPath)
}
err := filepath.Walk(rootPath, func(path string, f os.FileInfo, err error) error {
if !(f.IsDir()) {
fileList = append(fileList, strings.Replace(path, rootPath, "", 1))
}
return nil
})
if err != nil {
log.Fatal("Error reading directory of files")
}
return fileList
}
func getFlags() {
configFilePath = flag.String("config", "", "Specify the location to the configuration file to use")
flag.Parse()
// Let's make sure we have a config file present and verify it finds the file.
if *configFilePath == "" {
fmt.Println("Please supply a config file")
os.Exit(1)
} else if _, err := os.Stat(*configFilePath); os.IsNotExist(err) {
fmt.Println("Config file not found")
os.Exit(1)
}
}
func (c *Config) initialScan() {
for _, folder := range c.Folders {
listFiles := getFiles(folder.RootFolder)
for _, file := range listFiles {
var found bool
for _, file2 := range folder.b2Files.Files {
if (folder.B2Folder + file) == file2.FileName {
found = true
}
}
if !found {
var newFile File
newFile.RootPath = folder.RootFolder
newFile.B2Path = folder.B2Folder
newFile.FilePath = file[1:]
newFile.BucketID = folder.BucketID
getSHAChan <- newFile
}
}
}
}
func (c *Config) parseConfig() {
configFile, _ := ioutil.ReadFile(*configFilePath)
err := yaml.UnmarshalStrict(configFile, &config)
if err != nil {
fmt.Println("There was an error reading the config file. Error was ", err)
os.Exit(1)
}
if config.ConnConnections == 0 {
config.ConnConnections = 4
}
for _, folder := range config.Folders {
if (folder.Minute == 0) && (folder.Hour == 0) {
folder.Minute = 30
}
}
}