diff options
| author | winter | 2025-01-18 00:22:36 +0000 |
|---|---|---|
| committer | winter | 2025-01-18 00:22:36 +0000 |
| commit | ba61c6435b668375a411716f3a35447aa38b03a4 (patch) | |
| tree | c305f2a994888a773d2b4a781176b8dffbe90576 /Digitigrade/StorageProvider | |
| parent | 141f11ba980fe019c12f0a8fa456f520ec48e522 (diff) | |
implement object storage
Diffstat (limited to 'Digitigrade/StorageProvider')
| -rw-r--r-- | Digitigrade/StorageProvider/FilesystemStorage.php | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/Digitigrade/StorageProvider/FilesystemStorage.php b/Digitigrade/StorageProvider/FilesystemStorage.php new file mode 100644 index 0000000..8f67653 --- /dev/null +++ b/Digitigrade/StorageProvider/FilesystemStorage.php @@ -0,0 +1,52 @@ +<?php +namespace Digitigrade\StorageProvider; + +use Digitigrade\StorageProvider; + +class FilesystemStorage extends StorageProvider { + private const BASE_PATH = __DIR__ . '/../../upload/'; + + private static function tokenToPath(string $token): string { + return self::BASE_PATH . $token; + } + + protected function storeFileInternal(string $path): ?string { + $token = hash_file('sha256', $path); + rename($path, self::tokenToPath($token)); + return $token; + } + + public function exists(string $token): bool { + return is_file(self::tokenToPath($token)); + } + + public function retrieve(string $token): ?string { + $path = self::tokenToPath($token); + if (!is_readable($path)) { + return null; + } + return file_get_contents($path); + } + + public function directUrl(string $token): ?string { + return path_to_uri("/upload/$token"); + } + + public function passthrough(string $token, ?string $mimetype = null) { + if (!$this->exists($token)) { + throw new \RuntimeException('requested object does not exist'); + } + $path = self::tokenToPath($token); + if (!headers_sent()) { + header('Content-Type: ' . ($mimetype ?? (mime_content_type($path) ?: 'application/octet-stream'))); + } + readfile($path); + } + + public function delete(string $token) { + $path = self::tokenToPath($token); + if (is_file($path)) { + unlink($path); + } + } +}
\ No newline at end of file |
