mirror of
https://github.com/wintercms/winter.git
synced 2024-06-28 05:33:29 +02:00
Fixes #3332. FileUpload widget uploads file to the disk specified by default in config/filesystem.php instead of storage.uploads.disk in config/cms.php, if we use System\Models\File following the instruction in here. Although we can still create another class extending System\Models\File or October\Rain\Database\Attach\File and use it as the model for attachOne/Many relation, System\Models\File seems to be the one that responsible to look at storage.uploads.disk in config/cms.php, because the existing methods are using storage.uploads.*. Credit to @pikanji
81 lines
1.9 KiB
PHP
81 lines
1.9 KiB
PHP
<?php namespace System\Models;
|
|
|
|
use Url;
|
|
use Config;
|
|
use File as FileHelper;
|
|
use Storage;
|
|
use October\Rain\Database\Attach\File as FileBase;
|
|
|
|
/**
|
|
* 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';
|
|
|
|
/**
|
|
* If working with local storage, determine the absolute local path.
|
|
*/
|
|
protected function getLocalRootPath()
|
|
{
|
|
return Config::get('filesystems.disks.local.root', storage_path('app'));
|
|
}
|
|
|
|
/**
|
|
* Define the public address for the storage path.
|
|
*/
|
|
public function getPublicPath()
|
|
{
|
|
$uploadsPath = Config::get('cms.storage.uploads.path', '/storage/app/uploads');
|
|
|
|
if ($this->isPublic()) {
|
|
$uploadsPath .= '/public';
|
|
}
|
|
else {
|
|
$uploadsPath .= '/protected';
|
|
}
|
|
|
|
return Url::asset($uploadsPath) . '/';
|
|
}
|
|
|
|
/**
|
|
* Define the internal storage path.
|
|
*/
|
|
public function getStorageDirectory()
|
|
{
|
|
$uploadsFolder = Config::get('cms.storage.uploads.folder');
|
|
|
|
if ($this->isPublic()) {
|
|
return $uploadsFolder . '/public/';
|
|
}
|
|
else {
|
|
return $uploadsFolder . '/protected/';
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 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';
|
|
}
|
|
|
|
/**
|
|
* Copy the local file to Storage
|
|
* @return bool True on success, false on failure.
|
|
*/
|
|
protected function copyLocalToStorage($localPath, $storagePath)
|
|
{
|
|
$disk = Storage::disk(Config::get('cms.storage.uploads.disk'));
|
|
return $disk->put($storagePath, FileHelper::get($localPath), ($this->isPublic()) ? 'public' : null);
|
|
}
|
|
}
|