This repository has been archived by the owner on Sep 27, 2021. It is now read-only.
forked from thoas/picfit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
108 lines (90 loc) · 2.14 KB
/
handlers.go
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
package application
import (
"encoding/json"
"net/http"
"github.com/thoas/muxer"
)
func NotFoundHandler() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "404 Not found", http.StatusNotFound)
})
}
type Handler func(muxer.Response, *Request, *Application)
var ImageHandler Handler = func(res muxer.Response, req *Request, app *Application) {
debug, err := app.Jq.Bool("debug")
if err != nil {
debug = false
}
file, err := app.ImageFileFromRequest(req, true, true)
if err != nil {
if debug {
panic(err)
} else {
res.Abort(404, "404 Not found")
return
}
}
res.SetHeaders(file.Headers, true)
res.ResponseWriter.Write(file.Content())
}
// var UploadHandler = func(res muxer.Response, req *http.Request, app *Application) {
// if !app.EnableUpload {
// res.Forbidden()
// return
// }
//
// if app.SourceStorage == nil {
// res.Abort(500, "Your application doesn't have a source storage")
// return
// }
//
// var err error
//
// multipartForm := new(MultipartForm)
// errs := binding.Bind(req, multipartForm)
// if errs.Handle(res) {
// return
// }
//
// file, err := multipartForm.Upload(app.SourceStorage)
//
// if err != nil {
// panic(err)
// }
//
// content, err := json.Marshal(map[string]string{
// "filename": file.Filename(),
// "path": file.Path(),
// "url": file.URL(),
// })
//
// if err != nil {
// panic(err)
// }
//
// res.ContentType("application/json")
// res.ResponseWriter.Write(content)
// }
var GetHandler Handler = func(res muxer.Response, req *Request, app *Application) {
file, err := app.ImageFileFromRequest(req, false, false)
if err != nil {
panic(err)
}
content, err := json.Marshal(map[string]string{
"filename": file.Filename(),
"path": file.Path(),
"url": file.URL(),
})
if err != nil {
panic(err)
}
res.ContentType("application/json")
res.ResponseWriter.Write(content)
}
var RedirectHandler Handler = func(res muxer.Response, req *Request, app *Application) {
file, err := app.ImageFileFromRequest(req, false, false)
if err != nil {
panic(err)
}
res.PermanentRedirect(file.URL())
}