pico
created pr with
65.1
cmds
checkout latest patchset:
ssh pr.pico.sh print 65 | git am -3checkout any patchset in a patch request:
ssh pr.pico.sh print 65.[rev] | git am -3add changes to patch request:
git format-patch main --stdout | ssh pr.pico.sh pr add 65
Patchset
65.1
refactor(pobj): metadata for FS
Eric Bower
2025-04-18T16:47:39ZSemantic diff summary
3 added,
4 modified,
1 signature changed,
0 removed
across 1 analyzed file
pkg/pobj/storage/fs.go
-
chunklines 11-17modified -
method_declarationfindObjInfoadded -
method_declarationupsertObjInfoadded -
type_declarationStorageFSmodified -
function_declarationNewStorageFSsignature changed -
type_declarationObjInfoadded -
function_declarationGetObjectmodified -
function_declarationPutObjectmodified
+47
-4
pkg/pobj/storage/fs.go
#
| ... | ... | @@ -33,13 +34,48 @@ func dirSize(path string) (int64, error) { | |
| 33 | 34 | type StorageFS struct { | |
| 34 | 35 | Dir string | |
| 35 | 36 | Logger *slog.Logger | |
| 37 | + | Sqlx sqlx.DB | |
| 36 | 38 | } | |
| 37 | 39 | ||
| 38 | 40 | var _ ObjectStorage = &StorageFS{} | |
| 39 | 41 | var _ ObjectStorage = (*StorageFS)(nil) | |
| 40 | 42 | ||
| 41 | - | func NewStorageFS(logger *slog.Logger, dir string) (*StorageFS, error) { | |
| 42 | - | return &StorageFS{Logger: logger, Dir: dir}, nil | |
| 43 | + | func NewStorageFS(logger *slog.Logger, dir string, sqlx sqlx.DB) (*StorageFS, error) { | |
| 44 | + | return &StorageFS{Logger: logger, Dir: dir, Sqlx: sqlx}, nil | |
| 45 | + | } | |
| 46 | + | ||
| 47 | + | type ObjInfo struct { | |
| 48 | + | ID string `db:"id"` | |
| 49 | + | Etag string `db:"etag"` | |
| 50 | + | ContentType string `db:"content_type"` | |
| 51 | + | CreatedAt time.Time `db:"created_at"` | |
| 52 | + | UpdatedAt time.Time `db:"updated_at"` | |
| 53 | + | } | |
| 54 | + | ||
| 55 | + | func (s *StorageFS) findObjInfo(name string) (*ObjInfo, error) { | |
| 56 | + | var info ObjInfo | |
| 57 | + | err := s.Sqlx.Get(&info, "select * from object_infos where id=?", name) | |
| 58 | + | return &info, err | |
| 59 | + | } | |
| 60 | + | ||
| 61 | + | func (s *StorageFS) upsertObjInfo(name, etag, contentType string) error { | |
| 62 | + | _, err := s.findObjInfo(name) | |
| 63 | + | if err != nil { | |
| 64 | + | row := s.Sqlx.QueryRow( | |
| 65 | + | "update object_infos set etag=? AND content_type=? AND updated_at=?", | |
| 66 | + | etag, | |
| 67 | + | contentType, | |
| 68 | + | time.Now(), | |
| 69 | + | ) | |
| 70 | + | return row.Scan() | |
| 71 | + | } | |
| 72 | + | row := s.Sqlx.QueryRow( | |
| 73 | + | "insert into object_infos (id, etag, content_type) VALUES(?, ?, ?)", | |
| 74 | + | name, | |
| 75 | + | etag, | |
| 76 | + | contentType, | |
| 77 | + | ) | |
| 78 | + | return row.Scan() | |
| 43 | 79 | } | |
| 44 | 80 | ||
| 45 | 81 | func (s *StorageFS) GetBucket(name string) (Bucket, error) { |
| ... | ... | @@ -100,7 +136,8 @@ func (s *StorageFS) GetObject(bucket Bucket, fpath string) (utils.ReadAndReaderA | |
| 100 | 136 | UserMetadata: map[string]string{}, | |
| 101 | 137 | } | |
| 102 | 138 | ||
| 103 | - | dat, err := os.Open(filepath.Join(bucket.Path, fpath)) | |
| 139 | + | loc := filepath.Join(bucket.Path, fpath) | |
| 140 | + | dat, err := os.Open(loc) | |
| 104 | 141 | if err != nil { | |
| 105 | 142 | return nil, objInfo, err | |
| 106 | 143 | } |
| ... | ... | @@ -112,6 +149,10 @@ func (s *StorageFS) GetObject(bucket Bucket, fpath string) (utils.ReadAndReaderA | |
| 112 | 149 | ||
| 113 | 150 | objInfo.Size = info.Size() | |
| 114 | 151 | objInfo.LastModified = info.ModTime() | |
| 152 | + | ||
| 153 | + | i, _ := s.findObjInfo(loc) | |
| 154 | + | objInfo.Metadata.Set("content-type", i.ContentType) | |
| 155 | + | ||
| 115 | 156 | return dat, objInfo, nil | |
| 116 | 157 | } | |
| 117 | 158 |
| ... | ... | @@ -138,7 +179,9 @@ func (s *StorageFS) PutObject(bucket Bucket, fpath string, contents io.Reader, e | |
| 138 | 179 | _ = os.Chtimes(loc, uTime, uTime) | |
| 139 | 180 | } | |
| 140 | 181 | ||
| 141 | - | return loc, size, nil | |
| 182 | + | err = s.upsertObjInfo(loc, "", "") | |
| 183 | + | ||
| 184 | + | return loc, size, err | |
| 142 | 185 | } | |
| 143 | 186 | ||
| 144 | 187 | func (s *StorageFS) DeleteObject(bucket Bucket, fpath string) error { |