* Make the download action its own handler so that:
authorRobert Sesek <rsesek@bluestatic.org>
Sat, 28 May 2011 22:55:59 +0000 (18:55 -0400)
committerRobert Sesek <rsesek@bluestatic.org>
Sat, 28 May 2011 22:55:59 +0000 (18:55 -0400)
  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
web_frontend/actor.js

index 3ff4705830ce900eca3138dad3ee97437f11eef9..cde9c33d32d70c36d876c34ff2360d376424b6a0 100644 (file)
@@ -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
index 4a83c97efb8acf00a051059a770d20187a9c0731..1e346326b5324979dde06df873169c8ff9a595b7 100644 (file)
@@ -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_|.