aboutsummaryrefslogtreecommitdiffhomepage
path: root/Digitigrade/StorageProvider/FilesystemStorage.php
diff options
context:
space:
mode:
Diffstat (limited to 'Digitigrade/StorageProvider/FilesystemStorage.php')
-rw-r--r--Digitigrade/StorageProvider/FilesystemStorage.php52
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