Derive blob file paths from their IDs

This commit is contained in:
Ian Gulliver
2026-10-20 21:53:37 -07:00
parent c1d3f401b2
commit a1d498bac8
3 changed files with 12 additions and 12 deletions
+10 -11
View File
@@ -27,28 +27,27 @@ func (s *Store) CreateBlob(ctx context.Context, contentType string, data []byte,
CreatedBy: actor,
CreatedAt: time.Now().UTC(),
}
rel := filepath.Join("blobs", b.Id)
path := s.blobPath(b.Id)
err := os.MkdirAll(filepath.Join(s.dataDir(), "blobs"), 0o755)
err := os.MkdirAll(filepath.Dir(path), 0o755)
if err != nil {
return b, err
}
err = os.WriteFile(filepath.Join(s.dataDir(), rel), data, 0o644)
err = os.WriteFile(path, data, 0o644)
if err != nil {
return b, err
}
_, err = s.db.ExecContext(ctx, `INSERT INTO blobs (id, content_type, path, created_by, created_at) VALUES (?, ?, ?, ?, ?)`,
b.Id, b.ContentType, rel, b.CreatedBy, formatTime(b.CreatedAt))
_, err = s.db.ExecContext(ctx, `INSERT INTO blobs (id, content_type, created_by, created_at) VALUES (?, ?, ?, ?)`,
b.Id, b.ContentType, b.CreatedBy, formatTime(b.CreatedAt))
return b, err
}
func (s *Store) GetBlob(ctx context.Context, id string) (api.Blob, string, error) {
b := api.Blob{}
rel := ""
createdAt := ""
err := s.db.QueryRowContext(ctx, `SELECT id, content_type, path, created_by, created_at FROM blobs WHERE id = ?`, id).
Scan(&b.Id, &b.ContentType, &rel, &b.CreatedBy, &createdAt)
err := s.db.QueryRowContext(ctx, `SELECT id, content_type, created_by, created_at FROM blobs WHERE id = ?`, id).
Scan(&b.Id, &b.ContentType, &b.CreatedBy, &createdAt)
if errors.Is(err, sql.ErrNoRows) {
return b, "", errors.Join(ErrNotFound, errors.New("no blob "+id))
}
@@ -56,7 +55,7 @@ func (s *Store) GetBlob(ctx context.Context, id string) (api.Blob, string, error
return b, "", err
}
b.CreatedAt, err = parseTime(createdAt)
return b, filepath.Join(s.dataDir(), rel), err
return b, s.blobPath(b.Id), err
}
func (s *Store) ReadBlob(ctx context.Context, id string) (api.Blob, []byte, error) {
@@ -68,6 +67,6 @@ func (s *Store) ReadBlob(ctx context.Context, id string) (api.Blob, []byte, erro
return b, data, err
}
func (s *Store) dataDir() string {
return filepath.Dir(s.path)
func (s *Store) blobPath(id string) string {
return filepath.Join(filepath.Dir(s.path), "blobs", id)
}
@@ -0,0 +1 @@
ALTER TABLE blobs DROP COLUMN path;