aboutsummaryrefslogtreecommitdiffhomepage
path: root/Digitigrade/Model/StorageObject.php
diff options
context:
space:
mode:
Diffstat (limited to 'Digitigrade/Model/StorageObject.php')
-rw-r--r--Digitigrade/Model/StorageObject.php107
1 files changed, 107 insertions, 0 deletions
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 @@
+<?php
+namespace Digitigrade\Model;
+
+use Digitigrade\Model;
+use Digitigrade\StorageProvider;
+
+class StorageObject extends Model {
+ public string $oid;
+ public string $provider;
+ public string $token;
+ public string $filename;
+ public string $mimetype;
+ public \DateTimeImmutable $created;
+
+ /**
+ * Creates a new storage object on the given provider by consuming a local file
+ * @param string $provider class name of the storage provider to use
+ * @param string $path path to local file (will be deleted)
+ * @param bool $checkIsUpload if true, ensures that the local file was uploaded via HTTP
+ * @return self
+ */
+ public static function createFromFile(string $provider, string $path, string $filename, bool $checkIsUpload = false): self {
+ if (!is_file($path)) {
+ throw new \RuntimeException('local file does not exist');
+ }
+ $mimetype = mime_content_type($path);
+ $p = new $provider();
+ assert($p instanceof StorageProvider);
+ $token = $p->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