-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupload.go
More file actions
137 lines (112 loc) · 2.88 KB
/
Copy pathupload.go
File metadata and controls
137 lines (112 loc) · 2.88 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
package main
import (
"fmt"
"time"
"crypto/md5"
"io"
"strconv"
"os"
"text/template"
"net/http"
"github.com/dchest/uniuri"
"github.com/disintegration/imaging"
"runtime"
"encoding/json"
"github.com/juju/errgo"
"strings"
)
func upload(w http.ResponseWriter, r *http.Request) {
//fmt.Println("method:", r.Method)
if r.Method == "GET" {
crutime := time.Now().Unix()
h := md5.New()
io.WriteString(h, strconv.FormatInt(crutime, 10))
token := fmt.Sprintf("%x", h.Sum(nil))
t, _ := template.ParseFiles("upload.gtpl")
t.Execute(w, token)
} else {
r.ParseMultipartForm(32 << 20)
file, handler, err := r.FormFile("uploadfile")
if err != nil {
fmt.Println(err)
return
}
defer file.Close()
contentType := handler.Header["Content-Type"][0]
if !isAllowedContentType(contentType) {
fmt.Println("Only JPEG or PNG files allowed")
}
filename := generateRandomFilename(contentType)
photoname := strings.Split(filename, ".")
f, err := os.Create(filename)
if err != nil {
fmt.Println(err)
return
}
defer f.Close()
io.Copy(f, file)
runtime.GOMAXPROCS(runtime.NumCPU())
// load original image
img, err := imaging.Open(filename)
if err != nil {
fmt.Println(err)
return
}
dstimg := imaging.Resize(img, 128, 0, imaging.Box)
// save resized image
err = imaging.Save(dstimg, photoname[0]+"_resized.jpg")
if err != nil {
fmt.Println(err)
return
}
photo := make(map[string]string)
photo["title"] = handler.Filename
photo["url"] = "http://localhost/upload/"+photoname[0]+".jpg"
photo["resized_url"] = "http://localhost/upload/"+photoname[0]+"_resized.jpg"
renderJSON(w, photo, http.StatusCreated)
}
}
func main() {
//http.HandleFunc("/", handler)
http.HandleFunc("/upload", upload)
http.ListenAndServe("localhost:8080", nil)
}
func generateRandomFilename(contentType string) string {
var ext string
switch contentType {
case "image/jpeg":
ext = ".jpg"
case "image/png":
ext = ".png"
case "image/gif":
ext = ".gif"
}
return uniuri.New() + ext
}
var allowedContentTypes = []string{
"image/png",
"image/jpeg",
"image/gif"}
func isAllowedContentType(contentType string) bool {
for _, value := range allowedContentTypes {
if contentType == value {
return true
}
}
return false
}
func renderJSON(w http.ResponseWriter, value interface{}, status int) error {
body, err := json.Marshal(value)
if err != nil {
return errgo.Mask(err)
}
return writeBody(w, body, status, "application/json")
}
func writeBody(w http.ResponseWriter, body []byte, status int, contentType string) error {
w.Header().Set("Content-Type", contentType+"; charset=UTF8")
w.Header().Set("Content-Length", strconv.Itoa(len(body)))
w.Header().Set("Access-Control-Allow-Origin", "*")
w.WriteHeader(status)
_, err := w.Write(body)
return errgo.Mask(err)
}