125 lines
3.2 KiB
PHP
125 lines
3.2 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Простой слой загрузки файлов на сервер - привязка к DBObject.php
|
|
* Версия 2018-01-14
|
|
* (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',
|
|
];
|
|
|
|
protected function get_disk_path()
|
|
{
|
|
return FileHandler::getPath(false, $this->data);
|
|
}
|
|
|
|
protected function get_raw_url()
|
|
{
|
|
return FileHandler::getPath(true, $this->data);
|
|
}
|
|
|
|
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_url()
|
|
{
|
|
return App::url('api', [ 'action' => 'Files.thumb', 'sha1' => $this->data['sha1'] ]);
|
|
}
|
|
|
|
protected function get_gps()
|
|
{
|
|
return FileHandler::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);
|
|
}
|
|
|
|
public function cropThumb($width, $height, $alignY = 0.5)
|
|
{
|
|
return FileHandler::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);
|
|
}
|
|
|
|
public function cropXThumb($max_width, $height)
|
|
{
|
|
return FileHandler::getThumb($this->data, $max_width, $height, false, self::CROP_X);
|
|
}
|
|
|
|
public static function upload(LocalFile $localFile, $allowedFormats = File::ANYTHING)
|
|
{
|
|
$file = new File();
|
|
$file->data = FileHandler::upload($localFile, $allowedFormats);
|
|
return $file->data ? $file : NULL;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
public function delete()
|
|
{
|
|
return FileHandler::deleteFiles([ 'id' => $this->data['id'] ]);
|
|
}
|
|
|
|
public static function newFromRow($row)
|
|
{
|
|
$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');
|
|
}
|
|
}
|