Skip to content
This repository has been archived by the owner on Dec 16, 2021. It is now read-only.

Commit

Permalink
Duplicated download function has been moved to local variable
Browse files Browse the repository at this point in the history
  • Loading branch information
Bios-Marcel committed Oct 12, 2020
1 parent 5247123 commit fa7f634
Showing 1 changed file with 17 additions and 28 deletions.
45 changes: 17 additions & 28 deletions ui/window.go
Original file line number Diff line number Diff line change
Expand Up @@ -439,27 +439,29 @@ 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 {
downloadFunction := 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())
})
}
}

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) {
_, 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, file.URL)
go downloadFunction(targetFile, file.URL)
}

urlMatches := urlRegex.FindAllString(message.Content, 1000)
Expand All @@ -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
Expand Down

0 comments on commit fa7f634

Please sign in to comment.