php-file-layer/DBObjectFile.php

150 lines
4.0 KiB
PHP

<?php
/**
* Простой слой загрузки файлов на сервер - привязка к DBObject.php
* Версия 2020-01-01
* (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',
];
public static $handler;
protected function get_disk_path()
{
return File::$handler->getPath(false, $this->data);
}
protected function get_url()
{
return File::$handler->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_gps()
{
return File::$handler->getGPS($this->data);
}
public function getThumb($width, $height, $force = false, $crop = false, $alignY = 0.5)
{
return File::$handler->getThumb($this->data, $width, $height, $force, $crop, $alignY);
}
public function cropThumb($width, $height, $alignY = 0.5)
{
return File::$handler->getThumb($this->data, $width, $height, false, self::CROP_XY, $alignY);
}
public function cropYThumb($width, $max_height, $alignY = 0.5)
{
return File::$handler->getThumb($this->data, $width, $max_height, false, self::CROP_Y, $alignY);
}
public function cropXThumb($max_width, $height)
{
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)
{
return File::doUpload(File::getHandler()->upload($localFile, $allowedFormats));
}
public static function uploadUrl($url, $allowedFormats = File::ONLY_IMAGES, $curl_options = [])
{
return File::doUpload(File::getHandler()->uploadUrl($url, $allowedFormats, $curl_options));
}
public function delete()
{
return File::$handler->deleteFiles([ 'id' => $this->data['id'] ]);
}
public static function newFromRow($row, $noCache = 0)
{
File::getHandler();
$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');
}
}