diff --git a/src/Controllers/Config/PbConfigController.php b/src/Controllers/Config/PbConfigController.php index 74ab5cf..7ee8379 100644 --- a/src/Controllers/Config/PbConfigController.php +++ b/src/Controllers/Config/PbConfigController.php @@ -15,6 +15,7 @@ use Auth; use DB; +use ReflectionException; use Session; use Inertia\Response as InertiaResponse; @@ -43,7 +44,7 @@ function __construct(Request $request, $crud_perms = false) * * @param string $route * @return InertiaResponse|JsonResponse - * @throws \ReflectionException + * @throws ReflectionException */ public function create(string $route = 'level'): InertiaResponse|JsonResponse { @@ -73,7 +74,7 @@ public function store(Request $request): Redirector|RedirectResponse|Application * @param bool $multiple * @param string $route * @return RedirectResponse|InertiaResponse|JsonResponse - * @throws \ReflectionException + * @throws ReflectionException */ public function edit( int $id, diff --git a/src/Controllers/File/PbFileController.php b/src/Controllers/File/PbFileController.php new file mode 100644 index 0000000..8d912f9 --- /dev/null +++ b/src/Controllers/File/PbFileController.php @@ -0,0 +1,210 @@ +varsObject([ + 'keys' => [ + 'level' => 'File' + ], + 'validationRules' => [ + 'name' => ['max:128'], + 'description' => [], + 'alt' => [], + ], + 'modelExclude' => ['file'], + 'showId' => true, + 'defaults' => [ + 'status' => 1, + ], + ]); + // Parent construct + parent::__construct($request, true); + } + + /** + * Store a newly created resource in storage. + * + * @param Request $request + * @return Application|Redirector|RedirectResponse|null + */ + public function store(Request $request): Redirector|RedirectResponse|Application|null + { + $this->pushValidationRules([ + 'file' => ['required', 'file', 'mimes:jpeg,png,jpg,gif,svg,pdf', 'max:8192'], + 'module' => ['nullable', 'max:32'], + 'permission' => ['required', 'max:32'], + ]); + + // Validation + if ($failed = $this->validateRequest($this->vars->validationRules, $request)) { + return $failed; + } + + $file = $request->file('file'); + + // Process + try { + // Build model + $model = resolve($this->vars->level->modelPath)->setLocale(app()->getLocale()); + // Add requests + $model = $this->processModelRequests( + validationRules: $this->vars->validationRules, + request: $request, + replacers: $this->vars->replacers, + model: $model + ); + + // Default module for file storage + $model->module = $model->module ?: $this->vars->level->name; + + $name = PbUtilities::checkName( + dir: $model->module ?: $this->vars->level->name, + name: ($model->name ?: $file->getClientOriginalName()), + extension: $file->extension() + ); + if (!$name) { + return $this->redirectResponseCRUDFail($request, 'create', 'No valid name for file'); + } + if ($newfile = PbUtilities::saveFile($file, $model->module, $name)) { + $model->name = $name; + $model->mime_type = $newfile->getMimeType(); + $model->user_id = Auth::user()->id; + $model->hash = md5($newfile->getContent()); + if (!$model->save()) { + return $this->redirectResponseCRUDFail($request, 'create', "Error saving {$this->vars->level->name}"); + } + + PbCache::clearIndex( + package: $this->vars->helper->package, + models: $this->vars->level->names, + ); + } else { + return $this->redirectResponseCRUDFail($request, 'create', 'File could not be saved'); + } + + return $this->redirectResponseCRUDSuccess($request, 'create'); + } catch (Exception $e) { + return $this->redirectResponseCRUDFail($request, 'create', $e->getMessage()); + } + } + + /** + * Update the specified resource in storage. + * + * @param Request $request + * @param int $id + * @return Application|Redirector|RedirectResponse|null + */ + public function update(Request $request, int $id): Redirector|RedirectResponse|Application|null + { + // Validation + if ($failed = $this->validateRequest($this->vars->validationRules, $request)) { + return $failed; + } + + // Set cache/methods arguments + $this->initArgs([ + 'function' => 'update', + ]); + + // Process + try { + // Build model + if (!$model = $this->vars->level->modelPath::find($id)) { + return $this->redirectResponseCRUDFail($request, 'update', "Error finding {$this->vars->level->name} with id $id"); + } + // Check Permissions + $this->checkPermissions($model); + // Set Locale + $model->setLocale(app()->getLocale()); + // Prepare file name and extension + $currentName = $model->name; + $extension = PbUtilities::getFileExtension($model->name); + // Build requests + $model = $this->processModelRequests( + validationRules: $this->vars->validationRules, + request: $request, + replacers: $this->vars->replacers, + model: $model + ); + $updateAuthor = true; + if (!$model->name) { + $model->name = $currentName; + } elseif ($currentName != $model->name) { + $model->name = PbUtilities::checkName($model->module, $model->name, $extension, "", $currentName); + if ($currentName != $model->name) { + $updateAuthor = PbUtilities::updateFile($model->module, $currentName, $model->name); + } + } + if ($updateAuthor) { + $model->user_id = Auth::user()->id; + // Model update + if (!$model->save()) { + return $this->redirectResponseCRUDFail($request, 'update', "Error updating {$this->vars->level->name}"); + } + + PbCache::clearModel( + package: $this->vars->helper->package, + models: $this->vars->level->names, + modelId: $id, + ); + } else { + return $this->redirectResponseCRUDFail($request, 'update', 'File could not be saved'); + } + + return $this->redirectResponseCRUDSuccess($request, 'update'); + } catch (Exception $e) { + return $this->redirectResponseCRUDFail($request, 'update', $e->getMessage()); + } + } + + /** + * Remove the specified resource from storage. + * + * @param Request $request + * @param int $id + * @return Application|Redirector|RedirectResponse + */ + public function destroy(Request $request, int $id): Redirector|RedirectResponse|Application + { + // Set cache/methods arguments + $this->initArgs([ + 'function' => 'destroy', + ]); + // Process + try { + if (!$model = $this->vars->level->modelPath::find($id)) { + return $this->redirectResponseCRUDFail($request, 'delete', "Error finding {$this->vars->level->name}"); + } + // Check Permissions + $this->checkPermissions($model); + if (PbUtilities::deleteFile($model->module, $model->name)) { + // Model delete + if (!$model->delete()) { + return $this->redirectResponseCRUDFail($request, 'delete', "Error deleting {$this->vars->level->name}"); + } + + PbCache::clearAll(); + } + + return $this->redirectResponseCRUDSuccess($request, 'delete'); + } catch (Exception $e) { + return $this->redirectResponseCRUDFail($request, 'delete', $e->getMessage()); + } + } +} diff --git a/src/Database/Migrations/create_files_table.php.stub b/src/Database/Migrations/create_files_table.php.stub new file mode 100644 index 0000000..01f5cbd --- /dev/null +++ b/src/Database/Migrations/create_files_table.php.stub @@ -0,0 +1,39 @@ +id(); + $table->string('name', 128)->nullable(false); + $table->json('description')->nullable(); + $table->json('alt')->nullable(); + $table->string('mime_type', 32)->nullable(false); + $table->string('hash', 32)->nullable(false); + $table->string('module', 32)->nullable(false); + $table->string('permission', 32)->nullable(false); + $table->unsignedInteger('user_id')->nullable(false); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('files'); + } +} diff --git a/src/Database/Seeders/PbConfigSeeder.php b/src/Database/Seeders/PbConfigSeeder.php index 3333286..df95a51 100644 --- a/src/Database/Seeders/PbConfigSeeder.php +++ b/src/Database/Seeders/PbConfigSeeder.php @@ -16,6 +16,7 @@ class PbConfigSeeder extends Seeder public function run() { $moduleConfig = PbModule::where('modulekey', 'config')->first(); + $moduleFile = PbModule::where('modulekey', 'file')->first(); // Default Config Config::upsert([ ['configkey' => '_APP_NAME_', 'configvalue' => 'Builder', 'name' => json_encode(['en' => 'App Name', 'es' => 'Nombre de la Aplicación']), 'description' => json_encode(['en' => 'App Description', 'es' => 'Nombre de la Aplicación']), 'module_id' => $moduleConfig->id], @@ -32,6 +33,7 @@ public function run() ['configkey' => '_DISABLE_REGISTER_', 'configvalue' => false, 'name' => json_encode(['en' => 'Disable registration form', 'es' => 'Deshabilitar formulario de registro']), 'description' => json_encode(['en' => 'Disable the registration form', 'es' => 'Deshabilitar el formulario de registro']), 'module_id' => $moduleConfig->id], ['configkey' => '_DEFAULT_TABLE_SIZE_', 'configvalue' => false, 'name' => json_encode(['en' => 'Default table size', 'es' => 'Tamaño de la tabla por defecto']), 'description' => json_encode(['en' => 'Number of rows to be shown in tables per page', 'es' => 'Número de registros a mostrar en las tablas por defecto']), 'module_id' => $moduleConfig->id], ['configkey' => '_CACHE_ENABLED_', 'configvalue' => false, 'name' => json_encode(['en' => 'Enable Cache', 'es' => 'Habilitar Caché']), 'description' => json_encode(['en' => 'Enable queries caching to increase performance', 'es' => 'Habilitar el caché de consultas para mejorar el rendimiento']), 'module_id' => $moduleConfig->id], + ['configkey' => '_HASH_FILE_NAME_', 'configvalue' => '0', 'name' => json_encode(['en' => 'File Name Hashing', 'es' => 'Hashear Nombres de Archivos']), 'description' => json_encode(['en' => 'Force file names hashing (recommended for higher security)', 'es' => 'Forzar hasheo de nombres (recomendado para mayor seguridad)']), 'module_id' => $moduleFile->id], ], ['configkey'], ['configvalue', 'name', 'description', 'module_id']); } } diff --git a/src/Database/Seeders/PbModulesSeeder.php b/src/Database/Seeders/PbModulesSeeder.php index 5b7a38d..74781c2 100644 --- a/src/Database/Seeders/PbModulesSeeder.php +++ b/src/Database/Seeders/PbModulesSeeder.php @@ -21,6 +21,7 @@ public function run() ['modulekey' => 'permission', 'name' => json_encode(['en' => 'Permissions', 'es' => 'Permisos']), 'status' => 1], ['modulekey' => 'role', 'name' => json_encode(['en' => 'Roles', 'es' => 'Roles']), 'status' => 1], ['modulekey' => 'logger', 'name' => json_encode(['en' => 'Logger', 'es' => 'Logger']), 'status' => 1], + ['modulekey' => 'file', 'name' => json_encode(['en' => 'Files Manager', 'es' => 'Administrador de Archivos']), 'status' => 1], ], ['modulekey'], ['name', 'status']); } } diff --git a/src/Database/Seeders/PbNavigationSeeder.php b/src/Database/Seeders/PbNavigationSeeder.php index ed037f0..b075123 100644 --- a/src/Database/Seeders/PbNavigationSeeder.php +++ b/src/Database/Seeders/PbNavigationSeeder.php @@ -24,11 +24,13 @@ public function run() $readConfigsPermission = PbPermission::where('name', 'read configs')->first(); $readNavigationsPermission = PbPermission::where('name', 'read navigations')->first(); $readLoggersPermission = PbPermission::where('name', 'read loggers')->first(); + $readFilemanagerPermission = PbPermission::where('name', 'read files')->first(); $moduleUser = PbModule::where('modulekey', 'user')->first(); $moduleConfig = PbModule::where('modulekey', 'config')->first(); $moduleNavigation = PbModule::where('modulekey', 'navigation')->first(); $moduleRole = PbModule::where('modulekey', 'role')->first(); $modulePermission = PbModule::where('modulekey', 'permission')->first(); + $moduleFilemanager = PbModule::where('modulekey', 'file')->first(); Navigation::query()->delete(); @@ -67,5 +69,13 @@ public function run() ['destiny' => 'navigations.index', 'module_id' => $moduleNavigation->id, 'name' => json_encode(['en' => 'Navigation', 'es' => 'Navegación']), 'type' => 'route', 'parent' => $settingsParent->id, 'permission_id' => $readNavigationsPermission->id, 'position' => 1], ], ['destiny', 'module_id'], ['name', 'type', 'parent', 'permission_id', 'position']); } + + // Files + if ($filesParent = Navigation::updateOrCreate(['destiny' => 'files.index', 'module_id' => $moduleFilemanager->id], ['name' => 'Files List', 'type' => 'route', 'parent' => 0, 'permission_id' => $readFilemanagerPermission->id, 'position' => 0])) { + // Spanish name update + $filesParent->setLocale('es'); + $filesParent->name = 'Lista de Archivos'; + $filesParent->save(); + } } } diff --git a/src/Database/Seeders/PbSpatieSeeder.php b/src/Database/Seeders/PbSpatieSeeder.php index b2d4b99..e9e07e0 100644 --- a/src/Database/Seeders/PbSpatieSeeder.php +++ b/src/Database/Seeders/PbSpatieSeeder.php @@ -25,6 +25,7 @@ public function run() $moduleNavigation = PbModule::whereModulekey('navigation')->first(); $moduleRole = PbModule::whereModulekey('role')->first(); $modulePermission = PbModule::whereModulekey('permission')->first(); + $moduleFile = PbModule::where('modulekey', 'file')->first(); Permission::query()->delete(); Role::query()->delete(); @@ -76,12 +77,18 @@ public function run() ['guard_name' => 'admin', 'name' => 'create roles', 'alias' => json_encode(['en' => 'Create Roles', 'es' => 'Crear Roles']), 'module_id' => $moduleRole->id], ['guard_name' => 'admin', 'name' => 'read roles', 'alias' => json_encode(['en' => 'Read Roles', 'es' => 'Leer Roles']), 'module_id' => $moduleRole->id], ['guard_name' => 'admin', 'name' => 'update roles', 'alias' => json_encode(['en' => 'Update Roles', 'es' => 'Actualizar Roles']), 'module_id' => $moduleRole->id], - ['guard_name' => 'admin', 'name' => 'delete roles', 'alias' => json_encode(['en' => 'Delete Roles', 'es' => 'Borrar Roles']), 'module_id' => $moduleRole->id] + ['guard_name' => 'admin', 'name' => 'delete roles', 'alias' => json_encode(['en' => 'Delete Roles', 'es' => 'Borrar Roles']), 'module_id' => $moduleRole->id], + // CRUD Files + ['guard_name' => 'admin', 'name' => 'config files', 'alias' => json_encode(['en' => 'Configure Files', 'es' => 'Configurar Archivos']), 'module_id' => $moduleFile->id], + ['guard_name' => 'admin', 'name' => 'create files', 'alias' => json_encode(['en' => 'Create Files', 'es' => 'Crear Archivos']), 'module_id' => $moduleFile->id], + ['guard_name' => 'admin', 'name' => 'read files', 'alias' => json_encode(['en' => 'Read Files', 'es' => 'Leer Archivos']), 'module_id' => $moduleFile->id], + ['guard_name' => 'admin', 'name' => 'update files', 'alias' => json_encode(['en' => 'Update Files', 'es' => 'Actualizar Archivos']), 'module_id' => $moduleFile->id], + ['guard_name' => 'admin', 'name' => 'delete files', 'alias' => json_encode(['en' => 'Delete Files', 'es' => 'Borrar Archivos']), 'module_id' => $moduleFile->id], ], ['guard_name', 'name'], ['alias', 'module_id']); // updateOrCreate roles and assign updateOrCreated permissions Role::updateOrCreate(['guard_name' => 'admin', 'name' => 'user'], ['guard_name' => 'admin', 'name' => 'user', 'alias' => ['en' => 'User', 'es' => 'Usuario']]) - ->givePermissionTo(['login']); + ->givePermissionTo(['login', 'create files', 'read files', 'update files', 'delete files', 'config files']); Role::updateOrCreate(['guard_name' => 'admin', 'name' => 'api-user'], ['guard_name' => 'admin', 'name' => 'api-user', 'alias' => ['en' => 'API User', 'es' => 'Usuario API']]) ->givePermissionTo(['api access']); Role::updateOrCreate(['guard_name' => 'admin', 'name' => 'admin'], ['guard_name' => 'admin', 'name' => 'admin', 'alias' => ['en' => 'Admin', 'es' => 'Administrador']]) @@ -99,9 +106,14 @@ public function run() 'config builder', 'login', 'clear cache', + 'config files', + 'create files', + 'read files', + 'update files', + 'delete files', ]); Role::updateOrCreate(['guard_name' => 'admin', 'name' => 'developer'], ['guard_name' => 'admin', 'name' => 'developer', 'alias' => ['en' => 'Developer', 'es' => 'Desarrollador']]) - ->givePermissionTo(['developer options', 'read loggers', 'delete loggers', 'config loggers', 'clear cache']); + ->givePermissionTo(['developer options', 'read loggers', 'delete loggers', 'config loggers', 'clear cache', 'create files', 'read files', 'update files', 'delete files', 'config files']); Role::updateOrCreate(['guard_name' => 'admin', 'name' => 'super-admin'], ['guard_name' => 'admin', 'name' => 'super-admin', 'alias' => ['en' => 'Super Admin', 'es' => 'Superadministrador']]) ->givePermissionTo(Permission::all()); } diff --git a/src/Helpers/helpers.php b/src/Helpers/helpers.php index 5899ca6..64e74d4 100644 --- a/src/Helpers/helpers.php +++ b/src/Helpers/helpers.php @@ -428,3 +428,26 @@ function is_closure($t): bool { return $t instanceof Closure; } + +/** + * Returns existing migration file if found, else uses the current timestamp. + * + * @param $name + * @return mixed + */ +function renameFile($name): string +{ + $noMime = explode('.', $name); + $mime = array_pop($noMime); + if (count($noMime) > 0) { + $aName = explode('-', implode('.', $noMime)); + if (is_numeric(end($aName))) { + $aName[count($aName) - 1] = end($aName) + 1; + } else { + $aName[count($aName)] = '1'; + } + return implode('-', $aName) . '.' . $mime; + } else { + return $name; + } +} diff --git a/src/Models/PbFile.php b/src/Models/PbFile.php new file mode 100644 index 0000000..71d267d --- /dev/null +++ b/src/Models/PbFile.php @@ -0,0 +1,204 @@ +publicRelations = ['author']; + $this->allRelations = ['author']; + $this->translatable = ['description', 'alt']; + $this->appends = [...$this->appends, ...['descriptions', 'alts', 'url']]; + } + + /** + * The attributes that are mass assignable. + * + * @var array + */ + protected $fillable = [ + 'name', + 'description', + 'alt', + 'mime_type', + 'hash', + 'module', + 'permission', + 'user_id' + ]; + + public function getDescriptionAttribute($value) + { + return translateString($value); + } + + public function getAltAttribute($value) + { + return translateString($value); + } + + /** + * The attributes that are mass assignable. + * + * + * @return array|string + * @var array + */ + public function getAltsAttribute($value) + { + return $this->getRawOriginal('alt'); + } + + /** + * The attributes that are mass assignable. + * + * + * @return array|string + * @var array + */ + public function getUrlAttribute() + { + return config('app.url') . Storage::url($this->module.'/'.$this->name); + } + + /** + * The attributes that are mass assignable. + * + * + * @return BelongsTo + */ + public function author(): BelongsTo + { + return $this->belongsTo(PbUser::class, 'user_id', 'id'); + } + + /** + * The attributes that are mass assignable. + * + * + * @param $id + * @param $newId + */ + public static function replaceAuthor($id, $newId): void + { + self::where('user_id', $id)->update([ + 'user_id' => $newId + ]); + } + + /** + * The attributes that are mass assignable. + * + * + * @param bool $includeForm + * @return array + */ + public static function getCrudConfig(bool $includeForm = false): array + { + $config = parent::getCrudConfig(); + + if ($includeForm) { + $config['form'] = [ + 'file' => [ + 'type' => 'file', + ], + 'name' => [ + 'type' => 'text', + ], + 'description' => [ + 'type' => 'text', + ], + 'alt' => [ + 'type' => 'text', + ], + 'permission' => [ + 'type' => 'select', + 'list' => [ + [ + 'id' => 'public', + 'name' => 'Public' + ], + [ + 'id' => 'private', + 'name' => 'Private' + ], + ], + ], + 'module' => [ + 'type' => 'hidden', + 'default' => 'file' + ], + 'url' => [ + 'type' => 'hidden', + ], + ]; + } + + $config['fields'] = [ + 'name' => [ + 'href' => [ + 'route' => 'files.show', + 'id' => true, + ], + 'orderable' => true, + ], + 'url' => [ + 'href' => [ + 'custom' => 'self', + 'text' => '#', + 'target' => '_blank', + ], + ], + 'description' => [], + 'alt' => [], + 'mime_type' => [ + 'name' => 'Mime Type', + 'orderable' => true, + ], + 'module' => [], + 'permission' => [], + 'author' => [ + 'arrval' => [ + 'key' => 'name', + 'href' => [ + 'route' => 'users.show', + 'id' => 'id', + ], + ], + ], + ]; + + $config['relations'] = ['author']; + + $config['custom_order'] = [ + 'id', 'url', 'name', 'description', 'alt', 'mime_type', 'module', 'permission', 'author', + ]; + + $config['pagination'] = [ + 'per_page' => 20, + 'location' => 'both', + ]; + + $config['heading'] = [ + 'location' => 'both', + ]; + + return $config; + } +} diff --git a/src/Observers/PbUserObserver.php b/src/Observers/PbUserObserver.php index 3d0f481..2e33283 100644 --- a/src/Observers/PbUserObserver.php +++ b/src/Observers/PbUserObserver.php @@ -2,6 +2,7 @@ namespace Anibalealvarezs\Projectbuilder\Observers; +use Anibalealvarezs\Projectbuilder\Models\PbFile; use Anibalealvarezs\Projectbuilder\Models\PbLogger; use Anibalealvarezs\Projectbuilder\Models\PbModule; use Anibalealvarezs\Projectbuilder\Models\PbUser; @@ -38,9 +39,9 @@ public function updated(PbUser $user) */ public function deleted(PbUser $user) { - if (PbModule::exists('file') && class_exists(\Anibalealvarezs\Filemanager\Models\FmFile::class) && $default = PbUser::getDefaultUser()) { + if ($default = PbUser::getDefaultUser()) { try { - \Anibalealvarezs\Filemanager\Models\FmFile::replaceAuthor($user->id, $default->id); + PbFile::replaceAuthor($user->id, $default->id); } catch (Exception $e) { PbLogger::create([ 'severity' => 3, diff --git a/src/Providers/PbMigrationServiceProvider.php b/src/Providers/PbMigrationServiceProvider.php index 3dbde35..5dd765f 100644 --- a/src/Providers/PbMigrationServiceProvider.php +++ b/src/Providers/PbMigrationServiceProvider.php @@ -2,6 +2,7 @@ namespace Anibalealvarezs\Projectbuilder\Providers; +use Illuminate\Support\Arr; use Illuminate\Support\ServiceProvider; class PbMigrationServiceProvider extends ServiceProvider diff --git a/src/Utilities/PbUtilities.php b/src/Utilities/PbUtilities.php index 5a623cb..75e625f 100644 --- a/src/Utilities/PbUtilities.php +++ b/src/Utilities/PbUtilities.php @@ -5,6 +5,9 @@ use Anibalealvarezs\Projectbuilder\Models\PbModule; use Illuminate\Support\Facades\Schema; use Illuminate\Support\Facades\Route; +use Illuminate\Support\Facades\Storage; +use Illuminate\Support\Str; +use Symfony\Component\HttpFoundation\File\File; class PbUtilities { @@ -132,4 +135,88 @@ function() use ($name, $modelClass, $controllerClass, $api, $sortable) { } ); } + + /** + * Returns existing migration file if found, else uses the current timestamp. + * + * @param string $dir + * @param string $name + * @param string $extension + * @param string $hashName + * @param string $allowed + * @return string + */ + public static function checkName(string $dir, string $name = "", string $extension = "", string $hashName = "", string $allowed = ""): string + { + $explodedName = explode('.', $name); + $explodedName = array_filter($explodedName); + if (count($explodedName) > 1 && end($explodedName) === $extension) { + array_pop($explodedName); + } elseif (empty($explodedName)) { + if ($hashName) { + $explodedName = [$hashName]; + } else { + $explodedName = [Hash::make(date('Y_m_d_His'))]; + } + } + $name = Str::slug(implode("-", $explodedName)).'.'.$extension; + while (Storage::disk('public')->exists($dir . DIRECTORY_SEPARATOR . $name) && $allowed !== $name) { + $name = self::checkName($dir, renameFile($name)); + } + return $name; + } + + /** + * Returns existing migration file if found, else uses the current timestamp. + * + * @param $name + * @return mixed + */ + public static function getFileExtension($name): string + { + $explodedName = explode('.', $name); + return end($explodedName); + } + + /** + * Returns existing migration file if found, else uses the current timestamp. + * + * @param $file + * @param $dir + * @param $name + * @return File + */ + public static function saveFile($file, $dir, $name): File + { + return $file->move(Storage::disk('public')->path($dir), $name); + } + + /** + * Returns existing migration file if found, else uses the current timestamp. + * + * @param $dir + * @param $name + * @param $newName + * @return mixed + */ + public static function updateFile($dir, $name, $newName): bool + { + return Storage::disk('public')->move($dir . DIRECTORY_SEPARATOR . $name, $dir . DIRECTORY_SEPARATOR . $newName); + } + + /** + * Returns existing migration file if found, else uses the current timestamp. + * + * @param $dir + * @param $name + * @return mixed + */ + public static function deleteFile($dir, $name): bool + { + if (Storage::disk('public')->exists($dir . DIRECTORY_SEPARATOR . $name)) { + return Storage::disk('public')->delete($dir . DIRECTORY_SEPARATOR . $name); + } + + return true; + } } diff --git a/src/config/pbuilder.php b/src/config/pbuilder.php index 9912c56..d4aaaf1 100644 --- a/src/config/pbuilder.php +++ b/src/config/pbuilder.php @@ -55,7 +55,7 @@ | */ - 'modulekeys' => ['user', 'config', 'navigation', 'permission', 'role', 'logger'], + 'modulekeys' => ['user', 'config', 'navigation', 'permission', 'role', 'logger', 'file'], /* |--------------------------------------------------------------------------