-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathhelperfuncs.go
65 lines (55 loc) · 1.17 KB
/
helperfuncs.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
package main
import (
"os"
"path"
"sort"
"strings"
)
func notAlreadyDownloaded(done, all []Photo) []Photo {
photoMap := make(map[string]Photo)
// Add all photos to map
for _, p := range all {
photoMap[p.ID] = p
}
// Remove all downloaded photos.
for _, p := range done {
delete(photoMap, p.ID)
}
// Convert back to slice.
var notDownloaded []Photo
for _, p := range photoMap {
notDownloaded = append(notDownloaded, p)
}
// Sort photos to avoid duplicate saves if partially downloaded before
sort.Sort(PhotoSet(notDownloaded))
return notDownloaded
}
func createFolder(rootPath, name string) string {
cleanName := sanitize(name)
folderPath := path.Join(rootPath, cleanName)
if _, err := os.Stat(folderPath); os.IsNotExist(err) {
os.Mkdir(folderPath, 0700)
}
return folderPath
}
func sanitize(dirty string) string {
illegalSubStrings := []struct {
Illegal string
ReplaceWith string
}{
{"/", "-"},
{"<", "-"},
{">", "-"},
{":", "-"},
{"\\", "-"},
{"|", "-"},
{"?", "-"},
{"*", "-"},
{"\"", "-"},
}
clean := dirty
for _, v := range illegalSubStrings {
clean = strings.Replace(clean, v.Illegal, v.ReplaceWith, -1)
}
return clean
}