/
upload.go
167 lines (148 loc) · 3.42 KB
/
upload.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
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
package server
import (
"crypto/sha1"
"database/sql"
"encoding/hex"
"io/ioutil"
"mime/multipart"
"github.com/cutechan/cutechan/go/common"
"github.com/cutechan/cutechan/go/config"
"github.com/cutechan/cutechan/go/db"
"github.com/cutechan/cutechan/go/ipc"
)
const (
// Maximum number of thumbnailer processes executing at the same time.
thumbProcesses = 1
)
var (
jobs = make(chan jobRequest)
// Map of MIME types to the constants used internally.
mimeTypes = map[string]uint8{
"image/jpeg": common.JPEG,
"image/png": common.PNG,
"image/gif": common.GIF,
"application/pdf": common.PDF,
"video/webm": common.WEBM,
"application/ogg": common.OGG,
"video/mp4": common.MP4,
"audio/mpeg": common.MP3,
}
)
type jobRequest struct {
fd multipart.File
jresults chan<- jobResult
}
type jobResult struct {
res uploadResult
err error
}
type uploadResult struct {
file *common.ImageCommon
token string
}
func uploadFile(fh *multipart.FileHeader) (res uploadResult, err error) {
if fh.Size > config.Get().MaxSize*1024*1024 {
err = aerrTooLarge
return
}
fd, err := fh.Open()
if err != nil {
err = aerrUploadRead.Hide(err)
return
}
defer fd.Close()
jresults := make(chan jobResult)
jreq := jobRequest{fd, jresults}
jobs <- jreq
jres := <-jresults
return jres.res, jres.err
}
func worker(user string) {
for {
jreq := <-jobs
res, err := work(user, jreq)
jreq.jresults <- jobResult{res, err}
}
}
func work(user string, jreq jobRequest) (res uploadResult, err error) {
data, err := ioutil.ReadAll(jreq.fd)
if err != nil {
err = aerrUploadRead.Hide(err)
return
}
hash := getSha1(data)
file, err := db.GetImage(hash)
switch err {
case nil:
// Already have thumbnail.
return newFileToken(&file)
case sql.ErrNoRows:
file.SHA1 = hash
return saveFile(user, data, &file)
default:
err = aerrInternal.Hide(err)
return
}
}
func getSha1(data []byte) string {
hash := sha1.Sum(data)
return hex.EncodeToString(hash[:])
}
func newFileToken(file *common.ImageCommon) (res uploadResult, err error) {
res.file = file
res.token, err = db.NewImageToken(file.SHA1)
if err != nil {
err = aerrInternal.Hide(err)
return
}
return
}
// Create a new thumbnail, commit its resources to the DB and
// filesystem, and return resulting token.
func saveFile(user string, srcData []byte, file *common.ImageCommon) (res uploadResult, err error) {
thumb, err := ipc.GetThumbnail(user, srcData)
switch err {
case nil:
// Do nothing.
case ipc.ErrThumbUnsupported:
err = aerrUnsupported
return
case ipc.ErrThumbDimensions:
err = aerrBadDimensions
return
case ipc.ErrThumbTracks:
err = aerrNoTracks
return
case ipc.ErrThumbProcess:
err = aerrCorrupted
return
default:
err = aerrInternal.Hide(err)
return
}
// Map fields.
file.Size = len(srcData)
file.Video = thumb.HasVideo
file.Audio = thumb.HasAudio
file.FileType = mimeTypes[thumb.Mime]
if thumb.HasAlpha {
file.ThumbType = common.PNG
} else {
file.ThumbType = common.JPEG
}
file.Length = thumb.Duration
file.Title = thumb.Title
file.Dims = [4]uint16{thumb.SrcWidth, thumb.SrcHeight, thumb.Width, thumb.Height}
if err = db.AllocateImage(srcData, thumb.Data, *file); err != nil {
err = aerrInternal.Hide(err)
return
}
return newFileToken(file)
}
// Start thumbnailer workers.
func startThumbWorkers(user string) (err error) {
for i := 0; i < thumbProcesses; i++ {
go worker(user)
}
return
}