openFile now accesses cache.

This commit is contained in:
Marcel Schramm 2020-08-22 11:48:08 +02:00
parent 98fe7d4864
commit 7ca54242cf
No known key found for this signature in database
GPG Key ID: 05971054C70EEDC7
2 changed files with 51 additions and 30 deletions

View File

@ -388,35 +388,9 @@ 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)
if openError != nil {
window.ShowCustomErrorDialog("Couldn't open file", openError.Error())
}
openError := window.openFile(targetFolder, link)
if openError != nil {
window.ShowCustomErrorDialog("Couldn't open file", openError.Error())
}
}
@ -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 {

View File

@ -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)
}