repos / pico

pico services mono repo
git clone https://github.com/picosh/pico.git

commit
1043ee3
parent
27e24e6
author
Eric Bower
date
2025-04-18 13:09:48 -0400 EDT
refactor(shared): mime type pkg
6 files changed,  +90, -85
M pkg/apps/pgs/calc_route.go
+2, -2
 1@@ -10,7 +10,7 @@ import (
 2 
 3 	"github.com/picosh/pico/pkg/send/utils"
 4 	"github.com/picosh/pico/pkg/shared"
 5-	"github.com/picosh/pico/pkg/shared/storage"
 6+	"github.com/picosh/pico/pkg/shared/mime"
 7 )
 8 
 9 type HttpReply struct {
10@@ -23,7 +23,7 @@ func expandRoute(projectName, fp string, status int) []*HttpReply {
11 	if fp == "" {
12 		fp = "/"
13 	}
14-	mimeType := storage.GetMimeType(fp)
15+	mimeType := mime.GetMimeType(fp)
16 	fname := filepath.Base(fp)
17 	fdir := filepath.Dir(fp)
18 	fext := filepath.Ext(fp)
A pkg/shared/mime/mime.go
+82, -0
 1@@ -0,0 +1,82 @@
 2+package mime
 3+
 4+import "path/filepath"
 5+
 6+func GetMimeType(fpath string) string {
 7+	ext := filepath.Ext(fpath)
 8+	if ext == ".svg" {
 9+		return "image/svg+xml"
10+	} else if ext == ".css" {
11+		return "text/css"
12+	} else if ext == ".js" {
13+		return "text/javascript"
14+	} else if ext == ".ico" {
15+		return "image/x-icon"
16+	} else if ext == ".pdf" {
17+		return "application/pdf"
18+	} else if ext == ".html" || ext == ".htm" {
19+		return "text/html"
20+	} else if ext == ".jpg" || ext == ".jpeg" {
21+		return "image/jpeg"
22+	} else if ext == ".png" {
23+		return "image/png"
24+	} else if ext == ".gif" {
25+		return "image/gif"
26+	} else if ext == ".webp" {
27+		return "image/webp"
28+	} else if ext == ".otf" {
29+		return "font/otf"
30+	} else if ext == ".woff" {
31+		return "font/woff"
32+	} else if ext == ".woff2" {
33+		return "font/woff2"
34+	} else if ext == ".ttf" {
35+		return "font/ttf"
36+	} else if ext == ".md" {
37+		return "text/markdown; charset=UTF-8"
38+	} else if ext == ".json" || ext == ".map" {
39+		return "application/json"
40+	} else if ext == ".rss" {
41+		return "application/rss+xml"
42+	} else if ext == ".atom" {
43+		return "application/atom+xml"
44+	} else if ext == ".webmanifest" {
45+		return "application/manifest+json"
46+	} else if ext == ".xml" {
47+		return "application/xml"
48+	} else if ext == ".xsl" {
49+		return "application/xml"
50+	} else if ext == ".avif" {
51+		return "image/avif"
52+	} else if ext == ".heif" {
53+		return "image/heif"
54+	} else if ext == ".heic" {
55+		return "image/heif"
56+	} else if ext == ".opus" {
57+		return "audio/opus"
58+	} else if ext == ".wav" {
59+		return "audio/wav"
60+	} else if ext == ".mp3" {
61+		return "audio/mpeg"
62+	} else if ext == ".mp4" {
63+		return "video/mp4"
64+	} else if ext == ".mpeg" {
65+		return "video/mpeg"
66+	} else if ext == ".wasm" {
67+		return "application/wasm"
68+	} else if ext == ".opml" {
69+		return "text/x-opml"
70+	} else if ext == ".eot" {
71+		return "application/vnd.ms-fontobject"
72+	} else if ext == ".yml" || ext == ".yaml" {
73+		return "text/x-yaml"
74+	} else if ext == ".zip" {
75+		return "application/zip"
76+	} else if ext == ".rar" {
77+		return "application/vnd.rar"
78+	} else if ext == ".txt" {
79+		return "text/plain"
80+	}
81+
82+	return "text/plain"
83+}
M pkg/shared/storage/fs.go
+2, -1
 1@@ -9,6 +9,7 @@ import (
 2 	"strings"
 3 
 4 	sst "github.com/picosh/pico/pkg/pobj/storage"
 5+	"github.com/picosh/pico/pkg/shared/mime"
 6 )
 7 
 8 type StorageFS struct {
 9@@ -28,7 +29,7 @@ func (s *StorageFS) ServeObject(bucket sst.Bucket, fpath string, opts *ImgProces
10 	var rc io.ReadCloser
11 	info := &sst.ObjectInfo{}
12 	var err error
13-	mimeType := GetMimeType(fpath)
14+	mimeType := mime.GetMimeType(fpath)
15 	if !strings.HasPrefix(mimeType, "image/") || opts == nil || os.Getenv("IMGPROXY_URL") == "" {
16 		rc, info, err = s.GetObject(bucket, fpath)
17 		if info.Metadata == nil {
M pkg/shared/storage/memory.go
+2, -1
 1@@ -6,6 +6,7 @@ import (
 2 	"time"
 3 
 4 	sst "github.com/picosh/pico/pkg/pobj/storage"
 5+	"github.com/picosh/pico/pkg/shared/mime"
 6 )
 7 
 8 type StorageMemory struct {
 9@@ -32,7 +33,7 @@ func (s *StorageMemory) ServeObject(bucket sst.Bucket, fpath string, opts *ImgPr
10 	if info.ETag == "" {
11 		info.ETag = "static-etag-for-testing-purposes"
12 	}
13-	mimeType := GetMimeType(fpath)
14+	mimeType := mime.GetMimeType(fpath)
15 	info.Metadata.Set("content-type", mimeType)
16 	return obj, info, err
17 }
M pkg/shared/storage/minio.go
+2, -1
 1@@ -9,6 +9,7 @@ import (
 2 	"strings"
 3 
 4 	sst "github.com/picosh/pico/pkg/pobj/storage"
 5+	"github.com/picosh/pico/pkg/shared/mime"
 6 )
 7 
 8 type StorageMinio struct {
 9@@ -27,7 +28,7 @@ func (s *StorageMinio) ServeObject(bucket sst.Bucket, fpath string, opts *ImgPro
10 	var rc io.ReadCloser
11 	info := &sst.ObjectInfo{}
12 	var err error
13-	mimeType := GetMimeType(fpath)
14+	mimeType := mime.GetMimeType(fpath)
15 	if !strings.HasPrefix(mimeType, "image/") || opts == nil || os.Getenv("IMGPROXY_URL") == "" {
16 		rc, info, err = s.GetObject(bucket, fpath)
17 		if info.Metadata == nil {
M pkg/shared/storage/proxy.go
+0, -80
 1@@ -10,7 +10,6 @@ import (
 2 	"log/slog"
 3 	"net/http"
 4 	"os"
 5-	"path/filepath"
 6 	"strconv"
 7 	"strings"
 8 	"time"
 9@@ -18,85 +17,6 @@ import (
10 	"github.com/picosh/pico/pkg/pobj/storage"
11 )
12 
13-func GetMimeType(fpath string) string {
14-	ext := filepath.Ext(fpath)
15-	if ext == ".svg" {
16-		return "image/svg+xml"
17-	} else if ext == ".css" {
18-		return "text/css"
19-	} else if ext == ".js" {
20-		return "text/javascript"
21-	} else if ext == ".ico" {
22-		return "image/x-icon"
23-	} else if ext == ".pdf" {
24-		return "application/pdf"
25-	} else if ext == ".html" || ext == ".htm" {
26-		return "text/html"
27-	} else if ext == ".jpg" || ext == ".jpeg" {
28-		return "image/jpeg"
29-	} else if ext == ".png" {
30-		return "image/png"
31-	} else if ext == ".gif" {
32-		return "image/gif"
33-	} else if ext == ".webp" {
34-		return "image/webp"
35-	} else if ext == ".otf" {
36-		return "font/otf"
37-	} else if ext == ".woff" {
38-		return "font/woff"
39-	} else if ext == ".woff2" {
40-		return "font/woff2"
41-	} else if ext == ".ttf" {
42-		return "font/ttf"
43-	} else if ext == ".md" {
44-		return "text/markdown; charset=UTF-8"
45-	} else if ext == ".json" || ext == ".map" {
46-		return "application/json"
47-	} else if ext == ".rss" {
48-		return "application/rss+xml"
49-	} else if ext == ".atom" {
50-		return "application/atom+xml"
51-	} else if ext == ".webmanifest" {
52-		return "application/manifest+json"
53-	} else if ext == ".xml" {
54-		return "application/xml"
55-	} else if ext == ".xsl" {
56-		return "application/xml"
57-	} else if ext == ".avif" {
58-		return "image/avif"
59-	} else if ext == ".heif" {
60-		return "image/heif"
61-	} else if ext == ".heic" {
62-		return "image/heif"
63-	} else if ext == ".opus" {
64-		return "audio/opus"
65-	} else if ext == ".wav" {
66-		return "audio/wav"
67-	} else if ext == ".mp3" {
68-		return "audio/mpeg"
69-	} else if ext == ".mp4" {
70-		return "video/mp4"
71-	} else if ext == ".mpeg" {
72-		return "video/mpeg"
73-	} else if ext == ".wasm" {
74-		return "application/wasm"
75-	} else if ext == ".opml" {
76-		return "text/x-opml"
77-	} else if ext == ".eot" {
78-		return "application/vnd.ms-fontobject"
79-	} else if ext == ".yml" || ext == ".yaml" {
80-		return "text/x-yaml"
81-	} else if ext == ".zip" {
82-		return "application/zip"
83-	} else if ext == ".rar" {
84-		return "application/vnd.rar"
85-	} else if ext == ".txt" {
86-		return "text/plain"
87-	}
88-
89-	return "text/plain"
90-}
91-
92 func UriToImgProcessOpts(uri string) (*ImgProcessOpts, error) {
93 	opts := &ImgProcessOpts{}
94 	parts := strings.Split(uri, "/")