php-file-layer/File.php

100 lines
2.7 KiB
PHP

<?php
/**
* Simple file upload layer. Handles file metadata and storage
* Version 2020-01-01
* (c) Vitaliy Filippov 2018+
*/
class File
{
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 function getDiskPath($file)
{
return FileHandler::getPath(false, $file);
}
public static function getUrl($file)
{
return FileHandler::getPath(true, $file);
}
public static function getThumbPath($file, $type)
{
return FileHandler::getThumbPath(false, $file, $type);
}
public static function getGPS($file)
{
return FileHandler::getGPS($file);
}
public static function getSizeString($file, $lang = 'ru')
{
return FileUtils::sizeString($file['size'], $lang);
}
public static function getThumb($file, $width, $height, $force = false, $crop = false, $alignY = 0.5)
{
return FileHandler::getThumb($file, $width, $height, $force, $crop, $alignY);
}
public static function cropThumb($file, $width, $height, $alignY = 0.5)
{
return FileHandler::getThumb($file, $width, $height, false, self::CROP_XY, $alignY);
}
public static function cropYThumb($file, $width, $max_height, $alignY = 0.5)
{
return FileHandler::getThumb($file, $width, $max_height, false, self::CROP_Y, $alignY);
}
public static function cropXThumb($file, $max_width, $height)
{
return FileHandler::getThumb($file, $max_width, $height, false, self::CROP_X);
}
protected static function doUpload($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(File::$table, [
'props' => json_encode($row['props'], JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES),
'added' => time(),
] + $row);
return $row;
}
public static function upload(LocalFile $localFile, $allowedFormats = File::ANYTHING)
{
return self::doUpload(FileHandler::upload($localFile, $allowedFormats));
}
public static function uploadUrl($url, $allowedFormats = File::ONLY_IMAGES, $curl_options = [])
{
return self::doUpload(FileHandler::uploadUrl($url, $allowedFormats, $curl_options));
}
public static function deleteFiles($where)
{
return FileHandler::deleteFiles($where);
}
}