-
Notifications
You must be signed in to change notification settings - Fork 1
/
preview.go
221 lines (187 loc) · 5.07 KB
/
preview.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package main
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"log"
"mime/multipart"
"net/http"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"github.com/disintegration/imaging"
"github.com/xbsoftware/wfs"
)
var nonLatin = regexp.MustCompile(("[[:^ascii:]]"))
func getIconURL(size, ftype, name, skin string) string {
var re = regexp.MustCompile(`[^A-Za-z0-9.]`)
size = re.ReplaceAllString(size, "")
skin = re.ReplaceAllString(skin, "")
ftype = re.ReplaceAllString(ftype, "")
name = re.ReplaceAllString(name, "")
test := "icons/default/" + size + "/" + name
_, err := os.Stat(test)
if !os.IsNotExist(err) {
return test
}
if skin != "" {
test = "icons/" + skin + "/" + size + "/types/" + ftype + filepath.Ext(name)
_, err = os.Stat(test)
if !os.IsNotExist(err) {
return test
}
}
return "icons/default/" + size + "/types/" + ftype + filepath.Ext(name)
}
func serveIconPreview(w http.ResponseWriter, r *http.Request, info wfs.File) {
http.ServeFile(w, r, getIconURL("big", info.Type, filepath.Ext(info.Name)[1:]+".svg", "none"))
}
func getFilePreview(w http.ResponseWriter, r *http.Request) {
if Config.Preview == "none" {
format.Text(w, 500, "Previews not configured")
return
}
id := r.URL.Query().Get("id")
info, err := drive.Info(id)
if err != nil {
format.Text(w, 500, "Access denied")
return
}
widthStr := r.URL.Query().Get("width")
heightStr := r.URL.Query().Get("height")
width, err := strconv.Atoi(widthStr)
if err != nil {
format.Text(w, 500, "incorrect width value")
return
}
height, err := strconv.Atoi(heightStr)
if err != nil {
format.Text(w, 500, "incorrect height value")
return
}
if info.Size > 50*1000*1000 || width > 2000 || height > 2000 {
// file is too large, still it is a valid use-case so return some image
serveIconPreview(w, r, info)
return
}
target := getImagePreviewName(Config.DataFolder, id, widthStr, heightStr)
// check previously generated preview
ext := ".jpg"
ps, err := os.Stat(target + ext)
if err != nil {
ext = ".png"
ps, err = os.Stat(target + ext)
}
if err == nil {
if ps.Size() == 0 {
// there is a preview placeholder, which means preview can't be generated for this file
serveIconPreview(w, r, info)
return
}
http.ServeFile(w, r, target+ext)
return
}
source, _ := drive.Read(id)
if x, ok := source.(io.Closer); ok {
defer x.Close()
}
if Config.Preview != "" {
ext, err = getExternalPreview(source, target, info.Name, width, height)
} else {
if info.Type == "image" {
ext, err = getImagePreview(source, target, info.Name, width, height)
}
}
if err != nil {
log.Print(err.Error())
ioutil.WriteFile(target+".jpg", []byte{}, 0664)
serveIconPreview(w, r, info)
return
}
http.ServeFile(w, r, target+ext)
}
func getImagePreviewName(base, id, width, height string) string {
//folder := filepath.Join(base, filepath.Dir(id), ".preview")
err := os.MkdirAll("/tmp/preview", 0777)
if err != nil {
log.Println("Can't create folder for previews")
}
return filepath.Join("/tmp/preview", width+"x"+height+"___"+strings.Replace(id, "/", "___", -1))
// return filepath.Join(folder, filepath.Base(id)+"___"+width+"x"+height)
}
func getImagePreview(source io.Reader, target, name string, width, height int) (string, error) {
src, err := imaging.Decode(source)
if err != nil {
return "", err
}
ext := strings.ToLower(filepath.Ext(name))
targetExt := ".jpg"
if ext == ".png" || ext == ".gif" {
targetExt = ".png"
}
dst := imaging.Thumbnail(src, width, height, imaging.Lanczos)
err = imaging.Save(dst, target+targetExt)
return targetExt, err
}
func getExternalPreview(source io.ReadSeeker, target, name string, width, height int) (string, error) {
body, writer := io.Pipe()
defer body.Close()
form := multipart.NewWriter(writer)
safeName := nonLatin.ReplaceAllLiteralString(name, "x")
go func() {
defer writer.Close()
defer form.Close()
fw, err := form.CreateFormField("width")
if err != nil {
log.Println(err.Error())
return
}
io.Copy(fw, bytes.NewBufferString(strconv.Itoa(width)))
fw, err = form.CreateFormField("height")
if err != nil {
log.Println(err.Error())
return
}
io.Copy(fw, bytes.NewBufferString(strconv.Itoa(height)))
fw, err = form.CreateFormField("name")
if err != nil {
log.Println(err.Error())
return
}
io.Copy(fw, bytes.NewBufferString(safeName))
fw, err = form.CreateFormFile("file", safeName)
if err != nil {
log.Println(err.Error())
return
}
io.Copy(fw, source)
}()
req, err := http.NewRequest(http.MethodPost, Config.Preview, body)
if err != nil {
return "", err
}
req.Header.Add("Content-Type", form.FormDataContentType())
client := &http.Client{}
res, err := client.Do(req)
if err != nil {
return "", fmt.Errorf("preview service %w", err)
}
if res.StatusCode != 200 {
return "", fmt.Errorf("preview service %d", res.StatusCode)
}
ext := ".jpg"
if res.Header.Get("Content-type") == "image/png" {
ext = ".png"
}
defer res.Body.Close()
fw, err := os.Create(target + ext)
if err != nil {
return "", err
}
defer fw.Close()
_, err = io.Copy(fw, res.Body)
return ext, err
}