-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.go
264 lines (228 loc) · 7 KB
/
app.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package main
import (
"context"
"database/sql"
"fmt"
"github.com/wailsapp/wails/v2/pkg/runtime"
_ "image/jpeg"
_ "image/png"
_ "modernc.org/sqlite"
"os"
"os/exec"
"path/filepath"
"sort"
"strings"
)
// App struct
type App struct {
ctx context.Context
startDirectories []string
destinationDirectory string
recursive bool
minSize int64
minWidth int
minHeight int
namingConvention string
moveOrCopy string
skipOrRename string
verifyResults bool
substitutions []Substitution
cache *CacheDatabase
}
type Substitution struct {
From string `json:"from"`
To string `json:"to"`
Suffix int `json:"suffix"`
Relocated bool `json:"relocated"`
}
type ExampleSubstitution struct {
Path string `json:"path"`
Description string `json:"description"`
}
type RelocationStatus struct {
Substitutions []Substitution `json:"files"`
TotalSubstitutions int `json:"totalFiles"`
TotalRelocated int `json:"totalRelocated"`
}
type CacheDatabase struct {
DB *sql.DB
}
// NewApp creates a new App application struct
func NewApp() *App {
return &App{}
}
// startup is called when the app starts. The context is saved
// so that we can call the runtime methods
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
homeDir, _ := os.UserHomeDir()
appStorePath := homeDir + "/photo-organiser"
if _, err := os.Stat(appStorePath); os.IsNotExist(err) {
err := os.Mkdir(appStorePath, 0755)
checkErr(err, fmt.Sprintf("Cannot create required '%s' directory", appStorePath))
}
cacheDb, err := NewCacheDatabase(appStorePath + "/location-cache.db")
checkErr(err, "Failed to initialize location cache database")
a.cache = cacheDb
}
func (a *App) ResetEverything() {
a.startDirectories = []string{}
a.destinationDirectory = ""
a.recursive = true
a.minSize = 0
a.minWidth = 0
a.minHeight = 0
a.namingConvention = ""
a.moveOrCopy = ""
a.skipOrRename = ""
a.verifyResults = false
a.substitutions = []Substitution{}
}
func (a *App) SelectDestinationDirectory() string {
selection, err := runtime.OpenDirectoryDialog(a.ctx, runtime.OpenDialogOptions{
Title: "Select a directory where the images will end up",
CanCreateDirectories: true,
ShowHiddenFiles: false,
})
if err != nil || selection == "" {
return a.destinationDirectory
}
a.destinationDirectory = filepath.ToSlash(filepath.Clean(selection))
return a.destinationDirectory
}
func (a *App) SelectStartDirectories() []string {
selection, err := runtime.OpenDirectoryDialog(a.ctx, runtime.OpenDialogOptions{
Title: "Select a directory in which to search for images",
CanCreateDirectories: false,
ShowHiddenFiles: false,
})
if err != nil {
return []string{}
}
if selection != "" {
a.startDirectories = AppendPath(a.startDirectories, selection, a.recursive)
sort.Strings(a.startDirectories)
}
return a.startDirectories
}
func (a *App) RefreshStartDirectories() []string {
a.startDirectories = RemovePathsWithCommonParent(a.startDirectories)
return a.startDirectories
}
func (a *App) RemoveStartDirectory(directory string) []string {
for i, prevSelection := range a.startDirectories {
if prevSelection == directory {
a.startDirectories = append(a.startDirectories[:i], a.startDirectories[i+1:]...)
break
}
}
return a.startDirectories
}
func (a *App) ClearStartDirectories() []string {
a.startDirectories = []string{}
return a.startDirectories
}
func (a *App) BackendSetRecursive(recursive bool) {
a.recursive = recursive
}
func (a *App) BackendSetMinSize(size int64) {
a.minSize = size
}
func (a *App) BackendSetMinWidth(width int) {
a.minWidth = width
}
func (a *App) BackendSetMinHeight(height int) {
a.minHeight = height
}
func (a *App) BackendSetNamingConvention(namingConvention string) {
a.namingConvention = namingConvention
}
func (a *App) BackendSetMoveOrCopy(moveOrCopy string) {
a.moveOrCopy = moveOrCopy
}
func (a *App) BackendSetSkipOrRename(skipOrRename string) {
a.skipOrRename = skipOrRename
}
func (a *App) BackendSetVerifyResults(verifyResults bool) {
a.verifyResults = verifyResults
}
func (a *App) GetExamplePathSubstitution(namingConvention string, metadata *ImageMetadata, description string) ExampleSubstitution {
return ExampleSubstitution{ProcessPathSubstitution(namingConvention, metadata), description}
}
func (a *App) ProcessImages() {
a.substitutions = nil
getLocationDetails := HasLocationPlaceholders(a.namingConvention)
for _, dir := range a.startDirectories {
fmt.Println("Processing directory", dir)
maxDepth := strings.Count(dir, string(os.PathSeparator))
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
path = NormalizePath(path)
depth := strings.Count(path, string(os.PathSeparator))
if (depth > maxDepth) || (info.IsDir() && dir != path) {
if a.recursive {
return nil
}
return filepath.SkipDir
}
if !IsValidExtension(filepath.Ext(path)) {
//fmt.Println(" Skipping", path, "because it's not a valid image")
return nil
}
// Get all the info we need about the image
imageData, _ := GetImageMetadata(path, getLocationDetails, a.cache)
// Skip if we're filtering by size and the file is too small
if (a.minSize > 0) && (imageData.FileSize < a.minSize) {
//fmt.Println(" Skipping", path, "because it's too small")
return nil
}
// Skip if we're filtering by width and the file is too small
if (a.minWidth > 0) && (imageData.Width < a.minWidth) {
//fmt.Println(" Skipping", path, "because it's too narrow:", imageData.Width, "<", a.minWidth)
return nil
}
// Skip if we're filtering by height and the file is too small
if (a.minHeight > 0) && (imageData.Height < a.minHeight) {
//fmt.Println(" Skipping", path, "because it's too short")
return nil
}
from := imageData.FilePath
to := NormalizePath(filepath.Join(a.destinationDirectory, ProcessPathSubstitution(a.namingConvention, imageData)))
AppendSubstitution(&a.substitutions, Substitution{from, to, 0, false})
runtime.EventsEmit(a.ctx, "finding-files", a.substitutions)
return nil
})
if err != nil {
fmt.Println(err)
}
}
runtime.EventsEmit(a.ctx, "finding-complete")
fmt.Printf("Found %d image files\n", len(a.substitutions))
if !a.verifyResults {
RelocateFiles(a)
}
}
func (a *App) VerifyRelocation() {
RelocateFiles(a)
}
func (a *App) OpenHostLocation(filePath string) {
var cmd *exec.Cmd
switch platform := runtime.Environment(a.ctx).Platform; platform {
case "darwin":
cmd = exec.Command("open", filePath)
break
case "linux":
cmd = exec.Command("xdg-open", filePath)
break
case "windows":
runDll32 := filepath.Join(os.Getenv("SYSTEMROOT"), "System32", "rundll32.exe")
cmd = exec.Command(runDll32, "url.dll,FileProtocolHandler", filePath)
break
}
err := cmd.Start()
if err != nil {
runtime.LogError(a.ctx, fmt.Sprintf("Failed to open file: %s", err))
}
}