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(rw 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 rw.Header().Set("Content-Type", "text/html")
50 func listService(rw http.ResponseWriter, req *http.Request) {
51 if !requestIsPOST(rw, req) {
55 files, err := ListPath(req.FormValue("path"))
57 httpError(rw, err.Error(), http.StatusNotFound)
63 func removeService(rw http.ResponseWriter, req *http.Request) {
64 if !requestIsPOST(rw, req) {
68 err := RemovePath(req.FormValue("path"))
70 httpError(rw, err.Error(), http.StatusNotFound)
72 data := map[string]int{
79 func moveService(rw http.ResponseWriter, req *http.Request) {
80 if !requestIsPOST(rw, req) {
84 source := req.FormValue("source")
85 target := req.FormValue("target")
86 err := MovePath(source, target)
88 httpError(rw, err.Error(), http.StatusNotFound)
90 data := map[string]interface{}{
98 func mkdirService(rw http.ResponseWriter, req *http.Request) {
99 if !requestIsPOST(rw, req) {
103 path := req.FormValue("path")
106 httpError(rw, err.Error(), http.StatusUnauthorized)
108 data := map[string]interface{}{
116 func tvRenameService(rw http.ResponseWriter, req *http.Request) {
117 if !requestIsPOST(rw, req) {
121 newPath, err := RenameTVEpisode(req.FormValue("path"))
123 httpError(rw, err.Error(), http.StatusBadRequest)
125 data := map[string]interface{}{
133 func downloadHandler(response http.ResponseWriter, request *http.Request) {
134 valid, fullPath := IsValidPath(request.FormValue("path"))
136 info, _ := os.Lstat(fullPath) // Error is already checked by |valid|.
138 http.Error(response, "Path is a directory", http.StatusBadRequest)
140 http.ServeFile(response, request, fullPath)
143 http.NotFound(response, request)
147 func httpError(rw http.ResponseWriter, message string, code int) {
148 message = strings.Replace(message, gConfig.JailRoot, "/", -1)
150 rw.Header().Set("Content-Type", "text/plain")
151 fmt.Fprint(rw, message)
154 func okResponse(rw http.ResponseWriter, data interface{}) {
155 rw.Header().Set("Content-Type", "application/json")
156 jsonData, err := json.Marshal(data)
158 httpError(rw, "Internal error: " + err.Error(), 500)
164 func requestIsPOST(rw http.ResponseWriter, req *http.Request) bool {
165 if req.Method != "POST" {
166 httpError(rw, "Service requests must be sent via POST", http.StatusMethodNotAllowed)
172 func RunBackEnd(c *config.Configuration) {
175 mux := http.NewServeMux()
176 mux.HandleFunc("/", indexHandler)
177 mux.Handle("/fe/", http.StripPrefix("/fe/", http.FileServer(http.Dir(kFrontEndFiles))))
178 mux.HandleFunc("/service/list", listService)
179 mux.HandleFunc("/service/move", moveService)
180 mux.HandleFunc("/service/remove", removeService)
181 mux.HandleFunc("/service/mkdir", mkdirService)
182 mux.HandleFunc("/service/tv_rename", tvRenameService)
183 mux.HandleFunc("/download", downloadHandler)
185 error := http.ListenAndServe(fmt.Sprintf(":%d", gConfig.Port), mux)
186 fmt.Printf("error %v", error)