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 type pathReseponse struct {
51 Path string `json:"path"`
54 func listService(rw http.ResponseWriter, req *http.Request) {
55 if !requestIsPOST(rw, req) {
59 files, err := ListPath(req.FormValue("path"))
61 httpError(rw, err.Error(), http.StatusNotFound)
67 func removeService(rw http.ResponseWriter, req *http.Request) {
68 if !requestIsPOST(rw, req) {
72 err := RemovePath(req.FormValue("path"))
74 httpError(rw, err.Error(), http.StatusNotFound)
80 func moveService(rw http.ResponseWriter, req *http.Request) {
81 if !requestIsPOST(rw, req) {
85 source := req.FormValue("source")
86 target := req.FormValue("target")
87 err := MovePath(source, target)
89 httpError(rw, err.Error(), http.StatusNotFound)
91 okResponse(rw, pathReseponse{target})
95 func mkdirService(rw http.ResponseWriter, req *http.Request) {
96 if !requestIsPOST(rw, req) {
100 path := req.FormValue("path")
103 httpError(rw, err.Error(), http.StatusUnauthorized)
105 okResponse(rw, pathReseponse{path})
109 func tvRenameService(rw http.ResponseWriter, req *http.Request) {
110 if !requestIsPOST(rw, req) {
114 newPath, err := RenameTVEpisode(req.FormValue("path"))
116 httpError(rw, err.Error(), http.StatusBadRequest)
118 okResponse(rw, pathReseponse{newPath})
122 func downloadHandler(response http.ResponseWriter, request *http.Request) {
123 valid, fullPath := IsValidPath(request.FormValue("path"))
125 info, _ := os.Lstat(fullPath) // Error is already checked by |valid|.
127 http.Error(response, "Path is a directory", http.StatusBadRequest)
129 http.ServeFile(response, request, fullPath)
132 http.NotFound(response, request)
136 func httpError(rw http.ResponseWriter, message string, code int) {
137 message = strings.Replace(message, gConfig.JailRoot, "/", -1)
139 rw.Header().Set("Content-Type", "text/plain")
140 fmt.Fprint(rw, message)
143 func okResponse(rw http.ResponseWriter, data interface{}) {
144 rw.Header().Set("Content-Type", "application/json")
145 jsonData, err := json.Marshal(data)
147 httpError(rw, "Internal error: " + err.Error(), 500)
153 func requestIsPOST(rw http.ResponseWriter, req *http.Request) bool {
154 if req.Method != "POST" {
155 httpError(rw, "Service requests must be sent via POST", http.StatusMethodNotAllowed)
161 func RunBackEnd(c *config.Configuration) {
164 mux := http.NewServeMux()
165 mux.HandleFunc("/", indexHandler)
166 mux.Handle("/fe/", http.StripPrefix("/fe/", http.FileServer(http.Dir(kFrontEndFiles))))
167 mux.HandleFunc("/service/list", listService)
168 mux.HandleFunc("/service/move", moveService)
169 mux.HandleFunc("/service/remove", removeService)
170 mux.HandleFunc("/service/mkdir", mkdirService)
171 mux.HandleFunc("/service/tv_rename", tvRenameService)
172 mux.HandleFunc("/download", downloadHandler)
174 error := http.ListenAndServe(fmt.Sprintf(":%d", gConfig.Port), mux)
175 fmt.Printf("error %v", error)