Download the PHP package themusicdev/files without Composer
On this page you can find all versions of the php package themusicdev/files. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download themusicdev/files
More information about themusicdev/files
Files in themusicdev/files
Package files
Short Description CakePHP 5 plugin: reusable uploads and file management with Flysystem v3 storage, a stored_files ledger, collections (virtual folders) and gated serving
License MIT
Homepage https://github.com/TheMusicDev/cakephp-files
Informations about the package files
TheMusicDev/Files
Uploads and file management for CakePHP 5 sites, built once and reused: Flysystem
v3 storage, one stored_files ledger row per file, dot-path collections as virtual folders, a validation and
lifecycle seam (FileStore), and gated serving through a route the plugin ships. Your application never touches the
filesystem and keeps only a thin *_file_id column in its own tables.
Status: stable, v1.0.0. It has run in production on one site (themusicdev.llc) and is now a standalone package.
Requires PHP 8.2+ (with ext-fileinfo) and CakePHP 5.2+.
Why
Uploads recur in every site, and the naive per-module version loses the original filename, leaves orphan files
behind when a write fails, and reinvents MIME validation each time. This plugin does the hard-to-get-right parts once:
a module declares a collection and stores a file id. The reasoning behind each decision is in
docs/decisions.md and the design of record in
docs/file-management-and-uploads.md; this README is the how.
Install
Loading the plugin also mounts its one route, GET /files/serve/{id}. After the migration on an existing install, run bin/cake schema_cache clear: Cake silently drops columns its cached
schema does not know when saving.
Configure
Defaults ship in the plugin's config/app_default.php and are merged under your values, so a host overrides only
what it differs on. Put yours in config/app.php under Files:
| Key | Default | Meaning |
|---|---|---|
backend |
local |
Which entry of backends is active. |
backends |
local at ROOT/data/uploads |
Flysystem adapters: class plus the arguments that adapter needs. The local adapter and the S3 adapter (league/flysystem-aws-s3-v3) are wired to their arguments; any other adapter needs a small change in FileStore::adapterArgs(). The local root is created on first run. |
collections |
uploads.misc (25 MB, any type) |
Your collections are added beside the built-in one; redefine uploads.misc to change it. |
authCallable |
null |
The serve gate. null means nothing is served (403). |
S3 (and any S3-compatible object store)
The package is not shipped as a dependency — run
composer require league/flysystem-aws-s3-v3 in your app first, then:
Without the flysystem-s3 package installed, selecting an S3 backend throws a
FileStoreException naming the package to install (instead of a fatal
error). Nothing about the ledger, collections or serve gate changes with the
backend.
Opt-in integration test against a real server
The fake-client tests stay hermetic; a real round-trip (put → exists →
read → purge) runs only when you ask for it, against any S3-compatible
server (RustFS, MinIO, AWS, …):
Without S3_TEST_ENDPOINT the group skips itself; CI never starts a server.
S3_TEST_KEY / S3_TEST_SECRET / S3_TEST_BUCKET / S3_TEST_REGION /
S3_TEST_PATH_STYLE override the defaults for other endpoints.
The serve gate (Files.authCallable)
GET /files/serve/{id} calls authCallable($file) with the ledger row before it reads a byte. The plugin has no
auth of its own: your callable decides, and refuses by throwing (a RedirectException to your login page, a
ForbiddenException, whatever fits). Returning normally allows the download. Collections with 'public' => true skip
the callable.
Two details that bite: build the login URL with 'plugin' => false (the closure runs inside the plugin's request), and
re-check that the account still exists, not just that a session is present, or a deleted user keeps downloading.
Use
put()sniffs the MIME from the bytes (never the client's declared type), checks the collection's allow-list and size, derives the file extension from the verified MIME, names the file with random hex (no client input reaches a path), keeps the original filename in the ledger, and saves the row before writing the file, so a failed write leaves a visible "file missing" row instead of an orphan binary.- Link to a file by building the route, never by a disk path:
$this->Url->build(['plugin' => 'TheMusicDev/Files', 'controller' => 'Dl', 'action' => 'serve', $id]). StoredFilesTablehasfind('active'),find('trashed')andcollectionCounts(). Soft delete is a plaindeletedcolumn (not muffin/trash), written byFileStore::delete().- Trash keeps files; only
purge()removes the binary. If your own table referencesstored_files, decide its foreign key accordingly (ON DELETE SET NULLkeeps the referencing row when a file is purged).
Host your own admin screens
The plugin ships storage, the ledger and the serving route, not admin screens: branding, auth and layout belong to
the site. examples/ has a working admin file browser (controller, template, routes: list collections,
paginated file list, upload, soft delete) to copy and restyle. It is not autoloaded. Whatever gate you put on those
screens, put the same check in Files.authCallable.
Gotchas
- Files are never served from disk paths; always through the route (the storage root is not in webroot).
FileStorecaches its Flysystem operator per backend definition (key plus the definition JSON), so repointing a key at another root, as tests do, builds a fresh operator.- Cake 5's
Responsehas nowithStreamedBody(); the serve action wraps the stream in a PSR-7 body. - A host that registers
Files.collectionsorFiles.backendsreplaces the plugin default for the same key only; a missinguploads.miscis filled in from the defaults. put()reads the whole upload into memory, bounded by the collection size cap; stream to disk if you ever allow more than about 100 MB.- There is no orphan-scan command yet (design item D, deferred until real data exists).
Development
Tests run on MariaDB, never sqlite. Override the connection with DATABASE_TEST_URL. Conventions shared by every
TheMusicDev plugin (naming, layout, CI, workflow) live in
TheMusicDev/cakephp-conventions.
License
MIT, see LICENSE.
All versions of files with dependencies
ext-fileinfo Version *
cakephp/cakephp Version ^5.2
league/flysystem Version ^3.0
league/flysystem-local Version ^3.0