Files can now be downloaded with the d key by default.
Fixes
MODIFY
config/config.go
config/config.go
@@ -151,9 +151,10 @@ type Config struct {
// FileOpenSaveFilesPermanently decides whether opened files are saved
// in the system cache (temporary) or in the user specified path.
FileOpenSaveFilesPermanently bool
- // FileOpenSaveFolder defines the folder where opened files are downloaded
- // to, if FileOpenSaveFilesPermanently is set to true.
- FileOpenSaveFolder string
+ // FileDownloadSaveLocation defines the folder where cordless generally
+ // download files to. If FileOpenSaveFilesPermanently has been set to
+ // true, then all opened files are saved in this folder for example.
+ FileDownloadSaveLocation string
}
// Account has a name and a token. The name is just for the users recognition.
@@ -209,7 +210,7 @@ func createDefaultConfig() *Config {
ShowNicknames: true,
FileOpenHandlers: make(map[string]string),
FileOpenSaveFilesPermanently: false,
- FileOpenSaveFolder: "~/Downloads",
+ FileDownloadSaveLocation: "~/Downloads",
}
}
@@ -324,11 +325,7 @@ func GetConfigDirectory() (string, error) {
func ensureDirectory(directoryPath string) error {
_, statError := os.Stat(directoryPath)
if os.IsNotExist(statError) {
- createDirsError := os.MkdirAll(directoryPath, 0766)
- if createDirsError != nil {
- return createDirsError
- }
- return nil
+ return os.MkdirAll(directoryPath, 0766)
}
return statError
}

MODIFY
shortcuts/shortcuts.go
shortcuts/shortcuts.go
@@ -22,6 +22,8 @@ var (
chatview, tcell.NewEventKey(tcell.KeyRune, 'q', tcell.ModNone))
EditSelectedMessage = addShortcut("edit_selected_message", "Edit selected message",
chatview, tcell.NewEventKey(tcell.KeyRune, 'e', tcell.ModNone))
+ DownloadMessageFiles = addShortcut("dowbload_message_files", "Download all files in selected message",
+ chatview, tcell.NewEventKey(tcell.KeyRune, 'd', tcell.ModNone))
ReplySelectedMessage = addShortcut("reply_selected_message", "Reply to author selected message",
chatview, tcell.NewEventKey(tcell.KeyRune, 'r', tcell.ModNone))
CopySelectedMessageLink = addShortcut("copy_selected_message_link", "Copy link to selected message",

MODIFY
ui/window.go
ui/window.go
@@ -367,7 +367,7 @@ func NewWindow(doRestart chan bool, app *tview.Application, session *discordgo.S
var targetFolder string
if config.Current.FileOpenSaveFilesPermanently {
- absolutePath, pathError := files.ToAbsolutePath(config.Current.FileOpenSaveFolder)
+ absolutePath, pathError := files.ToAbsolutePath(config.Current.FileDownloadSaveLocation)
if pathError == nil {
targetFolder = absolutePath
}
@@ -408,6 +408,38 @@ func NewWindow(doRestart chan bool, app *tview.Application, session *discordgo.S
return nil
}
+ if shortcuts.DownloadMessageFiles.Equals(event) {
+ absolutePath, pathError := files.ToAbsolutePath(config.Current.FileDownloadSaveLocation)
+ 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) {
+ _, statErr := os.Stat(savePath)
+ //If it's a different error, we don't care. Other errors
+ //will run into later anyways.
+ if statErr == os.ErrExist {
+ return
+ }
+
+ downloadError := files.DownloadFile(savePath, fileURL)
+ if downloadError != nil {
+ window.app.QueueUpdateDraw(func() {
+ window.ShowErrorDialog("Error download file: " + downloadError.Error())
+ })
+ }
+ }(targetFile, file.URL)
+ }
+ }
+ return nil
+ }
+
return event
})
window.messageContainer = window.chatView.GetPrimitive()

Files can now be downloaded with the d key by default.

Fixes #304