Commit c26a047
Eric Bower
·
2026-04-20 20:39:24 -0400 EDT
parent 532a539
refactor(storage): put accepts ObjectInfo instead of FileEntry This is a symmetric change so everything uses the same objects
7 files changed,
+41,
-16
+20,
-3
| ... | ... | @@ -275,6 +275,14 @@ func findPlusFF(dbpool pgsdb.PgsDB, cfg *PgsConfig, userID string) *db.FeatureFl | |
| 275 | 275 | return ff | |
| 276 | 276 | } | |
| 277 | 277 | ||
| 278 | + | func mtimeToTime(entry *sendutils.FileEntry) time.Time { | |
| 279 | + | var mtime time.Time | |
| 280 | + | if entry.Mtime > 0 { | |
| 281 | + | return time.Unix(entry.Mtime, 0) | |
| 282 | + | } | |
| 283 | + | return mtime | |
| 284 | + | } | |
| 285 | + | ||
| 278 | 286 | func (h *UploadAssetHandler) Write(s *pssh.SSHServerConnSession, entry *sendutils.FileEntry) (string, error) { | |
| 279 | 287 | logger := pssh.GetLogger(s) | |
| 280 | 288 | user := pssh.GetUser(s) |
| ... | ... | @@ -322,12 +330,15 @@ func (h *UploadAssetHandler) Write(s *pssh.SSHServerConnSession, entry *sendutil | |
| 322 | 330 | return "", fmt.Errorf(msg, project.Blocked) | |
| 323 | 331 | } | |
| 324 | 332 | ||
| 333 | + | info := &storage.ObjectInfo{ | |
| 334 | + | LastModified: mtimeToTime(entry), | |
| 335 | + | } | |
| 325 | 336 | if entry.Mode.IsDir() { | |
| 326 | 337 | _, _, err := h.Cfg.Storage.PutObject( | |
| 327 | 338 | bucket, | |
| 328 | 339 | path.Join(shared.GetAssetFileName(entry), "._pico_keep_dir"), | |
| 329 | 340 | bytes.NewReader([]byte{}), | |
| 330 | - | entry, | |
| 341 | + | info, | |
| 331 | 342 | ) | |
| 332 | 343 | return "", err | |
| 333 | 344 | } |
| ... | ... | @@ -498,11 +509,14 @@ func (h *UploadAssetHandler) Delete(s *pssh.SSHServerConnSession, entry *senduti | |
| 498 | 509 | }) | |
| 499 | 510 | ||
| 500 | 511 | if len(sibs) == 0 { | |
| 512 | + | info := &storage.ObjectInfo{ | |
| 513 | + | LastModified: mtimeToTime(entry), | |
| 514 | + | } | |
| 501 | 515 | _, _, err := h.Cfg.Storage.PutObject( | |
| 502 | 516 | bucket, | |
| 503 | 517 | filepath.Join(pathDir, "._pico_keep_dir"), | |
| 504 | 518 | bytes.NewReader([]byte{}), | |
| 505 | - | entry, | |
| 519 | + | info, | |
| 506 | 520 | ) | |
| 507 | 521 | if err != nil { | |
| 508 | 522 | return err |
| ... | ... | @@ -555,11 +569,14 @@ func (h *UploadAssetHandler) writeAsset(s *pssh.SSHServerConnSession, reader io. | |
| 555 | 569 | "filename", assetFilepath, | |
| 556 | 570 | ) | |
| 557 | 571 | ||
| 572 | + | info := &storage.ObjectInfo{ | |
| 573 | + | LastModified: mtimeToTime(data.FileEntry), | |
| 574 | + | } | |
| 558 | 575 | _, fsize, err := h.Cfg.Storage.PutObject( | |
| 559 | 576 | data.Bucket, | |
| 560 | 577 | assetFilepath, | |
| 561 | 578 | reader, | |
| 562 | - | data.FileEntry, | |
| 579 | + | info, | |
| 563 | 580 | ) | |
| 564 | 581 | return fsize, err | |
| 565 | 582 | } |
+1,
-1
| ... | ... | @@ -289,7 +289,7 @@ func (h *UploadImgHandler) metaImg(data *PostMetaData) error { | |
| 289 | 289 | bucket, | |
| 290 | 290 | h.getObjectPath(data.Filename), | |
| 291 | 291 | sendutils.NopReadAndReaderAtCloser(reader), | |
| 292 | - | &sendutils.FileEntry{}, | |
| 292 | + | &storage.ObjectInfo{}, | |
| 293 | 293 | ) | |
| 294 | 294 | if err != nil { | |
| 295 | 295 | return err |
+3,
-3
| ... | ... | @@ -148,7 +148,7 @@ func (s *StorageFS) GetObject(bucket Bucket, fpath string) (utils.ReadAndReaderA | |
| 148 | 148 | return dat, objInfo, nil | |
| 149 | 149 | } | |
| 150 | 150 | ||
| 151 | - | func (s *StorageFS) PutObject(bucket Bucket, fpath string, contents io.Reader, entry *utils.FileEntry) (string, int64, error) { | |
| 151 | + | func (s *StorageFS) PutObject(bucket Bucket, fpath string, contents io.Reader, info *ObjectInfo) (string, int64, error) { | |
| 152 | 152 | loc := filepath.Join(bucket.Path, fpath) | |
| 153 | 153 | err := os.MkdirAll(filepath.Dir(loc), os.ModePerm) | |
| 154 | 154 | if err != nil { |
| ... | ... | @@ -168,8 +168,8 @@ func (s *StorageFS) PutObject(bucket Bucket, fpath string, contents io.Reader, e | |
| 168 | 168 | return "", 0, err | |
| 169 | 169 | } | |
| 170 | 170 | ||
| 171 | - | if entry.Mtime > 0 { | |
| 172 | - | uTime := time.Unix(entry.Mtime, 0) | |
| 171 | + | if !info.LastModified.IsZero() { | |
| 172 | + | uTime := info.LastModified | |
| 173 | 173 | _ = os.Chtimes(loc, uTime, uTime) | |
| 174 | 174 | } | |
| 175 | 175 |
+6,
-6
| ... | ... | @@ -58,8 +58,8 @@ func TestFsAdapter(t *testing.T) { | |
| 58 | 58 | ||
| 59 | 59 | str := "here is a test file" | |
| 60 | 60 | reader := strings.NewReader(str) | |
| 61 | - | actualPath, size, err := st.PutObject(bucket, "./nice/test.txt", reader, &utils.FileEntry{ | |
| 62 | - | Mtime: modTime.Unix(), | |
| 61 | + | actualPath, size, err := st.PutObject(bucket, "./nice/test.txt", reader, &ObjectInfo{ | |
| 62 | + | LastModified: modTime, | |
| 63 | 63 | }) | |
| 64 | 64 | if err != nil { | |
| 65 | 65 | t.Fatal(err) |
| ... | ... | @@ -98,8 +98,8 @@ func TestFsAdapter(t *testing.T) { | |
| 98 | 98 | ||
| 99 | 99 | str = "a deeply nested test file" | |
| 100 | 100 | reader = strings.NewReader(str) | |
| 101 | - | _, _, err = st.PutObject(bucket, "./here/we/go/again.txt", reader, &utils.FileEntry{ | |
| 102 | - | Mtime: modTime.Unix(), | |
| 101 | + | _, _, err = st.PutObject(bucket, "./here/we/go/again.txt", reader, &ObjectInfo{ | |
| 102 | + | LastModified: modTime, | |
| 103 | 103 | }) | |
| 104 | 104 | if err != nil { | |
| 105 | 105 | t.Fatal(err) |
| ... | ... | @@ -193,8 +193,8 @@ func TestFsAdapter(t *testing.T) { | |
| 193 | 193 | ||
| 194 | 194 | str = "a deeply nested test file" | |
| 195 | 195 | reader = strings.NewReader(str) | |
| 196 | - | _, _, err = st.PutObject(bucket, "./here/yes/we/can.txt", reader, &utils.FileEntry{ | |
| 197 | - | Mtime: modTime.Unix(), | |
| 196 | + | _, _, err = st.PutObject(bucket, "./here/yes/we/can.txt", reader, &ObjectInfo{ | |
| 197 | + | LastModified: modTime, | |
| 198 | 198 | }) | |
| 199 | 199 | if err != nil { | |
| 200 | 200 | t.Fatal(err) |
+9,
-1
| ... | ... | @@ -252,11 +252,19 @@ func (h *UploadAssetHandler) writeAsset(s *pssh.SSHServerConnSession, data *File | |
| 252 | 252 | objectFileName, | |
| 253 | 253 | ) | |
| 254 | 254 | ||
| 255 | + | var mtime time.Time | |
| 256 | + | if data.Mtime > 0 { | |
| 257 | + | mtime = time.Unix(data.Mtime, 0) | |
| 258 | + | } | |
| 259 | + | info := &ObjectInfo{ | |
| 260 | + | LastModified: mtime, | |
| 261 | + | } | |
| 262 | + | ||
| 255 | 263 | _, _, err = h.Cfg.Storage.PutObject( | |
| 256 | 264 | data.Bucket, | |
| 257 | 265 | objectFileName, | |
| 258 | 266 | utils.NopReadAndReaderAtCloser(reader), | |
| 259 | - | data.FileEntry, | |
| 267 | + | info, | |
| 260 | 268 | ) | |
| 261 | 269 | if err != nil { | |
| 262 | 270 | return err |
+1,
-1
| ... | ... | @@ -105,7 +105,7 @@ func (s *StorageMemory) GetObject(bucket Bucket, fpath string) (utils.ReadAndRea | |
| 105 | 105 | return &seekableReader{bytes.NewReader([]byte(dat))}, objInfo, nil | |
| 106 | 106 | } | |
| 107 | 107 | ||
| 108 | - | func (s *StorageMemory) PutObject(bucket Bucket, fpath string, contents io.Reader, entry *utils.FileEntry) (string, int64, error) { | |
| 108 | + | func (s *StorageMemory) PutObject(bucket Bucket, fpath string, contents io.Reader, info *ObjectInfo) (string, int64, error) { | |
| 109 | 109 | s.mu.Lock() | |
| 110 | 110 | defer s.mu.Unlock() | |
| 111 | 111 |
+1,
-1
| ... | ... | @@ -32,7 +32,7 @@ type BucketStorage interface { | |
| 32 | 32 | ||
| 33 | 33 | type ObjectStorage interface { | |
| 34 | 34 | GetObject(bucket Bucket, fpath string) (utils.ReadAndReaderAtCloser, *ObjectInfo, error) | |
| 35 | - | PutObject(bucket Bucket, fpath string, contents io.Reader, entry *utils.FileEntry) (string, int64, error) | |
| 35 | + | PutObject(bucket Bucket, fpath string, contents io.Reader, info *ObjectInfo) (string, int64, error) | |
| 36 | 36 | DeleteObject(bucket Bucket, fpath string) error | |
| 37 | 37 | ListObjects(bucket Bucket, dir string, recursive bool) ([]os.FileInfo, error) | |
| 38 | 38 | } |