diff --git a/item.php b/item.php index 136e334..e1c34a9 100644 --- a/item.php +++ b/item.php @@ -2,7 +2,16 @@ class Item implements JsonSerializable { + + // TODO: create a persist() method that updates the line if the offset where + // it originally existed in is given by 'ftell'. If it doesn't have an + // ID, then try to find it in the file and if it doesn't exist, append + // + // TODO: Maybe implement tags as an associative array or somethign? + // // TODO: Change to id here in all occurrences + // + private string $hash; private string $extension; private array $tags = []; @@ -46,18 +55,20 @@ class Item implements JsonSerializable Json::error('Trying to upload illegal or non-existent file')->die(); // --- INITIALIZE --- - $ret = new Item(); - $ret->hash = hash_file('sha256', $from_path); - $ret->extensionFromUpload($php_file); + $item = new Item(); + $item->hash = hash_file('sha256', $from_path); + $item->extensionFromUpload($php_file); // --- ACTUALLY GRAB FILE --- - $new_path = $ret->getPath(); + $new_path = $item->getPath(); if (file_exists($new_path)) Json::error('File already exists')->die(); if (!move_uploaded_file($from_path, $new_path)) Json::error('Failed to move uploaded file')->die(); - return $ret; + MediaDB::add($item); + + return $item; } private function extensionFromTags() @@ -69,6 +80,8 @@ class Item implements JsonSerializable return; } } + + Json::error('Detected file without file format tag')->die(); } // Assumes is_uploaded_file has been done @@ -83,8 +96,7 @@ class Item implements JsonSerializable // REVIEW: Maybe change it so it doesn't have to be separated like this but // do somethign like ['mime/type', null] be equivalent to this? if (array_search($ext, ['webp', 'png', 'jpeg']) !== false) { - $this->extension = $ext; - return; + goto success; } // If it doesn't work, try to figure it out from the original extension @@ -101,23 +113,34 @@ class Item implements JsonSerializable $ext = pathinfo($name, PATHINFO_EXTENSION); if (array_search([$mime, $ext], $whitelist) !== false) { - $this->extension = $ext; - return; + goto success; } } Json::error('File mime-type and or extension not allowed.')->die(); + + success: + $this->extension = $ext; + $this->tags[] = 'format:' . $ext; + + return; } public function getHash(): string { return $this->hash; } + public function getExtension(): string { return $this->extension; } + public function getTags(): array + { + return $this->tags; + } + public function getPath(bool $absolute = false): string { return sprintf( diff --git a/main.php b/main.php index 6340cf7..e2612e7 100644 --- a/main.php +++ b/main.php @@ -45,6 +45,7 @@ function get_item() return $item->getJson(); } + function post_item() { // The checks are performed internally @@ -53,6 +54,7 @@ function post_item() return $item->getJson(); }; + function get_search() { $query = $_GET['q'] ?? ''; diff --git a/mediadb.php b/mediadb.php index 1ef9759..2b3476a 100644 --- a/mediadb.php +++ b/mediadb.php @@ -5,6 +5,11 @@ class MediaDB private $handler; private array $lines = []; + // TODO: create a persist() method that updates the line if the offset where + // it originally existed in is given by 'ftell'. If it doesn't have + // an ID, then try to find it in the file and if it doesn't exist, + // append + public function __construct() { // Just initialize the file handler @@ -22,6 +27,22 @@ class MediaDB return $ret; } + public static function add(array|Item $item) + { + $file = fopen($GLOBALS['path_mediadb'], 'a+'); + if ($file === false) + Json::error('Failed opening media DB for adding new Item')->die(); + + $line = $item->getHash() . ' ' . implode(' ', $item->getTags()); + + if (fwrite($file, $line) == false) + Json::error('Write operation failed on Item adding')->die(); + + fclose($file); + + // The $this->list will be updated on trying to find new items + } + public function map(callable $func) { // First read from what's already in memory @@ -29,8 +50,8 @@ class MediaDB if ($func($i_line) === true) return; } - // If we run out, read from the file and append to array so the next lookup - // is fast + // If we run out, read from the file and append to array so the next + // lookup is fast while ($line = $this->getLine()) { $this->lines[] = $line;