forked from gustaavik/wails-sveltekit-ts-tw-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.go
More file actions
346 lines (284 loc) · 9.43 KB
/
app.go
File metadata and controls
346 lines (284 loc) · 9.43 KB
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
package main
import (
"context"
"fmt"
"strings"
"github.com/wailsapp/wails/v2/pkg/runtime"
"converzen/internal/config"
"converzen/internal/database"
"converzen/internal/logger"
"converzen/internal/models"
"converzen/internal/repository"
"converzen/internal/services"
)
// App struct holds the application state and dependencies
type App struct {
ctx context.Context
// Configuration
config *config.Config
// Logger
log *logger.Logger
// Database
db *database.Database
// Services
fileService services.FileService
conversionService services.ConversionService
settingsService services.SettingsService
formatProvider services.FormatProvider
}
// NewApp creates a new App application struct
func NewApp() *App {
return &App{}
}
// startup is called when the app starts
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
// Initialize configuration
cfg, err := config.New()
if err != nil {
fmt.Printf("Failed to initialize config: %v\n", err)
return
}
a.config = cfg
// Initialize logger
logLevel := logger.INFO
if cfg.Debug {
logLevel = logger.DEBUG
}
log, err := logger.New(cfg.LogFile, logLevel)
if err != nil {
fmt.Printf("Failed to initialize logger: %v\n", err)
return
}
a.log = log
log.Info("app", "Starting %s v%s", cfg.AppName, cfg.Version)
log.Debug("app", "Data directory: %s", cfg.DataDir)
log.Debug("app", "Log file: %s", cfg.LogFile)
// Initialize database
db, err := database.New(cfg.DatabaseURL, log)
if err != nil {
log.Error("app", "Failed to initialize database: %v", err)
return
}
a.db = db
// Initialize repositories
conversionRepo := repository.NewConversionRepository(db.DB, log)
settingsRepo := repository.NewSettingsRepository(db.DB, log)
// Initialize services
a.fileService = services.NewFileService(log)
videoConverter := a.initVideoConverter(log)
imageConverter := services.NewImageConverter(log)
a.conversionService = services.NewConversionService(
a.fileService,
videoConverter,
imageConverter,
conversionRepo,
log,
)
a.settingsService = services.NewSettingsService(settingsRepo, log)
a.formatProvider = services.NewFormatProvider(videoConverter, imageConverter, a.getConverterBackend())
log.Info("app", "Application startup complete")
}
// shutdown is called when the app is closing
func (a *App) shutdown(ctx context.Context) {
if a.log != nil {
a.log.Info("app", "Application shutting down")
}
if a.db != nil {
a.db.Close()
}
if a.log != nil {
a.log.Close()
}
}
// SelectFiles opens a file dialog for selecting files
func (a *App) SelectFiles() ([]models.FileInfo, error) {
a.log.Debug("app", "Opening file selection dialog")
// Build file filters dynamically based on supported formats
filters := a.buildFileFilters()
files, err := runtime.OpenMultipleFilesDialog(a.ctx, runtime.OpenDialogOptions{
Title: "Select Files to Convert",
Filters: filters,
})
if err != nil {
a.log.Error("app", "File selection error: %v", err)
return nil, err
}
if len(files) == 0 {
a.log.Debug("app", "No files selected")
return []models.FileInfo{}, nil
}
// Validate the selected files
validFiles, err := a.fileService.ValidateFiles(files)
if err != nil {
a.log.Error("app", "File validation error: %v", err)
return nil, err
}
a.log.Info("app", "Selected %d valid files", len(validFiles))
return validFiles, nil
}
// buildFileFilters creates file filters based on supported formats from the formatProvider
func (a *App) buildFileFilters() []runtime.FileFilter {
videoFormats := a.formatProvider.GetSupportedVideoInputFormats()
imageFormats := a.formatProvider.GetSupportedImageInputFormats()
// Build pattern strings (e.g., "*.mp4;*.mov;*.m4v")
videoPattern := buildPatternFromFormats(videoFormats)
imagePattern := buildPatternFromFormats(imageFormats)
var filters []runtime.FileFilter
if videoPattern != "" {
filters = append(filters, runtime.FileFilter{
DisplayName: "Video Files",
Pattern: videoPattern,
})
}
if imagePattern != "" {
filters = append(filters, runtime.FileFilter{
DisplayName: "Image Files",
Pattern: imagePattern,
})
}
// Add "All Supported Files" option if we have formats
if videoPattern != "" || imagePattern != "" {
allPatterns := videoPattern
if allPatterns != "" && imagePattern != "" {
allPatterns += ";" + imagePattern
} else if imagePattern != "" {
allPatterns = imagePattern
}
filters = append(filters, runtime.FileFilter{
DisplayName: "All Supported Files",
Pattern: allPatterns,
})
}
return filters
}
// buildPatternFromFormats converts a slice of format strings to a file pattern
func buildPatternFromFormats(formats []string) string {
if len(formats) == 0 {
return ""
}
patterns := make([]string, len(formats))
for i, format := range formats {
patterns[i] = "*." + format
}
return strings.Join(patterns, ";")
}
// SelectDirectory opens a directory selection dialog
func (a *App) SelectDirectory() (string, error) {
a.log.Debug("app", "Opening directory selection dialog")
dir, err := runtime.OpenDirectoryDialog(a.ctx, runtime.OpenDialogOptions{
Title: "Select Output Directory",
})
if err != nil {
a.log.Error("app", "Directory selection error: %v", err)
return "", err
}
if dir != "" {
a.log.Info("app", "Selected output directory: %s", dir)
// Save as last used directory
a.settingsService.SetSetting(models.SettingLastOutputDir, dir)
}
return dir, nil
}
// GetOutputFormats returns available output formats for a file type
func (a *App) GetOutputFormats(fileType string) []string {
ft := models.FileType(fileType)
formats := a.fileService.GetOutputFormats(ft)
a.log.Debug("app", "Output formats for %s: %v", fileType, formats)
return formats
}
// ConvertFiles converts multiple files
func (a *App) ConvertFiles(request models.BatchConversionRequest) (*models.BatchConversionResult, error) {
a.log.Info("app", "Starting batch conversion: %d files to %s", len(request.Files), request.OutputFormat)
result, err := a.conversionService.ConvertBatch(request, func(progress models.ConversionProgress) {
// Emit progress event to frontend
runtime.EventsEmit(a.ctx, "conversion:progress", progress)
})
if err != nil {
a.log.Error("app", "Batch conversion error: %v", err)
return nil, err
}
// Emit completion event
runtime.EventsEmit(a.ctx, "conversion:complete", result)
a.log.Info("app", "Batch conversion complete: %d success, %d failed",
result.SuccessCount, result.FailCount)
return result, nil
}
// GetConversionHistory retrieves the conversion history
func (a *App) GetConversionHistory(limit int) ([]models.Conversion, error) {
a.log.Debug("app", "Getting conversion history (limit: %d)", limit)
return a.conversionService.GetConversionHistory(limit)
}
// GetSettings retrieves user settings
func (a *App) GetSettings() (*models.UserSettings, error) {
return a.settingsService.GetSettings()
}
// SaveSettings saves user settings
func (a *App) SaveSettings(settings models.UserSettings) error {
return a.settingsService.SaveSettings(settings)
}
// CheckFFmpeg checks if the video converter backend is available
func (a *App) CheckFFmpeg() bool {
return a.isFFmpegAvailable()
}
// GetFFmpegVersion returns the video converter backend version string
func (a *App) GetFFmpegVersion() (string, error) {
return a.getConverterVersion()
}
// AppInfoResponse contains application information for the frontend
type AppInfoResponse struct {
Name string `json:"name"`
Version string `json:"version"`
DataDir string `json:"dataDir"`
LogFile string `json:"logFile"`
ConverterBackend string `json:"converterBackend"`
FFmpegVersion string `json:"ffmpegVersion,omitempty"`
}
// GetAppInfo returns application information
func (a *App) GetAppInfo() AppInfoResponse {
info := AppInfoResponse{
Name: a.config.AppName,
Version: a.config.Version,
DataDir: a.config.DataDir,
LogFile: a.config.LogFile,
ConverterBackend: a.getConverterBackend(),
}
if version, err := a.getConverterVersion(); err == nil {
info.FFmpegVersion = version
}
return info
}
// SupportedFormatsResponse contains the supported formats for the frontend
type SupportedFormatsResponse struct {
VideoFormats []string `json:"videoFormats"`
ImageFormats []string `json:"imageFormats"`
Backend string `json:"backend"`
}
// GetSupportedFormats returns the supported output formats for the current backend
// This allows the frontend to dynamically show only formats that the backend supports
func (a *App) GetSupportedFormats() SupportedFormatsResponse {
return SupportedFormatsResponse{
VideoFormats: a.formatProvider.GetSupportedVideoOutputFormats(),
ImageFormats: a.formatProvider.GetSupportedImageOutputFormats(),
Backend: a.formatProvider.GetBackendName(),
}
}
// GetSupportedVideoFormats returns the supported video output formats
func (a *App) GetSupportedVideoFormats() []string {
return a.formatProvider.GetSupportedVideoOutputFormats()
}
// GetSupportedImageFormats returns the supported image output formats
func (a *App) GetSupportedImageFormats() []string {
return a.formatProvider.GetSupportedImageOutputFormats()
}
// CanConvert checks if conversion from input to output format is supported
func (a *App) CanConvert(fileType string, outputFormat string) bool {
switch models.FileType(fileType) {
case models.FileTypeVideo:
return a.formatProvider.CanConvertVideo(outputFormat)
case models.FileTypeImage:
return a.formatProvider.CanConvertImage(outputFormat)
default:
return false
}
}