package api import ( "context" "encoding/json" "fmt" "log" "net/http" "time" "pilotvault/apiserver/internal/config" ) // PocketBase file storage. Document blobs live in PocketBase's file storage, and // by default that is the pb_data volume next to the SQLite database. When the // PB_S3_* variables describe a bucket (the SeaweedFS and S3 compose files set // them), the API Server points PocketBase's "Files storage" at that bucket on // every boot. Clients never notice: a document is always downloaded through // GET /api/documents/{id}/file, which streams from PocketBase whichever backend // it uses. // storageSettings is the slice of PocketBase's settings this step owns. The json // names are PocketBase's own (core.S3Config), so the PATCH body is this type // marshalled back out. type storageSettings struct { Enabled bool `json:"enabled"` Bucket string `json:"bucket"` Region string `json:"region"` Endpoint string `json:"endpoint"` AccessKey string `json:"accessKey"` Secret string `json:"secret,omitempty"` ForcePathStyle bool `json:"forcePathStyle"` } // sameExceptSecret compares everything a read of the settings can be trusted // on. PocketBase masks the stored secret, so a rotation of the secret alone is // invisible from here — changing any other PB_S3_* value forces the write, and // so does editing it in the PocketBase admin UI. func (s storageSettings) sameExceptSecret(other storageSettings) bool { s.Secret, other.Secret = "", "" return s == other } func wantedStorage(cfg config.Config) storageSettings { return storageSettings{ Enabled: true, Bucket: cfg.S3Bucket, Region: cfg.S3Region, Endpoint: cfg.S3Endpoint, AccessKey: cfg.S3AccessKey, Secret: cfg.S3Secret, ForcePathStyle: cfg.S3ForcePathStyle, } } // StartFileStorage applies the configured S3 file storage in the background. // It does nothing when no bucket was asked for. A PocketBase that is still // starting is retried for a while; the server itself never waits on this, so a // bad endpoint cannot keep the panel (where the connection is fixed) down. func (s *Server) StartFileStorage() { s.mu.RLock() cfg := s.cfg s.mu.RUnlock() if !cfg.StorageConfigured() { if cfg.S3Enabled { log.Println("storage: WARNING: PB_S3_ENABLED is set but the bucket, endpoint or credentials are incomplete — file storage stays on the local volume") } return } if !s.admin.configured() { log.Println("storage: WARNING: PB_S3_* is set but POCKETBASE_ADMIN_EMAIL/PASSWORD are not — cannot point PocketBase at the bucket") return } want := wantedStorage(cfg) go func() { const attempts = 10 for i := 1; ; i++ { ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) err := s.ensureFileStorage(ctx, want) cancel() if err == nil { return } if i == attempts { log.Printf("storage: WARNING: could not apply S3 file storage after %d attempts: %v", attempts, err) return } time.Sleep(3 * time.Second) } }() } // ensureFileStorage points PocketBase's record-file storage at the bucket. // // It never turns S3 *off*: files already written to a bucket are reachable only // while PocketBase still points at it, so running the plain compose file again // leaves the setting where it is rather than stranding every existing document. // Moving back to local storage is a deliberate act in PocketBase's admin UI. func (s *Server) ensureFileStorage(ctx context.Context, want storageSettings) error { raw, status, err := s.admin.do(ctx, http.MethodGet, "/api/settings", nil) if err != nil { return err } if status < 200 || status >= 300 { return fmt.Errorf("read settings: status %d: %s", status, raw) } var current struct { S3 storageSettings `json:"s3"` } if err := json.Unmarshal(raw, ¤t); err != nil { return err } if current.S3.sameExceptSecret(want) { log.Printf("storage: • file storage already on S3 (%s)", want.Bucket) } else { // Only the s3 block is sent: everything else in the settings — mail, // backups, rate limits — belongs to whoever set it. raw, status, err = s.admin.do(ctx, http.MethodPatch, "/api/settings", map[string]any{"s3": want}) if err != nil { return err } if status < 200 || status >= 300 { return fmt.Errorf("apply settings: status %d: %s", status, raw) } log.Printf("storage: ✓ file storage → S3 (%s at %s)", want.Bucket, want.Endpoint) } s.testFileStorage(ctx) return nil } // testFileStorage asks PocketBase to prove it can reach the bucket, and only // says so in the log: uploads will fail if it cannot, but the server still has // to come up. func (s *Server) testFileStorage(ctx context.Context) { raw, status, err := s.admin.do(ctx, http.MethodPost, "/api/settings/test/s3", map[string]any{"filesystem": "storage"}) switch { case err != nil: log.Printf("storage: WARNING: S3 storage test failed: %v", err) case status < 200 || status >= 300: log.Printf("storage: WARNING: S3 storage unreachable (status %d): %s", status, raw) default: log.Printf("storage: ✓ S3 storage reachable") } }