/
copyAssets.go
148 lines (125 loc) · 3.29 KB
/
copyAssets.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
148
package wp_spotlight
import (
"fmt"
"log"
"os"
"regexp"
"strings"
"time"
"github.com/pjsoftware/update-wallpaper/pkg/sha"
"github.com/pjsoftware/update-wallpaper/pkg/util"
"github.com/pjsoftware/update-wallpaper/pkg/wallpaper"
)
/////////////////////////////////////////////////////////////////////////////
// Code above works; code below not so much
/////////////////////////////////////////////////////////////////////////////
// count returns the number of valid Assets found
func (as *assets) count() int {
return len(as.byName)
}
// compareWithExisting scans targetPath folder and compares to Assets
func (as *assets) compareWithExisting() (int, int) {
wpFound := 0
matchesFound := 0
files, err := os.ReadDir(as.targetFolder)
if err != nil {
log.Fatalf("compare error reading %s: %v", as.targetFolder, err)
}
for _, file := range files {
filePath := as.targetFolder + "/" + file.Name()
fileInfo, _ := file.Info()
fileSize := fileInfo.Size()
wpFound++
if _, ok := as.sumBySize[fileSize]; ok {
existingHash, err := sha.FileHash(filePath)
if err != nil {
log.Fatalf("Error calculating hash: %s, %v", filePath, err)
}
for name, assetHash := range as.sumBySize[fileSize] {
if existingHash == assetHash {
as.byName[name].replace = file.Name()
}
}
}
}
as.matches = matchesFound
return wpFound, matchesFound
}
// Copy copies all new, non-matched assets to wallpaper
func (as *assets) Copy() (int, int) {
copied := 0
renamed := 0
if as.count() <= as.matches {
return copied, renamed
}
for _, asset := range as.byName {
cc, rc := asset.publish(as.sourceFolder, as.targetFolder)
copied += cc
renamed += rc
}
return copied, renamed
}
func (a *asset) setNewName(targetPath string) {
a.newPath = targetPath + "/"
a.newName = a.name
a.newFilename()
a.newName += ".jpg"
a.newPath += a.newName
}
func (a *asset) newFilename() {
type repl struct {
in string
out string
}
rs := []repl{
{`[<>:"/\|?*]+`, ` + `},
{`\s+`, ` `},
{`(\s*©)+\s*`, ` © `},
{`(\s+[+])+\s+`, ` + `},
}
nfn := strings.TrimSpace(a.description + " © " + a.copyright)
for _, r := range rs {
re := regexp.MustCompile(r.in)
nfn = re.ReplaceAllString(nfn, r.out)
}
a.newName = nfn
}
func (a *asset) publish(sourcePath, targetPath string) (int, int) {
if !a.toBeCopied {
return 0, 0
}
a.setNewName(targetPath)
if _, err := os.Stat(a.newPath); err == nil {
return 0, 0
}
if a.replace != "" {
if util.FirstN(a.newName, len(NO_DESCRIPTION)) == NO_DESCRIPTION {
// fmt.Printf("- Skip attempt to rename to %s\n", a.newName)
return 0, 0
}
old := targetPath + "/" + a.replace
new := targetPath + "/" + a.newName
fmt.Printf("- Renaming: %s\n to: %s\n", a.replace, a.newName)
os.Rename(old, new)
setTime(new)
return 0, 1
}
_, err := a.copyFile(sourcePath)
if err == nil {
fmt.Printf("- Copying: %s\n", a.newName)
setTime(targetPath + "/" + a.newName)
return 1, 0
}
fmt.Printf("Error copying file: %v\n", err)
return 0, 0
}
func (a *asset) copyFile(fromFolder string) (bool, error) {
return wallpaper.Copy(fromFolder+"/"+a.name, a.newPath)
}
func setTime(fileName string) {
currentTime := time.Now().Local()
err := os.Chtimes(fileName, currentTime, currentTime)
if err != nil {
fmt.Printf(" > error changing file time: %v\n", err)
}
}