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.
13 "github.com/rsesek/armadillo/config"
23 var dir, file = path.Split(path.Clean(os.Getenv("_")))
24 var kFrontEndFiles string = path.Join(dir, "frontend")
25 var gConfig *config.Configuration
27 func indexHandler(response http.ResponseWriter, request *http.Request) {
28 fd, err := os.Open(path.Join(kFrontEndFiles, "index.html"))
30 fmt.Print("Error opening file ", err.Error(), "\n")
35 response.Header().Set("Content-Type", "text/html")
39 func serviceHandler(response http.ResponseWriter, request *http.Request) {
40 if request.Method != "POST" {
41 io.WriteString(response, "Error: Not a POST request")
45 switch request.FormValue("action") {
47 files, err := ListPath(request.FormValue("path"))
49 errorResponse(response, err.Error())
51 okResponse(response, files)
54 err := RemovePath(request.FormValue("path"))
56 errorResponse(response, err.Error())
58 data := map[string]int{
61 okResponse(response, data)
64 source := request.FormValue("source")
65 target := request.FormValue("target")
66 err := MovePath(source, target)
68 errorResponse(response, err.Error())
70 data := map[string]interface{}{
74 okResponse(response, data)
77 path := request.FormValue("path")
80 errorResponse(response, err.Error())
82 data := map[string]interface{}{
86 okResponse(response, data)
89 newPath, err := RenameTVEpisode(request.FormValue("path"))
91 errorResponse(response, err.Error())
93 data := map[string]interface{}{
97 okResponse(response, data)
100 fmt.Printf("Invalid action: '%s'\n", request.FormValue("action"))
101 errorResponse(response, "Unhandled action")
105 func downloadHandler(response http.ResponseWriter, request *http.Request) {
106 valid, fullPath := IsValidPath(request.FormValue("path"))
108 info, _ := os.Lstat(fullPath) // Error is already checked by |valid|.
110 http.Error(response, "Path is a directory", http.StatusBadRequest)
112 http.ServeFile(response, request, fullPath)
115 http.NotFound(response, request)
119 func errorResponse(response http.ResponseWriter, message string) {
120 message = strings.Replace(message, gConfig.JailRoot, "/", -1)
121 data := map[string]interface{}{
125 json_data, err := json.Marshal(data)
127 response.Header().Set("Content-Type", "text/json")
129 io.WriteString(response, "{\"error\":\"-9\",\"message\":\"Internal encoding error\"}")
131 response.Write(json_data)
135 func okResponse(response http.ResponseWriter, data interface{}) {
136 response.Header().Set("Content-Type", "text/json")
137 json_data, err := json.Marshal(data)
139 errorResponse(response, "Internal encoding error")
141 response.Write(json_data)
145 func RunBackEnd(c *config.Configuration) {
148 mux := http.NewServeMux()
149 mux.HandleFunc("/", indexHandler)
150 mux.Handle("/fe/", http.StripPrefix("/fe/", http.FileServer(http.Dir(kFrontEndFiles))))
151 mux.HandleFunc("/service", serviceHandler)
152 mux.HandleFunc("/download", downloadHandler)
154 error := http.ListenAndServe(fmt.Sprintf(":%d", gConfig.Port), mux)
155 fmt.Printf("error %v", error)