Esempio n. 1
0
 public static function logItem($resourceType, $resourceId, $logType, $formatId = null)
 {
     $resourceIdColumn = $resourceType . '_id';
     $logItem = new ResourceLogItem();
     $logItem->{$resourceIdColumn} = $resourceId;
     $logItem->created_at = Carbon::now();
     $logItem->log_type = $logType;
     $logItem->track_format_id = $formatId;
     $logItem->ip_address = Request::getClientIp();
     if (Auth::check()) {
         $logItem->user_id = Auth::user()->id;
     }
     $logItem->save();
     $resourceTable = $resourceType . 's';
     $countColumn = '';
     if ($logType == self::VIEW) {
         $countColumn = 'view_count';
     } else {
         if ($logType == self::DOWNLOAD) {
             $countColumn = 'download_count';
         } else {
             if ($logType == self::PLAY) {
                 $countColumn = 'play_count';
             }
         }
     }
     // We do this to prevent a race condition. Sure I could simply increment the count columns and re-save back to the db
     // but that would require an additional SELECT and the operation would be non-atomic. If two log items are created
     // for the same resource at the same time, the cached values will still be correct with this method.
     DB::table($resourceTable)->whereId($resourceId)->update([$countColumn => DB::raw('(SELECT
                 COUNT(id)
             FROM
                 resource_log_items
             WHERE ' . $resourceIdColumn . ' = ' . $resourceId . '
             AND
                 log_type = ' . $logType . ')')]);
     if (Auth::check()) {
         $resourceUserId = ResourceUser::getId(Auth::user()->id, $resourceType, $resourceId);
         DB::table('resource_users')->whereId($resourceUserId)->update([$countColumn => DB::raw('(SELECT
                     COUNT(id)
                 FROM
                     resource_log_items
                 WHERE
                     user_id = ' . Auth::user()->id . '
                 AND ' . $resourceIdColumn . ' = ' . $resourceId . '
                 AND
                     log_type = ' . $logType . ')')]);
     }
 }
Esempio n. 2
0
 public function getShow($id)
 {
     $playlist = Playlist::with(['tracks.user', 'tracks.genre', 'tracks.cover', 'tracks.album', 'tracks' => function ($query) {
         $query->userDetails();
     }, 'comments', 'comments.user'])->userDetails()->find($id);
     if (!$playlist || !$playlist->canView(Auth::user())) {
         App::abort('404');
     }
     if (Input::get('log')) {
         ResourceLogItem::logItem('playlist', $id, ResourceLogItem::VIEW);
         $playlist->view_count++;
     }
     return Response::json(Playlist::mapPublicPlaylistShow($playlist), 200);
 }
Esempio n. 3
0
 public function getShow($id)
 {
     $track = Track::userDetails()->withComments()->find($id);
     if (!$track || !$track->canView(Auth::user())) {
         return $this->notFound('Track not found!');
     }
     if (Input::get('log')) {
         ResourceLogItem::logItem('track', $id, ResourceLogItem::VIEW);
         $track->view_count++;
     }
     $returned_track = Track::mapPublicTrackShow($track);
     if ($returned_track['is_downloadable'] != 1) {
         unset($returned_track['formats']);
     }
     return Response::json(['track' => $returned_track], 200);
 }
Esempio n. 4
0
 public function getShow($id)
 {
     $album = Album::with(['tracks' => function ($query) {
         $query->userDetails();
     }, 'tracks.cover', 'tracks.genre', 'tracks.user', 'user', 'comments', 'comments.user'])->userDetails()->find($id);
     if (!$album) {
         App::abort(404);
     }
     if (Input::get('log')) {
         ResourceLogItem::logItem('album', $id, ResourceLogItem::VIEW);
         $album->view_count++;
     }
     $returned_album = Album::mapPublicAlbumShow($album);
     if ($returned_album['is_downloadable'] == 0) {
         unset($returned_album['formats']);
     }
     return Response::json(['album' => $returned_album], 200);
 }
Esempio n. 5
0
 public function getDownload($id, $extension)
 {
     $playlist = Playlist::with('tracks', 'user', 'tracks.album')->find($id);
     if (!$playlist || !$playlist->is_public && !Auth::check() || !$playlist->is_public && $playlist->user_id !== Auth::user()->id) {
         App::abort(404);
     }
     $format = null;
     $formatName = null;
     foreach (Track::$Formats as $name => $item) {
         if ($item['extension'] == $extension) {
             $format = $item;
             $formatName = $name;
             break;
         }
     }
     if ($format == null) {
         App::abort(404);
     }
     ResourceLogItem::logItem('playlist', $id, ResourceLogItem::DOWNLOAD, $format['index']);
     $downloader = new PlaylistDownloader($playlist, $formatName);
     $downloader->download();
 }
Esempio n. 6
0
 /**
  * Execute the console command.
  *
  * @return mixed
  */
 public function handle()
 {
     $items = ResourceLogItem::where('created_at', '0000-00-00 00:00:00')->orderBy('id', 'asc')->get();
     $totalItems = $items->count();
     // calculate the start and end of the logging gap
     $lastGoodId = (int) $items[0]->id - 1;
     $lastGoodItem = ResourceLogItem::find($lastGoodId);
     $lastGoodDate = $lastGoodItem->created_at;
     $now = Carbon::now();
     $secondsDifference = $now->diffInSeconds($lastGoodDate);
     $oneInterval = $secondsDifference / $totalItems;
     $this->info('Correcting records...');
     $bar = $this->output->createProgressBar($totalItems);
     foreach ($items as $i => $item) {
         $bar->advance();
         $dateOffset = (int) ($oneInterval * $i);
         $item->created_at = $lastGoodDate->copy()->addSeconds($dateOffset);
         $item->save();
     }
     $bar->finish();
     $this->line('');
     $this->info('All done!');
 }
 /**
  * Execute the console command.
  *
  * @return mixed
  */
 public function handle()
 {
     // Get list of affected users
     $usernames = DB::table('users')->select(['username', DB::raw('COUNT(*) as count')])->whereNull('disabled_at')->whereNotNull('username')->groupBy(DB::raw('LOWER(username)'))->having('count', '>=', 2)->lists('username');
     foreach ($usernames as $username) {
         // Find the relevant accounts
         // ==========================
         /** @var Collection $accounts */
         $accounts = User::where('username', $username)->orderBy('created_at', 'ASC')->get();
         $firstAccount = $accounts[0];
         $accounts->forget(0);
         $accountIds = $accounts->pluck('id');
         // Reassign content
         // ================
         // This is done with the less-efficient-than-raw-SQL Eloquent
         // methods to generate appropriate revision logs.
         $this->info('Merging duplicates for: ' . $firstAccount->username);
         DB::transaction(function () use($accounts, $accountIds, $firstAccount) {
             foreach (Album::whereIn('user_id', $accountIds)->get() as $album) {
                 $album->user_id = $firstAccount->id;
                 $album->save();
             }
             foreach (Comment::whereIn('user_id', $accountIds)->get() as $comment) {
                 $comment->user_id = $firstAccount->id;
                 $comment->save();
             }
             foreach (Favourite::whereIn('user_id', $accountIds)->get() as $favourite) {
                 $favourite->user_id = $firstAccount->id;
                 $favourite->save();
             }
             foreach (Follower::whereIn('artist_id', $accountIds)->get() as $follow) {
                 $follow->artist_id = $firstAccount->id;
                 $follow->save();
             }
             foreach (Image::whereIn('uploaded_by', $accountIds)->get() as $image) {
                 $image->uploaded_by = $firstAccount->id;
                 $image->save();
             }
             foreach (Image::whereIn('uploaded_by', $accountIds)->get() as $image) {
                 $image->uploaded_by = $firstAccount->id;
                 $image->save();
             }
             DB::table('oauth2_tokens')->whereIn('user_id', $accountIds)->update(['user_id' => $firstAccount->id]);
             foreach (PinnedPlaylist::whereIn('user_id', $accountIds)->get() as $playlist) {
                 $playlist->user_id = $firstAccount->id;
                 $playlist->save();
             }
             foreach (Playlist::whereIn('user_id', $accountIds)->get() as $playlist) {
                 $playlist->user_id = $firstAccount->id;
                 $playlist->save();
             }
             foreach (ResourceLogItem::whereIn('user_id', $accountIds)->get() as $item) {
                 $item->user_id = $firstAccount->id;
                 $item->save();
             }
             foreach (ResourceUser::whereIn('user_id', $accountIds)->get() as $item) {
                 $item->user_id = $firstAccount->id;
                 $item->save();
             }
             foreach (Track::whereIn('user_id', $accountIds)->get() as $track) {
                 $track->user_id = $firstAccount->id;
                 $track->save();
             }
             foreach ($accounts as $account) {
                 $account->disabled_at = Carbon::now();
                 $account->save();
             }
         });
     }
 }
Esempio n. 8
0
 public function getDownload($id, $extension)
 {
     $track = Track::find($id);
     if (!$track || !$track->canView(Auth::user())) {
         App::abort(404);
     }
     $trackFile = TrackFile::findOrFailByExtension($track->id, $extension);
     ResourceLogItem::logItem('track', $id, ResourceLogItem::DOWNLOAD, $trackFile->getFormat()['index']);
     $response = Response::make('', 200);
     $filename = $trackFile->getFile();
     if (Config::get('app.sendfile')) {
         $response->header('X-Sendfile', $filename);
         $response->header('Content-Disposition', 'attachment; filename="' . $trackFile->getDownloadFilename() . '"');
     } else {
         $response->header('X-Accel-Redirect', $filename);
         $response->header('Content-Disposition', 'attachment; filename="' . $trackFile->getDownloadFilename() . '"');
     }
     $time = gmdate(filemtime($filename));
     if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && $time == $_SERVER['HTTP_IF_MODIFIED_SINCE']) {
         header('HTTP/1.0 304 Not Modified');
         exit;
     }
     $response->header('Last-Modified', $time);
     $response->header('Content-Type', $trackFile->getFormat()['mime_type']);
     return $response;
 }