Repository navigation
feat: Add local filesystem cloud storage adapter - #5305
Open
realmeylisdev wants to merge 2 commits into
Open
realmeylisdev wants to merge 2 commits into
realmeylisdev wants to merge 2 commits into
Conversation
Adds serverpod_cloud_storage_local, a CloudStorage adapter that stores files on the server's local filesystem without external services or database tables. Closes serverpod#1817.
1 of 3 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #1817
Supersedes #4525 — rebuilt from scratch on current
maininstead of rebasing, sincemainhas since restructured the integrations (s3_compat/r2, generated pubspecs, shared testing conventions) and the old branch was conflicting. All review feedback from #4525 is addressed (details below).Summary
Adds
serverpod_cloud_storage_local, aCloudStorageadapter that stores files on the server's local filesystem — no external cloud provider and no database tables. Useful for development, testing, and self-hosted deployments.DatabaseCloudStoragewhere possible: noServerpodconstructor dependency, public URLs keyed onstorageId == 'public'and served by the built-in/serverpod_cloud_storageendpoint, same exception conventions.CloudStorageWithOptionsmixin (preventOverwrite,contentLength) with the same semantics asDatabaseCloudStorage.storeFileStream/retrieveFileStream/getFileSize) for memory-efficient large-file handling. These are extra public API beyond theCloudStorageinterface — happy to trim them if you prefer the minimal surface.startCleanupScheduler/cleanupExpiredFiles).How the
.metafiles work (answering the question from #4525)Expiration and verification state live in a small JSON sidecar (
<file>.meta) next to the data file:verified: false). A plain store removes any stale sidecar, so re-storing a path cannot inherit old expiration/verification state.retrieveFile,fileExists,getPublicUrl, streams) treat files without a sidecar as verified and non-expiring, unverified or expired files as absent, and corrupt/unparseable sidecars as unavailable (fail closed).verifyDirectFileUploadflips a pendingverified: falsesidecar to verified (keeping the expiration), mirroringDatabaseCloudStorage(returns false for missing or already-verified files)..metasuffix is reserved: user paths ending in.metaare rejected — in any casing, since macOS/Windows filesystems are case-insensitive — so metadata cannot be forged or read through the public API.cleanupExpiredFileswalks the storage directory and deletes expired data+sidecar pairs. The optional scheduler runs it periodically; runs are reentrancy-guarded and errors never escape to the timer.Path safety
Paths are sanitized with
package:path(normalize+rootPrefix/relative, as suggested in the previous review): backslashes are treated as separators, absolute paths become relative, and anything that would escape the storage directory throws aCloudStorageException— the silent'file'fallback questioned in #4525 is gone. Segments containing:or ending in./space are rejected (Windows drive letters, NTFS alternate data streams, trailing-dot aliasing), and a finalp.isWithinguard catches Windows drive-qualified edge cases. Covered by tests for../chains, mixed separators, absolute paths, and.metacasing variants.Feedback from #4525 addressed
CHANGELOG.md/README.mdare the root copies maintained byutil/update_pubspecs(cp lines added); the pubspec is generated fromtemplates/pubspecs/like all siblingsutil/all_packages,util/run_tests_analyze, andPUBLISHABLE_PACKAGESp.rootPrefix/p.relativesanitization; redundant manual filtering removed; escaping paths throw*Sync()calls, no constructor IO)Uint8List.sublistView(byteData)for writes (andByteData.sublistViewfor reads, which also fixes a latent offset bug)unawaited(...)and cleanup contains its own errorsREADME.md;docs/design/cloud_storage.mddiagram updated. I'll prepare a serverpod_docs PR with usage documentation once this lands.Notes for reviewers / open questions
CloudStoragePublicEndpointonly when aDatabaseCloudStorageis registered (_unguardedStartinserverpod.dart). That holds in the default setup via the 'private' storage, but if a user replaces both storages withLocalCloudStorage(or disables the database feature),getPublicUrlURLs would 404. This is documented on the class; if you'd like, I can extend the core registration condition in this PR — the endpoint'sfilemethod is already storage-agnostic.CloudStorageException(→ error log) rather than a 404, since invalid paths throw by design. If that's a concern,CloudStoragePublicEndpoint.filecould mapCloudStorageExceptiontoEndpointNotFoundException— say the word and I'll include it.createDirectFileUploadDescriptionreturnsnull): they require database-backed upload-entry tracking, which contradicts this package's no-database premise. Documented, withstoreFilethrough an endpoint as the alternative.fileExistsreports availability (verified and not expired), which is slightly stricter thanDatabaseCloudStorage; the consequence forpreventOverwrite(expired/unverified files get replaced) is documented and pinned by tests.Test plan
dart test)dart analyze --fatal-infoscleandart formatcleanutil/update_pubspecs+util/ensure_no_changesidempotent (generated pubspec matches the template)