repos / pico

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

pico / shared / storage
Eric Bower · 18 Feb 24

fs.go

 1package storage
 2
 3import (
 4	"fmt"
 5	"io"
 6	"os"
 7	"path/filepath"
 8
 9	sst "github.com/picosh/pobj/storage"
10)
11
12type StorageFS struct {
13	*sst.StorageFS
14}
15
16func NewStorageFS(dir string) (*StorageFS, error) {
17	st, err := sst.NewStorageFS(dir)
18	if err != nil {
19		return nil, err
20	}
21	return &StorageFS{st}, nil
22}
23
24func (s *StorageFS) ServeObject(bucket sst.Bucket, fpath string, opts *ImgProcessOpts) (io.ReadCloser, string, error) {
25	if opts == nil || os.Getenv("IMGPROXY_URL") == "" {
26		contentType := GetMimeType(fpath)
27		rc, _, _, err := s.GetObject(bucket, fpath)
28		return rc, contentType, err
29	}
30
31	filePath := filepath.Join(bucket.Path, fpath)
32	dataURL := fmt.Sprintf("local://%s", filePath)
33	return HandleProxy(dataURL, opts)
34}
35
36func (s *StorageFS) GetObjectSize(bucket sst.Bucket, fpath string) (int64, error) {
37	fi, err := os.Stat(filepath.Join(bucket.Path, fpath))
38	if err != nil {
39		return 0, err
40	}
41	size := fi.Size()
42	return size, nil
43}