Exemple #1
0
 /**
  * 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]);
 }