-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwebapi.go
More file actions
240 lines (224 loc) · 7.28 KB
/
webapi.go
File metadata and controls
240 lines (224 loc) · 7.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package main
import (
"context"
"encoding/json"
"fmt"
"net/http"
"path"
"path/filepath"
"strings"
"github.com/GeertJohan/go.rice"
"github.com/midstar/llog"
)
// WebAPI represents the REST API server.
type WebAPI struct {
server *http.Server
templatePath string // Path to the templates
media *Media
box *rice.Box
userName string // User name ("" means no authentication)
password string // Password
tlsCertFile string // TLS certification file ("" means no TLS)
tlsKeyFile string // TLS key file ("" means no TLS)
}
// CreateWebAPI creates a new Web API instance
func CreateWebAPI(port int, ip, templatePath string, media *Media, box *rice.Box, userName, password,
tlsCertFile, tlsKeyFile string) *WebAPI {
portStr := fmt.Sprintf("%s:%d", ip, port)
server := &http.Server{Addr: portStr}
webAPI := &WebAPI{
server: server,
templatePath: templatePath,
media: media,
box: box,
userName: userName,
password: password,
tlsCertFile: tlsCertFile,
tlsKeyFile: tlsKeyFile}
http.Handle("/", webAPI)
return webAPI
}
// Start starts the HTTP server. Stop it using the Stop function. Non-blocking.
// Returns a channel that is written to when the HTTP server has stopped.
func (wa *WebAPI) Start() chan bool {
done := make(chan bool)
go func() {
llog.Info("Starting Web API on port %s\n", wa.server.Addr)
if wa.tlsCertFile != "" && wa.tlsKeyFile != "" {
llog.Info("Using TLS (HTTPS)")
if err := wa.server.ListenAndServeTLS(wa.tlsCertFile, wa.tlsKeyFile); err != nil {
// cannot panic, because this probably is an intentional close
llog.Info("WebAPI: ListenAndServeTLS() shutdown reason: %s", err)
}
} else {
if err := wa.server.ListenAndServe(); err != nil {
// cannot panic, because this probably is an intentional close
llog.Info("WebAPI: ListenAndServeTLS() shutdown reason: %s", err)
}
}
// TODO fix this wa.media.stopWatcher() // Stop the folder watcher (if it is running)
done <- true // Signal that http server has stopped
}()
return done
}
// Stop stops the HTTP server.
func (wa *WebAPI) Stop() {
wa.server.Shutdown(context.Background())
}
// ServeHTTP handles incoming HTTP requests
func (wa *WebAPI) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// Handle authentication
if wa.userName != "" {
// Authentication required
user, pass, _ := r.BasicAuth()
if wa.userName != user || wa.password != pass {
llog.Info("Invalid user login attempt. user: %s, password: %s", user, pass)
w.Header().Set("WWW-Authenticate", "Basic realm=\"MediaWEB requires username and password\"")
http.Error(w, "Unauthorized. Invalid username or password.", http.StatusUnauthorized)
return
}
}
// Handle request
var head string
originalURL := r.URL.Path
llog.Trace("Got request: %s", r.URL.Path)
head, r.URL.Path = shiftPath(r.URL.Path)
if head == "shutdown" && r.Method == "POST" {
wa.Stop()
} else if head == "folder" && r.Method == "GET" {
wa.serveHTTPFolder(w, r)
} else if head == "media" && r.Method == "GET" {
wa.serveHTTPMedia(w, r)
} else if head == "thumb" && r.Method == "GET" {
wa.serveHTTPThumbnail(w, r)
} else if head == "isPreCacheInProgress" && r.Method == "GET" {
toJSON(w, wa.media.isPreCacheInProgress())
} else if r.Method == "GET" {
r.URL.Path = originalURL
wa.serveHTTPStatic(w, r)
} else {
w.WriteHeader(http.StatusNotFound)
fmt.Fprintf(w, "This is not a valid path: %s or method %s!", r.URL.Path, r.Method)
}
}
func (wa *WebAPI) serveHTTPStatic(w http.ResponseWriter, r *http.Request) {
fileName := r.URL.Path
if len(r.URL.Path) > 0 {
fileName = r.URL.Path[1:] // Remove '/'
}
if fileName == "" {
// Default is index page
fileName = "index.html"
}
bytes, err := wa.box.Bytes(fileName)
if err != nil || len(bytes) == 0 {
w.WriteHeader(http.StatusNotFound)
fmt.Fprintf(w, "Unable to find: %s!", fileName)
} else {
if filepath.Ext(fileName) == ".html" {
w.Header().Set("Content-Type", "text/html")
} else if filepath.Ext(fileName) == ".ico" {
w.Header().Set("Content-Type", "image/x-icon")
} else {
w.Header().Set("Content-Type", "image/png")
}
w.Write(bytes)
}
}
// serveHTTPFolder generates JSON will files in folder
func (wa *WebAPI) serveHTTPFolder(w http.ResponseWriter, r *http.Request) {
folder := ""
if len(r.URL.Path) > 0 {
folder = r.URL.Path[1:] // Remove '/'
}
files, err := wa.media.getFiles(folder)
if err != nil {
http.Error(w, "Get files: "+err.Error(), http.StatusNotFound)
return
}
toJSON(w, files)
}
// serveHTTPMedia opens the media
func (wa *WebAPI) serveHTTPMedia(w http.ResponseWriter, r *http.Request) {
relativePath := r.URL.Path
// Only accept media files of security reasons
if wa.media.getFileType(relativePath) == "" {
http.Error(w, "Not a valid media file: "+relativePath, http.StatusNotFound)
return
}
originalImage, hasOriginalImageQuery := r.URL.Query()["original-image"]
// Write preview file if possible and allowed
if !hasOriginalImageQuery || originalImage[0] != "true" {
err := wa.media.writePreview(w, relativePath)
if err == nil {
// Previews are always in JPEG format
w.Header().Set("Content-Type", "image/jpeg")
return
}
}
if wa.media.isRotationNeeded(relativePath) {
// This is a JPEG file which requires rotation.
w.Header().Set("Content-Type", "image/jpeg")
err := wa.media.rotateAndWrite(w, relativePath)
if err != nil {
http.Error(w, "Rotate file: "+err.Error(), http.StatusInternalServerError)
return
}
} else {
// This is any other media file
fullPath, err := wa.media.getFullMediaPath(relativePath)
if err != nil {
http.Error(w, "Get files: "+err.Error(), http.StatusNotFound)
return
}
http.ServeFile(w, r, fullPath)
}
}
// serveHTTPThumbnail opens the media thumbnail or the default thumbnail
// if no thumbnail exist.
func (wa *WebAPI) serveHTTPThumbnail(w http.ResponseWriter, r *http.Request) {
relativePath := r.URL.Path
err := wa.media.writeThumbnail(w, relativePath)
if err == nil {
w.Header().Set("Content-Type", "image/jpeg")
} else {
// No thumbnail. Use the default
w.Header().Set("Content-Type", "image/png")
fileType := wa.media.getFileType(relativePath)
if fileType == "image" {
iconImage, _ := wa.box.Bytes("icon_image.png")
w.Write(iconImage)
//http.ServeFile(w, r, wa.templatePath+"/icon_image.png")
} else if fileType == "video" {
iconVideo, _ := wa.box.Bytes("icon_video.png")
w.Write(iconVideo)
//http.ServeFile(w, r, wa.templatePath+"/icon_video.png")
} else {
// Folder
iconFolder, _ := wa.box.Bytes("icon_folder.png")
w.Write(iconFolder)
//http.ServeFile(w, r, wa.templatePath+"/icon_folder.png")
}
}
}
// toJSON converts the v object to JSON and writes result to the response
func toJSON(w http.ResponseWriter, v interface{}) {
js, err := json.Marshal(v)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(js)
}
// shiftPath splits off the first component of p, which will be cleaned of
// relative components before processing. head will never contain a slash and
// tail will always be a rooted path without trailing slash.
func shiftPath(p string) (head, tail string) {
p = path.Clean("/" + p)
i := strings.Index(p[1:], "/") + 1
if i <= 0 {
return p[1:], "/"
}
return p[1:i], p[i:]
}