openFile now accesses cache.
MODIFY
ui/window.go
ui/window.go
@@ -388,37 +388,11 @@ func NewWindow(doRestart chan bool, app *tview.Application, session *discordgo.S
}
for _, link := range links {
- targetFile := filepath.Join(targetFolder, filepath.Base(link))
- downloadError := files.DownloadFile(targetFile, link)
- if downloadError != nil {
- window.ShowCustomErrorDialog("Couldn't open file", downloadError.Error())
- continue
- }
-
- extension := strings.TrimPrefix(filepath.Ext(targetFile), ".")
- handler, handlerSet := config.Current.FileOpenHandlers[extension]
- if handlerSet {
- handlerTrimmed := strings.TrimSpace(handler)
- //Empty means to not open files with the given extension.
- if handlerTrimmed == "" {
- log.Printf("skip opening link %s, as the extension %s has been disabled.\n", link, extension)
- continue
- }
-
- commandParts := commands.ParseCommand(strings.ReplaceAll(handlerTrimmed, "{$file}", targetFile))
- command := exec.Command(commandParts[0], commandParts[1:]...)
- startError := command.Start()
- if startError != nil {
- window.ShowCustomErrorDialog("Couldn't open file", startError.Error())
- }
- } else {
- log.Println("Attempting to open file: " + targetFile)
- openError := open.Run(targetFile)
+ openError := window.openFile(targetFolder, link)
if openError != nil {
window.ShowCustomErrorDialog("Couldn't open file", openError.Error())
}
}
- }
return nil
}
@@ -1271,6 +1245,40 @@ important changes of the last two versions officially released.
`, version.Version)
}
+func (window *Window) openFile(targetFolder, link string) error {
+ targetFile := filepath.Join(targetFolder, filepath.Base(link))
+ downloadError := files.DownloadFileOrAccessCache(targetFile, link)
+ if downloadError != nil {
+ return downloadError
+ }
+
+ extension := strings.TrimPrefix(filepath.Ext(targetFile), ".")
+ handler, handlerSet := config.Current.FileOpenHandlers[extension]
+ if handlerSet {
+ handlerTrimmed := strings.TrimSpace(handler)
+ //Empty means to not open files with the given extension.
+ if handlerTrimmed == "" {
+ log.Printf("skip opening link %s, as the extension %s has been disabled.\n", link, extension)
+ return nil
+ }
+
+ commandParts := commands.ParseCommand(strings.ReplaceAll(handlerTrimmed, "{$file}", targetFile))
+ command := exec.Command(commandParts[0], commandParts[1:]...)
+ startError := command.Start()
+ if startError != nil {
+ return startError
+ }
+ } else {
+ log.Println("Attempting to open file: " + targetFile)
+ openError := open.Run(targetFile)
+ if openError != nil {
+ return openError
+ }
+ }
+
+ return nil
+}
+
// initExtensionEngine injections necessary functions into the engine.
// those functions can be called by each script inside of an engine.
func (window *Window) initExtensionEngine(engine scripting.Engine) error {

MODIFY
util/files/files.go
util/files/files.go
@@ -47,7 +47,7 @@ func ToAbsolutePath(input string) (string, error) {
// DownloadFile will download a url to a local file. It's efficient because it will
// write as it downloads and not load the whole file into memory.
-func DownloadFile(filepath string, url string) error {
+func DownloadFile(filepath, url string) error {
response, httpError := http.Get(url)
if httpError != nil {
return httpError
@@ -63,3 +63,16 @@ func DownloadFile(filepath string, url string) error {
_, writeError := io.Copy(outputFile, response.Body)
return writeError
}
+
+// DownloadFileOrAccessCache checks whether the file already exists on the
+// users filesystem and only downloads it if it doesn't.
+func DownloadFileOrAccessCache(filepath, url string) error {
+ _, statError := os.Stat(filepath)
+
+ //File already exists
+ if statError == nil {
+ return nil
+ }
+
+ return DownloadFile(filepath, url)
+}

openFile now accesses cache.