2018-09-09 15:16:31 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Простой слой загрузки файлов на сервер - привязка к DBObject.php
|
2020-01-01 01:53:06 +03:00
|
|
|
* Версия 2020-01-01
|
2018-09-09 15:16:31 +03:00
|
|
|
* (c) Виталий Филиппов 2018
|
|
|
|
*/
|
|
|
|
|
|
|
|
class File extends DBObject
|
|
|
|
{
|
|
|
|
const ONLY_BINARY = 1;
|
|
|
|
const ONLY_IMAGES = 2;
|
|
|
|
const ONLY_SWF = 4;
|
|
|
|
const ONLY_VIDEO = 8;
|
|
|
|
const IMAGES_VIDEO = 10;
|
|
|
|
const ANY_MEDIA = 14;
|
|
|
|
const ANYTHING = 15;
|
|
|
|
|
|
|
|
const CROP_XY = 1;
|
|
|
|
const CROP_Y = 2;
|
|
|
|
const CROP_X = 3;
|
|
|
|
|
|
|
|
public static $table = 'files';
|
|
|
|
public static $clean = [], $dirty = [];
|
|
|
|
public static $fields = [
|
|
|
|
'id' => false,
|
|
|
|
'user_id' => false,
|
|
|
|
'sha1' => false,
|
|
|
|
'format' => false,
|
|
|
|
'mimetype' => false,
|
|
|
|
'size' => true,
|
|
|
|
'width' => true,
|
|
|
|
'height' => true,
|
|
|
|
'added' => true,
|
|
|
|
'props' => true,
|
|
|
|
];
|
|
|
|
public static $joins = [
|
|
|
|
'user' => 'User',
|
|
|
|
];
|
2020-01-01 01:53:06 +03:00
|
|
|
public static $handler;
|
2018-09-09 15:16:31 +03:00
|
|
|
|
|
|
|
protected function get_disk_path()
|
|
|
|
{
|
2020-01-01 01:53:06 +03:00
|
|
|
return File::$handler->getPath(false, $this->data);
|
2018-09-09 15:16:31 +03:00
|
|
|
}
|
|
|
|
|
2020-01-01 01:53:06 +03:00
|
|
|
protected function get_url()
|
2018-09-09 15:16:31 +03:00
|
|
|
{
|
2020-01-01 01:53:06 +03:00
|
|
|
return File::$handler->getPath(true, $this->data);
|
2018-09-09 15:16:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function get_fsize_ru()
|
|
|
|
{
|
|
|
|
return FileUtils::sizeString($this->data['size'], 'ru');
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function get_fsize_en()
|
|
|
|
{
|
|
|
|
return FileUtils::sizeString($this->data['size'], 'en');
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function get_gps()
|
|
|
|
{
|
2020-01-01 01:53:06 +03:00
|
|
|
return File::$handler->getGPS($this->data);
|
2018-09-09 15:16:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getThumb($width, $height, $force = false, $crop = false, $alignY = 0.5)
|
|
|
|
{
|
2020-01-01 01:53:06 +03:00
|
|
|
return File::$handler->getThumb($this->data, $width, $height, $force, $crop, $alignY);
|
2018-09-09 15:16:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function cropThumb($width, $height, $alignY = 0.5)
|
|
|
|
{
|
2020-01-01 01:53:06 +03:00
|
|
|
return File::$handler->getThumb($this->data, $width, $height, false, self::CROP_XY, $alignY);
|
2018-09-09 15:16:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function cropYThumb($width, $max_height, $alignY = 0.5)
|
|
|
|
{
|
2020-01-01 01:53:06 +03:00
|
|
|
return File::$handler->getThumb($this->data, $width, $max_height, false, self::CROP_Y, $alignY);
|
2018-09-09 15:16:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function cropXThumb($max_width, $height)
|
|
|
|
{
|
2020-01-01 01:53:06 +03:00
|
|
|
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;
|
2018-09-09 15:16:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function upload(LocalFile $localFile, $allowedFormats = File::ANYTHING)
|
|
|
|
{
|
2020-01-01 01:53:06 +03:00
|
|
|
return File::doUpload(File::getHandler()->upload($localFile, $allowedFormats));
|
2018-09-09 15:16:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function uploadUrl($url, $allowedFormats = File::ONLY_IMAGES, $curl_options = [])
|
|
|
|
{
|
2020-01-01 01:53:06 +03:00
|
|
|
return File::doUpload(File::getHandler()->uploadUrl($url, $allowedFormats, $curl_options));
|
2018-09-09 15:16:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function delete()
|
|
|
|
{
|
2020-01-01 01:53:06 +03:00
|
|
|
return File::$handler->deleteFiles([ 'id' => $this->data['id'] ]);
|
2018-09-09 15:16:31 +03:00
|
|
|
}
|
|
|
|
|
2020-01-01 01:53:06 +03:00
|
|
|
public static function newFromRow($row, $noCache = 0)
|
2018-09-09 15:16:31 +03:00
|
|
|
{
|
2020-01-01 01:53:06 +03:00
|
|
|
File::getHandler();
|
2018-09-09 15:16:31 +03:00
|
|
|
$obj = parent::newFromRow($row);
|
|
|
|
if ($obj)
|
|
|
|
{
|
|
|
|
$obj->data['props'] = json_decode($obj->data['props'], true);
|
|
|
|
}
|
|
|
|
return $obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function saveMe()
|
|
|
|
{
|
|
|
|
throw new Exception('File objects are immutable');
|
|
|
|
}
|
|
|
|
}
|