From ba61c6435b668375a411716f3a35447aa38b03a4 Mon Sep 17 00:00:00 2001 From: winter Date: Sat, 18 Jan 2025 00:22:36 +0000 Subject: implement object storage --- Digitigrade/Model/StorageObject.php | 107 ++++++++++++++++++++++ Digitigrade/StorageProvider.php | 62 +++++++++++++ Digitigrade/StorageProvider/FilesystemStorage.php | 52 +++++++++++ 3 files changed, 221 insertions(+) create mode 100644 Digitigrade/Model/StorageObject.php create mode 100644 Digitigrade/StorageProvider.php create mode 100644 Digitigrade/StorageProvider/FilesystemStorage.php (limited to 'Digitigrade') diff --git a/Digitigrade/Model/StorageObject.php b/Digitigrade/Model/StorageObject.php new file mode 100644 index 0000000..333b5b1 --- /dev/null +++ b/Digitigrade/Model/StorageObject.php @@ -0,0 +1,107 @@ +storeFile($path, $checkIsUpload); + + $obj = new self(); + $obj->oid = random_printable_bytes(); + $obj->provider = $p::class; + $obj->token = $token; + $obj->filename = $filename; + $obj->mimetype = $mimetype; + $obj->created = new \DateTimeImmutable(); + + $obj->save(); + return $obj; + } + + public static function findByOid(string $oid): ?self { + return self::findWhere('oid = ?', [$oid]); + } + + public static function findByProviderTokenPair(string $provider, string $token): ?self { + return self::findWhere('provider = ? AND token = ?', [$provider, $token]); + } + + protected function getUpdateWhereClause(\PDO $db): ?string { + if (isset($this->oid) && self::findByOid($this->oid) != null) { + return 'oid = ' . $db->quote($this->oid); + } + if (self::findByProviderTokenPair($this->provider, $this->token) != null) { + return 'provider = ' . $db->quote($this->provider) . ' AND token = ' . $db->quote($this->token); + } + return null; + } + + private function getProvider(): StorageProvider { + return new $this->provider(); + } + + /** + * Deletes this object from the storage backend and removes it from the database. + * @return void + */ + public function delete() { + $this->getProvider()->delete($this->token); + $this->remove(); + } + + /** + * Gets the entire content of this object. + * @return string + */ + public function retrieve(): string { + return $this->getProvider()->retrieve($this->token); + } + + /** + * Streams the content of this object to the web client (stdout). + * @return void + */ + public function passthrough() { + $this->getProvider()->passthrough($this->token, $this->mimetype); + } + + /** + * Gets a direct public URL to this object, if one exists. + * @return ?string the URL or null if none is available + */ + public function getDirectUrl(): ?string { + return $this->getProvider()->directUrl($this->token); + } + + public function getUrl(bool $allowDirect = true): string { + if ($allowDirect) { + $url = $this->getDirectUrl(); + if ($url != null) { + return $url; + } + } + return path_to_uri("/storage/$this->oid"); + } +} \ No newline at end of file diff --git a/Digitigrade/StorageProvider.php b/Digitigrade/StorageProvider.php new file mode 100644 index 0000000..53b3549 --- /dev/null +++ b/Digitigrade/StorageProvider.php @@ -0,0 +1,62 @@ +storeFileInternal($path); + } + + abstract protected function storeFileInternal(string $path): ?string; + + /** + * @param string $token the token of the object to look for + * @return bool whether the requested object exists or not + */ + abstract public function exists(string $token): bool; + + /** + * Retrieves an object's contents from the storage backend. + * @param string $token the token of the object to retrieve + * @return ?string the entire content, or null if not found + */ + abstract public function retrieve(string $token): ?string; + + /** + * Gets a direct, public URL to the given object, if possible. + * @param string $token the token of the object to look up + * @return ?string the URL or null if one is not available + */ + abstract public function directUrl(string $token): ?string; + + /** + * Streams the data of an object to the web client (stdout). + * @param string $token the token of the object to retrieve + * @param ?string $mimetype value of the Content-Type header to send (if + * null, will try to figure it out anyway) + * @return void + */ + abstract public function passthrough(string $token, ?string $mimetype = null); + + /** + * Deletes an object from the backend. If the object doesn't exist, does + * nothing. + * @param string $token the token of the object to delete + * @return void + */ + abstract public function delete(string $token); +} \ No newline at end of file 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 @@ +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 -- cgit v1.3