Merge branch 'api-proto'
[armadillo.git] / server / server.go
1 //
2 // Armadillo File Manager
3 // Copyright (c) 2010-2012, Robert Sesek <http://www.bluestatic.org>
4 //
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.
8 //
9
10 package server
11
12 import (
13 "encoding/json"
14 "fmt"
15 "github.com/rsesek/armadillo/config"
16 "io"
17 "net/http"
18 "os"
19 "path"
20 "runtime"
21 "strings"
22 )
23
24 var (
25 kFrontEndFiles string
26 gConfig *config.Configuration
27 )
28
29 func init() {
30 _, thisFile, _, ok := runtime.Caller(0)
31 if !ok {
32 panic("unable to get file information from runtime.Caller so the frontend files cannot be found")
33 }
34 // thisFile = /armadillo/server/server.go, so compute /armadillo/frontend/
35 kFrontEndFiles = path.Join(path.Dir(path.Dir(thisFile)), "frontend")
36 }
37
38 func indexHandler(rw http.ResponseWriter, request *http.Request) {
39 fd, err := os.Open(path.Join(kFrontEndFiles, "index.html"))
40 if err != nil {
41 fmt.Print("Error opening file ", err.Error(), "\n")
42 return
43 }
44 defer fd.Close()
45
46 rw.Header().Set("Content-Type", "text/html")
47 io.Copy(rw, fd)
48 }
49
50 func listService(rw http.ResponseWriter, req *http.Request) {
51 if !requestIsPOST(rw, req) {
52 return
53 }
54
55 files, err := ListPath(req.FormValue("path"))
56 if err != nil {
57 httpError(rw, err.Error(), http.StatusNotFound)
58 } else {
59 okResponse(rw, files)
60 }
61 }
62
63 func removeService(rw http.ResponseWriter, req *http.Request) {
64 if !requestIsPOST(rw, req) {
65 return
66 }
67
68 err := RemovePath(req.FormValue("path"))
69 if err != nil {
70 httpError(rw, err.Error(), http.StatusNotFound)
71 } else {
72 data := map[string]int{
73 "error": 0,
74 }
75 okResponse(rw, data)
76 }
77 }
78
79 func moveService(rw http.ResponseWriter, req *http.Request) {
80 if !requestIsPOST(rw, req) {
81 return
82 }
83
84 source := req.FormValue("source")
85 target := req.FormValue("target")
86 err := MovePath(source, target)
87 if err != nil {
88 httpError(rw, err.Error(), http.StatusNotFound)
89 } else {
90 data := map[string]interface{}{
91 "path": target,
92 "error": 0,
93 }
94 okResponse(rw, data)
95 }
96 }
97
98 func mkdirService(rw http.ResponseWriter, req *http.Request) {
99 if !requestIsPOST(rw, req) {
100 return
101 }
102
103 path := req.FormValue("path")
104 err := MakeDir(path)
105 if err != nil {
106 httpError(rw, err.Error(), http.StatusUnauthorized)
107 } else {
108 data := map[string]interface{}{
109 "path": path,
110 "error": 0,
111 }
112 okResponse(rw, data)
113 }
114 }
115
116 func tvRenameService(rw http.ResponseWriter, req *http.Request) {
117 if !requestIsPOST(rw, req) {
118 return
119 }
120
121 newPath, err := RenameTVEpisode(req.FormValue("path"))
122 if err != nil {
123 httpError(rw, err.Error(), http.StatusBadRequest)
124 } else {
125 data := map[string]interface{}{
126 "path": newPath,
127 "error": 0,
128 }
129 okResponse(rw, data)
130 }
131 }
132
133 func downloadHandler(response http.ResponseWriter, request *http.Request) {
134 valid, fullPath := IsValidPath(request.FormValue("path"))
135 if valid {
136 info, _ := os.Lstat(fullPath) // Error is already checked by |valid|.
137 if info.IsDir() {
138 http.Error(response, "Path is a directory", http.StatusBadRequest)
139 } else {
140 http.ServeFile(response, request, fullPath)
141 }
142 } else {
143 http.NotFound(response, request)
144 }
145 }
146
147 func httpError(rw http.ResponseWriter, message string, code int) {
148 message = strings.Replace(message, gConfig.JailRoot, "/", -1)
149 rw.WriteHeader(code)
150 rw.Header().Set("Content-Type", "text/plain")
151 fmt.Fprint(rw, message)
152 }
153
154 func okResponse(rw http.ResponseWriter, data interface{}) {
155 rw.Header().Set("Content-Type", "application/json")
156 jsonData, err := json.Marshal(data)
157 if err != nil {
158 httpError(rw, "Internal error: " + err.Error(), 500)
159 } else {
160 rw.Write(jsonData)
161 }
162 }
163
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)
167 return false
168 }
169 return true
170 }
171
172 func RunBackEnd(c *config.Configuration) {
173 gConfig = c
174
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)
184
185 error := http.ListenAndServe(fmt.Sprintf(":%d", gConfig.Port), mux)
186 fmt.Printf("error %v", error)
187 }