512 lines
17 KiB
PHP
512 lines
17 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Jobs\CompressVideoJob;
|
|
use App\Mail\VideoUploaded;
|
|
use App\Models\Playlist;
|
|
use App\Models\Video;
|
|
use FFMpeg\FFMpeg;
|
|
use FFMpeg\FFProbe;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\Mail;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Illuminate\Support\Str;
|
|
|
|
class VideoController extends Controller
|
|
{
|
|
public function __construct()
|
|
{
|
|
$this->middleware('auth')->except(['index', 'show', 'search', 'stream', 'trending', 'shorts']);
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
$videos = Video::public()->latest()->get();
|
|
|
|
return view('videos.index', compact('videos'));
|
|
}
|
|
|
|
public function search(Request $request)
|
|
{
|
|
$query = $request->get('q', '');
|
|
|
|
if (empty($query)) {
|
|
return redirect()->route('videos.index');
|
|
}
|
|
|
|
$videos = Video::public()
|
|
->where(function ($q) use ($query) {
|
|
$q->where('title', 'like', "%{$query}%")
|
|
->orWhere('description', 'like', "%{$query}%");
|
|
})
|
|
->latest()
|
|
->get();
|
|
|
|
return view('videos.index', compact('videos', 'query'));
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
return view('videos.create');
|
|
}
|
|
|
|
public function store(Request $request)
|
|
{
|
|
$request->validate([
|
|
'title' => 'required|string|max:255',
|
|
'description' => 'nullable|string',
|
|
'video' => 'required|file|mimes:mp4,webm,ogg,mov,avi,wmv,flv,mkv|max:512000',
|
|
'thumbnail' => 'nullable|image|mimes:jpg,jpeg,png,webp|max:5120',
|
|
'visibility' => 'nullable|in:public,unlisted,private',
|
|
'type' => 'nullable|in:generic,music,match',
|
|
]);
|
|
|
|
$videoFile = $request->file('video');
|
|
$filename = Str::slug($request->title).'-'.time().'.'.$videoFile->getClientOriginalExtension();
|
|
$path = $videoFile->storeAs('public/videos', $filename);
|
|
|
|
// Get file info
|
|
$fileSize = $videoFile->getSize();
|
|
$mimeType = $videoFile->getMimeType();
|
|
|
|
$thumbnailPath = null;
|
|
if ($request->hasFile('thumbnail')) {
|
|
$thumbFilename = Str::uuid().'.'.$request->file('thumbnail')->getClientOriginalExtension();
|
|
$thumbnailPath = $request->file('thumbnail')->storeAs('public/thumbnails', $thumbFilename);
|
|
} else {
|
|
// Extract thumbnail from video using FFmpeg
|
|
try {
|
|
$ffmpeg = FFMpeg::create();
|
|
$videoPath = storage_path('app/'.$path);
|
|
|
|
if (file_exists($videoPath)) {
|
|
$video = $ffmpeg->open($videoPath);
|
|
$frame = $video->frame(\FFMpeg\Coordinate\TimeCode::fromSeconds(1));
|
|
|
|
$thumbFilename = Str::uuid().'.jpg';
|
|
$thumbFullPath = storage_path('app/public/thumbnails/'.$thumbFilename);
|
|
|
|
// Ensure thumbnails directory exists
|
|
if (! file_exists(storage_path('app/public/thumbnails'))) {
|
|
mkdir(storage_path('app/public/thumbnails'), 0755, true);
|
|
}
|
|
|
|
$frame->save($thumbFullPath);
|
|
$thumbnailPath = 'public/thumbnails/'.$thumbFilename;
|
|
}
|
|
} catch (\Exception $e) {
|
|
// Log the error but don't fail the upload
|
|
\Log::error('FFmpeg failed to extract thumbnail: '.$e->getMessage());
|
|
}
|
|
}
|
|
|
|
// Get video dimensions and detect orientation using FFmpeg
|
|
$width = null;
|
|
$height = null;
|
|
$orientation = 'landscape';
|
|
|
|
try {
|
|
$ffprobe = FFProbe::create();
|
|
$videoPath = storage_path('app/'.$path);
|
|
|
|
if (file_exists($videoPath)) {
|
|
$streams = $ffprobe->streams($videoPath);
|
|
$videoStream = $streams->videos()->first();
|
|
|
|
if ($videoStream) {
|
|
$width = $videoStream->get('width');
|
|
$height = $videoStream->get('height');
|
|
|
|
// Auto-detect orientation based on dimensions
|
|
if ($width && $height) {
|
|
if ($height > $width) {
|
|
$orientation = 'portrait';
|
|
} elseif ($width > $height) {
|
|
$orientation = 'landscape';
|
|
} else {
|
|
$orientation = 'square';
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} catch (\Exception $e) {
|
|
// Log the error but don't fail the upload
|
|
\Log::error('FFprobe failed to get video dimensions: '.$e->getMessage());
|
|
// Use default orientation
|
|
}
|
|
|
|
$video = Video::create([
|
|
'user_id' => Auth::id(),
|
|
'title' => $request->title,
|
|
'description' => $request->description,
|
|
'filename' => $filename,
|
|
'path' => $path,
|
|
'thumbnail' => $thumbnailPath ? basename($thumbnailPath) : null,
|
|
'size' => $fileSize,
|
|
'mime_type' => $mimeType,
|
|
'orientation' => $orientation,
|
|
'width' => $width,
|
|
'height' => $height,
|
|
'status' => 'processing',
|
|
'visibility' => $request->visibility ?? 'public',
|
|
'type' => $request->type ?? 'generic',
|
|
]);
|
|
|
|
// Dispatch compression job in the background
|
|
CompressVideoJob::dispatch($video);
|
|
|
|
// Load user relationship for email
|
|
$video->load('user');
|
|
|
|
// Send email notification
|
|
try {
|
|
Mail::to(Auth::user()->email)->send(new VideoUploaded($video, Auth::user()->name));
|
|
} catch (\Exception $e) {
|
|
// Log the error but don't fail the upload
|
|
\Log::error('Email notification failed: '.$e->getMessage());
|
|
}
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'redirect' => route('videos.show', $video->id),
|
|
]);
|
|
}
|
|
|
|
public function show(Request $request, Video $video)
|
|
{
|
|
// Check if user can view this video
|
|
if (! $video->canView(Auth::user())) {
|
|
abort(404, 'Video not found');
|
|
}
|
|
|
|
// Track view if user is logged in
|
|
if (Auth::check()) {
|
|
$user = Auth::user();
|
|
// Add view if not already viewed recently (within last hour)
|
|
$existingView = \DB::table('video_views')
|
|
->where('user_id', $user->id)
|
|
->where('video_id', $video->id)
|
|
->where('watched_at', '>', now()->subHour())
|
|
->first();
|
|
|
|
if (! $existingView) {
|
|
\DB::table('video_views')->insert([
|
|
'user_id' => $user->id,
|
|
'video_id' => $video->id,
|
|
'watched_at' => now(),
|
|
]);
|
|
}
|
|
}
|
|
|
|
// Load comments with user relationship
|
|
$video->load(['comments.user', 'comments.replies.user', 'matchRounds.points', 'coachReviews']);
|
|
|
|
// Handle playlist navigation if playlist parameter is provided
|
|
$playlist = null;
|
|
$nextVideo = null;
|
|
$previousVideo = null;
|
|
$playlistVideos = null;
|
|
|
|
$playlistId = $request->query('playlist');
|
|
if ($playlistId) {
|
|
$playlist = Playlist::find($playlistId);
|
|
if ($playlist && $playlist->canView(Auth::user())) {
|
|
$nextVideo = $playlist->getNextVideo($video);
|
|
$previousVideo = $playlist->getPreviousVideo($video);
|
|
$playlistVideos = $playlist->videos;
|
|
}
|
|
}
|
|
|
|
// Get recommended videos (exclude current video)
|
|
$recommendedVideos = Video::public()
|
|
->where('id', '!=', $video->id)
|
|
->latest()
|
|
->limit(20)
|
|
->get();
|
|
|
|
// Render the appropriate view based on video type
|
|
$view = match ($video->type) {
|
|
'match' => 'videos.types.match',
|
|
'music' => 'videos.types.music',
|
|
default => 'videos.types.generic',
|
|
};
|
|
|
|
return view($view, compact('video', 'playlist', 'nextVideo', 'previousVideo', 'recommendedVideos', 'playlistVideos'));
|
|
}
|
|
|
|
public function matchData(Video $video)
|
|
{
|
|
if (! $video->canView(Auth::user())) {
|
|
abort(403);
|
|
}
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'rounds' => $video->matchRounds()->with('points')->orderBy('round_number')->get(),
|
|
'reviews' => $video->coachReviews()->orderBy('start_time_seconds')->get(),
|
|
]);
|
|
}
|
|
|
|
public function edit(Video $video, Request $request)
|
|
{
|
|
// Check if user owns the video
|
|
if (Auth::id() !== $video->user_id) {
|
|
abort(403, 'You do not have permission to edit this video.');
|
|
}
|
|
|
|
// If not AJAX request, redirect to show page with edit parameter
|
|
if (! $request->expectsJson() && ! $request->ajax()) {
|
|
return redirect()->route('videos.show', $video->id)->with('openEditModal', true);
|
|
}
|
|
|
|
// For AJAX request, return JSON
|
|
return response()->json([
|
|
'success' => true,
|
|
'video' => [
|
|
'id' => $video->id,
|
|
'title' => $video->title,
|
|
'description' => $video->description,
|
|
'thumbnail' => $video->thumbnail,
|
|
'thumbnail_url' => $video->thumbnail ? asset('storage/thumbnails/'.$video->thumbnail) : null,
|
|
'visibility' => $video->visibility ?? 'public',
|
|
'type' => $video->type ?? 'generic',
|
|
],
|
|
]);
|
|
}
|
|
|
|
public function update(Request $request, Video $video)
|
|
{
|
|
// Check if user owns the video
|
|
if (Auth::id() !== $video->user_id) {
|
|
abort(403, 'You do not have permission to edit this video.');
|
|
}
|
|
|
|
$request->validate([
|
|
'title' => 'required|string|max:255',
|
|
'description' => 'nullable|string',
|
|
'thumbnail' => 'nullable|image|mimes:jpg,jpeg,png,webp|max:5120',
|
|
'visibility' => 'nullable|in:public,unlisted,private',
|
|
'type' => 'nullable|in:generic,music,match',
|
|
]);
|
|
|
|
$data = $request->only(['title', 'description', 'visibility', 'type']);
|
|
|
|
if ($request->hasFile('thumbnail')) {
|
|
if ($video->thumbnail) {
|
|
Storage::delete('public/thumbnails/'.$video->thumbnail);
|
|
}
|
|
$thumbFilename = Str::uuid().'.'.$request->file('thumbnail')->getClientOriginalExtension();
|
|
$data['thumbnail'] = $request->file('thumbnail')->storeAs('public/thumbnails', $thumbFilename);
|
|
$data['thumbnail'] = basename($data['thumbnail']);
|
|
}
|
|
|
|
// Set default visibility if not provided
|
|
if (! isset($data['visibility'])) {
|
|
unset($data['visibility']);
|
|
}
|
|
|
|
$video->update($data);
|
|
|
|
// Return JSON for AJAX requests
|
|
if ($request->expectsJson() || $request->ajax()) {
|
|
return response()->json([
|
|
'success' => true,
|
|
'message' => 'Video updated successfully!',
|
|
'video' => [
|
|
'id' => $video->id,
|
|
'title' => $video->title,
|
|
'description' => $video->description,
|
|
'visibility' => $video->visibility,
|
|
],
|
|
]);
|
|
}
|
|
|
|
return redirect()->route('videos.show', $video)->with('success', 'Video updated!');
|
|
}
|
|
|
|
public function destroy(Request $request, Video $video)
|
|
{
|
|
// Check if user owns the video
|
|
if (Auth::id() !== $video->user_id) {
|
|
abort(403, 'You do not have permission to delete this video.');
|
|
}
|
|
|
|
$videoTitle = $video->title;
|
|
|
|
Storage::delete('public/videos/'.$video->filename);
|
|
if ($video->thumbnail) {
|
|
Storage::delete('public/thumbnails/'.$video->thumbnail);
|
|
}
|
|
$video->delete();
|
|
|
|
// Return JSON for AJAX requests
|
|
if ($request->expectsJson() || $request->ajax()) {
|
|
return response()->json([
|
|
'success' => true,
|
|
'message' => 'Video deleted successfully!',
|
|
]);
|
|
}
|
|
|
|
return redirect()->route('videos.index')->with('success', 'Video deleted!');
|
|
}
|
|
|
|
public function stream(Video $video)
|
|
{
|
|
// Check if user can view this video
|
|
if (! $video->canView(Auth::user())) {
|
|
abort(404, 'Video not found');
|
|
}
|
|
|
|
$path = storage_path('app/public/videos/'.$video->filename);
|
|
|
|
if (! file_exists($path)) {
|
|
abort(404, 'Video file not found');
|
|
}
|
|
|
|
$fileSize = filesize($path);
|
|
$mimeType = $video->mime_type ?: 'video/mp4';
|
|
|
|
$handle = fopen($path, 'rb');
|
|
if (! $handle) {
|
|
abort(500, 'Cannot open video file');
|
|
}
|
|
|
|
$range = request()->header('Range');
|
|
|
|
if ($range) {
|
|
// Parse range header
|
|
preg_match('/bytes=(\d+)-(\d*)/', $range, $matches);
|
|
$start = intval($matches[1] ?? 0);
|
|
$end = $matches[2] ? intval($matches[2]) : $fileSize - 1;
|
|
|
|
$length = $end - $start + 1;
|
|
|
|
header('HTTP/1.1 206 Partial Content');
|
|
header('Content-Type: '.$mimeType);
|
|
header('Content-Length: '.$length);
|
|
header('Content-Range: bytes '.$start.'-'.$end.'/'.$fileSize);
|
|
header('Accept-Ranges: bytes');
|
|
header('Cache-Control: public, max-age=3600');
|
|
|
|
fseek($handle, $start);
|
|
$chunkSize = 8192;
|
|
$bytesToRead = $length;
|
|
|
|
while (! feof($handle) && $bytesToRead > 0) {
|
|
$buffer = fread($handle, min($chunkSize, $bytesToRead));
|
|
echo $buffer;
|
|
flush();
|
|
$bytesToRead -= strlen($buffer);
|
|
}
|
|
|
|
fclose($handle);
|
|
exit;
|
|
} else {
|
|
// No range requested, stream entire file
|
|
header('Content-Type: '.$mimeType);
|
|
header('Content-Length: '.$fileSize);
|
|
header('Accept-Ranges: bytes');
|
|
header('Cache-Control: public, max-age=3600');
|
|
|
|
fpassthru($handle);
|
|
fclose($handle);
|
|
exit;
|
|
}
|
|
}
|
|
|
|
public function download(Video $video)
|
|
{
|
|
// Check if user can view this video
|
|
if (! $video->canView(Auth::user())) {
|
|
abort(404, 'Video not found');
|
|
}
|
|
|
|
$path = storage_path('app/public/videos/'.$video->filename);
|
|
|
|
if (! file_exists($path)) {
|
|
abort(404, 'Video file not found');
|
|
}
|
|
|
|
$filename = $video->title.'.'.pathinfo($video->filename, PATHINFO_EXTENSION);
|
|
|
|
return response()->download($path, $filename);
|
|
}
|
|
|
|
// Trending videos page
|
|
public function trending(Request $request)
|
|
{
|
|
$hours = $request->get('hours', 48); // Default: 48 hours
|
|
$limit = $request->get('limit', 50);
|
|
|
|
// Validate parameters
|
|
$hours = min(max($hours, 24), 168); // Between 24h and 7 days
|
|
$limit = min(max($limit, 10), 100);
|
|
|
|
// Get all public ready videos first
|
|
$videos = Video::public()
|
|
->where('status', 'ready')
|
|
->where('created_at', '>=', now()->subDays(10))
|
|
->with('user')
|
|
->get();
|
|
|
|
// Calculate trending score for each video
|
|
$videos = $videos->map(function ($video) use ($hours) {
|
|
$recentViews = \DB::table('video_views')
|
|
->where('video_id', $video->id)
|
|
->where('watched_at', '>=', now()->subHours($hours))
|
|
->count();
|
|
|
|
$likeCount = \DB::table('video_likes')
|
|
->where('video_id', $video->id)
|
|
->count();
|
|
|
|
// Calculate age in hours
|
|
$ageHours = $video->created_at->diffInHours(now());
|
|
|
|
// Calculate trending score
|
|
// 70% recent views, 15% velocity, 10% recency, 5% likes
|
|
$velocity = $recentViews / $hours;
|
|
$recencyBonus = max(0, 1 - ($ageHours / 240));
|
|
|
|
$score = ($recentViews * 0.70) +
|
|
($velocity * 100 * 0.15) +
|
|
($recencyBonus * 50 * 0.10) +
|
|
($likeCount * 0.1 * 0.05);
|
|
|
|
$video->trending_score = round($score, 2);
|
|
$video->view_count = $recentViews;
|
|
$video->like_count = $likeCount;
|
|
|
|
return $video;
|
|
});
|
|
|
|
// Filter and sort by trending score
|
|
$trendingVideos = $videos
|
|
->filter(fn ($v) => $v->trending_score > 0)
|
|
->sortByDesc('trending_score')
|
|
->take($limit)
|
|
->values();
|
|
|
|
return view('videos.trending', [
|
|
'videos' => $trendingVideos,
|
|
'hours' => $hours,
|
|
'limit' => $limit,
|
|
]);
|
|
}
|
|
|
|
// Shorts page
|
|
public function shorts(Request $request)
|
|
{
|
|
$videos = Video::public()
|
|
->where('is_shorts', true)
|
|
->where('status', 'ready')
|
|
->with('user')
|
|
->latest()
|
|
->get();
|
|
|
|
return view('videos.shorts', compact('videos'));
|
|
}
|
|
}
|