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 files, err := ListPath(req.FormValue("path"))
53 httpError(rw, err.Error(), http.StatusNotFound)
59 func removeService(rw http.ResponseWriter, req *http.Request) {
60 err := RemovePath(req.FormValue("path"))
62 httpError(rw, err.Error(), http.StatusNotFound)
64 data := map[string]int{
71 func moveService(rw http.ResponseWriter, req *http.Request) {
72 source := req.FormValue("source")
73 target := req.FormValue("target")
74 err := MovePath(source, target)
76 httpError(rw, err.Error(), http.StatusNotFound)
78 data := map[string]interface{}{
86 func mkdirService(rw http.ResponseWriter, req *http.Request) {
87 path := req.FormValue("path")
90 httpError(rw, err.Error(), http.StatusUnauthorized)
92 data := map[string]interface{}{
100 func tvRenameService(rw http.ResponseWriter, req *http.Request) {
101 newPath, err := RenameTVEpisode(req.FormValue("path"))
103 httpError(rw, err.Error(), http.StatusBadRequest)
105 data := map[string]interface{}{
113 func downloadHandler(response http.ResponseWriter, request *http.Request) {
114 valid, fullPath := IsValidPath(request.FormValue("path"))
116 info, _ := os.Lstat(fullPath) // Error is already checked by |valid|.
118 http.Error(response, "Path is a directory", http.StatusBadRequest)
120 http.ServeFile(response, request, fullPath)
123 http.NotFound(response, request)
127 func httpError(rw http.ResponseWriter, message string, code int) {
128 message = strings.Replace(message, gConfig.JailRoot, "/", -1)
130 rw.Header().Set("Content-Type", "text/plain")
131 fmt.Fprint(rw, message)
134 func okResponse(rw http.ResponseWriter, data interface{}) {
135 rw.Header().Set("Content-Type", "application/json")
136 jsonData, err := json.Marshal(data)
138 httpError(rw, "Internal error: " + err.Error(), 500)
144 func RunBackEnd(c *config.Configuration) {
147 mux := http.NewServeMux()
148 mux.HandleFunc("/", indexHandler)
149 mux.Handle("/fe/", http.StripPrefix("/fe/", http.FileServer(http.Dir(kFrontEndFiles))))
150 mux.HandleFunc("/service/list", listService)
151 mux.HandleFunc("/service/move", moveService)
152 mux.HandleFunc("/service/remove", removeService)
153 mux.HandleFunc("/service/mkdir", mkdirService)
154 mux.HandleFunc("/service/tv_rename", tvRenameService)
155 mux.HandleFunc("/download", downloadHandler)
157 error := http.ListenAndServe(fmt.Sprintf(":%d", gConfig.Port), mux)
158 fmt.Printf("error %v", error)