summaryrefslogtreecommitdiff
path: root/src/http/assets.go
blob: 1f5f0d657ee637d5c84de6dbd70648b0f06b483d (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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package http

import (
	"bytes"
	"compress/gzip"
	"errors"
	"fmt"
	"image"
	"image/jpeg"
	"image/png"
	"io"
	"io/fs"
	"net/http"
	"path/filepath"
	"strings"
	"time"

	"github.com/mediocregopher/blog.mediocregopher.com/srv/http/apiutil"
	"github.com/mediocregopher/blog.mediocregopher.com/srv/post/asset"
	"github.com/omeid/go-tarfs"
	"golang.org/x/image/draw"
)

func isImgResizable(path string) bool {
	switch strings.ToLower(filepath.Ext(path)) {
	case ".jpg", ".jpeg", ".png":
		return true
	default:
		return false
	}
}

func resizeImage(out io.Writer, in io.Reader, maxWidth float64) error {

	img, format, err := image.Decode(in)
	if err != nil {
		return fmt.Errorf("decoding image: %w", err)
	}

	imgRect := img.Bounds()
	imgW, imgH := float64(imgRect.Dx()), float64(imgRect.Dy())

	if imgW > maxWidth {

		newH := imgH * maxWidth / imgW
		newImg := image.NewRGBA(image.Rect(0, 0, int(maxWidth), int(newH)))

		// Resize
		draw.BiLinear.Scale(
			newImg, newImg.Bounds(), img, img.Bounds(), draw.Over, nil,
		)

		img = newImg
	}

	switch format {
	case "jpeg":
		return jpeg.Encode(out, img, nil)
	case "png":
		return png.Encode(out, img)
	default:
		return fmt.Errorf("unknown image format %q", format)
	}
}

func (a *api) managePostAssetsHandler() http.Handler {

	tpl := a.mustParseBasedTpl("post-assets-manage.html")

	return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {

		ids, err := a.params.PostAssetStore.List()

		if err != nil {
			apiutil.InternalServerError(
				rw, r, fmt.Errorf("getting list of asset ids: %w", err),
			)
			return
		}

		tplPayload := struct {
			IDs []string
		}{
			IDs: ids,
		}

		executeTemplate(rw, r, tpl, tplPayload)
	})
}

type postAssetArchiveInfo struct {
	path      string
	id        string
	subPath   string
	isGzipped bool
}

func extractPostAssetArchiveInfo(path string) (postAssetArchiveInfo, bool) {

	var info postAssetArchiveInfo

	info.path = strings.TrimPrefix(path, "/")

	info.id, info.subPath, _ = strings.Cut(info.path, "/")

	switch {

	case strings.HasSuffix(info.id, ".tar.gz"),
		strings.HasSuffix(info.id, ".tgz"):
		info.isGzipped = true

	case strings.HasSuffix(info.id, ".tar"):
		// ok

	default:
		// unsupported
		return postAssetArchiveInfo{}, false
	}

	return info, true
}

func (a *api) writePostAsset(
	rw http.ResponseWriter,
	r *http.Request,
	path string,
	from io.ReadSeeker,
) {

	maxWidth, err := apiutil.StrToInt(r.FormValue("w"), 0)
	if err != nil {
		apiutil.BadRequest(rw, r, fmt.Errorf("invalid w parameter: %w", err))
		return
	}

	if maxWidth == 0 {
		http.ServeContent(rw, r, path, time.Time{}, from)
		return
	}

	if !isImgResizable(path) {
		apiutil.BadRequest(rw, r, fmt.Errorf("cannot resize asset %q", path))
		return
	}

	resizedBuf := new(bytes.Buffer)

	if err := resizeImage(resizedBuf, from, float64(maxWidth)); err != nil {
		apiutil.InternalServerError(
			rw, r,
			fmt.Errorf(
				"resizing image %q to size %d: %w",
				path, maxWidth, err,
			),
		)
	}

	http.ServeContent(
		rw, r, path, time.Time{}, bytes.NewReader(resizedBuf.Bytes()),
	)
}

func (a *api) handleGetPostAssetArchive(
	rw http.ResponseWriter,
	r *http.Request,
	info postAssetArchiveInfo,
) {

	buf := new(bytes.Buffer)

	err := a.params.PostAssetStore.Get(info.id, buf)

	if errors.Is(err, asset.ErrNotFound) {
		http.Error(rw, "asset not found", 404)
		return
	} else if err != nil {
		apiutil.InternalServerError(
			rw, r,
			fmt.Errorf("fetching archive asset with id %q: %w", info.id, err),
		)
		return
	}

	var from io.Reader = buf

	if info.isGzipped {

		if from, err = gzip.NewReader(from); err != nil {
			apiutil.InternalServerError(
				rw, r,
				fmt.Errorf("decompressing archive asset with id %q: %w", info.id, err),
			)
			return
		}
	}

	tarFS, err := tarfs.New(from)

	if err != nil {
		apiutil.InternalServerError(
			rw, r,
			fmt.Errorf("reading archive asset with id %q as fs: %w", info.id, err),
		)
		return
	}

	f, err := tarFS.Open(info.subPath)

	if errors.Is(err, fs.ErrExist) {
		http.Error(rw, "Asset not found", 404)
		return

	} else if err != nil {

		apiutil.InternalServerError(
			rw, r,
			fmt.Errorf(
				"opening path %q from archive asset with id %q as fs: %w",
				info.subPath, info.id, err,
			),
		)
		return
	}

	defer f.Close()

	a.writePostAsset(rw, r, info.path, f)
}

func (a *api) getPostAssetHandler() http.Handler {

	return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {

		archiveInfo, ok := extractPostAssetArchiveInfo(r.URL.Path)

		if ok {
			a.handleGetPostAssetArchive(rw, r, archiveInfo)
			return
		}

		id := filepath.Base(r.URL.Path)

		buf := new(bytes.Buffer)

		err := a.params.PostAssetStore.Get(id, buf)

		if errors.Is(err, asset.ErrNotFound) {
			http.Error(rw, "Asset not found", 404)
			return
		} else if err != nil {
			apiutil.InternalServerError(
				rw, r, fmt.Errorf("fetching asset with id %q: %w", id, err),
			)
			return
		}

		a.writePostAsset(rw, r, id, bytes.NewReader(buf.Bytes()))
	})
}

func (a *api) postPostAssetHandler() http.Handler {

	return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {

		id := r.PostFormValue("id")
		if id == "" {
			apiutil.BadRequest(rw, r, errors.New("id is required"))
			return
		}

		file, _, err := r.FormFile("file")
		if err != nil {
			apiutil.BadRequest(rw, r, fmt.Errorf("reading multipart file: %w", err))
			return
		}
		defer file.Close()

		if err := a.params.PostAssetStore.Set(id, file); err != nil {
			apiutil.InternalServerError(rw, r, fmt.Errorf("storing file: %w", err))
			return
		}

		a.executeRedirectTpl(rw, r, a.manageAssetsURL(false))
	})
}

func (a *api) deletePostAssetHandler() http.Handler {

	return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {

		id := filepath.Base(r.URL.Path)

		if id == "/" {
			apiutil.BadRequest(rw, r, errors.New("id is required"))
			return
		}

		err := a.params.PostAssetStore.Delete(id)

		if errors.Is(err, asset.ErrNotFound) {
			http.Error(rw, "Asset not found", 404)
			return
		} else if err != nil {
			apiutil.InternalServerError(
				rw, r, fmt.Errorf("deleting asset with id %q: %w", id, err),
			)
			return
		}

		a.executeRedirectTpl(rw, r, a.manageAssetsURL(false))
	})
}