2014-05-14 23:24:20 +10:00
|
|
|
<?php namespace System\Models;
|
|
|
|
|
2017-02-18 12:23:31 +11:00
|
|
|
use Url;
|
2014-05-14 23:24:20 +10:00
|
|
|
use Config;
|
2018-01-02 08:04:47 +09:00
|
|
|
use File as FileHelper;
|
|
|
|
use Storage;
|
2014-05-14 23:24:20 +10:00
|
|
|
use October\Rain\Database\Attach\File as FileBase;
|
2019-01-16 12:29:28 -06:00
|
|
|
use Backend\Controllers\Files;
|
2014-05-14 23:24:20 +10:00
|
|
|
|
|
|
|
/**
|
|
|
|
* File attachment model
|
|
|
|
*
|
|
|
|
* @package october\system
|
|
|
|
* @author Alexey Bobkov, Samuel Georges
|
|
|
|
*/
|
|
|
|
class File extends FileBase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var string The database table used by the model.
|
|
|
|
*/
|
|
|
|
protected $table = 'system_files';
|
|
|
|
|
2019-01-15 14:25:26 -06:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function getThumb($width, $height, $options = [])
|
|
|
|
{
|
|
|
|
$url = '';
|
2019-01-16 12:29:28 -06:00
|
|
|
if (!$this->isPublic() && class_exists(Files::class)) {
|
2019-06-12 09:15:19 +03:00
|
|
|
$options = $this->getDefaultThumbOptions($options);
|
2019-01-15 15:11:29 -06:00
|
|
|
// Ensure that the thumb exists first
|
|
|
|
parent::getThumb($width, $height, $options);
|
|
|
|
|
|
|
|
// Return the Files controller handler for the URL
|
2019-01-16 12:29:28 -06:00
|
|
|
$url = Files::getThumbUrl($this, $width, $height, $options);
|
2019-01-15 14:25:26 -06:00
|
|
|
} else {
|
|
|
|
$url = parent::getThumb($width, $height, $options);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $url;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
2019-05-30 17:47:01 -06:00
|
|
|
public function getPath($fileName = null)
|
2019-01-15 14:25:26 -06:00
|
|
|
{
|
|
|
|
$url = '';
|
2019-01-16 12:29:28 -06:00
|
|
|
if (!$this->isPublic() && class_exists(Files::class)) {
|
|
|
|
$url = Files::getDownloadUrl($this);
|
2019-01-15 14:25:26 -06:00
|
|
|
} else {
|
2019-05-30 17:47:01 -06:00
|
|
|
$url = parent::getPath($fileName);
|
2019-01-15 14:25:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return $url;
|
|
|
|
}
|
|
|
|
|
2015-02-22 14:04:07 +11:00
|
|
|
/**
|
|
|
|
* If working with local storage, determine the absolute local path.
|
|
|
|
*/
|
|
|
|
protected function getLocalRootPath()
|
|
|
|
{
|
2016-01-15 10:07:39 +01:00
|
|
|
return Config::get('filesystems.disks.local.root', storage_path('app'));
|
2015-02-22 14:04:07 +11:00
|
|
|
}
|
2014-05-14 23:24:20 +10:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Define the public address for the storage path.
|
|
|
|
*/
|
2015-02-22 14:04:07 +11:00
|
|
|
public function getPublicPath()
|
2014-05-14 23:24:20 +10:00
|
|
|
{
|
2015-02-23 19:55:41 +11:00
|
|
|
$uploadsPath = Config::get('cms.storage.uploads.path', '/storage/app/uploads');
|
2015-02-17 20:58:38 +11:00
|
|
|
|
2014-10-18 11:58:50 +02:00
|
|
|
if ($this->isPublic()) {
|
2017-02-18 12:23:31 +11:00
|
|
|
$uploadsPath .= '/public';
|
2014-11-01 12:00:45 +11:00
|
|
|
}
|
|
|
|
else {
|
2017-02-18 12:23:31 +11:00
|
|
|
$uploadsPath .= '/protected';
|
2014-10-18 11:58:50 +02:00
|
|
|
}
|
2017-02-18 12:23:31 +11:00
|
|
|
|
|
|
|
return Url::asset($uploadsPath) . '/';
|
2014-05-14 23:24:20 +10:00
|
|
|
}
|
2017-02-18 12:23:31 +11:00
|
|
|
|
2015-08-18 15:42:26 +02:00
|
|
|
/**
|
|
|
|
* Define the internal storage path.
|
|
|
|
*/
|
|
|
|
public function getStorageDirectory()
|
|
|
|
{
|
|
|
|
$uploadsFolder = Config::get('cms.storage.uploads.folder');
|
|
|
|
|
|
|
|
if ($this->isPublic()) {
|
|
|
|
return $uploadsFolder . '/public/';
|
|
|
|
}
|
2018-08-15 19:25:42 +02:00
|
|
|
|
|
|
|
return $uploadsFolder . '/protected/';
|
2015-08-18 15:42:26 +02:00
|
|
|
}
|
2018-01-02 08:04:47 +09:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if storage.uploads.disk in config/cms.php is "local".
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected function isLocalStorage()
|
|
|
|
{
|
|
|
|
return Config::get('cms.storage.uploads.disk') == 'local';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-05-30 17:47:01 -06:00
|
|
|
* Returns the storage disk the file is stored on
|
|
|
|
* @return FilesystemAdapter
|
2018-01-02 08:04:47 +09:00
|
|
|
*/
|
2019-05-30 17:47:01 -06:00
|
|
|
public function getDisk()
|
2018-01-02 08:04:47 +09:00
|
|
|
{
|
2019-05-30 17:47:01 -06:00
|
|
|
return Storage::disk(Config::get('cms.storage.uploads.disk'));
|
2018-01-02 08:04:47 +09:00
|
|
|
}
|
2014-05-14 23:24:20 +10:00
|
|
|
}
|