-
Notifications
You must be signed in to change notification settings - Fork 0
/
filequeuehandler.go
127 lines (110 loc) · 2.91 KB
/
filequeuehandler.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
package main
import (
"errors"
"os"
"path/filepath"
"strings"
"time"
"github.com/c2h5oh/datasize"
log "github.com/sirupsen/logrus"
)
// == F I L E Q U E U E =========================
// FileQueue list of files with methods
type FileQueue struct {
files []File
}
// check if the FileQueue is Empty
func (q *FileQueue) empty() bool {
if len(q.files) > 0 {
return false
}
return true
}
// add file to filequeue
// TODO: missing errorhandling
func (q *FileQueue) add(f File) {
// empty check if the queue is empty
if q.empty() {
q.files = append(q.files, f)
log.Infoln("added", f.Name, "to FileQueue")
return
}
// stopped here ! Want to add file is NOT in queue
for _, entry := range q.files {
if entry.Path == f.Path {
// log.Debugln("skipping", f.Name, " file already in queue")
return
}
}
q.files = append(q.files, f)
log.Infoln("added", f.Name, "to FileQueue")
return
}
// list creates a simple list of files in string format for easy debuging
func (q *FileQueue) list() string {
list := "List of all entries:"
for _, entry := range q.files {
list = list + "\n" + entry.Name
}
return list
}
// get gets the first not locked file from the FileQueue and locks it before handing over
func (q *FileQueue) get() (*File, error) {
if q.empty() {
return nil, errors.New("error: queue empty")
}
for i := range q.files {
if !q.files[i].Locked {
q.files[i].Locked = true
var f File
f = q.files[i]
return &f, nil
}
}
return nil, errors.New("error: All file in list are locked")
}
func (q *FileQueue) remove(f File) error {
for i := range q.files {
if q.files[i].Path == f.Path {
// TODO: some logging
// actualy removing in golang .. xD
q.files = append(q.files[:i], q.files[i+1:]...)
return nil
}
}
return errors.New("error: No eilequeue entrie matches this file")
}
// == F I L E P A T H W A L K E R ===============
// FilePathWalker is starts a loop that scanns the folder for files and tries to add them to the queue
func FilePathWalker(inputFolder string, q *FileQueue, interval time.Duration) error {
for {
// beware the embedded func that will do stuff
filepath.Walk(inputFolder, func(path string, info os.FileInfo, err error) error {
// this is a loop of all files found
// this will skip anthing files in subdirectories
if info.IsDir() && path != inputFolder {
return filepath.SkipDir
}
// ignore folders and unix hidden files
// TODO: Windows-hidden files work difrently
if !info.IsDir() && !strings.HasPrefix(info.Name(), ".") {
// this is a file we want to use
// log.Debugln("found a file: " + info.Name())
s := datasize.ByteSize(info.Size())
// create new File Object
f := File{
Name: info.Name(),
Path: path,
Size: s,
ModTime: info.ModTime(),
Locked: false,
}
// add to file Queue
q.add(f)
}
return nil
})
// the scanning frequency
time.Sleep(2 * time.Second)
}
}