2 // Armadillo File Manager
3 // Copyright (c) 2010-2012, Robert Sesek <http://www.bluestatic.org>
5 // This program is free software: you can redistribute it and/or modify it under
6 // the terms of the GNU General Public License as published by the Free Software
7 // Foundation, either version 3 of the License, or any later version.
15 "github.com/rsesek/armadillo/config"
26 gConfig *config.Configuration
30 _, thisFile, _, ok := runtime.Caller(0)
32 panic("unable to get file information from runtime.Caller so the frontend files cannot be found")
34 // thisFile = /armadillo/server/server.go, so compute /armadillo/frontend/
35 kFrontEndFiles = path.Join(path.Dir(path.Dir(thisFile)), "frontend")
38 func indexHandler(response http.ResponseWriter, request *http.Request) {
39 fd, err := os.Open(path.Join(kFrontEndFiles, "index.html"))
41 fmt.Print("Error opening file ", err.Error(), "\n")
46 response.Header().Set("Content-Type", "text/html")
50 func serviceHandler(response http.ResponseWriter, request *http.Request) {
51 if request.Method != "POST" {
52 io.WriteString(response, "Error: Not a POST request")
56 switch request.FormValue("action") {
58 files, err := ListPath(request.FormValue("path"))
60 errorResponse(response, err.Error())
62 okResponse(response, files)
65 err := RemovePath(request.FormValue("path"))
67 errorResponse(response, err.Error())
69 data := map[string]int{
72 okResponse(response, data)
75 source := request.FormValue("source")
76 target := request.FormValue("target")
77 err := MovePath(source, target)
79 errorResponse(response, err.Error())
81 data := map[string]interface{}{
85 okResponse(response, data)
88 path := request.FormValue("path")
91 errorResponse(response, err.Error())
93 data := map[string]interface{}{
97 okResponse(response, data)
100 newPath, err := RenameTVEpisode(request.FormValue("path"))
102 errorResponse(response, err.Error())
104 data := map[string]interface{}{
108 okResponse(response, data)
111 fmt.Printf("Invalid action: '%s'\n", request.FormValue("action"))
112 errorResponse(response, "Unhandled action")
116 func downloadHandler(response http.ResponseWriter, request *http.Request) {
117 valid, fullPath := IsValidPath(request.FormValue("path"))
119 info, _ := os.Lstat(fullPath) // Error is already checked by |valid|.
121 http.Error(response, "Path is a directory", http.StatusBadRequest)
123 http.ServeFile(response, request, fullPath)
126 http.NotFound(response, request)
130 func errorResponse(response http.ResponseWriter, message string) {
131 message = strings.Replace(message, gConfig.JailRoot, "/", -1)
132 data := map[string]interface{}{
136 json_data, err := json.Marshal(data)
138 response.Header().Set("Content-Type", "text/json")
140 io.WriteString(response, "{\"error\":\"-9\",\"message\":\"Internal encoding error\"}")
142 response.Write(json_data)
146 func okResponse(response http.ResponseWriter, data interface{}) {
147 response.Header().Set("Content-Type", "text/json")
148 json_data, err := json.Marshal(data)
150 errorResponse(response, "Internal encoding error")
152 response.Write(json_data)
156 func RunBackEnd(c *config.Configuration) {
159 mux := http.NewServeMux()
160 mux.HandleFunc("/", indexHandler)
161 mux.Handle("/fe/", http.StripPrefix("/fe/", http.FileServer(http.Dir(kFrontEndFiles))))
162 mux.HandleFunc("/service", serviceHandler)
163 mux.HandleFunc("/download", downloadHandler)
165 error := http.ListenAndServe(fmt.Sprintf(":%d", gConfig.Port), mux)
166 fmt.Printf("error %v", error)