--- /dev/null
+<?php
+
+namespace App\Http\Controllers\Admin\Operations\Files;
+
+
+use App\Models\File;
+use App\Models\Quiz;
+use App\Models\QuizTranslation;
+use Cubist\Util\Files\Files;
+use Illuminate\Http\UploadedFile;
+use Illuminate\Support\Facades\Route;
+use Prologue\Alerts\Facades\Alert;
+use ZipArchive;
+
+trait UploadOperation
+{
+ protected function setupImportRoutes($segment, $routeName, $controller)
+ {
+ Route::match(['post'], $segment . '/upload', $controller . '@upload');
+ }
+
+ protected function setupImportDefaults()
+ {
+ $this->crud->addButtonFromView('top', 'import', 'files.upload', 'end');
+ }
+
+ protected function upload()
+ {
+ /** @var UploadedFile[] $files */
+ $files = request()->allFiles()['file'];
+
+ if (!count($files)) {
+ Alert::warning(__('Aucun fichier chargé'))->flash();
+ return;
+ }
+
+ $j = 0;
+ foreach ($files as $file) {
+ File::importUploadedFile($file);
+ $j++;
+ }
+
+ if ($j === 0) {
+ Alert::warning(__('Aucun fichier chargé'))->flash();
+ } else {
+ Alert::success(__(':nb fichiers chargés', ['nb' => '<b>' . $j . '</b>']))->flash();
+ }
+ return redirect($this->crud->route);
+ }
+}
namespace App\Models;
use App\Http\Controllers\Admin\Operations\Files\DownloadOperation;
+use App\Http\Controllers\Admin\Operations\Files\UploadOperation;
use App\Models\Base\ToolboxModel;
use Cubist\Backpack\CubistBackpackServiceProvider;
use Cubist\Backpack\Magic\Fields\Date;
use Cubist\Backpack\Magic\Traits\CustomDataSource;
use Cubist\Util\Files\Files;
use Illuminate\Database\Eloquent\Builder;
+use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Auth;
-use Sushi\Sushi;
+use Illuminate\Support\Facades\Cache;
class File extends ToolboxModel
{
protected $_enableEdition = false;
protected $_enableClone = false;
+ protected $_enableCreation = false;
protected $primaryKey = 'hash';
public $incrementing = false;
protected $keyType = 'string';
- protected $_operations = [DownloadOperation::class];
+ protected $_operations = [DownloadOperation::class, UploadOperation::class];
- public static $basePath = '/application/ftp/';
+ public static string $basePath = '/application/ftp/';
public function setFields()
{
parent::setFields();
- //$this->addField('hash', Text::class, __('Hash'), ['column' => false, 'database_index' => true]);
$this->addField('name', Text::class, __('Nom du fichier'), ['column' => true]);
$this->addField('path', Hidden::class, __('Chemin'));
$this->addField('ext', Text::class, __('Type'), ['column' => true]);
$this->addOwnerField(['column' => false]);
}
- protected function _getData()
+ protected static function _getData()
{
/** @var User $user */
$users = User::withoutGlobalScopes()->get();
$files = [];
foreach ($users as $user) {
- $this->_listFilesOfUser($user->id, $files);
+ static::_listFilesOfUser($user->id, $files);
}
return $files;
}
- protected function _listFilesOfUser($id, &$files)
+ protected static function _cacheKeyUserFolder($userId)
{
- $uFiles = cache()->remember('files_list__' . $id, 86400, function () use ($id) {
- return $this->___listFilesOfUser($id);
+ return 'files_list__' . $userId;
+ }
+
+ protected static function _listFilesOfUser($id, &$files)
+ {
+ $uFiles = cache()->remember(static::_cacheKeyUserFolder($id), 86400, function () use ($id) {
+ return static::___listFilesOfUser($id);
});
foreach ($uFiles as $hash => $uFile) {
}
}
- protected function ___listFilesOfUser($id)
+ protected static function ___listFilesOfUser($id)
{
-
- $path = self::$basePath . $id;
+ $path = static::$basePath . $id;
if (!file_exists($path) || !is_dir($path)) {
return [];
}
echo "Delete " . $f->path . "\n";
$f->delete();
}
+ static::refreshDatabase(true);
}
public function onDeleting(): bool
{
$res = parent::onDeleting();
- unlink($this->path);
- self::touchChangeFile();
-
+ if (file_exists($this->path)) {
+ unlink($this->path);
+ }
+ Cache::forget(self::_cacheKeyUserFolder($this->owner));
+ static::touchChangeFile();
return $res;
}
- public static function touchChangeFile()
+ /**
+ * @param $file UploadedFile
+ * @return void
+ */
+ public static function importUploadedFile($file)
{
- touch(self::$basePath . '__LAST_CHANGE');
+ $userId = backpack_user()->id;
+ $dest = Files::mkdir(self::$basePath . '/' . $userId) . Files::tidyName($file->getClientOriginalName());
+ move_uploaded_file($file->getPathname(), $dest);
+ Cache::forget(self::_cacheKeyUserFolder($userId));
+ static::refreshDatabase(true);
}
-
public static function hash($pathname)
{
return sha1('!!//' . trim(str_replace(self::$basePath, '', $pathname), '/') . '||--');