From f55472442fe4ca70d5dadeb8883f3c5f76069a0d Mon Sep 17 00:00:00 2001 From: Peter Papp Date: Sat, 27 Feb 2021 11:49:01 +0100 Subject: [PATCH] functionality for setting default folders for app installation --- .env.testing | 1 + app/Console/Commands/SetupDevEnvironment.php | 5 ++ app/Http/Helpers/helpers.php | 15 ---- app/Http/Tools/Editor.php | 76 +++++--------------- app/Models/User.php | 6 +- app/Services/SetupService.php | 35 +++++++++ storage/app/public/.gitignore | 2 - tests/Feature/SetupServiceTest.php | 32 +++++++++ tests/Feature/UserTest.php | 7 ++ 9 files changed, 101 insertions(+), 78 deletions(-) create mode 100644 app/Services/SetupService.php delete mode 100644 storage/app/public/.gitignore create mode 100644 tests/Feature/SetupServiceTest.php diff --git a/.env.testing b/.env.testing index 5cb8ce06..7abb73f5 100644 --- a/.env.testing +++ b/.env.testing @@ -13,6 +13,7 @@ DB_DATABASE=database/test.sqlite DB_USERNAME=null DB_PASSWORD=null +FILESYSTEM_DRIVER=local BROADCAST_DRIVER=log CACHE_DRIVER=file SESSION_DRIVER=file diff --git a/app/Console/Commands/SetupDevEnvironment.php b/app/Console/Commands/SetupDevEnvironment.php index 914d3c1c..a55c2fc8 100644 --- a/app/Console/Commands/SetupDevEnvironment.php +++ b/app/Console/Commands/SetupDevEnvironment.php @@ -3,6 +3,7 @@ namespace App\Console\Commands; use App\Page; +use App\Services\SetupService; use App\Setting; use App\User; use Illuminate\Console\Command; @@ -34,6 +35,7 @@ class SetupDevEnvironment extends Command { parent::__construct(); $this->faker = Faker\Factory::create(); + $this->setup = app()->make(SetupService::class); } /** @@ -45,6 +47,9 @@ class SetupDevEnvironment extends Command { $this->info('Setting up development environment'); + $this->info('Creating system directories...'); + $this->setup->create_directories(); + $this->migrate_and_generate(); $this->store_data(); $this->seed_default_content(); diff --git a/app/Http/Helpers/helpers.php b/app/Http/Helpers/helpers.php index 7592570f..a08a6aa4 100644 --- a/app/Http/Helpers/helpers.php +++ b/app/Http/Helpers/helpers.php @@ -295,21 +295,6 @@ function store_system_image($image, $path) return $path . '/' . $image_path; } -/** - * Check if directory exist, if no, then create it - * - * @param $directory - * @return mixed - */ -function check_directory($directory) -{ - if (!Storage::exists($directory)) { - Storage::makeDirectory($directory); - } - - return $directory; -} - /** * Make input from request * diff --git a/app/Http/Tools/Editor.php b/app/Http/Tools/Editor.php index 756887ee..e6538dd5 100644 --- a/app/Http/Tools/Editor.php +++ b/app/Http/Tools/Editor.php @@ -85,22 +85,12 @@ class Editor // Local storage instance $disk_local = Storage::disk('local'); - // Create zip directory - if (!$disk_local->exists('zip')) { - $disk_local->makeDirectory('zip'); - } - // Move file to local storage if (!is_storage_driver('local')) { - // Create temp directory - if (!$disk_local->exists('temp')) { - $disk_local->makeDirectory('temp'); - } - foreach ($files as $file) { try { - $disk_local->put('temp/' . $file['basename'], Storage::get('file-manager/' . $file['basename'])); + $disk_local->put('temp/' . $file['basename'], Storage::get('files/' . $file['basename'])); } catch (FileNotFoundException $e) { throw new HttpException(404, 'File not found'); } @@ -154,22 +144,12 @@ class Editor // Local storage instance $disk_local = Storage::disk('local'); - // Create zip directory - if (!$disk_local->exists('zip')) { - $disk_local->makeDirectory('zip'); - } - // Move file to local storage from external storage service if (!is_storage_driver('local')) { - // Create temp directory - if (!$disk_local->exists('temp')) { - $disk_local->makeDirectory('temp'); - } - foreach ($files as $file) { try { - $disk_local->put('temp/' . $file['basename'], Storage::get('file-manager/' . $file['basename'])); + $disk_local->put('temp/' . $file['basename'], Storage::get('files/' . $file['basename'])); } catch (FileNotFoundException $e) { throw new HttpException(404, 'File not found'); } @@ -184,7 +164,7 @@ class Editor $zip = Madzipper::make(storage_path() . '/app/' . $zip_path); // Get files folder on local storage drive - $files_directory = is_storage_driver('local') ? 'file-manager' : 'temp'; + $files_directory = is_storage_driver('local') ? 'files' : 'temp'; // Add files to zip $files->each(function ($file) use ($zip, $files_directory) { @@ -322,10 +302,10 @@ class Editor foreach ($files as $file) { // Delete file - Storage::delete('/file-manager/' . $file->basename); + Storage::delete('/files/' . $file->basename); // Delete thumbnail if exist - if (!is_null($file->thumbnail)) Storage::delete('/file-manager/' . $file->getRawOriginal('thumbnail')); + if (!is_null($file->thumbnail)) Storage::delete('/files/' . $file->getRawOriginal('thumbnail')); // Delete file permanently $file->forceDelete(); @@ -357,10 +337,10 @@ class Editor if ($item['force_delete']) { // Delete file - Storage::delete('/file-manager/' . $item->basename); + Storage::delete('/files/' . $item->basename); // Delete thumbnail if exist - if ($item->thumbnail) Storage::delete('/file-manager/' . $item->getRawOriginal('thumbnail')); + if ($item->thumbnail) Storage::delete('/files/' . $item->getRawOriginal('thumbnail')); // Delete file permanently $item->forceDelete(); @@ -418,9 +398,6 @@ class Editor // Get parent_id from request $file = $request->file('file'); - // Check or create directories - self::check_directories(['chunks', 'file-manager']); - // File name $user_file_name = basename('chunks/' . substr($file->getClientOriginalName(), 17), '.part'); $disk_file_name = basename('chunks/' . $file->getClientOriginalName(), '.part'); @@ -464,7 +441,7 @@ class Editor $thumbnail = self::get_image_thumbnail('chunks/' . $temp_filename, $disk_file_name); // Move finished file from chunk to file-manager directory - $disk_local->move('chunks/' . $temp_filename, 'file-manager/' . $disk_file_name); + $disk_local->move('chunks/' . $temp_filename, 'files/' . $disk_file_name); // Move files to external storage if (!is_storage_driver(['local'])) { @@ -518,7 +495,7 @@ class Editor // Get all files from storage $files = collect([ - $local_disk->allFiles('file-manager'), + $local_disk->allFiles('files'), $local_disk->allFiles('chunks') ])->collapse(); @@ -557,7 +534,7 @@ class Editor if (!$file) continue; // Get file size - $filesize = $disk_local->size('file-manager/' . $file); + $filesize = $disk_local->size('files/' . $file); // If file is bigger than 5.2MB then run multipart upload if ($filesize > 5242880) { @@ -572,9 +549,9 @@ class Editor $client = $adapter->getClient(); // Prepare the upload parameters. - $uploader = new MultipartUploader($client, config('filesystems.disks.local.root') . '/file-manager/' . $file, [ + $uploader = new MultipartUploader($client, config('filesystems.disks.local.root') . '/files/' . $file, [ 'bucket' => $adapter->getBucket(), - 'key' => 'file-manager/' . $file + 'key' => 'files/' . $file ]); try { @@ -588,7 +565,7 @@ class Editor Log::error($e->getMessage()); // Delete file after error - $disk_local->delete('file-manager/' . $file); + $disk_local->delete('files/' . $file); throw new HttpException(409, $e->getMessage()); } @@ -596,32 +573,11 @@ class Editor } else { // Stream file object to s3 - Storage::putFileAs('file-manager', config('filesystems.disks.local.root') . '/file-manager/' . $file, $file, 'private'); + Storage::putFileAs('files', config('filesystems.disks.local.root') . '/files/' . $file, $file, 'private'); } // Delete file after upload - $disk_local->delete('file-manager/' . $file); - } - } - - /** - * Check if directories 'chunks' and 'file-manager exist', if no, then create - * - * @param $directories - */ - private static function check_directories($directories): void - { - foreach ($directories as $directory) { - - if (!Storage::disk('local')->exists($directory)) { - Storage::disk('local')->makeDirectory($directory); - } - - if (!is_storage_driver(['local'])) { - if (!Storage::exists($directory)) { - Storage::makeDirectory($directory); - } - } + $disk_local->delete('files/' . $file); } } @@ -652,7 +608,7 @@ class Editor })->stream(); // Store thumbnail to disk - $local_disk->put('file-manager/' . $thumbnail, $image); + $local_disk->put('files/' . $thumbnail, $image); } // Return thumbnail as svg file diff --git a/app/Models/User.php b/app/Models/User.php index 73c72f9f..bec1a845 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -295,7 +295,11 @@ class User extends Authenticatable parent::boot(); static::creating(function ($model) { - $model->id = (string)Str::uuid(); + // Store uuid into model + $model->id = Str::uuid(); + + // Create user directory + Storage::makeDirectory("files/$model->id"); }); } } diff --git a/app/Services/SetupService.php b/app/Services/SetupService.php new file mode 100644 index 00000000..d2eda0f3 --- /dev/null +++ b/app/Services/SetupService.php @@ -0,0 +1,35 @@ +makeDirectory($directory); + + // Create directory for external driver + Storage::makeDirectory($directory); + } + } +} \ No newline at end of file diff --git a/storage/app/public/.gitignore b/storage/app/public/.gitignore deleted file mode 100644 index d6b7ef32..00000000 --- a/storage/app/public/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -* -!.gitignore diff --git a/tests/Feature/SetupServiceTest.php b/tests/Feature/SetupServiceTest.php new file mode 100644 index 00000000..b095e3e0 --- /dev/null +++ b/tests/Feature/SetupServiceTest.php @@ -0,0 +1,32 @@ +setup = app()->make(SetupService::class); + } + + /** + * @test + */ + public function it_create_system_folders() + { + $this->setup->create_directories(); + + collect(['avatars', 'chunks', 'system', 'files', 'temp', 'zip']) + ->each(function ($directory) { + Storage::disk('local')->assertExists($directory); + }); + } +} diff --git a/tests/Feature/UserTest.php b/tests/Feature/UserTest.php index d976b6fe..4cfd4d21 100644 --- a/tests/Feature/UserTest.php +++ b/tests/Feature/UserTest.php @@ -7,6 +7,7 @@ use App\Models\Folder; use App\Models\User; use Illuminate\Foundation\Testing\DatabaseMigrations; use Laravel\Sanctum\Sanctum; +use Storage; use Tests\TestCase; class UserTest extends TestCase @@ -29,6 +30,9 @@ class UserTest extends TestCase $this->assertDatabaseHas('user_settings', [ 'user_id' => $user->id, ]); + + Storage::disk('local') + ->assertExists('files/' . User::first()->id); } /** @@ -50,6 +54,9 @@ class UserTest extends TestCase $this->assertDatabaseHas('user_settings', [ 'name' => 'John Doe', ]); + + Storage::disk('local') + ->assertExists('files/' . User::first()->id); } /**