mirror of
https://github.com/h2non/imaginary.git
synced 2026-03-05 18:23:37 +01:00
57 lines
1.3 KiB
Go
57 lines
1.3 KiB
Go
package main
|
|
|
|
import "net/http"
|
|
|
|
func indexController(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path != "/" {
|
|
ErrorReply(w, "Not found", NOT_FOUND)
|
|
return
|
|
}
|
|
w.Header().Set("Content-Type", "text/plain")
|
|
w.Write([]byte("imaginary server " + Version))
|
|
}
|
|
|
|
func formController(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "text/html")
|
|
w.Write([]byte(formText))
|
|
}
|
|
|
|
func imageController(w http.ResponseWriter, r *http.Request, Operation Operation) {
|
|
if r.Method != "POST" {
|
|
ErrorReply(w, "Method not allowed for this endpoint", NOT_ALLOWED)
|
|
return
|
|
}
|
|
|
|
buf, err := readBody(r)
|
|
if err != nil {
|
|
ErrorReply(w, "Cannot read the body: "+err.Error(), BAD_REQUEST)
|
|
return
|
|
}
|
|
if len(buf) == 0 {
|
|
ErrorReply(w, "Empty or invalid body", BAD_REQUEST)
|
|
return
|
|
}
|
|
|
|
mimeType := http.DetectContentType(buf)
|
|
if IsImageMimeTypeSupported(mimeType) == false {
|
|
ErrorReply(w, "Unsupported media type: "+mimeType, UNSUPPORTED)
|
|
return
|
|
}
|
|
|
|
opts := readParams(r)
|
|
|
|
if opts.Type != "" && ImageType(opts.Type) == 0 {
|
|
ErrorReply(w, "Unsupported conversion image format: "+opts.Type, BAD_REQUEST)
|
|
return
|
|
}
|
|
|
|
image, err := Operation.Run(buf, opts)
|
|
if err != nil {
|
|
ErrorReply(w, "Error while processing the image: "+err.Error(), BAD_REQUEST)
|
|
return
|
|
}
|
|
|
|
w.Header().Set("Content-Type", image.Mime)
|
|
w.Write(image.Body)
|
|
}
|