Add format tag upon upload
This commit is contained in:
parent
463bd267a8
commit
1b544280d9
41
item.php
41
item.php
|
@ -2,7 +2,16 @@
|
||||||
|
|
||||||
class Item implements JsonSerializable
|
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
|
// TODO: Change to id here in all occurrences
|
||||||
|
//
|
||||||
|
|
||||||
private string $hash;
|
private string $hash;
|
||||||
private string $extension;
|
private string $extension;
|
||||||
private array $tags = [];
|
private array $tags = [];
|
||||||
|
@ -46,18 +55,20 @@ class Item implements JsonSerializable
|
||||||
Json::error('Trying to upload illegal or non-existent file')->die();
|
Json::error('Trying to upload illegal or non-existent file')->die();
|
||||||
|
|
||||||
// --- INITIALIZE ---
|
// --- INITIALIZE ---
|
||||||
$ret = new Item();
|
$item = new Item();
|
||||||
$ret->hash = hash_file('sha256', $from_path);
|
$item->hash = hash_file('sha256', $from_path);
|
||||||
$ret->extensionFromUpload($php_file);
|
$item->extensionFromUpload($php_file);
|
||||||
|
|
||||||
// --- ACTUALLY GRAB FILE ---
|
// --- ACTUALLY GRAB FILE ---
|
||||||
$new_path = $ret->getPath();
|
$new_path = $item->getPath();
|
||||||
if (file_exists($new_path))
|
if (file_exists($new_path))
|
||||||
Json::error('File already exists')->die();
|
Json::error('File already exists')->die();
|
||||||
if (!move_uploaded_file($from_path, $new_path))
|
if (!move_uploaded_file($from_path, $new_path))
|
||||||
Json::error('Failed to move uploaded file')->die();
|
Json::error('Failed to move uploaded file')->die();
|
||||||
|
|
||||||
return $ret;
|
MediaDB::add($item);
|
||||||
|
|
||||||
|
return $item;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function extensionFromTags()
|
private function extensionFromTags()
|
||||||
|
@ -69,6 +80,8 @@ class Item implements JsonSerializable
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Json::error('Detected file without file format tag')->die();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Assumes is_uploaded_file has been done
|
// 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
|
// 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?
|
// do somethign like ['mime/type', null] be equivalent to this?
|
||||||
if (array_search($ext, ['webp', 'png', 'jpeg']) !== false) {
|
if (array_search($ext, ['webp', 'png', 'jpeg']) !== false) {
|
||||||
$this->extension = $ext;
|
goto success;
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// If it doesn't work, try to figure it out from the original extension
|
// 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);
|
$ext = pathinfo($name, PATHINFO_EXTENSION);
|
||||||
|
|
||||||
if (array_search([$mime, $ext], $whitelist) !== false) {
|
if (array_search([$mime, $ext], $whitelist) !== false) {
|
||||||
$this->extension = $ext;
|
goto success;
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Json::error('File mime-type and or extension not allowed.')->die();
|
Json::error('File mime-type and or extension not allowed.')->die();
|
||||||
|
|
||||||
|
success:
|
||||||
|
$this->extension = $ext;
|
||||||
|
$this->tags[] = 'format:' . $ext;
|
||||||
|
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getHash(): string
|
public function getHash(): string
|
||||||
{
|
{
|
||||||
return $this->hash;
|
return $this->hash;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getExtension(): string
|
public function getExtension(): string
|
||||||
{
|
{
|
||||||
return $this->extension;
|
return $this->extension;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getTags(): array
|
||||||
|
{
|
||||||
|
return $this->tags;
|
||||||
|
}
|
||||||
|
|
||||||
public function getPath(bool $absolute = false): string
|
public function getPath(bool $absolute = false): string
|
||||||
{
|
{
|
||||||
return sprintf(
|
return sprintf(
|
||||||
|
|
2
main.php
2
main.php
|
@ -45,6 +45,7 @@ function get_item()
|
||||||
return $item->getJson();
|
return $item->getJson();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function post_item()
|
function post_item()
|
||||||
{
|
{
|
||||||
// The checks are performed internally
|
// The checks are performed internally
|
||||||
|
@ -53,6 +54,7 @@ function post_item()
|
||||||
return $item->getJson();
|
return $item->getJson();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
function get_search()
|
function get_search()
|
||||||
{
|
{
|
||||||
$query = $_GET['q'] ?? '';
|
$query = $_GET['q'] ?? '';
|
||||||
|
|
25
mediadb.php
25
mediadb.php
|
@ -5,6 +5,11 @@ class MediaDB
|
||||||
private $handler;
|
private $handler;
|
||||||
private array $lines = [];
|
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()
|
public function __construct()
|
||||||
{
|
{
|
||||||
// Just initialize the file handler
|
// Just initialize the file handler
|
||||||
|
@ -22,6 +27,22 @@ class MediaDB
|
||||||
return $ret;
|
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)
|
public function map(callable $func)
|
||||||
{
|
{
|
||||||
// First read from what's already in memory
|
// First read from what's already in memory
|
||||||
|
@ -29,8 +50,8 @@ class MediaDB
|
||||||
if ($func($i_line) === true) return;
|
if ($func($i_line) === true) return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// If we run out, read from the file and append to array so the next lookup
|
// If we run out, read from the file and append to array so the next
|
||||||
// is fast
|
// lookup is fast
|
||||||
while ($line = $this->getLine()) {
|
while ($line = $this->getLine()) {
|
||||||
$this->lines[] = $line;
|
$this->lines[] = $line;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue