- commit
- 2dc39dc
- parent
- f8e4071
- author
- Eric Bower
- date
- 2023-11-13 03:55:09 +0000 UTC
refactor(imgs): dont bring libwebp into every service (#55) Most of our libwebp related code lived inside the `shared` package. This is problematic since every service imports that package, thereby requiring libwebp for every service. Now references to libwebp live solely inside `imgs` library. Unfortunately, all of our post-based SSH services import `imgs` so they still have the requirement.
8 files changed,
+69,
-64
+2,
-2
1@@ -50,9 +50,9 @@ func main() {
2 }
3 defer reader.Close()
4
5- opt := shared.NewImgOptimizer(cfg.Logger, "")
6+ opt := imgs.NewImgOptimizer(cfg.Logger, "")
7 contents := &bytes.Buffer{}
8- img, err := shared.GetImageForOptimization(reader, post.MimeType)
9+ img, err := imgs.GetImageForOptimization(reader, post.MimeType)
10 if err != nil {
11 cfg.Logger.Error(err)
12 continue
+39,
-0
1@@ -0,0 +1,39 @@
2+package uploadimgs
3+
4+import (
5+ "github.com/charmbracelet/ssh"
6+ "github.com/picosh/pico/db"
7+ "github.com/picosh/pico/imgs"
8+ "github.com/picosh/pico/shared"
9+ "github.com/picosh/pico/shared/storage"
10+ "github.com/picosh/pico/wish/send/utils"
11+)
12+
13+type ImgsAPI struct {
14+ Cfg *shared.ConfigSite
15+ Db db.DB
16+ St storage.ObjectStorage
17+}
18+
19+func NewImgsAPI(dbpool db.DB, st storage.ObjectStorage) *ImgsAPI {
20+ cfg := imgs.NewConfigSite()
21+ return &ImgsAPI{
22+ Cfg: cfg,
23+ Db: dbpool,
24+ St: st,
25+ }
26+}
27+
28+func (img *ImgsAPI) HasAccess(userID string) bool {
29+ return img.Db.HasFeatureForUser(userID, "imgs")
30+}
31+
32+func (img *ImgsAPI) Upload(s ssh.Session, file *utils.FileEntry) (string, error) {
33+ handler := NewUploadImgHandler(img.Db, img.Cfg, img.St)
34+ err := handler.Validate(s)
35+ if err != nil {
36+ return "", err
37+ }
38+
39+ return handler.Write(s, file)
40+}
+4,
-3
1@@ -9,6 +9,7 @@ import (
2
3 "github.com/charmbracelet/ssh"
4 "github.com/picosh/pico/db"
5+ "github.com/picosh/pico/imgs"
6 "github.com/picosh/pico/shared"
7 "github.com/picosh/pico/wish/send/utils"
8 )
9@@ -67,7 +68,7 @@ func (h *UploadImgHandler) metaImg(data *PostMetaData) error {
10 return err
11 }
12
13- opt := shared.NewImgOptimizer(h.Cfg.Logger, "")
14+ opt := imgs.NewImgOptimizer(h.Cfg.Logger, "")
15 // for small images we want to preserve quality
16 // since it can have a dramatic effect
17 if data.FileSize < 3*shared.MB {
18@@ -81,9 +82,9 @@ func (h *UploadImgHandler) metaImg(data *PostMetaData) error {
19 var webpReader *bytes.Reader
20 contents := &bytes.Buffer{}
21
22- img, err := shared.GetImageForOptimization(tee, data.MimeType)
23+ img, err := imgs.GetImageForOptimization(tee, data.MimeType)
24 finalName := shared.SanitizeFileExt(data.Filename)
25- if errors.Is(err, shared.ErrAlreadyWebPError) {
26+ if errors.Is(err, imgs.ErrAlreadyWebPError) {
27 h.Cfg.Logger.Infof("(%s) is already webp, skipping encoding", data.Filename)
28 finalName = fmt.Sprintf("%s.webp", finalName)
29 webpReader = tee
+3,
-3
1@@ -12,7 +12,7 @@ import (
2
3 "github.com/charmbracelet/ssh"
4 "github.com/picosh/pico/db"
5- "github.com/picosh/pico/imgs"
6+ uploadimgs "github.com/picosh/pico/filehandlers/imgs"
7 "github.com/picosh/pico/shared"
8 "github.com/picosh/pico/shared/storage"
9 "github.com/picosh/pico/wish/cms/util"
10@@ -48,11 +48,11 @@ type ScpUploadHandler struct {
11 DBPool db.DB
12 Cfg *shared.ConfigSite
13 Hooks ScpFileHooks
14- ImgClient *imgs.ImgsAPI
15+ ImgClient *uploadimgs.ImgsAPI
16 }
17
18 func NewScpPostHandler(dbpool db.DB, cfg *shared.ConfigSite, hooks ScpFileHooks, st storage.ObjectStorage) *ScpUploadHandler {
19- client := imgs.NewImgsAPI(dbpool, st)
20+ client := uploadimgs.NewImgsAPI(dbpool, st)
21
22 return &ScpUploadHandler{
23 DBPool: dbpool,
+5,
-5
1@@ -192,7 +192,7 @@ type ImgHandler struct {
2 Storage storage.ObjectStorage
3 Logger *zap.SugaredLogger
4 Cache *gocache.Cache
5- Img *shared.ImgOptimizer
6+ Img *ImgOptimizer
7 // We should try to use the optimized image if it's available
8 // not all images are optimized so this flag isn't enough
9 // because we also need to check the mime type
10@@ -203,7 +203,7 @@ type ImgResizer struct {
11 Key string
12 contents utils.ReaderAtCloser
13 writer io.Writer
14- Img *shared.ImgOptimizer
15+ Img *ImgOptimizer
16 Cache *gocache.Cache
17 }
18
19@@ -277,7 +277,7 @@ func imgHandler(w http.ResponseWriter, h *ImgHandler) {
20
21 contentType := post.MimeType
22 fname := post.Filename
23- isWebOptimized := shared.IsWebOptimized(contentType)
24+ isWebOptimized := IsWebOptimized(contentType)
25
26 if h.UseOptimized && isWebOptimized {
27 contentType = "image/webp"
28@@ -357,7 +357,7 @@ func imgRequestOriginal(w http.ResponseWriter, r *http.Request) {
29 Storage: st,
30 Logger: logger,
31 Cache: cache,
32- Img: shared.NewImgOptimizer(logger, ""),
33+ Img: NewImgOptimizer(logger, ""),
34 UseOptimized: false,
35 })
36 }
37@@ -395,7 +395,7 @@ func imgRequest(w http.ResponseWriter, r *http.Request) {
38 Storage: st,
39 Logger: logger,
40 Cache: cache,
41- Img: shared.NewImgOptimizer(logger, dimes),
42+ Img: NewImgOptimizer(logger, dimes),
43 UseOptimized: true,
44 })
45 }
+0,
-34
1@@ -1,11 +1,6 @@
2 package imgs
3
4 import (
5- "github.com/charmbracelet/ssh"
6- "github.com/picosh/pico/db"
7- uploadimgs "github.com/picosh/pico/filehandlers/imgs"
8- "github.com/picosh/pico/shared"
9- "github.com/picosh/pico/shared/storage"
10 "github.com/picosh/pico/wish/send/utils"
11 )
12
13@@ -13,32 +8,3 @@ type IImgsAPI interface {
14 HasAccess(userID string) bool
15 Upload(file *utils.FileEntry) (string, error)
16 }
17-
18-type ImgsAPI struct {
19- Cfg *shared.ConfigSite
20- Db db.DB
21- St storage.ObjectStorage
22-}
23-
24-func NewImgsAPI(dbpool db.DB, st storage.ObjectStorage) *ImgsAPI {
25- cfg := NewConfigSite()
26- return &ImgsAPI{
27- Cfg: cfg,
28- Db: dbpool,
29- St: st,
30- }
31-}
32-
33-func (img *ImgsAPI) HasAccess(userID string) bool {
34- return img.Db.HasFeatureForUser(userID, "imgs")
35-}
36-
37-func (img *ImgsAPI) Upload(s ssh.Session, file *utils.FileEntry) (string, error) {
38- handler := uploadimgs.NewUploadImgHandler(img.Db, img.Cfg, img.St)
39- err := handler.Validate(s)
40- if err != nil {
41- return "", err
42- }
43-
44- return handler.Write(s, file)
45-}
R shared/img.go =>
imgs/optimizer.go
+1,
-17
1@@ -1,7 +1,6 @@
2-package shared
3+package imgs
4
5 import (
6- "bytes"
7 "errors"
8 "fmt"
9 "image"
10@@ -69,21 +68,6 @@ func IsWebOptimized(mimeType string) bool {
11 return false
12 }
13
14-func CreateImgURL(linkify Linkify) func([]byte) []byte {
15- return func(url []byte) []byte {
16- if url[0] == '/' {
17- name := SanitizeFileExt(string(url))
18- nextURL := linkify.Create(name)
19- return []byte(nextURL)
20- } else if bytes.HasPrefix(url, []byte{'.', '/'}) {
21- name := SanitizeFileExt(string(url[1:]))
22- nextURL := linkify.Create(name)
23- return []byte(nextURL)
24- }
25- return url
26- }
27-}
28-
29 func GetRatio(dimes string) (*Ratio, error) {
30 if dimes == "" {
31 return nil, nil
1@@ -205,6 +205,21 @@ func (r *ImgRender) renderImage(w util.BufWriter, source []byte, node ast.Node,
2 return ast.WalkSkipChildren, nil
3 }
4
5+func CreateImgURL(linkify Linkify) func([]byte) []byte {
6+ return func(url []byte) []byte {
7+ if url[0] == '/' {
8+ name := SanitizeFileExt(string(url))
9+ nextURL := linkify.Create(name)
10+ return []byte(nextURL)
11+ } else if bytes.HasPrefix(url, []byte{'.', '/'}) {
12+ name := SanitizeFileExt(string(url[1:]))
13+ nextURL := linkify.Create(name)
14+ return []byte(nextURL)
15+ }
16+ return url
17+ }
18+}
19+
20 func ParseText(text string, linkify Linkify) (*ParsedText, error) {
21 parsed := ParsedText{
22 MetaData: &MetaData{