How jsdevart/laravel-file-vault works
1. What this repository is
- This is a Laravel package, not a standalone app.
- Its boundary is “private file storage primitives” for Laravel apps: storing files under a structured namespace, generating URLs, checking existence, deleting, and resolving a file to a real path when needed.
2. Package bootstrap
- src/FileVaultServiceProvider.php is the integration point with Laravel’s container and config system.
- The package is discovered automatically by Laravel and reads config/file-vault.php, which controls the default disk, signed URL expiry, the route name used for serving files, and whether to force app-routed URLs instead of driver-native temporary URLs.
3. Core registry subsystem
- src/FileStorageRegistry.php is the central lookup mechanism.
- The application registers one storage service per “context” such as user, imports, or avatars.
- This registry is the main architectural boundary between the host app and the package: callers ask for a context, and the registry returns the appropriate file-storage service.
4. Storage-service abstraction
- src/Contracts/FileStorageInterface.php defines the contract for file storage behavior.
- src/Services/BaseFileStorageService.php provides the reusable implementation used by app-specific subclasses.
- The base service is where the package likely centralizes file naming, path composition, disk selection, URL generation, deletion, existence checks, and path resolution.
5. Context + element path model
- The repository is organized around a two-level logical namespace:
- a context root, e.g. user
- an element category, e.g. picture or document
- The README shows the physical layout as {context}/{element_folder}/XX/XX/{uuid}.{ext}, which means the storage service is responsible for mapping business labels into a deterministic directory structure.
6. URL generation boundary
- Signed URL behavior is a key architectural feature.
- For local/path-based disks, the package expects the host app to expose a signed route that streams the file.
- For cloud disks such as S3, getUrl() normally delegates to the storage driver’s temporary URL capability.
- src/Support/ServeSignedRoutePolicy.php encapsulates the decision of when to use the app route versus the storage driver’s own temporary URL, especially when force_serve_route is enabled.
7. Serving private files is intentionally outside the package
- The package does not ship a controller.
- That keeps authorization, middleware, cache headers, and response streaming in the host application.
- The package only standardizes the signed-route policy and the route name; the actual serving endpoint remains an application responsibility.
8. Real-path resolution subsystem
- src/Support/FilePathResult.php and src/Support/TemporaryFile.php support the getPath() workflow.
- For local disks, the package can return the real filesystem path directly.
- For remote disks, it downloads to a temp file so downstream libraries that require an actual path can still operate.
- The result object owns the temp file’s lifetime, which is an important design boundary: keep the result object alive as long as the file is needed.
9. Infrastructure and external storage
- The package is designed around Laravel’s filesystem abstraction, so the actual backend may be local storage or an S3-compatible service.
- The architecture changes depending on the disk:
- local/path-based disks can be streamed from disk and resolved with Storage::path()
- remote disks may need signed URLs or temporary downloads to a local temp file
- The force_serve_route option exists specifically for environments where the storage endpoint is not browser-reachable, such as internal MinIO or Docker-only hosts.
10. Developer extension point
- stubs/ExampleStorageService.php is the intended template for app developers to copy and customize.
- The usual extension model is: subclass BaseFileStorageService, define the context root, and optionally override how element names map to subfolders.
11. Testing surface
- The test suite is small and focused, with tests/Unit/ServeSignedRoutePolicyTest.php validating the signed-route decision logic.
- That suggests the package’s most important behavioral seam is the URL-serving policy rather than heavy storage integration tests.
12. Architectural takeaway for a diagram
- The main flow is:
- application code asks FileStorageRegistry for a context service
- the service stores/reads/deletes via Laravel Storage
- URL generation branches through signed-route policy or driver temporary URLs
- getPath() may return either a direct path or a temp-file-backed result
- The key boundaries are: host app vs package, context registry vs storage implementation, and local path vs remote object storage.
Main entry points: BaseFileStorageService.php Base service · ServeSignedRoutePolicy.php Serve policy · FileVaultServiceProvider.php Service provider · FileStorageRegistry.php Registry
Components 12
Laravel integration
Package bootstrap and configuration entry points.
- Service provider · Registers the package with Laravel and wires config/container integration.
src/FileVaultServiceProvider.php - Config · Controls default disk, signed URL expiry, route name, and route forcing.
config/file-vault.php
Core storage
Registry and reusable file-storage implementation.
- Registry · Resolves a storage service for each logical context such as users or imports.
src/FileStorageRegistry.php - Storage contract · Defines the file-storage behavior expected from context-specific services.
src/Contracts/FileStorageInterface.php - Base service · Reusable file-storage implementation for app-specific storage services.
src/Services/BaseFileStorageService.php - Example service · Starter subclass developers copy and customize for a storage context.
stubs/ExampleStorageService.php
Support utilities
URL policy and path-resolution helpers.
- Serve policy · Chooses between app-routed signed URLs and driver temporary URLs.
src/Support/ServeSignedRoutePolicy.php - Path result · Represents resolved file paths and keeps temporary-file lifetime attached.
src/Support/FilePathResult.php - Temp file · Local scratch file used when remote storage must be materialized on disk.
src/Support/TemporaryFile.php
External boundaries
Host application and Laravel filesystem dependencies.
- Host app · Owns authorization, middleware, streaming controller, and signed file route.
- Laravel Storage · Filesystem abstraction used for local disks and remote object storage.
- Disk backend · Concrete storage target such as local filesystem, S3, or MinIO.
Connections 14
- Service provider → Config: reads · Loads package configuration during bootstrapping.
- Service provider → Registry: registers · Makes the registry available to the Laravel container.
- Registry → Storage contract: returns · Looks up a context service that implements the storage contract.
- Example service → Base service: extends · Template subclass builds on the reusable base implementation.
- Base service → Storage contract: implements · Provides the package’s main storage behavior behind the contract.
- Base service → Laravel Storage: uses · Performs read/write/delete and URL operations through Laravel filesystem.
- Base service → Serve policy: consults · Branches URL generation through signed-route policy.
- Serve policy → Config: uses · Respects force-route and expiry settings from config.
- Serve policy → Host app: targets · Falls back to an application-owned serving route when needed.
- Base service → Path result: returns · Produces a path result when callers need a real filesystem path.
- Path result → Temp file: owns · Keeps temporary-file lifetime tied to the result object.
- Base service → Temp file: creates · Downloads remote files to a local temp file when direct paths are unavailable.
- Laravel Storage → Disk backend: backed by · Abstracts over local and remote storage backends.
- Base service → Disk backend: stores on · Persists files into the configured disk backend.