/** * Store a newly created resource in storage. * * @param Request $request * @return Response */ public function store(Request $request) { $user = auth()->check() ? auth()->user() : null; if (is_null($user)) { return redirect('/')->with('error', 'You need to be logged in'); } if (!$user->can('break_upload_limit') && $user->videos()->newlyups()->count() >= 20) { return redirect()->back()->with('error', 'Uploadlimit reached')->withInput(); } if (!$request->hasFile('file')) { return redirect()->back()->with('error', 'No file')->withInput(); } $file = $request->file('file'); if (!$file->isValid() || $file->getClientOriginalExtension() != 'webm' || $file->getMimeType() != 'video/webm') { return redirect()->back()->with('error', 'Invalid file'); } if (!$user->can('break_max_filesize') && $file->getSize() > 30000000.0) { return redirect()->back()->with('error', 'File to big. Max 30MB')->withInput(); } if (($v = Video::withTrashed()->where('hash', '=', sha1_file($file->getRealPath()))->first()) !== null) { return redirect($v->id)->with('error', 'Video already exists'); } $file = $file->move(public_path() . '/b/', time() . '.webm'); $hash = sha1_file($file->getRealPath()); $video = new Video(); $video->file = basename($file->getRealPath()); $video->interpret = $request->get('interpret', null); $video->songtitle = $request->get('songtitle', null); $video->imgsource = $request->get('imgsource', null); $video->user()->associate($user); $video->category()->associate(Category::findOrFail($request->get('category'))); $video->hash = $hash; $video->save(); return redirect($video->id)->with('success', 'Upload successful'); }
/** * Store a newly created resource in storage. * * @param Request $request * @return Response */ public function store(Request $request) { if (!$request->hasFile('file') || !$request->has('category') || !$request->has('tags')) { return new JsonResponse(['error' => 'invalid_request']); } $tags = $request->get('tags'); if (mb_strpos($tags, 'sfw') === false && mb_strpos($tags, 'nsfw') === false) { return new JsonResponse(['error' => 'invalid_request']); } $user = auth()->check() ? auth()->user() : null; if (is_null($user)) { return new JsonResponse(['error' => 'not_logged_in']); } if (!$user->can('break_upload_limit') && $user->videos()->newlyups()->count() >= 10) { return new JsonResponse(['error' => 'uploadlimit_reached']); } $file = $request->file('file'); if (!$file->isValid() || mb_strtolower($file->getClientOriginalExtension()) !== 'webm' || mb_strtolower($file->getMimeType()) !== 'video/webm') { return new JsonResponse(['error' => 'invalid_file']); } if (!$user->can('break_max_filesize') && $file->getSize() > 31457280) { return new JsonResponse(['error' => 'file_too_big']); } if (($v = Video::withTrashed()->where('hash', '=', sha1_file($file->getRealPath()))->first()) !== null) { if ($v->trashed()) { return new JsonResponse(['error' => 'already_exists']); } return new JsonResponse(['error' => 'already_exists', 'video_id' => $v->id]); } $file = $file->move(public_path() . '/b/', time() . '.webm'); if (!$this->checkFileEncoding(basename($file->getRealPath()))) { unlink($file->getRealPath()); return new JsonResponse(['error' => 'erroneous_file_encoding']); } $hash = sha1_file($file->getRealPath()); $video = new Video(); $video->file = basename($file->getRealPath()); $video->interpret = $request->get('interpret', null); $video->songtitle = $request->get('songtitle', null); $video->imgsource = $request->get('imgsource', null); $video->user()->associate($user); $video->category()->associate(Category::findOrFail($request->get('category'))); $video->hash = $hash; $video->save(); $video->tag($tags); $video->tag($video->interpret); $video->tag($video->songtitle); $video->tag($video->imgsource); $video->tag($video->category->shortname); $video->tag($video->category->name); $this->createThumbnail(basename($file->getRealPath())); return new JsonResponse(['error' => 'null', 'video_id' => $video->id]); }