Mohammad Khazaee
2 years ago
17 changed files with 554 additions and 114 deletions
-
36app/Http/Controllers/CollectionController.php
-
30app/Http/Controllers/FileController.php
-
60app/Http/Requests/FileStoreRequest.php
-
44app/Http/Resources/CollectionResource.php
-
37app/Http/Resources/FileResource.php
-
62app/Models/Collection.php
-
55app/Models/File.php
-
57database/factories/CollectionFactory.php
-
41database/factories/FileFactory.php
-
46database/migrations/2022_07_27_073858_create_files_table.php
-
52database/migrations/2022_07_27_073906_create_collections_table.php
-
3database/seeders/DatabaseSeeder.php
-
BINpublic/image-modified.png
-
BINpublic/image-modified.webp
-
112resources/views/welcome.blade.php
-
14routes/api.php
-
19routes/web.php
@ -0,0 +1,36 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace App\Http\Controllers; |
||||
|
|
||||
|
use App\Http\Resources\CollectionResource; |
||||
|
use App\Models\Collection; |
||||
|
use Illuminate\Http\Request; |
||||
|
|
||||
|
class CollectionController extends Controller |
||||
|
{ |
||||
|
|
||||
|
public function show(Collection $collection) |
||||
|
{ |
||||
|
return new CollectionResource($collection); |
||||
|
} |
||||
|
|
||||
|
public function store(Request $request) |
||||
|
{ |
||||
|
$collection = Collection::create($request->all()); |
||||
|
return new CollectionResource($collection); |
||||
|
} |
||||
|
|
||||
|
public function update(Request $request, Collection $collection) |
||||
|
{ |
||||
|
$collection->update($request->all()); |
||||
|
return new CollectionResource($collection); |
||||
|
} |
||||
|
|
||||
|
public function destroy(Collection $collection) |
||||
|
{ |
||||
|
if ($collection->trashed()) { |
||||
|
return $collection->restore(); |
||||
|
} |
||||
|
return $collection->delete(); |
||||
|
} |
||||
|
} |
@ -0,0 +1,30 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace App\Http\Controllers; |
||||
|
|
||||
|
use App\Http\Requests\FileStoreRequest; |
||||
|
use Illuminate\Http\Request; |
||||
|
|
||||
|
class FileController extends Controller |
||||
|
{ |
||||
|
public function show($id) |
||||
|
{ |
||||
|
//
|
||||
|
} |
||||
|
|
||||
|
public function store(FileStoreRequest $fileStoreRequest) |
||||
|
{ |
||||
|
$fileStoreRequest; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
public function update(Request $request, $id) |
||||
|
{ |
||||
|
//
|
||||
|
} |
||||
|
|
||||
|
public function destroy($id) |
||||
|
{ |
||||
|
//
|
||||
|
} |
||||
|
} |
@ -0,0 +1,60 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace App\Http\Requests; |
||||
|
|
||||
|
use App\Models\Collection; |
||||
|
|
||||
|
use Illuminate\Foundation\Http\FormRequest; |
||||
|
|
||||
|
class FileStoreRequest extends FormRequest |
||||
|
{ |
||||
|
|
||||
|
private $collection = null; |
||||
|
|
||||
|
public function __construct() |
||||
|
{ |
||||
|
|
||||
|
$this->collection = Collection::where('name',$this->collection_name)->get(); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Determine if the user is authorized to make this request. |
||||
|
* |
||||
|
* @return bool |
||||
|
*/ |
||||
|
public function authorize() |
||||
|
{ |
||||
|
return true; |
||||
|
} |
||||
|
|
||||
|
private function getExts() |
||||
|
{ |
||||
|
return implode(",",$this->collection->exts); |
||||
|
} |
||||
|
|
||||
|
private function getMimeTypes() |
||||
|
{ |
||||
|
return implode(",",$this->collection->mimetypes); |
||||
|
} |
||||
|
|
||||
|
// private function getExt()
|
||||
|
// {
|
||||
|
// return implode(",",$this->collection->ext);
|
||||
|
// }
|
||||
|
|
||||
|
/** |
||||
|
* Get the validation rules that apply to the request. |
||||
|
* |
||||
|
* @return array<string, mixed> |
||||
|
*/ |
||||
|
public function rules() |
||||
|
{ |
||||
|
return [ |
||||
|
"ext" => ["mimes:" . $this->getExts], |
||||
|
"memetype" => ["mimetypes:" . $this->getMimeTypes], |
||||
|
"width"=> [''], |
||||
|
"height"=> [''], |
||||
|
"file_size"=> [''], |
||||
|
]; |
||||
|
} |
||||
|
} |
@ -0,0 +1,44 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace App\Http\Resources; |
||||
|
|
||||
|
use Illuminate\Http\Resources\Json\JsonResource; |
||||
|
|
||||
|
class CollectionResource extends JsonResource |
||||
|
{ |
||||
|
/** |
||||
|
* Transform the resource into an array. |
||||
|
* |
||||
|
* @param \Illuminate\Http\Request $request |
||||
|
* @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable |
||||
|
*/ |
||||
|
public function toArray($request) |
||||
|
{ |
||||
|
return [ |
||||
|
"id" => $this->id, |
||||
|
"name" => $this->name, |
||||
|
"public" => $this->public, |
||||
|
"disk" => $this->disk, |
||||
|
"count" => $this->count, |
||||
|
"tmp_support" => $this->tmp_support, |
||||
|
"remove_tmp_time" => $this->remove_tmp_time, |
||||
|
"max_file_size" => $this->max_file_size, |
||||
|
"min_file_size" => $this->min_file_size, |
||||
|
"max_width" => $this->max_width, |
||||
|
"min_width" => $this->min_width, |
||||
|
"max_height" => $this->max_height, |
||||
|
"min_height" => $this->min_height, |
||||
|
"alt_required" => $this->alt_required, |
||||
|
"description_required" => $this->description_required, |
||||
|
"count" => $this->count, |
||||
|
"exts" => $this->exts, |
||||
|
"avalible_exts" => $this->avalible_exts, |
||||
|
"memetypes" => $this->memetypes, |
||||
|
"model" => $this->model, |
||||
|
"expire_date" => $this->expire_date, |
||||
|
"created_at" => $this->created_at, |
||||
|
"updated_at" => $this->updated_at, |
||||
|
"deleted_at" => $this->deleted_at |
||||
|
]; |
||||
|
} |
||||
|
} |
@ -0,0 +1,37 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace App\Http\Resources; |
||||
|
|
||||
|
use Illuminate\Http\Resources\Json\JsonResource; |
||||
|
|
||||
|
class FileResource extends JsonResource |
||||
|
{ |
||||
|
/** |
||||
|
* Transform the resource into an array. |
||||
|
* |
||||
|
* @param \Illuminate\Http\Request $request |
||||
|
* @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable |
||||
|
*/ |
||||
|
public function toArray($request) |
||||
|
{ |
||||
|
return [ |
||||
|
"uuid" => $this->uuid, |
||||
|
"original_name" => $this->original_name, |
||||
|
"ext" => $this->ext, |
||||
|
"memetype" => $this->memetype, |
||||
|
"width" => $this->width, |
||||
|
"height" => $this->height, |
||||
|
"file_size" => $this->file_size, |
||||
|
"sort" => $this->sort, |
||||
|
"alts" => $this->alts, |
||||
|
"description" => $this->description, |
||||
|
"user_id" => $this->user_id, |
||||
|
"ip" => $this->ip, |
||||
|
"collection_id" => $this->collection_id, |
||||
|
"published_at" => $this->published_at, |
||||
|
"created_at" => $this->created_at, |
||||
|
"updated_at" => $this->updated_at, |
||||
|
"deleted_at" => $this->deleted_at, |
||||
|
]; |
||||
|
} |
||||
|
} |
@ -0,0 +1,62 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace App\Models; |
||||
|
|
||||
|
use Illuminate\Database\Eloquent\Casts\Attribute; |
||||
|
use Illuminate\Database\Eloquent\Factories\HasFactory; |
||||
|
use Illuminate\Database\Eloquent\Model; |
||||
|
use Illuminate\Database\Eloquent\SoftDeletes; |
||||
|
|
||||
|
class Collection extends Model |
||||
|
{ |
||||
|
use HasFactory, SoftDeletes; |
||||
|
|
||||
|
protected $fillable = [ |
||||
|
"name", |
||||
|
"path", |
||||
|
"public", |
||||
|
"disk", |
||||
|
"count", |
||||
|
"tmp_support", |
||||
|
"remove_tmp_time", |
||||
|
"max_file_size", |
||||
|
"min_file_size", |
||||
|
"max_width", |
||||
|
"min_width", |
||||
|
"max_height", |
||||
|
"min_height", |
||||
|
"alt_required", |
||||
|
"description_required", |
||||
|
"exts", |
||||
|
"avalible_exts", |
||||
|
"memetypes", |
||||
|
"model", |
||||
|
"expire_date", |
||||
|
]; |
||||
|
|
||||
|
protected $casts = [ |
||||
|
'exts' => 'array', |
||||
|
'avalible_exts' => 'array', |
||||
|
'memetypes' => 'array', |
||||
|
]; |
||||
|
|
||||
|
protected function exts(): Attribute |
||||
|
{ |
||||
|
return Attribute::make( |
||||
|
set: fn ($value) => json_encode($value), |
||||
|
); |
||||
|
} |
||||
|
protected function avalible_exts(): Attribute |
||||
|
{ |
||||
|
return Attribute::make( |
||||
|
set: fn ($value) => json_encode($value), |
||||
|
); |
||||
|
} |
||||
|
protected function memetypes(): Attribute |
||||
|
{ |
||||
|
return Attribute::make( |
||||
|
set: fn ($value) => json_encode($value), |
||||
|
); |
||||
|
} |
||||
|
|
||||
|
} |
@ -0,0 +1,55 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace App\Models; |
||||
|
|
||||
|
use App\Models\Traits\Validatable; |
||||
|
use Illuminate\Database\Eloquent\Casts\Attribute; |
||||
|
use Illuminate\Database\Eloquent\Factories\HasFactory; |
||||
|
use Illuminate\Database\Eloquent\Model; |
||||
|
use Illuminate\Database\Eloquent\SoftDeletes; |
||||
|
use Illuminate\Support\Str; |
||||
|
|
||||
|
class File extends Model |
||||
|
{ |
||||
|
use HasFactory, SoftDeletes; |
||||
|
|
||||
|
protected $primaryKey = 'uuid'; |
||||
|
|
||||
|
protected $fillable = [ |
||||
|
"uuid", |
||||
|
"original_name", |
||||
|
"ext", |
||||
|
"memetype", |
||||
|
"width", |
||||
|
"server_path", |
||||
|
"height", |
||||
|
"file_size", |
||||
|
"sort", |
||||
|
"alts", |
||||
|
"description", |
||||
|
"user_id", |
||||
|
"ip", |
||||
|
"collection_id", |
||||
|
"published_at", |
||||
|
]; |
||||
|
|
||||
|
protected $casts = [ |
||||
|
'alts' => 'array', |
||||
|
]; |
||||
|
|
||||
|
protected function uuid(): Attribute |
||||
|
{ |
||||
|
return Attribute::make( |
||||
|
set: fn ($value) => Str::uuid(), |
||||
|
); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
protected function alts(): Attribute |
||||
|
{ |
||||
|
return Attribute::make( |
||||
|
set: fn ($value) => json_encode($value), |
||||
|
); |
||||
|
} |
||||
|
|
||||
|
} |
@ -0,0 +1,57 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace Database\Factories; |
||||
|
|
||||
|
use DateTime; |
||||
|
use Illuminate\Database\Eloquent\Factories\Factory; |
||||
|
|
||||
|
/** |
||||
|
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Collection> |
||||
|
*/ |
||||
|
class CollectionFactory extends Factory |
||||
|
{ |
||||
|
/** |
||||
|
* Define the model's default state. |
||||
|
* |
||||
|
* @return array<string, mixed> |
||||
|
*/ |
||||
|
public function definition() |
||||
|
{ |
||||
|
return [ |
||||
|
"name" => fake()->name(), |
||||
|
"public" => "public", |
||||
|
"disk" => "local", |
||||
|
"count" => rand(3,18), |
||||
|
"tmp_support" => rand(0,1), |
||||
|
"remove_tmp_time" => 132, |
||||
|
"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), |
||||
|
"exts" => [ |
||||
|
"jpg", |
||||
|
"jpeg", |
||||
|
"png", |
||||
|
"webp" |
||||
|
], |
||||
|
"avalible_exts" => [ |
||||
|
"jpg", |
||||
|
"jpeg", |
||||
|
"png", |
||||
|
"webp" |
||||
|
], |
||||
|
"memetypes" => [ |
||||
|
"jpg", |
||||
|
"jpeg", |
||||
|
"png", |
||||
|
"webp" |
||||
|
], |
||||
|
"model" => fake()->name(), |
||||
|
"expire_date" => "2022-07-27 09:17:59" |
||||
|
]; |
||||
|
} |
||||
|
} |
@ -0,0 +1,41 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace Database\Factories; |
||||
|
|
||||
|
use App\Models\Collection; |
||||
|
use Illuminate\Database\Eloquent\Factories\Factory; |
||||
|
|
||||
|
/** |
||||
|
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\File> |
||||
|
*/ |
||||
|
class FileFactory extends Factory |
||||
|
{ |
||||
|
/** |
||||
|
* Define the model's default state. |
||||
|
* |
||||
|
* @return array<string, mixed> |
||||
|
*/ |
||||
|
public function definition() |
||||
|
{ |
||||
|
return [ |
||||
|
"uuid" => 1, |
||||
|
"original_name" => fake()->name(), |
||||
|
"ext" => ['jpg', 'jpeg', 'png', 'webp'][rand(0, 3)], |
||||
|
"memetype" => 'image', |
||||
|
"width" => rand(300, 2000), |
||||
|
"height" => rand(300, 2000), |
||||
|
"file_size" => rand(300, 2000), |
||||
|
"server_path" => date('y') . '/' . date('m'), |
||||
|
"sort" => rand(0, 23), |
||||
|
"alts" => [ |
||||
|
'hello wroldswdfouiwref iuwrhgf ow rgfaw ghfawej', |
||||
|
'jhsf asduyfsadf sadf safsuf isfjsdfsudifsduiyf sdiuf sd' |
||||
|
], |
||||
|
"description" => 'ajsfoisahjfoaspf asduf safsafjsh lh', |
||||
|
"user_id" => rand(43724, 382348), |
||||
|
"ip" => "127.0. 0.1", |
||||
|
"collection_id" => Collection::factory()->create()->id, |
||||
|
"published_at" => "2022-07-27 09:17:59", |
||||
|
]; |
||||
|
} |
||||
|
} |
@ -0,0 +1,46 @@ |
|||||
|
<?php |
||||
|
|
||||
|
use Illuminate\Database\Migrations\Migration; |
||||
|
use Illuminate\Database\Schema\Blueprint; |
||||
|
use Illuminate\Support\Facades\Schema; |
||||
|
|
||||
|
return new class extends Migration |
||||
|
{ |
||||
|
/** |
||||
|
* Run the migrations. |
||||
|
* |
||||
|
* @return void |
||||
|
*/ |
||||
|
public function up() |
||||
|
{ |
||||
|
Schema::create('files', function (Blueprint $table) { |
||||
|
$table->uuid('uuid')->primary(); |
||||
|
$table->string("original_name")->nullable(); |
||||
|
$table->string("ext"); |
||||
|
$table->string("mimetype"); |
||||
|
$table->string("server_path"); |
||||
|
$table->bigInteger("width")->nullable(); |
||||
|
$table->bigInteger("height")->nullable(); |
||||
|
$table->bigInteger("file_size")->nullable(); |
||||
|
$table->bigInteger("sort")->nullable(); |
||||
|
$table->json("alts")->nullable(); |
||||
|
$table->string("description")->nullable(); |
||||
|
$table->unsignedBigInteger("user_id")->nullable(); |
||||
|
$table->string("ip")->nullable(); |
||||
|
$table->unsignedBigInteger("collection_id")->nullable(); |
||||
|
$table->timestamp("published_at")->nullable(); |
||||
|
$table->timestamps(); |
||||
|
$table->softDeletes(); |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Reverse the migrations. |
||||
|
* |
||||
|
* @return void |
||||
|
*/ |
||||
|
public function down() |
||||
|
{ |
||||
|
Schema::dropIfExists('files'); |
||||
|
} |
||||
|
}; |
@ -0,0 +1,52 @@ |
|||||
|
<?php |
||||
|
|
||||
|
use Illuminate\Database\Migrations\Migration; |
||||
|
use Illuminate\Database\Schema\Blueprint; |
||||
|
use Illuminate\Support\Facades\Schema; |
||||
|
|
||||
|
return new class extends Migration |
||||
|
{ |
||||
|
/** |
||||
|
* Run the migrations. |
||||
|
* |
||||
|
* @return void |
||||
|
*/ |
||||
|
public function up() |
||||
|
{ |
||||
|
Schema::create('collections', function (Blueprint $table) { |
||||
|
$table->id(); |
||||
|
$table->string("name")->nullable()->unique(); |
||||
|
$table->string("path")->nullable(); |
||||
|
$table->string("public")->nullable(); |
||||
|
$table->string("disk")->nullable(); |
||||
|
$table->integer("count")->nullable(); |
||||
|
$table->boolean("tmp_support")->nullable(); |
||||
|
$table->time("remove_tmp_time")->nullable(); |
||||
|
$table->integer("max_file_size")->nullable(); |
||||
|
$table->integer("min_file_size")->nullable(); |
||||
|
$table->integer("max_width")->nullable(); |
||||
|
$table->integer("min_width")->nullable(); |
||||
|
$table->integer("max_height")->nullable(); |
||||
|
$table->integer("min_height")->nullable(); |
||||
|
$table->boolean("alt_required")->nullable(); |
||||
|
$table->boolean("description_required")->nullable(); |
||||
|
$table->json("exts")->nullable(); |
||||
|
$table->json("avalible_exts")->nullable(); |
||||
|
$table->json("mimetypes")->nullable(); |
||||
|
$table->string("model")->unique()->nullable(); |
||||
|
$table->timestamp("expire_date")->nullable(); |
||||
|
$table->timestamps(); |
||||
|
$table->softDeletes(); |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Reverse the migrations. |
||||
|
* |
||||
|
* @return void |
||||
|
*/ |
||||
|
public function down() |
||||
|
{ |
||||
|
Schema::dropIfExists('collections'); |
||||
|
} |
||||
|
}; |
Binary file not shown.
Before Width: 5175 | Height: 3372 | Size: 19 MiB After Width: 500 | Height: 300 | Size: 101 KiB |
Binary file not shown.
Write
Preview
Loading…
Cancel
Save
Reference in new issue