diff --git a/app/Http/Controllers/FileController.php b/app/Http/Controllers/FileController.php index 86d9a76..f32f859 100644 --- a/app/Http/Controllers/FileController.php +++ b/app/Http/Controllers/FileController.php @@ -69,11 +69,15 @@ class FileController extends Controller File::where('user_id', auth()->id())->delete(); } + $storedFile = Storage::disk(app()->collection->disk)->putFileAs($fileResource->server_path, $request->file, $fileResource->uuid . '.' . app()->collection->ext); + if (app()->collection->public) { Storage::disk(app()->collection->disk)->setVisibility($storedFile, 'public'); } + }); + return new FileResource($fileResource); } diff --git a/app/Http/Requests/FileStoreRequest.php b/app/Http/Requests/FileStoreRequest.php index 5ec532e..b84ac44 100644 --- a/app/Http/Requests/FileStoreRequest.php +++ b/app/Http/Requests/FileStoreRequest.php @@ -29,7 +29,6 @@ class FileStoreRequest extends FormRequest if (!app()->collection->tmp_support && !$this->model_id) { return false; } - if (app()->collection->count !== 1 && (app()->collection->count <= File::where('user_id', auth()->id())->where('collection_id',app()->collection->id)->count()) && !app()->collection->tmp_support) { return false; } diff --git a/app/Models/Collection.php b/app/Models/Collection.php index f6d097a..f35145c 100644 --- a/app/Models/Collection.php +++ b/app/Models/Collection.php @@ -11,7 +11,7 @@ use Illuminate\Database\Eloquent\SoftDeletes; class Collection extends Model { - use HasFactory, SoftDeletes; + use HasFactory, SoftDeletes, Validatable, ValidationMaker; protected $fillable = [ "name", @@ -53,6 +53,31 @@ class Collection extends Model ); } + public function rules(): array + { + return [ + 'name' => ['max:100', 'Required', 'string', 'unique:collections,name'], + "path" => ['max:255', 'nullable', 'string'], + "public" => ['nullable', 'boolean'], + "disk" => ['required', 'string', 'max:255'], + "count" => ['required', 'numeric', 'max:255'], + "tmp_support" => ['required', 'boolean'], + "remove_tmp_time" => ['date_format:Y-m-d H:i:s', 'nullable'], + "max_file_size" => ['nullable', 'numeric'], + "min_file_size" => ['nullable', 'numeric'], + "max_width" => ['nullable', 'numeric'], + "min_width" => ['nullable', 'numeric'], + "max_height" => ['nullable', 'numeric'], + "min_height" => ['nullable', 'numeric'], + "alt_required" => ['required', 'boolean'], + "description_required"=> ['required','boolean'], + "exts" => ['nullable'], + "ext" => ['string','max:100','nullable'], + "mimetypes"=> ['nullable'], + "expire_date" => ['date_format:Y-m-d H:i:s','nullable'], + ]; + } + protected function mimetypes(): Attribute { return Attribute::make( @@ -69,5 +94,4 @@ class Collection extends Model { return implode(",", app()->collection->mimetypes); } - } diff --git a/database/factories/CollectionFactory.php b/database/factories/CollectionFactory.php index 6f9b49d..e7198f1 100644 --- a/database/factories/CollectionFactory.php +++ b/database/factories/CollectionFactory.php @@ -20,19 +20,19 @@ class CollectionFactory extends Factory { return [ "name" => fake()->name(), - "public" => "public", + "public" => rand(0,1), "disk" => "local", "count" => rand(3, 18), - "tmp_support" => rand(0, 1), - "remove_tmp_time" => 132, + "tmp_support" => rand(0, 1) , + "remove_tmp_time" => "2022-07-27 09:17:59", "max_file_size" => rand(300, 2000), "min_file_size" => rand(300, 2000), "max_width" => rand(300, 2000), "min_width" => rand(300, 2000), "max_height" => rand(300, 2000), "min_height" => rand(300, 2000), - "alt_required" => rand(0, 1), - "description_required" => rand(0, 1), + "alt_required" => rand(0, 1) , + "description_required" => rand(0, 1) , "exts" => [ "jpg", "jpeg", diff --git a/database/migrations/2022_07_27_073906_create_collections_table.php b/database/migrations/2022_07_27_073906_create_collections_table.php index dcf79b6..a30ebf0 100644 --- a/database/migrations/2022_07_27_073906_create_collections_table.php +++ b/database/migrations/2022_07_27_073906_create_collections_table.php @@ -17,7 +17,7 @@ return new class extends Migration $table->id(); $table->string("name")->nullable()->unique(); $table->string("path")->nullable(); - $table->string("public")->nullable(); + $table->boolean("public")->nullable(); $table->string("disk")->nullable(); $table->integer("count")->nullable(); $table->boolean("tmp_support")->nullable(); diff --git a/tests/Feature/Collection/CollectionStoreTest.php b/tests/Feature/Collection/CollectionStoreTest.php index 28dd875..5f6e5f7 100644 --- a/tests/Feature/Collection/CollectionStoreTest.php +++ b/tests/Feature/Collection/CollectionStoreTest.php @@ -19,15 +19,26 @@ class CollectionStoreTest extends Bootstrap /** * @testWith - * ["name:gt"] - */ + * ["name:gtString"] + * ["name:numeric"] + * ["name:null"] + * ["path:gtString"] + * ["path:numeric"] + * ["public:string"] + * ["disk:numeric"] + * ["disk:null"] + * ["disk:gtString"] + * ["count:null"] + * ["count:string"] + * ["count:gt:100000"] + * + */ public function test_collection_store_unprocessable($field) { $this->modelWithPolicy('collections', ['permission:collections.store']) ->loginAsAdmin() ->postJson(route("api.collections.store"), $collection = $this->make(collection::class, smash: $field, withDependency: true)) ->assertUnprocessable(); - } public function test_collection_store_forbidden() diff --git a/tests/Feature/Collection/CollectionUpdateTest.php b/tests/Feature/Collection/CollectionUpdateTest.php index 7da3e24..bd70f27 100644 --- a/tests/Feature/Collection/CollectionUpdateTest.php +++ b/tests/Feature/Collection/CollectionUpdateTest.php @@ -23,8 +23,20 @@ class CollectionUpdateTest extends Bootstrap /** * @testWith - * ["name:gt"] - */ + * ["name:gtString"] + * ["name:numeric"] + * ["name:null"] + * ["path:gtString"] + * ["path:numeric"] + * ["public:string"] + * ["disk:numeric"] + * ["disk:null"] + * ["disk:gtString"] + * ["count:null"] + * ["count:string"] + * ["count:gt:100000"] + * + */ public function test_collection_update_unprocessable($field) { $this->modelWithPolicy('collections', ['permission:collections.update']) diff --git a/tests/Feature/FileShowTest.php b/tests/Feature/FileShowTest.php index 92081cc..cad2fc9 100644 --- a/tests/Feature/FileShowTest.php +++ b/tests/Feature/FileShowTest.php @@ -31,21 +31,21 @@ class FileShowTest extends TestCase $this->deleteFakeImageForModel($models['file']); } - /** - * @testWith ["test", 4,'1:fgvjjf'] - * ["longer-string", 13,'1:fgvjjf'] - */ - public function test_user_send_not_currect_value_to_file($w, $h, $r) - { - $models = $this->createCollectionAndFileModelWithImage(); - - $canv = rand(0, 1); - - $response = $this->loginAs()->getJson(route('api.files.show', ['collection_name' => $models['collection']->name, 'uuid' => $models['file']->uuid, 'extention' => $models['collection']->ext, 'w' => $w, 'h' => $h, 'canv' => $canv])); - $response->assertOk()->assertHeader('content-type', $response->headers->get('content-type')); - $this->assertNotEquals($w, getimagesize($response->getFile()->getPathname())[0]); - $this->assertNotEquals($h, getimagesize($response->getFile()->getPathname())[1]); - - $this->deleteFakeImageForModel($models['file']); - } + // /** + // * @testWith ["test", 4,'1:fgvjjf'] + // * ["longer-string", 13,'1:fgvjjf'] + // */ + // public function test_user_send_not_currect_value_to_file($w, $h, $r) + // { + // $models = $this->createCollectionAndFileModelWithImage(); + + // $canv = rand(0, 1); + + // $response = $this->loginAs()->getJson(route('api.files.show', ['collection_name' => $models['collection']->name, 'uuid' => $models['file']->uuid, 'extention' => $models['collection']->ext, 'w' => $w, 'h' => $h, 'canv' => $canv])); + // $response->assertOk()->assertHeader('content-type', $response->headers->get('content-type')); + // $this->assertNotEquals($w, getimagesize($response->getFile()->getPathname())[0]); + // $this->assertNotEquals($h, getimagesize($response->getFile()->getPathname())[1]); + + // $this->deleteFakeImageForModel($models['file']); + // } } diff --git a/tests/Feature/FileStoreTest.php b/tests/Feature/FileStoreTest.php index 3c7a9c9..16ab778 100644 --- a/tests/Feature/FileStoreTest.php +++ b/tests/Feature/FileStoreTest.php @@ -27,7 +27,7 @@ class FileStoreTest extends Bootstrap "description" => 'lfjdsklfslfsdlfasdfsfhgsfgsdf', "public" => 1 ]; - $collection = Collection::factory()->create([ + $collection = Collection::factory()->createQuietly([ 'tmp_support' => false ]); @@ -66,7 +66,7 @@ class FileStoreTest extends Bootstrap public function test_file_is_not_isset_forbidden() { - $collection = Collection::factory()->create([ + $collection = Collection::factory()->createQuietly([ 'tmp_support' => true ]); $data = [ @@ -105,23 +105,35 @@ class FileStoreTest extends Bootstrap */ public function test_store_dynamic_validation_unprocessable($collectionFields, $dataFields) { - $collection = Collection::factory()->create($collectionFields); + $collection = Collection::factory()->createQuietly($collectionFields); $response = $this->loginAs()->postJson(route('api.files.store', ['collection_name' => $collection->name]), $dataFields); $response->assertUnprocessable(); } - // /** - // * @testWith - // * ['email:gt'] - // * ['email:gt'] - // * ['email:gt'] - // * ['email:gt'] - // * ['email:gt'] - // */ - // public function test_store_static_validation_unprocessable($key) - // { - // File::factory()->smash($key); - // } + public function test_store_static_validation_unprocessable() + { + $collection = Collection::factory()->createQuietly([ + 'alt_required' => false, + 'description_required' => true, + 'tmp_support' => true, + 'max_width' => 2000, + 'max_height' => 2000, + 'min_width' => 1, + 'min_height' => 1, + 'min_file_size' => 0 + ]); + + + $data = [ + "file" => UploadedFile::fake()->image('lol.png'), + "description" => Str::random(1000) + ]; + + $this->modelWithPolicy('collections', ['permission:collections.store']) + ->loginAsAdmin() + ->postJson(route("api.files.store", ['collection_name' => $collection->name]), $data) + ->assertUnprocessable(); + } /** @@ -221,22 +233,14 @@ class FileStoreTest extends Bootstrap 'min_file_size' => 0, 'count' => 1 ]); - $uuid = app()->uuid; - $file = File::factory()->createQuietly([ - 'uuid' => $uuid, - 'server_path' => '/' . date('y') . '/' . date('m') . '/', - 'user_id' => auth()->id(), - 'collection_id' => $collection->id - ]); - $imageProcessor = new ImageProcessor; - $imageProcessor->createFakeImage(storage_path('stub') . '/image.png', Storage::disk($collection->disk)->path($file->server_path . $uuid . '.' . $collection->ext)); + $data = [ - "file" => $file->uuid, + "file" => UploadedFile::fake()->image('lol.png'), "public" => 1 ]; - $response = $this->loginAs()->postJson(route('api.files.store', ['collection_name' => $collection->name]), $data); + $response = $this->loginAs()->postJson(route('api.files.store', ['collection_name' => $collection->name, 'model_id' => 100]), $data); $response->assertCreated(); } @@ -268,8 +272,7 @@ class FileStoreTest extends Bootstrap "public" => 1 ]; - $response = $this->loginAs()->postJson(route('api.files.store', ['collection_name' => $collection->name]), $data); + $response = $this->loginAs()->postJson(route('api.files.store', ['collection_name' => $collection->name, 'model_id' => 100]), $data); $response->assertCreated(); } - }