Make DBObjectFile actually work O_o

master
Vitaliy Filippov 2020-01-01 01:53:06 +03:00
parent ea669d673f
commit 637c4c56df
2 changed files with 50 additions and 25 deletions

View File

@ -2,7 +2,7 @@
/**
* Простой слой загрузки файлов на сервер - привязка к DBObject.php
* Версия 2018-01-14
* Версия 2020-01-01
* (c) Виталий Филиппов 2018
*/
@ -37,15 +37,16 @@ class File extends DBObject
public static $joins = [
'user' => 'User',
];
public static $handler;
protected function get_disk_path()
{
return FileHandler::getPath(false, $this->data);
return File::$handler->getPath(false, $this->data);
}
protected function get_raw_url()
protected function get_url()
{
return FileHandler::getPath(true, $this->data);
return File::$handler->getPath(true, $this->data);
}
protected function get_fsize_ru()
@ -58,57 +59,81 @@ class File extends DBObject
return FileUtils::sizeString($this->data['size'], 'en');
}
protected function get_url()
{
return App::url('api', [ 'action' => 'Files.thumb', 'sha1' => $this->data['sha1'] ]);
}
protected function get_gps()
{
return FileHandler::getGPS($this->data);
return File::$handler->getGPS($this->data);
}
public function getThumb($width, $height, $force = false, $crop = false, $alignY = 0.5)
{
return FileHandler::getThumb($this->data, $width, $height, $force, $crop, $alignY);
return File::$handler->getThumb($this->data, $width, $height, $force, $crop, $alignY);
}
public function cropThumb($width, $height, $alignY = 0.5)
{
return FileHandler::getThumb($this->data, $width, $height, false, self::CROP_XY, $alignY);
return File::$handler->getThumb($this->data, $width, $height, false, self::CROP_XY, $alignY);
}
public function cropYThumb($width, $max_height, $alignY = 0.5)
{
return FileHandler::getThumb($this->data, $width, $max_height, false, self::CROP_Y, $alignY);
return File::$handler->getThumb($this->data, $width, $max_height, false, self::CROP_Y, $alignY);
}
public function cropXThumb($max_width, $height)
{
return FileHandler::getThumb($this->data, $max_width, $height, false, self::CROP_X);
return File::$handler->getThumb($this->data, $max_width, $height, false, self::CROP_X);
}
protected static function doUpload($row)
{
if (!$row)
{
return NULL;
}
$obj = File::selectRow(false, '*', [ 'sha1' => $row['sha1'] ]);
if ($obj)
{
return $obj;
}
$row['id'] = App::$db->insert_row(File::$table, [
'props' => json_encode($row['props'], JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES),
'user_id' => App::$user['id'] ?: NULL,
'added' => time(),
] + $row);
return File::newFromRow($row);
}
public static function getHandler()
{
if (!File::$handler)
{
File::$handler = new FileHandler([
'basedir' => App::$config['local_path'].'/files',
'baseurl' => App::domain().'/files/',
'mime_blacklist' => App::$config['mime_blacklist'],
]);
}
return File::$handler;
}
public static function upload(LocalFile $localFile, $allowedFormats = File::ANYTHING)
{
$file = new File();
$file->data = FileHandler::upload($localFile, $allowedFormats);
return $file->data ? $file : NULL;
return File::doUpload(File::getHandler()->upload($localFile, $allowedFormats));
}
public static function uploadUrl($url, $allowedFormats = File::ONLY_IMAGES, $curl_options = [])
{
$file = new File();
$file->data = FileHandler::uploadUrl($url, $allowedFormats, $curl_options);
return $file->data ? $file : NULL;
return File::doUpload(File::getHandler()->uploadUrl($url, $allowedFormats, $curl_options));
}
public function delete()
{
return FileHandler::deleteFiles([ 'id' => $this->data['id'] ]);
return File::$handler->deleteFiles([ 'id' => $this->data['id'] ]);
}
public static function newFromRow($row)
public static function newFromRow($row, $noCache = 0)
{
File::getHandler();
$obj = parent::newFromRow($row);
if ($obj)
{

View File

@ -2,7 +2,7 @@
/**
* Simple file upload layer. Handles file metadata and storage
* Version 2019-05-01
* Version 2020-01-01
* (c) Vitaliy Filippov 2018+
*/
@ -69,13 +69,13 @@ class File
protected static function doUpload($row)
{
$exist = App::$db->select($this->table, '*', [ 'sha1' => $row['sha1'] ], NULL, MS_ROW);
$exist = App::$db->select(File::$table, '*', [ 'sha1' => $row['sha1'] ], NULL, MS_ROW);
if ($exist)
{
$exist['props'] = json_decode($exist['props'], true);
return $exist;
}
$row['id'] = App::$db->insert_row($this->table, [
$row['id'] = App::$db->insert_row(File::$table, [
'props' => json_encode($row['props'], JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES),
'added' => time(),
] + $row);