diff --git a/.env.example b/.env.example index a1b3de4..a4e94bf 100644 --- a/.env.example +++ b/.env.example @@ -1,4 +1,4 @@ -APP_NAME=Laravel +APP_NAME=ManyVideos APP_ENV=local APP_KEY= APP_DEBUG=true @@ -12,6 +12,8 @@ APP_FAKER_LOCALE=en_US APP_MAINTENANCE_DRIVER=file # APP_MAINTENANCE_STORE=database +VIDEO_MAX_UPLOAD_SIZE_MB=20 + PHP_CLI_SERVER_WORKERS=4 BCRYPT_ROUNDS=12 @@ -21,12 +23,12 @@ LOG_STACK=single LOG_DEPRECATIONS_CHANNEL=null LOG_LEVEL=debug -DB_CONNECTION=sqlite -# DB_HOST=127.0.0.1 -# DB_PORT=3306 -# DB_DATABASE=laravel -# DB_USERNAME=root -# DB_PASSWORD= +DB_CONNECTION=mysql +DB_HOST=127.0.0.1 +DB_PORT=3306 +DB_DATABASE=laravel +DB_USERNAME=root +DB_PASSWORD= SESSION_DRIVER=database SESSION_LIFETIME=120 @@ -34,10 +36,18 @@ SESSION_ENCRYPT=false SESSION_PATH=/ SESSION_DOMAIN=null -BROADCAST_CONNECTION=log +BROADCAST_CONNECTION=pusher FILESYSTEM_DISK=local QUEUE_CONNECTION=database +PUSHER_APP_ID= +PUSHER_APP_KEY= +PUSHER_APP_SECRET= +PUSHER_APP_CLUSTER= + +VITE_PUSHER_APP_KEY="${PUSHER_APP_KEY}" +VITE_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}" + CACHE_STORE=database CACHE_PREFIX= diff --git a/.gitignore b/.gitignore index bec2973..1d223b7 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,7 @@ /vendor .env .env.backup +.env.testing .env.production .phpactor.json .phpunit.result.cache diff --git a/README.md b/README.md index 1a4c26b..cb2e6ce 100644 --- a/README.md +++ b/README.md @@ -1,66 +1,55 @@ -

Laravel Logo

+# ManyVideos -

-Build Status -Total Downloads -Latest Stable Version -License -

+## Installation -## About Laravel +Install dependencies: -Laravel is a web application framework with expressive, elegant syntax. We believe development must be an enjoyable and creative experience to be truly fulfilling. Laravel takes the pain out of development by easing common tasks used in many web projects, such as: +```bash +php composer install +npm run install +``` -- [Simple, fast routing engine](https://laravel.com/docs/routing). -- [Powerful dependency injection container](https://laravel.com/docs/container). -- Multiple back-ends for [session](https://laravel.com/docs/session) and [cache](https://laravel.com/docs/cache) storage. -- Expressive, intuitive [database ORM](https://laravel.com/docs/eloquent). -- Database agnostic [schema migrations](https://laravel.com/docs/migrations). -- [Robust background job processing](https://laravel.com/docs/queues). -- [Real-time event broadcasting](https://laravel.com/docs/broadcasting). +Generate the app key -Laravel is accessible, powerful, and provides tools required for large, robust applications. +```bash +php artisan key:generate +``` -## Learning Laravel +Copy the `.env.example` file to `.env` and fill in the database information -Laravel has the most extensive and thorough [documentation](https://laravel.com/docs) and video tutorial library of all modern web application frameworks, making it a breeze to get started with the framework. +```bash +cp .env.example .env -You may also try the [Laravel Bootcamp](https://bootcamp.laravel.com), where you will be guided through building a modern Laravel application from scratch. +# REMEMBER SET THE DATABASE INFORMATION AND PUSHER KEYS -If you don't feel like reading, [Laracasts](https://laracasts.com) can help. Laracasts contains thousands of video tutorials on a range of topics including Laravel, modern PHP, unit testing, and JavaScript. Boost your skills by digging into our comprehensive video library. +PUSHER_APP_ID= +PUSHER_APP_KEY= +PUSHER_APP_SECRET= +PUSHER_APP_CLUSTER= -## Laravel Sponsors +DB_CONNECTION=mysql +DB_HOST=127.0.0.1 +DB_PORT=3306 +DB_DATABASE=laravel +DB_USERNAME=root +DB_PASSWORD= +``` -We would like to extend our thanks to the following sponsors for funding Laravel development. If you are interested in becoming a sponsor, please visit the [Laravel Partners program](https://partners.laravel.com). +Run the migrations -### Premium Partners +```bash +php artisan migrate +``` -- **[Vehikl](https://vehikl.com/)** -- **[Tighten Co.](https://tighten.co)** -- **[WebReinvent](https://webreinvent.com/)** -- **[Kirschbaum Development Group](https://kirschbaumdevelopment.com)** -- **[64 Robots](https://64robots.com)** -- **[Curotec](https://www.curotec.com/services/technologies/laravel/)** -- **[Cyber-Duck](https://cyber-duck.co.uk)** -- **[DevSquad](https://devsquad.com/hire-laravel-developers)** -- **[Jump24](https://jump24.co.uk)** -- **[Redberry](https://redberry.international/laravel/)** -- **[Active Logic](https://activelogic.com)** -- **[byte5](https://byte5.de)** -- **[OP.GG](https://op.gg)** +Run the server -## Contributing +```bash +php artisan serve +``` -Thank you for considering contributing to the Laravel framework! The contribution guide can be found in the [Laravel documentation](https://laravel.com/docs/contributions). +Run the worker -## Code of Conduct +```bash +php artisan queue:work +``` -In order to ensure that the Laravel community is welcoming to all, please review and abide by the [Code of Conduct](https://laravel.com/docs/contributions#code-of-conduct). - -## Security Vulnerabilities - -If you discover a security vulnerability within Laravel, please send an e-mail to Taylor Otwell via [taylor@laravel.com](mailto:taylor@laravel.com). All security vulnerabilities will be promptly addressed. - -## License - -The Laravel framework is open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT). diff --git a/app/Enums/VideoStatusEnum.php b/app/Enums/VideoStatusEnum.php new file mode 100644 index 0000000..1efe856 --- /dev/null +++ b/app/Enums/VideoStatusEnum.php @@ -0,0 +1,11 @@ +user(); + $comment = $video->comments()->create([ + ...$request->validated(), + 'user_id' => $user->id, + ]); + + return redirect()->route('videos.show', $video); + } + + /** + * Display the specified resource. + */ + public function show(Comment $comment) + { + // + } + + /** + * Show the form for editing the specified resource. + */ + public function edit(Comment $comment) + { + // + } + + /** + * Update the specified resource in storage. + */ + public function update(UpdateCommentRequest $request, Video $video, Comment $comment) + { + + } + + /** + * Remove the specified resource from storage. + */ + public function destroy(Video $video, Comment $comment) + { + $comment->delete(); + + return redirect()->route('videos.show', $video); + } +} diff --git a/app/Http/Controllers/HomeController.php b/app/Http/Controllers/HomeController.php new file mode 100644 index 0000000..f68428c --- /dev/null +++ b/app/Http/Controllers/HomeController.php @@ -0,0 +1,51 @@ +perpage ?? 10; + + // it will show only the videos that are processed + $videos = Video::where('status', VideoStatusEnum::Processed) + ->when($request->search, function ($query, $search) { + $query->where('title', 'like', "%$search%"); + }) + ->when($request->size_min, function ($query, $size_min) { + $size_min = $size_min * 1024 * 1024; + $query->where('size', '>=', $size_min); + }) + ->when($request->size_max, function ($query, $size_max) { + $size_max = $size_max * 1024 * 1024; + $query->where('size', '<=', $size_max); + }) + ->when($request->duration_min, function ($query, $duration_min) { + $duration_min = $duration_min * 60; + $query->where('duration', '>=', $duration_min); + }) + ->when($request->duration_max, function ($query, $duration_max) { + $duration_max = $duration_max * 60; + $query->where('duration', '<=', $duration_max); + }) + ->withMinAttributes() + ->with('user', 'tags') + ->withCommentsCount() + ->latest() + ->paginate($perpage); + + return Inertia::render('Home', [ + 'canLogin' => Route::has('login'), + 'canRegister' => Route::has('register'), + 'videos' => $videos, + ]); + } +} diff --git a/app/Http/Controllers/TagController.php b/app/Http/Controllers/TagController.php new file mode 100644 index 0000000..a4428cc --- /dev/null +++ b/app/Http/Controllers/TagController.php @@ -0,0 +1,72 @@ +validated()); + + return response()->json($tag, 201); + } + + /** + * Display the specified resource. + */ + public function show(Tag $tag) + { + // + } + + /** + * Show the form for editing the specified resource. + */ + public function edit(Tag $tag) + { + // + } + + /** + * Update the specified resource in storage. + */ + public function update(UpdateTagRequest $request, Tag $tag) + { + $tag->update($request->validated()); + + return response()->json($tag); + } + + /** + * Remove the specified resource from storage. + */ + public function destroy(Tag $tag) + { + $tag->delete(); + + return response()->json(null, 204); + } +} diff --git a/app/Http/Controllers/VideoController.php b/app/Http/Controllers/VideoController.php new file mode 100644 index 0000000..4797a74 --- /dev/null +++ b/app/Http/Controllers/VideoController.php @@ -0,0 +1,165 @@ +user()->id) + ->with('tags') + ->withCommentsCount() + ->orderBy('created_at', 'desc') + ->get(); + + return Inertia::render('Videos/Index', [ + 'videos' => $videos, + ]); + } + + /** + * Show the form for creating a new resource. + */ + public function create() + { + $tag = Tag::all(); + return Inertia::render('Videos/Create', [ + 'tags' => $tag, + ]); + } + + /** + * Store a newly created resource in storage. + */ + public function store(StoreVideoRequest $request) + { + $data = [ + ...$request->only('title', 'description'), + 'thumbnail' => 'thumbnails/default.jpg', + 'size' => $request->file('file')->getSize(), + 'duration' => 60, + 'url' => $request->file('file')->store('videos', 'public'), + 'user_id' => $request->user()->id, + ]; + + // process video + $video = Video::create($data); + + Bus::chain([ + new ProcessVideo($video), + new GenerateVideoThumbnail($video), + new SaveVideoMetadata($video), + new UpdateVideoStatus($video, VideoStatusEnum::Processed), + new SendVideoProcessingCompletedNotification($video), + ])->dispatch(); + + if ($request->has('tags')) { + $tags = Arr::map($request->tags, function ($tag) { + $name = strtolower($tag); + $color = '#' . substr(md5($name), 0, 6); + return Tag::firstOrCreate(['name' => $name, 'color' => $color])->id; + }); + + $video->tags()->attach($tags); + } + + return redirect()->route('videos.index'); + } + + /** + * Display the specified resource. + */ + public function show(Request $request, Video $video) + { + Gate::authorize('view', $video); + + return Inertia::render('Videos/Show', [ + 'video' => $video->load('tags', 'user', 'comments.user'), + ]); + } + + /** + * Show the form for editing the specified resource. + */ + public function edit(Video $video) + { + Gate::authorize('update', $video); + + return Inertia::render('Videos/Edit', [ + 'video' => $video->load('tags'), + ]); + } + + /** + * Update the specified resource in storage. + */ + public function update(UpdateVideoRequest $request, Video $video) + { + Gate::authorize('update', $video); + + $video->update($request->only('title', 'description')); + + if ($request->hasFile('file')) { + $video->update([ + 'url' => $request->file('file')->store('videos', 'public'), + 'size' => $request->file('file')->getSize(), + 'status' => VideoStatusEnum::Processing, + ]); + + Bus::chain([ + new ProcessVideo($video), + new GenerateVideoThumbnail($video), + new SaveVideoMetadata($video), + new UpdateVideoStatus($video, VideoStatusEnum::Processed), + new SendVideoProcessingCompletedNotification($video), + ])->dispatch(); + } + + if ($request->has('tags')) { + $tags = Arr::map($request->tags, function ($tag) { + $name = strtolower($tag); + $color = '#' . substr(md5($name), 0, 6); + return Tag::firstOrCreate(['name' => $name, 'color' => $color])->id; + }); + + $video->tags()->sync($tags); + } + + return redirect()->route('videos.index'); + } + + /** + * Remove the specified resource from storage. + */ + public function destroy(Video $video) + { + Gate::authorize('delete', $video); + + Storage::disk('public')->delete($video->url); + Storage::disk('public')->delete($video->thumbnail); + + $video->delete(); + + return redirect()->route('videos.index'); + } +} diff --git a/app/Http/Middleware/HandleInertiaRequests.php b/app/Http/Middleware/HandleInertiaRequests.php index 81c999c..683aa89 100644 --- a/app/Http/Middleware/HandleInertiaRequests.php +++ b/app/Http/Middleware/HandleInertiaRequests.php @@ -36,7 +36,14 @@ public function version(Request $request): ?string public function share(Request $request): array { return array_merge(parent::share($request), [ - // + 'config' => [ + 'videos' => [ + 'max_file_size' => config('videos.max-upload-size'), + 'mime_types' => config('videos.mime-types'), + 'extensions' => config('videos.extensions'), + ], + ], + 'userUnreadNotifications' => fn () => $request->user() ? $request->user()->unreadNotifications : null, ]); } } diff --git a/app/Http/Requests/IndexHomeRequest.php b/app/Http/Requests/IndexHomeRequest.php new file mode 100644 index 0000000..124f60a --- /dev/null +++ b/app/Http/Requests/IndexHomeRequest.php @@ -0,0 +1,34 @@ +|string> + */ + public function rules(): array + { + return [ + 'perpage' => ['nullable', 'integer', 'min:1', 'max:20'], + 'page' => ['nullable', 'integer', 'min:1', 'max:100'], + 'search' => ['nullable', 'string', 'max:255'], + 'size_min' => ['nullable', 'integer', 'min:0', 'max:500'], + 'size_max' => ['nullable', 'integer', 'min:0', 'max:500'], + 'duration_min' => ['nullable', 'integer', 'min:0', 'max:10'], + 'duration_max' => ['nullable', 'integer', 'min:0', 'max:10'], + ]; + } +} diff --git a/app/Http/Requests/StoreCommentRequest.php b/app/Http/Requests/StoreCommentRequest.php new file mode 100644 index 0000000..7c38a60 --- /dev/null +++ b/app/Http/Requests/StoreCommentRequest.php @@ -0,0 +1,28 @@ +|string> + */ + public function rules(): array + { + return [ + 'content' => ['required', 'string'], + ]; + } +} diff --git a/app/Http/Requests/StoreTagRequest.php b/app/Http/Requests/StoreTagRequest.php new file mode 100644 index 0000000..8cf320f --- /dev/null +++ b/app/Http/Requests/StoreTagRequest.php @@ -0,0 +1,29 @@ +|string> + */ + public function rules(): array + { + return [ + 'name' => 'required|unique:tags,name', + 'color' => 'nullable', + ]; + } +} diff --git a/app/Http/Requests/StoreVideoRequest.php b/app/Http/Requests/StoreVideoRequest.php new file mode 100644 index 0000000..97d29a6 --- /dev/null +++ b/app/Http/Requests/StoreVideoRequest.php @@ -0,0 +1,36 @@ +|string> + */ + public function rules(): array + { + return [ + 'title' => ['required', 'string', 'max:255'], + 'description' => ['required', 'string'], + 'file' => [ + 'required', + 'file', + 'mimetypes:' . implode("," , config('videos.mime-types')), + 'max:' . config('videos.max-upload-size') / 1024, // in KB + ], + 'tags' => ['nullable', 'array', 'max:5', 'distinct'], + ]; + } +} diff --git a/app/Http/Requests/UpdateCommentRequest.php b/app/Http/Requests/UpdateCommentRequest.php new file mode 100644 index 0000000..c0cae39 --- /dev/null +++ b/app/Http/Requests/UpdateCommentRequest.php @@ -0,0 +1,28 @@ +|string> + */ + public function rules(): array + { + return [ + // + ]; + } +} diff --git a/app/Http/Requests/UpdateTagRequest.php b/app/Http/Requests/UpdateTagRequest.php new file mode 100644 index 0000000..a04055c --- /dev/null +++ b/app/Http/Requests/UpdateTagRequest.php @@ -0,0 +1,35 @@ +|string> + */ + public function rules(): array + { + return [ + 'name' => 'required|unique:tags,name,' . $this->tag->id, + 'color' => 'nullable', + ]; + } +} diff --git a/app/Http/Requests/UpdateVideoRequest.php b/app/Http/Requests/UpdateVideoRequest.php new file mode 100644 index 0000000..afecd7a --- /dev/null +++ b/app/Http/Requests/UpdateVideoRequest.php @@ -0,0 +1,35 @@ +|string> + */ + public function rules(): array + { + return [ + 'title' => ['required', 'string', 'max:255'], + 'description' => ['required', 'string'], + 'file' => [ + 'file', + 'mimetypes:' . implode("," , config('videos.mime-types')), + 'max:' . config('videos.max-upload-size') / 1024, // in KB + ], + 'tags' => ['nullable', 'array', 'max:5', 'distinct'], + ]; + } +} diff --git a/app/Interfaces/MultimediaService.php b/app/Interfaces/MultimediaService.php new file mode 100644 index 0000000..ab3ea1e --- /dev/null +++ b/app/Interfaces/MultimediaService.php @@ -0,0 +1,32 @@ +generateVideoThumbnail($this->video->url); + + $this->video->update([ + 'thumbnail' => $thumbnail_path, + ]); + } +} diff --git a/app/Jobs/ProcessVideo.php b/app/Jobs/ProcessVideo.php new file mode 100644 index 0000000..b325225 --- /dev/null +++ b/app/Jobs/ProcessVideo.php @@ -0,0 +1,56 @@ +compressVideo($this->video->url); + + $this->video->update([ + 'url' => $compressed_path, + ]); + } + + public function failed(\Throwable $exception) + { + Log::error($exception->getMessage()); + + $this->video->update([ + 'status' => VideoStatusEnum::Failed, + ]); + + Storage::disk('public')->delete($this->video->url); + + Notification::send($this->video->user, new VideoProcessingFailedNotification($this->video)); + } +} diff --git a/app/Jobs/SaveVideoMetadata.php b/app/Jobs/SaveVideoMetadata.php new file mode 100644 index 0000000..387357c --- /dev/null +++ b/app/Jobs/SaveVideoMetadata.php @@ -0,0 +1,39 @@ +getVideoMetadata($this->video->url); + + $this->video->update([ + 'duration' => $metadata['duration'], + 'size' => $metadata['size'], + 'width' => $metadata['width'], + 'height' => $metadata['height'], + ]); + } +} diff --git a/app/Jobs/SendVideoProcessingCompletedNotification.php b/app/Jobs/SendVideoProcessingCompletedNotification.php new file mode 100644 index 0000000..877a7f8 --- /dev/null +++ b/app/Jobs/SendVideoProcessingCompletedNotification.php @@ -0,0 +1,31 @@ +video->load('user'); + + $this->video->user->notify(new VideoProcessingCompleted($this->video)); + } +} diff --git a/app/Jobs/UpdateVideoStatus.php b/app/Jobs/UpdateVideoStatus.php new file mode 100644 index 0000000..4720509 --- /dev/null +++ b/app/Jobs/UpdateVideoStatus.php @@ -0,0 +1,32 @@ +video->update(['status' => $this->status]); + } +} diff --git a/app/Models/Comment.php b/app/Models/Comment.php new file mode 100644 index 0000000..5decb78 --- /dev/null +++ b/app/Models/Comment.php @@ -0,0 +1,25 @@ + */ + use HasFactory; + + protected $fillable = ['content', 'user_id', 'video_id']; + + public function user(): BelongsTo + { + return $this->belongsTo(User::class); + } + + public function video(): BelongsTo + { + return $this->belongsTo(Video::class); + } +} diff --git a/app/Models/Tag.php b/app/Models/Tag.php new file mode 100644 index 0000000..920b21a --- /dev/null +++ b/app/Models/Tag.php @@ -0,0 +1,20 @@ + */ + use HasFactory; + + protected $fillable = ['name', 'color']; + + public function videos(): BelongsToMany + { + return $this->belongsToMany(Video::class, 'video_tag', 'tag_id', 'video_id'); + } +} diff --git a/app/Models/User.php b/app/Models/User.php index 3dd467c..5c262f6 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -64,4 +64,9 @@ protected function casts(): array 'password' => 'hashed', ]; } + + public function videos() + { + return $this->hasMany(Video::class); + } } diff --git a/app/Models/Video.php b/app/Models/Video.php new file mode 100644 index 0000000..83b97e7 --- /dev/null +++ b/app/Models/Video.php @@ -0,0 +1,58 @@ + */ + use HasFactory; + + protected $fillable = [ + 'title', + 'description', + 'url', + 'thumbnail', + 'size', + 'duration', + 'width', + 'height', + 'status', + 'user_id', + ]; + + protected $casts = [ + 'status' => VideoStatusEnum::class, + ]; + + public function scopeWithCommentsCount($query) + { + $query->withCount('comments'); + } + + public function scopeWithMinAttributes($query) + { + $query->select('id', 'title', 'thumbnail', 'duration', 'status', 'user_id', 'created_at'); + } + + public function user(): BelongsTo + { + return $this->belongsTo(User::class); + } + + public function comments(): HasMany + { + return $this->hasMany(Comment::class); + } + + public function tags(): BelongsToMany + { + return $this->belongsToMany(Tag::class, 'video_tag', 'video_id', 'tag_id'); + } +} diff --git a/app/Notifications/VideoProcessingCompleted.php b/app/Notifications/VideoProcessingCompleted.php new file mode 100644 index 0000000..4abe9d5 --- /dev/null +++ b/app/Notifications/VideoProcessingCompleted.php @@ -0,0 +1,60 @@ + + */ + public function via(object $notifiable): array + { + return ['mail', 'database', 'broadcast']; + } + + /** + * Get the mail representation of the notification. + */ + public function toMail(object $notifiable): MailMessage + { + return (new MailMessage) + ->subject('Your video has been processed!') + ->line("Your video '{$this->video->title}' has been processed!"); + } + + /** + * Get the array representation of the notification. + * + * @return array + */ + public function toArray(object $notifiable): array + { + $this->video->load('user'); + + return [ + 'user_id' => $this->video->user->id, + 'video_id' => $this->video->id, + 'video_title' => $this->video->title, + ]; + } +} diff --git a/app/Notifications/VideoProcessingFailedNotification.php b/app/Notifications/VideoProcessingFailedNotification.php new file mode 100644 index 0000000..abfb17d --- /dev/null +++ b/app/Notifications/VideoProcessingFailedNotification.php @@ -0,0 +1,56 @@ + + */ + public function via(object $notifiable): array + { + return ['mail', 'database', 'broadcast']; + } + + /** + * Get the mail representation of the notification. + */ + public function toMail(object $notifiable): MailMessage + { + return (new MailMessage) + ->subject('Your video has failed to process!') + ->line("Your video '{$this->video->title}' has failed to process!"); + } + + /** + * Get the array representation of the notification. + * + * @return array + */ + public function toArray(object $notifiable): array + { + return [ + 'user_id' => $this->video->user->id, + 'video_id' => $this->video->id, + 'video_title' => $this->video->title, + ]; + } +} diff --git a/app/Policies/CommentPolicy.php b/app/Policies/CommentPolicy.php new file mode 100644 index 0000000..e131bf5 --- /dev/null +++ b/app/Policies/CommentPolicy.php @@ -0,0 +1,70 @@ +status === VideoStatusEnum::Processed; + } + + /** + * Determine whether the user can update the model. + */ + public function update(User $user, Comment $comment): bool + { + return false; + } + + /** + * Determine whether the user can delete the model. + */ + public function delete(User $user, Comment $comment): bool + { + return $user->id === $comment->user_id; + } + + /** + * Determine whether the user can restore the model. + */ + public function restore(User $user, Comment $comment): bool + { + return false; + } + + /** + * Determine whether the user can permanently delete the model. + */ + public function forceDelete(User $user, Comment $comment): bool + { + return false; + } +} diff --git a/app/Policies/VideoPolicy.php b/app/Policies/VideoPolicy.php new file mode 100644 index 0000000..8f0abb6 --- /dev/null +++ b/app/Policies/VideoPolicy.php @@ -0,0 +1,67 @@ +status === VideoStatusEnum::Processed; + } + + /** + * Determine whether the user can create models. + */ + public function create(User $user): bool + { + return true; + } + + /** + * Determine whether the user can update the model. + */ + public function update(User $user, Video $video): bool + { + return $video->user_id === $user->id; + } + + /** + * Determine whether the user can delete the model. + */ + public function delete(User $user, Video $video): bool + { + return $video->user_id === $user->id; + } + + /** + * Determine whether the user can restore the model. + */ + public function restore(User $user, Video $video): bool + { + return $video->user_id === $user->id; + } + + /** + * Determine whether the user can permanently delete the model. + */ + public function forceDelete(User $user, Video $video): bool + { + return $video->user_id === $user->id; + } +} diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 452e6b6..cbf64df 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -2,6 +2,14 @@ namespace App\Providers; +use App\Interfaces\MultimediaService; +use App\Models\Comment; +use App\Models\Video; +use App\Policies\CommentPolicy; +use App\Policies\VideoPolicy; +use App\Services\FFmpegService; +use Illuminate\Database\Eloquent\Model; +use Illuminate\Support\Facades\Gate; use Illuminate\Support\ServiceProvider; class AppServiceProvider extends ServiceProvider @@ -11,7 +19,10 @@ class AppServiceProvider extends ServiceProvider */ public function register(): void { - // + $this->app->bind( + MultimediaService::class, + FFmpegService::class + ); } /** @@ -19,6 +30,9 @@ public function register(): void */ public function boot(): void { - // + Model::preventLazyLoading(! app()->isProduction()); + + Gate::policy(Comment::class, CommentPolicy::class); + Gate::policy(Video::class, VideoPolicy::class); } } diff --git a/app/Services/FFmpegService.php b/app/Services/FFmpegService.php new file mode 100644 index 0000000..336386b --- /dev/null +++ b/app/Services/FFmpegService.php @@ -0,0 +1,62 @@ +open($filepath) + ->export() + ->toDisk(Storage::disk('public')) + ->inFormat(new \FFMpeg\Format\Video\X264('libmp3lame', 'libx264')) + ->save($compressed_filepath); + + return $compressed_filepath; + } + + public function generateVideoThumbnail(string $filepath): string + { + $filepath_extension = pathinfo($filepath, PATHINFO_EXTENSION); + $thumbnail_filepath = str_replace(".{$filepath_extension}", '-thumbnail.jpg', $filepath); + + FFMpeg::fromFilesystem(Storage::disk('public')) + ->open($filepath) + ->getFrameFromSeconds(5) + ->export() + ->toDisk(Storage::disk('public')) + ->save($thumbnail_filepath); + + return $thumbnail_filepath; + } + + public function getVideoMetadata(string $filepath): array + { + // we need width, height, duration, and size + if (!Storage::disk('public')->exists($filepath)) { + return []; + } + + $metadata = FFMpeg::fromFilesystem(Storage::disk('public')) + ->open($filepath) + ->getStreams(); + + $metadata = Arr::first($metadata)->all(); + $size = Storage::disk('public')->size($filepath); + + return [ + 'width' => $metadata['width'], + 'height' => $metadata['height'], + 'duration' => $metadata['duration'], + 'size' => $size, + ]; + } +} diff --git a/bootstrap/app.php b/bootstrap/app.php index 461aafd..13d0951 100644 --- a/bootstrap/app.php +++ b/bootstrap/app.php @@ -9,6 +9,7 @@ web: __DIR__.'/../routes/web.php', api: __DIR__.'/../routes/api.php', commands: __DIR__.'/../routes/console.php', + channels: __DIR__.'/../routes/channels.php', health: '/up', ) ->withMiddleware(function (Middleware $middleware) { diff --git a/composer.json b/composer.json index 4b309f4..770c776 100644 --- a/composer.json +++ b/composer.json @@ -12,6 +12,8 @@ "laravel/jetstream": "^5.3", "laravel/sanctum": "^4.0", "laravel/tinker": "^2.9", + "pbmedia/laravel-ffmpeg": "^8.6", + "pusher/pusher-php-server": "^7.2", "tightenco/ziggy": "^2.0" }, "require-dev": { diff --git a/config/broadcasting.php b/config/broadcasting.php new file mode 100644 index 0000000..ebc3fb9 --- /dev/null +++ b/config/broadcasting.php @@ -0,0 +1,82 @@ + env('BROADCAST_CONNECTION', 'null'), + + /* + |-------------------------------------------------------------------------- + | Broadcast Connections + |-------------------------------------------------------------------------- + | + | Here you may define all of the broadcast connections that will be used + | to broadcast events to other systems or over WebSockets. Samples of + | each available type of connection are provided inside this array. + | + */ + + 'connections' => [ + + 'reverb' => [ + 'driver' => 'reverb', + 'key' => env('REVERB_APP_KEY'), + 'secret' => env('REVERB_APP_SECRET'), + 'app_id' => env('REVERB_APP_ID'), + 'options' => [ + 'host' => env('REVERB_HOST'), + 'port' => env('REVERB_PORT', 443), + 'scheme' => env('REVERB_SCHEME', 'https'), + 'useTLS' => env('REVERB_SCHEME', 'https') === 'https', + ], + 'client_options' => [ + // Guzzle client options: https://docs.guzzlephp.org/en/stable/request-options.html + ], + ], + + 'pusher' => [ + 'driver' => 'pusher', + 'key' => env('PUSHER_APP_KEY'), + 'secret' => env('PUSHER_APP_SECRET'), + 'app_id' => env('PUSHER_APP_ID'), + 'options' => [ + 'cluster' => env('PUSHER_APP_CLUSTER'), + 'host' => env('PUSHER_HOST') ?: 'api-'.env('PUSHER_APP_CLUSTER', 'mt1').'.pusher.com', + 'port' => env('PUSHER_PORT', 443), + 'scheme' => env('PUSHER_SCHEME', 'https'), + 'encrypted' => true, + 'useTLS' => env('PUSHER_SCHEME', 'https') === 'https', + ], + 'client_options' => [ + // Guzzle client options: https://docs.guzzlephp.org/en/stable/request-options.html + ], + ], + + 'ably' => [ + 'driver' => 'ably', + 'key' => env('ABLY_KEY'), + ], + + 'log' => [ + 'driver' => 'log', + ], + + 'null' => [ + 'driver' => 'null', + ], + + ], + +]; diff --git a/config/laravel-ffmpeg.php b/config/laravel-ffmpeg.php new file mode 100644 index 0000000..25b8913 --- /dev/null +++ b/config/laravel-ffmpeg.php @@ -0,0 +1,21 @@ + [ + 'binaries' => env('FFMPEG_BINARIES', 'ffmpeg'), + + 'threads' => 12, // set to false to disable the default 'threads' filter + ], + + 'ffprobe' => [ + 'binaries' => env('FFPROBE_BINARIES', 'ffprobe'), + ], + + 'timeout' => 3600, + + 'log_channel' => env('LOG_CHANNEL', 'stack'), // set to false to completely disable logging + + 'temporary_files_root' => env('FFMPEG_TEMPORARY_FILES_ROOT', sys_get_temp_dir()), + + 'temporary_files_encrypted_hls' => env('FFMPEG_TEMPORARY_ENCRYPTED_HLS', env('FFMPEG_TEMPORARY_FILES_ROOT', sys_get_temp_dir())), +]; diff --git a/config/videos.php b/config/videos.php new file mode 100644 index 0000000..4640c8d --- /dev/null +++ b/config/videos.php @@ -0,0 +1,14 @@ + 'videos', + 'disk' => 'public', + 'max-upload-size' => env('VIDEO_MAX_UPLOAD_SIZE_MB', 20) * 1024 * 1024, + 'max-upload-size-human' => env('VIDEO_MAX_UPLOAD_SIZE_MB', 20) . 'MB', + 'mime-types' => [ + 'video/mp4', + ], + 'extensions' => [ + 'mp4', + ], +]; diff --git a/database/factories/CommentFactory.php b/database/factories/CommentFactory.php new file mode 100644 index 0000000..590e51d --- /dev/null +++ b/database/factories/CommentFactory.php @@ -0,0 +1,27 @@ + + */ +class CommentFactory extends Factory +{ + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + return [ + 'content' => $this->faker->sentence, + 'user_id' => User::factory(), + 'video_id' => Video::factory(), + ]; + } +} diff --git a/database/factories/TagFactory.php b/database/factories/TagFactory.php new file mode 100644 index 0000000..b5c13d3 --- /dev/null +++ b/database/factories/TagFactory.php @@ -0,0 +1,24 @@ + + */ +class TagFactory extends Factory +{ + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + return [ + 'name' => $this->faker->unique()->word, + 'color' => $this->faker->hexColor, + ]; + } +} diff --git a/database/factories/VideoFactory.php b/database/factories/VideoFactory.php new file mode 100644 index 0000000..883e9af --- /dev/null +++ b/database/factories/VideoFactory.php @@ -0,0 +1,29 @@ + + */ +class VideoFactory extends Factory +{ + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + return [ + 'title' => $this->faker->sentence, + 'description' => $this->faker->paragraph, + 'url' => $this->faker->url, + 'thumbnail' => $this->faker->imageUrl, + 'size' => $this->faker->randomNumber, + 'duration' => $this->faker->randomNumber, + 'user_id' => \App\Models\User::factory(), + ]; + } +} diff --git a/database/migrations/2024_11_26_211251_create_videos_table.php b/database/migrations/2024_11_26_211251_create_videos_table.php new file mode 100644 index 0000000..817a1f9 --- /dev/null +++ b/database/migrations/2024_11_26_211251_create_videos_table.php @@ -0,0 +1,34 @@ +id(); + $table->string('title'); + $table->string('description'); + $table->string('url'); + $table->string('thumbnail'); + $table->integer('size'); + $table->integer('duration'); + $table->foreignId('user_id')->constrained(); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('videos'); + } +}; diff --git a/database/migrations/2024_11_28_160843_add_width_height_fields_to_videos_table.php b/database/migrations/2024_11_28_160843_add_width_height_fields_to_videos_table.php new file mode 100644 index 0000000..9c78819 --- /dev/null +++ b/database/migrations/2024_11_28_160843_add_width_height_fields_to_videos_table.php @@ -0,0 +1,30 @@ +unsignedInteger('width')->nullable(); + $table->unsignedInteger('height')->nullable(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('videos', function (Blueprint $table) { + $table->dropColumn('width'); + $table->dropColumn('height'); + }); + } +}; diff --git a/database/migrations/2024_11_28_170324_create_notifications_table.php b/database/migrations/2024_11_28_170324_create_notifications_table.php new file mode 100644 index 0000000..d738032 --- /dev/null +++ b/database/migrations/2024_11_28_170324_create_notifications_table.php @@ -0,0 +1,31 @@ +uuid('id')->primary(); + $table->string('type'); + $table->morphs('notifiable'); + $table->text('data'); + $table->timestamp('read_at')->nullable(); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('notifications'); + } +}; diff --git a/database/migrations/2024_11_28_201406_add_status_field_to_videos_table.php b/database/migrations/2024_11_28_201406_add_status_field_to_videos_table.php new file mode 100644 index 0000000..fa3a0a2 --- /dev/null +++ b/database/migrations/2024_11_28_201406_add_status_field_to_videos_table.php @@ -0,0 +1,28 @@ +string('status')->default('processing'); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('videos', function (Blueprint $table) { + $table->dropColumn('status'); + }); + } +}; diff --git a/database/migrations/2024_11_29_010845_create_tags_table.php b/database/migrations/2024_11_29_010845_create_tags_table.php new file mode 100644 index 0000000..2754c13 --- /dev/null +++ b/database/migrations/2024_11_29_010845_create_tags_table.php @@ -0,0 +1,29 @@ +id(); + $table->string('name')->unique(); + $table->string('color')->nullable(); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('tags'); + } +}; diff --git a/database/migrations/2024_11_29_013840_create_video_tag_table.php b/database/migrations/2024_11_29_013840_create_video_tag_table.php new file mode 100644 index 0000000..99f9637 --- /dev/null +++ b/database/migrations/2024_11_29_013840_create_video_tag_table.php @@ -0,0 +1,29 @@ +id(); + $table->foreignId('video_id')->constrained()->cascadeOnDelete(); + $table->foreignId('tag_id')->constrained()->cascadeOnDelete(); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('video_tag'); + } +}; diff --git a/database/migrations/2024_11_29_133509_create_comments_table.php b/database/migrations/2024_11_29_133509_create_comments_table.php new file mode 100644 index 0000000..b1f4781 --- /dev/null +++ b/database/migrations/2024_11_29_133509_create_comments_table.php @@ -0,0 +1,30 @@ +id(); + $table->foreignId('video_id')->constrained()->cascadeOnDelete(); + $table->foreignId('user_id')->constrained()->cascadeOnDelete(); + $table->text('content'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('comments'); + } +}; diff --git a/database/migrations/2024_11_30_195409_alter_field_description_to_videos_table.php b/database/migrations/2024_11_30_195409_alter_field_description_to_videos_table.php new file mode 100644 index 0000000..741d3de --- /dev/null +++ b/database/migrations/2024_11_30_195409_alter_field_description_to_videos_table.php @@ -0,0 +1,28 @@ +text('description')->change(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('videos', function (Blueprint $table) { + $table->string('description')->change(); + }); + } +}; diff --git a/database/seeders/CommentSeeder.php b/database/seeders/CommentSeeder.php new file mode 100644 index 0000000..5f45105 --- /dev/null +++ b/database/seeders/CommentSeeder.php @@ -0,0 +1,17 @@ +count(10)->create(); + } +} diff --git a/database/seeders/VideoSeeder.php b/database/seeders/VideoSeeder.php new file mode 100644 index 0000000..17eaf5e --- /dev/null +++ b/database/seeders/VideoSeeder.php @@ -0,0 +1,23 @@ +create([ + 'user_id' => $user->id, + ]); + } +} diff --git a/package.json b/package.json index 401edfd..0af6762 100644 --- a/package.json +++ b/package.json @@ -13,8 +13,10 @@ "autoprefixer": "^10.4.16", "axios": "^1.7.4", "concurrently": "^9.0.1", + "laravel-echo": "^1.17.1", "laravel-vite-plugin": "^1.0", "postcss": "^8.4.32", + "pusher-js": "^8.4.0-rc2", "tailwindcss": "^3.4.0", "vite": "^5.0", "vite-plugin-vue-devtools": "^7.6.4", diff --git a/phpunit.xml b/phpunit.xml index c09b5bc..5c3671d 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -22,7 +22,6 @@ - diff --git a/resources/css/app.css b/resources/css/app.css index 0de2120..e4ee575 100644 --- a/resources/css/app.css +++ b/resources/css/app.css @@ -1,3 +1,5 @@ +@import url('https://fonts.googleapis.com/css2?family=Grand+Hotel&display=swap'); + @tailwind base; @tailwind components; @tailwind utilities; diff --git a/resources/js/Components/ActionMessage.vue b/resources/js/Components/ActionMessage.vue index 5974148..d631a91 100644 --- a/resources/js/Components/ActionMessage.vue +++ b/resources/js/Components/ActionMessage.vue @@ -7,7 +7,7 @@ defineProps({