aboutsummaryrefslogtreecommitdiff
path: root/internal/image/handler.go
blob: f41ed4d9416eba2aad51bf99291ae4f6b4d5f3d0 (plain)
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
package image

import (
	"fmt"
	"html/template"
	"io/ioutil"
	"log"
	"net/http"

	"github.com/go-chi/chi"
)

var fileTypes = map[string]string{
	"image/jpeg": "jpg",
	"image/png":  "png",
}

// Handler image handler interface
type Handler interface {
	GetImage(w http.ResponseWriter, r *http.Request)
	PostImage(w http.ResponseWriter, r *http.Request)
}

// NewHandler create image handler struct
func NewHandler(service Servicer) Handler {
	return &imageHandler{service}
}

type imageHandler struct {
	service Servicer
}

// GetImage handler for returning an image
func (h *imageHandler) GetImage(w http.ResponseWriter, r *http.Request) {
	fileID := chi.URLParam(r, "fileName")
	fileMeta, err := h.service.GetFile(fileID)
	if err != nil {
		w.WriteHeader(400)
		log.Printf("error: %+v", err)
		w.Write([]byte("WTF Incorrect Content Type"))
	}
	fileUrl := fmt.Sprintf("/f/%s", fileMeta.FilePath)
	data := struct {
		ImageUrl   string
		ImageTitle string
		ImageDesc  string
	}{
		fileUrl,
		fileMeta.Title,
		fileMeta.Desc,
	}
	h.service.Render(w, "image.tmpl", data)
}

// PostImage handler for creating an image post
func (h *imageHandler) PostImage(w http.ResponseWriter, r *http.Request) {
	// max size
	r.ParseMultipartForm(10 << 20)
	file, handler, err := r.FormFile("file")
	if err != nil {
		log.Printf("%s", err)
		return
	}
	defer file.Close()
	fileBytes, err := ioutil.ReadAll(file)
	if err != nil {
		log.Printf("%s", err)
		log.Printf("unsupported filetype")
		w.WriteHeader(400)
		w.Write([]byte("Incorrect Content Type"))
		return
	}
	fileType := http.DetectContentType(fileBytes)
	if fileType != handler.Header.Get("Content-Type") {
		log.Printf("file type and content type do not match")
		w.WriteHeader(400)
		w.Write([]byte("Incorrect Content Type"))
		return
	}
	extension, exists := fileTypes[fileType]
	if !exists {
		log.Printf("unsupported filetype")
		w.WriteHeader(400)
		w.Write([]byte("Incorrect Content Type"))
		return
	}
	formData := r.PostForm
	rawPostTitle := formData.Get("title")
	rawPostDesc := formData.Get("desc")
	postTitle := template.HTMLEscapeString(rawPostTitle)
	postDesc := template.HTMLEscapeString(rawPostDesc)

	if len(rawPostDesc) != len(postDesc) {
		log.Printf("description not clean")
		w.WriteHeader(400)
		w.Write([]byte("Incorrect Content Type"))
		return
	}
	if len(rawPostTitle) != len(postTitle) {
		log.Printf("invalid title")
		w.WriteHeader(400)
		w.Write([]byte("Invalid image meta data"))
		return
	}
	postMeta := &PostMeta{
		MimeType: fileType,
		Title:    postTitle,
		Desc:     postDesc,
	}
	fileName, fileID, err := h.service.AddFile(extension, postMeta, fileBytes)
	if err != nil {
		log.Printf("failed to write file")
		w.WriteHeader(500)
		w.Write([]byte("An Internal Error Occured"))
		return
	}
	w.WriteHeader(201)
	w.Write([]byte(fmt.Sprintf("%s %s", fileName, fileID)))
	return
}