public function addToQueue(Media $media, $createRequest = true) { //Get the config options $config = App::module('shoutzor')->config('shoutzor'); //Get the path to the file $filepath = $config['mediaDir'] . '/' . $media->filename; //Make sure the file is readable if (!is_readable($filepath)) { throw new Exception(__('Cannot read music file ' . $filepath . ', Permission denied.')); } //Add request to the playlist $liquidsoapManager = new LiquidsoapManager(); $liquidsoapManager->queueTrack($filepath); if ($createRequest === true) { //Save request in the database $request = Request::create(); $request->save(array('media_id' => $media->id, 'requester_id' => App::user()->id, 'requesttime' => (new \DateTime())->format('Y-m-d H:i:s'))); } return true; }
/** * Handles the file uploads * @method request * @param id the ID from the media file thats requested */ public function request($params) { if ($this->shoutzorRunning === false) { return $this->formatOutput(__('Shoutzor is currently not running or restarting, try again in a few seconds'), self::METHOD_NOT_AVAILABLE); } //Make sure file uploads are enabled if (!App::user()->hasAccess("shoutzor: add requests")) { return $this->formatOutput(__('You have no permission to request'), self::METHOD_NOT_AVAILABLE); } //Make sure file uploads are enabled if (App::module('shoutzor')->config('shoutzor.request') == 0) { return $this->formatOutput(__('File requests have been disabled'), self::METHOD_NOT_AVAILABLE); } //Validate the parameter value if (!is_numeric($params['id'])) { return $this->formatOutput(__('Not a valid numerical value provided for the media object ID'), self::INVALID_PARAMETER_VALUE); } //Check if the requested Music ID exists $media = Media::find($params['id']); if ($media == null || !$media) { return $this->formatOutput(__('No media object with the provided ID exists'), self::ITEM_NOT_FOUND); } //Check if the media file is in queue already $inQueue = Request::where('media_id = :id', ['id' => $media->id])->count() == 0 ? true : false; if ($inQueue === false) { return $this->formatOutput(__('This media file is already in the queue!'), self::ERROR_IN_REQUEST); } //Get the config options $config = App::module('shoutzor')->config('shoutzor'); $canRequestDateTime = (new DateTime())->sub(new DateInterval('PT' . $config['mediaRequestDelay'] . 'M'))->format('Y-m-d H:i:s'); //Check if the media file has been played too recently if (Media::isRecentlyPlayed($media->id, $canRequestDateTime)) { return $this->formatOutput(__('This media file has been requested too recently'), self::ERROR_IN_REQUEST); } $canRequestDateTime = (new DateTime())->sub(new DateInterval('PT' . $config['artistRequestDelay'] . 'M'))->format('Y-m-d H:i:s'); //Fetch a list of all artist id's if (Artist::isRecentlyPlayed($media->id, $canRequestDateTime)) { return $this->formatOutput(__('This artist has been played too recently'), self::ERROR_IN_REQUEST); } $canRequestDateTime = (new DateTime())->sub(new DateInterval('PT' . $config['userRequestDelay'] . 'M'))->format('Y-m-d H:i:s'); //Check if the user hasnt already recently requested a media file $canRequest = Request::where('requester_id = :user AND requesttime > :requesttime', ['user' => App::user()->id, 'requesttime' => $canRequestDateTime])->count() == 0 ? true : false; if ($canRequest === false) { return $this->formatOutput(__('You already recently requested a media file, try again in 10 minutes'), self::ERROR_IN_REQUEST); } try { $queueManager = new QueueManager(); $queueManager->addToQueue($media); } catch (Exception $e) { return $this->formatOutput($e->getMessage(), self::ERROR_IN_REQUEST); } return $this->formatOutput(true); }