repos / pico

pico services - prose.sh, pastes.sh, imgs.sh, feeds.sh, pgs.sh
git clone https://github.com/picosh/pico.git

commit
3154f99
parent
a2488d8
author
Eric Bower
date
2024-01-31 05:29:52 +0000 UTC
chore(prose): set img proc defaults
3 files changed,  +29, -5
M imgs/api.go
+10, -0
 1@@ -198,6 +198,16 @@ func ImgRequest(w http.ResponseWriter, r *http.Request) {
 2 		return
 3 	}
 4 
 5+	// set default quality for web optimization
 6+	if opts.Quality == 0 {
 7+		opts.Quality = 80
 8+	}
 9+
10+	// set default format to be webp
11+	if opts.Ext == "" {
12+		opts.Ext = "webp"
13+	}
14+
15 	ext := filepath.Ext(slug)
16 	// Files can contain periods.  `filepath.Ext` is greedy and will clip the last period in the slug
17 	// and call that a file extension so we want to be explicit about what
M pgs/api.go
+3, -2
 1@@ -239,6 +239,7 @@ func (h *AssetHandler) handle(w http.ResponseWriter) {
 2 
 3 	routes := calcPossibleRoutes(h.ProjectDir, h.Filepath, redirects)
 4 	var contents io.ReadCloser
 5+	contentType := ""
 6 	assetFilepath := ""
 7 	status := 200
 8 	attempts := []string{}
 9@@ -248,13 +249,14 @@ func (h *AssetHandler) handle(w http.ResponseWriter) {
10 		var c io.ReadCloser
11 		var err error
12 		if strings.HasPrefix(mimeType, "image/") {
13-			c, _, err = h.Storage.ServeFile(
14+			c, contentType, err = h.Storage.ServeFile(
15 				h.Bucket,
16 				fp.Filepath,
17 				h.ImgProcessOpts,
18 			)
19 		} else {
20 			c, _, _, err = h.Storage.GetFile(h.Bucket, fp.Filepath)
21+			contentType = storage.GetMimeType(assetFilepath)
22 		}
23 		if err == nil {
24 			contents = c
25@@ -275,7 +277,6 @@ func (h *AssetHandler) handle(w http.ResponseWriter) {
26 	}
27 	defer contents.Close()
28 
29-	contentType := storage.GetMimeType(assetFilepath)
30 	w.Header().Add("Content-Type", contentType)
31 	w.WriteHeader(status)
32 	_, err = io.Copy(w, contents)
M shared/storage/proxy.go
+16, -3
 1@@ -114,6 +114,14 @@ func UriToImgProcessOpts(uri string) (*ImgProcessOpts, error) {
 2 				return opts, err
 3 			}
 4 		}
 5+
 6+		if strings.HasPrefix(part, "ext:") {
 7+			ext := strings.Replace(part, "ext:", "", 1)
 8+			opts.Ext = ext
 9+			if err != nil {
10+				return opts, err
11+			}
12+		}
13 	}
14 
15 	return opts, nil
16@@ -123,6 +131,7 @@ type ImgProcessOpts struct {
17 	Quality int
18 	Ratio   *Ratio
19 	Rotate  int
20+	Ext     string
21 }
22 
23 func (img *ImgProcessOpts) String() string {
24@@ -136,7 +145,7 @@ func (img *ImgProcessOpts) String() string {
25 	// https://docs.imgproxy.net/usage/processing#size
26 	if img.Ratio != nil {
27 		processOpts = fmt.Sprintf(
28-			"%s/s:%d:%d",
29+			"%s/size:%d:%d",
30 			processOpts,
31 			img.Ratio.Width,
32 			img.Ratio.Height,
33@@ -156,6 +165,11 @@ func (img *ImgProcessOpts) String() string {
34 		}
35 	}
36 
37+	// https://docs.imgproxy.net/usage/processing#format
38+	if img.Ext != "" {
39+		processOpts = fmt.Sprintf("%s/ext:%s", processOpts, img.Ext)
40+	}
41+
42 	return processOpts
43 }
44 
45@@ -168,8 +182,7 @@ func HandleProxy(dataURL string, opts *ImgProcessOpts) (io.ReadCloser, string, e
46 
47 	processOpts := opts.String()
48 
49-	fileType := ""
50-	processPath := fmt.Sprintf("%s/%s%s", processOpts, base64.StdEncoding.EncodeToString([]byte(dataURL)), fileType)
51+	processPath := fmt.Sprintf("%s/%s", processOpts, base64.StdEncoding.EncodeToString([]byte(dataURL)))
52 
53 	if imgProxySalt != "" && imgProxyKey != "" {
54 		keyBin, err := hex.DecodeString(imgProxyKey)