Search by

bitshost / upmvc-files-pack

bitshost

Private file storage, MIME/size validation, and HMAC signed downloads for upMVC (works standalone or with upmvc-saas-pack; never stores uploads under public/).

Package info

github.com/upMVC/upMVC-Files-Pack

Homepage

pkg:composer/bitshost/upmvc-files-pack

Statistics

Installs: 1

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v0.1.0 2026-11-28 19:41 UTC

This package is auto-updated.

Last update: 2026-11-28 19:43:06 UTC


README

Private file storage, real MIME/size validation, and HMAC signed downloads for any upMVC (^2.8) application — not SaaS-only.

Works standalone AND in SaaS: bitshost/upmvc-files-pack depends only on bitshost/upmvc (^2.8) and ext-fileinfo. It does not require bitshost/upmvc-saas-pack and registers no HTTP routes. Use it in single-tenant apps (private docs, camera frames, exports) or in multi-tenant upMVC-SaaS apps (with automatic t_<tenantId>/ key prefix isolation).

  • Private by default — stores files under storage/ (never public/), with atomic writes and strict path-traversal rejection.
  • Real MIME detection — inspects actual file bytes via finfo (never trusts $_FILES['type']) and blocks executable extensions (.php, .phtml, .phar, .svg, .html, .sh, etc.) by default.
  • Stateless HMAC-SHA256 signed download tokens — short-lived, tamper-proof tokens verified with hash_equals.
  • Swappable storage adapter — StorageDriverInterface ships with LocalStorageDriver and is ready for S3/R2 adapters later.
  • Decoupled via upMVC 2.8 Events — emits files.stored and files.deleted.

Install

composer require bitshost/upmvc-files-pack

Usage

1. Store an uploaded file safely

use BitsHost\UpmvcFiles\FilePolicy;
use BitsHost\UpmvcFiles\FileVault;
use BitsHost\UpmvcFiles\LocalStorageDriver;

$vault = new FileVault(
    driver: new LocalStorageDriver(dirname(__DIR__) . '/storage/uploads'),
    policy: new FilePolicy(maxBytes: 5 * 1024 * 1024),
);

// Standalone (tenantId: null) or SaaS (tenantId: (int) $user['tenant_id'])
$stored = $vault->storeUpload($_FILES['document'], tenantId: null, prefix: 'invoices');
// Save $stored->key, $stored->originalName, $stored->mimeType, $stored->sha256 in your DB

2. Issue & verify an expiring download token

use BitsHost\UpmvcFiles\SignedDownloadToken;

$token = $vault->issueDownloadToken($stored->key, $appKey, ttlSeconds: 300, tenantId: 7);

// In your download controller:
$claim = SignedDownloadToken::verify($token, $appKey);
if ($claim === null) {
    http_response_code(403);
    exit;
}

$bytes = $vault->read($claim['key'], expectedTenantId: $claim['tenant_id']);

Local development

composer install
composer check   # PHPStan level 5 + PHPUnit