From bbfa0e138ca9c999bdcf16f2020118ace26e8634 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Sat, 28 May 2011 18:55:59 -0400 Subject: [PATCH] * Make the download action its own handler so that: 1) No POST request is needed (as it isn't routed through /service) 2) The FE can simply redirect to the URL rather than creating a request * Add the Download button to the frontend --- src/server.go | 27 +++++++++++++++------------ web_frontend/actor.js | 16 ++++++++++++++-- 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/src/server.go b/src/server.go index 3ff4705..cde9c33 100644 --- a/src/server.go +++ b/src/server.go @@ -84,18 +84,6 @@ func serviceHandler(response http.ResponseWriter, request *http.Request) { } okResponse(response, data) } - case "download": - valid, fullPath := paths.IsValid(request.FormValue("path")) - if valid { - info, _ := os.Lstat(fullPath) // Error is already checked by |valid|. - if info.IsDirectory() { - errorResponse(response, "File is a directory") - return - } - http.ServeFile(response, request, fullPath) - } else { - errorResponse(response, "Invalid path") - } default: fmt.Printf("Invalid action: '%s'\n", request.FormValue("action")) errorResponse(response, "Unhandled action") @@ -153,6 +141,20 @@ func performProxy(url *http.URL, response http.ResponseWriter, origRequest *http return err } +func downloadHandler(response http.ResponseWriter, request *http.Request) { + valid, fullPath := paths.IsValid(request.FormValue("path")) + if valid { + info, _ := os.Lstat(fullPath) // Error is already checked by |valid|. + if info.IsDirectory() { + http.Error(response, "Path is a directory", http.StatusBadRequest) + } else { + http.ServeFile(response, request, fullPath) + } + } else { + http.NotFound(response, request) + } +} + func errorResponse(response http.ResponseWriter, message string) { message = strings.Replace(message, gConfig.JailRoot, "/", -1) data := map[string]interface{}{ @@ -184,6 +186,7 @@ func RunBackEnd(config *config.Configuration) { mux.HandleFunc("/", indexHandler) mux.Handle("/fe/", http.FileServer(kFrontEndFiles, "/fe/")) mux.HandleFunc("/service", serviceHandler) + mux.HandleFunc("/download", downloadHandler) mux.HandleFunc("/proxy", proxyHandler) gConfig = config diff --git a/web_frontend/actor.js b/web_frontend/actor.js index 4a83c97..1e34632 100644 --- a/web_frontend/actor.js +++ b/web_frontend/actor.js @@ -66,7 +66,8 @@ armadillo.Actor.options_ = { OPEN : 'open', MOVE : 'move', DELETE : 'delete', - TV_RENAME : 'tv-rename' + TV_RENAME : 'tv-rename', + DOWNLOAD : 'download' }; /** @@ -76,7 +77,8 @@ armadillo.Actor.optionStrings_ = { 'open' : 'Open', 'move' : 'Move', 'delete' : 'Delete', - 'tv-rename' : 'Rename TV Episode' + 'tv-rename' : 'Rename TV Episode', + 'download' : 'Download' }; /** @@ -170,6 +172,8 @@ armadillo.Actor.prototype.tileClickHandler_ = function(e) { this.performDelete_(); } else if (option == armadillo.Actor.options_.TV_RENAME) { this.performTVRename_(); + } else if (option == armadillo.Actor.options_.DOWNLOAD) { + this.performDownload_(); } }; @@ -214,6 +218,14 @@ armadillo.Actor.prototype.performTVRename_ = function() { renamer.run(); }; +/** + * Subroutine that streams a file. + * @private + */ +armadillo.Actor.prototype.performDownload_ = function() { + window.location = '/download?path=' + this.file_.getFullPath(); +}; + /** * Creates two buttons: one for OK one for Cancel and attahes them to the * |controlContainer_|. -- 2.22.5