Duplicated download function has been moved to local variable
MODIFY
ui/window.go
ui/window.go
@@ -439,14 +439,7 @@ func NewWindow(doRestart chan bool, app *tview.Application, session *discordgo.S
if pathError != nil || absolutePath == "" {
window.ShowErrorDialog("Please specify a valid path in 'FileOpenSaveFolder' of your configuration.")
} else {
- for _, file := range message.Attachments {
- extension := strings.TrimPrefix(filepath.Ext(file.URL), ".")
- targetFile := filepath.Join(absolutePath, file.ID+"."+extension)
-
- //All files are downloaded separately in order to not
- //block the UI and not download for ages if one or more
- //page has a slow download speed.
- go func(savePath, fileURL string) {
+ downloadFunction := func(savePath, fileURL string) {
_, statErr := os.Stat(savePath)
//If the file exists already, we needn't do anything.
if statErr == nil {
@@ -459,7 +452,16 @@ func NewWindow(doRestart chan bool, app *tview.Application, session *discordgo.S
window.ShowErrorDialog("Error download file: " + downloadError.Error())
})
}
- }(targetFile, file.URL)
+ }
+
+ for _, file := range message.Attachments {
+ extension := strings.TrimPrefix(filepath.Ext(file.URL), ".")
+ targetFile := filepath.Join(absolutePath, file.ID+"."+extension)
+
+ //All files are downloaded separately in order to not
+ //block the UI and not download for ages if one or more
+ //page has a slow download speed.
+ go downloadFunction(targetFile, file.URL)
}
urlMatches := urlRegex.FindAllString(message.Content, 1000)
@@ -474,20 +476,7 @@ func NewWindow(doRestart chan bool, app *tview.Application, session *discordgo.S
//All files are downloaded separately in order to not
//block the UI and not download for ages if one or more
//page has a slow download speed.
- go func(savePath, fileURL string) {
- _, statErr := os.Stat(savePath)
- //If the file exists already, we needn't do anything.
- if statErr == nil {
- return
- }
-
- downloadError := files.DownloadFile(savePath, fileURL)
- if downloadError != nil {
- window.app.QueueUpdateDraw(func() {
- window.ShowErrorDialog("Error download file: " + downloadError.Error())
- })
- }
- }(targetFile, url)
+ go downloadFunction(targetFile, url)
}
}
return nil

Duplicated download function has been moved to local variable