blob: 8f67653bf7a3be8fea52c1ef7e2d9fcefbc880b1 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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);
}
}
}
|