Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- diff -u app/Http/Controllers/GetNzbController.php.BAK app/Http/Controllers/GetNzbController.php
- --- app/Http/Controllers/GetNzbController.php.BAK 2024-11-27 22:55:49.015108033 +0000
- +++ app/Http/Controllers/GetNzbController.php 2024-11-27 23:33:38.085508734 +0000
- @@ -16,117 +16,138 @@
- /**
- * @throws \Exception
- */
- - public function getNzb(Request $request)
- - {
- - $this->setPreferences();
- -
- - // Page is accessible only by the rss token, or logged in users.
- - if ($request->user()) {
- - $uid = $this->userdata->id;
- - $maxDownloads = $this->userdata->role->downloadrequests;
- - $rssToken = $this->userdata->api_token;
- - if ($this->userdata->hasRole('Disabled')) {
- - return Utility::showApiError(101);
- - }
- - } else {
- - if ($request->missing('r')) {
- - return Utility::showApiError(200);
- - }
- -
- - $res = User::getByRssToken($request->input('r'));
- - if (! $res) {
- - return Utility::showApiError(100);
- - }
- - $uid = $res['id'];
- - $rssToken = $res['api_token'];
- - $maxDownloads = $res->role->downloadrequests;
- - if ($res->hasRole('Disabled')) {
- - return Utility::showApiError(101);
- - }
- +public function getNzb(Request $request)
- +{
- + $this->setPreferences();
- +
- + // User validation (logged in or RSS token)
- + if ($request->user()) {
- + $uid = $this->userdata->id;
- + $maxDownloads = $this->userdata->role->downloadrequests;
- + $rssToken = $this->userdata->api_token;
- + if ($this->userdata->hasRole('Disabled')) {
- + return Utility::showApiError(101);
- + }
- + } else {
- + if ($request->missing('r')) {
- + return Utility::showApiError(200);
- + }
- +
- + $res = User::getByRssToken($request->input('r'));
- + if (!$res) {
- + return Utility::showApiError(100);
- + }
- +
- + $uid = $res['id'];
- + $rssToken = $res['api_token'];
- + $maxDownloads = $res->role->downloadrequests;
- + if ($res->hasRole('Disabled')) {
- + return Utility::showApiError(101);
- }
- + }
- - // Check download limit on user role.
- - $requests = UserDownload::getDownloadRequests($uid);
- - if ($requests > $maxDownloads) {
- - return Utility::showApiError(501);
- - }
- + // Check user download limits
- + $requests = UserDownload::getDownloadRequests($uid);
- + if ($requests >= $maxDownloads) {
- + return Utility::showApiError(501);
- + }
- - if (! $request->input('id')) {
- - return Utility::showApiError(200, 'Parameter id is required');
- - }
- + // Validate release ID
- + $releaseId = $request->input('id');
- + if (!$releaseId) {
- + return Utility::showApiError(200, 'Parameter id is required');
- + }
- - // Remove any suffixed id with .nzb which is added to help weblogging programs see nzb traffic.
- - $request->merge(['id' => str_ireplace('.nzb', '', $request->input('id'))]);
- + // Clean up the release ID (remove ".nzb" suffix)
- + $releaseId = str_ireplace('.nzb', '', $releaseId);
- - // User requested a zip of guid,guid,guid releases.
- - if ($request->has('zip') && $request->input('zip') === '1') {
- - $guids = explode(',', $request->input('id'));
- - if ($requests + \count($guids) > $maxDownloads) {
- - return Utility::showApiError(501);
- - }
- + // Handle ZIP downloads
- + if ($request->has('zip') && $request->input('zip') === '1') {
- + $guids = explode(',', $releaseId);
- + if ($requests + count($guids) > $maxDownloads) {
- + return Utility::showApiError(501);
- + }
- - $zip = getStreamingZip($guids);
- - if ($zip !== '') {
- - User::incrementGrabs($uid, \count($guids));
- - foreach ($guids as $guid) {
- - Release::updateGrab($guid);
- - UserDownload::addDownloadRequest($uid, $guid);
- -
- - if ($request->has('del') && (int) $request->input('del') === 1) {
- - UsersRelease::delCartByUserAndRelease($guid, $uid);
- - }
- - }
- + $zip = getStreamingZip($guids);
- + if ($zip !== '') {
- + User::incrementGrabs($uid, count($guids));
- + foreach ($guids as $guid) {
- + Release::updateGrab($guid);
- + UserDownload::addDownloadRequest($uid, $guid);
- - return $zip;
- + if ($request->has('del') && (int)$request->input('del') === 1) {
- + UsersRelease::delCartByUserAndRelease($guid, $uid);
- + }
- }
- - return response()->json(['message' => 'Unable to create .zip file'], 404);
- + return $zip;
- }
- - $nzbPath = (new NZB)->getNZBPath($request->input('id'));
- + return response()->json(['message' => 'Unable to create .zip file'], 404);
- + }
- - if (! File::exists($nzbPath)) {
- - return Utility::showApiError(300, 'NZB file not found!');
- - }
- + // Get NZB file path
- + $nzbPath = (new NZB())->getNZBPath($releaseId);
- + if (!$nzbPath || !File::exists($nzbPath)) {
- + return Utility::showApiError(300, 'NZB file not found!');
- + }
- - $relData = Release::getByGuid($request->input('id'));
- - if ($relData !== null) {
- - Release::updateGrab($request->input('id'));
- - UserDownload::addDownloadRequest($uid, $relData['id']);
- - User::incrementGrabs($uid);
- - if ($request->has('del') && (int) $request->input('del') === 1) {
- - UsersRelease::delCartByUserAndRelease($request->input('id'), $uid);
- - }
- - } else {
- - return Utility::showApiError(300, 'Release not found!');
- - }
- + // Fetch release details
- + $relData = Release::getByGuid($releaseId);
- + if (!$relData) {
- + return Utility::showApiError(300, 'Release not found!');
- + }
- - $headers = [
- - 'Content-Type' => 'application/x-nzb',
- - 'Expires' => date('r', now()->addDays(365)->timestamp),
- - 'X-DNZB-Failure' => url('/').'/failed'.'?guid='.$request->input('id').'&userid='.$uid.'&api_token='.$rssToken,
- - 'X-DNZB-Category' => $relData['category_name'],
- - 'X-DNZB-Details' => url('/').'/details/'.$request->input('id'),
- - ];
- + // Update release and user activity
- + Release::updateGrab($releaseId);
- + UserDownload::addDownloadRequest($uid, $relData['id']);
- + User::incrementGrabs($uid);
- - if (! empty($relData['imdbid']) && $relData['imdbid'] > 0) {
- - $headers += ['X-DNZB-MoreInfo' => 'http://www.imdb.com/title/tt'.$relData['imdbid']];
- - } elseif (! empty($relData['tvdb']) && $relData['tvdb'] > 0) {
- - $headers += ['X-DNZB-MoreInfo' => 'http://www.thetvdb.com/?tab=series&id='.$relData['tvdb']];
- - }
- + if ($request->has('del') && (int)$request->input('del') === 1) {
- + UsersRelease::delCartByUserAndRelease($releaseId, $uid);
- + }
- - if ((int) $relData['nfostatus'] === 1) {
- - $headers += ['X-DNZB-NFO' => url('/').'/nfo/'.$request->input('id')];
- + // Generate a clean filename
- + $cleanName = str_replace([',', ' ', '/', '\\'], '_', $relData['searchname']);
- +
- + // Add metadata headers
- + $headers = [
- + 'Content-Type' => 'application/x-nzb',
- + 'Expires' => date('r', now()->addDays(365)->timestamp),
- + 'X-DNZB-Failure' => url('/') . '/failed?guid=' . $releaseId . '&userid=' . $uid . '&api_token=' . $rssToken,
- + 'X-DNZB-Category' => $relData['category_name'],
- + 'X-DNZB-Details' => url('/') . '/details/' . $releaseId,
- + ];
- +
- + if (!empty($relData['imdbid']) && $relData['imdbid'] > 0) {
- + $headers['X-DNZB-MoreInfo'] = 'http://www.imdb.com/title/tt' . $relData['imdbid'];
- + } elseif (!empty($relData['tvdb']) && $relData['tvdb'] > 0) {
- + $headers['X-DNZB-MoreInfo'] = 'http://www.thetvdb.com/?tab=series&id=' . $relData['tvdb'];
- + }
- +
- + if ((int)$relData['nfostatus'] === 1) {
- + $headers['X-DNZB-NFO'] = url('/') . '/nfo/' . $releaseId;
- + }
- +
- + // Stream the decompressed NZB file
- + return response()->streamDownload(function () use ($nzbPath) {
- + $tempFile = sys_get_temp_dir() . '/' . uniqid('nzb_', true) . '.nzb';
- + $gzFile = gzopen($nzbPath, 'rb');
- + if (!$gzFile) {
- + throw new \Exception('Failed to open compressed NZB file: ' . $nzbPath);
- }
- - $headers += ['X-DNZB-RCode' => '200',
- - 'X-DNZB-RText' => 'OK, NZB content follows.', ];
- + $outFile = fopen($tempFile, 'wb');
- + while (!gzeof($gzFile)) {
- + fwrite($outFile, gzread($gzFile, 4096));
- + }
- - $cleanName = str_replace([',', ' ', '/', '\\'], '_', $relData['searchname']);
- + gzclose($gzFile);
- + fclose($outFile);
- - return response()->streamDownload(function () use ($nzbPath) {
- - echo $nzbPath;
- - }, $cleanName.'.nzb', $headers);
- - }
- + readfile($tempFile);
- + unlink($tempFile);
- + }, $cleanName . '.nzb', $headers);
- +}
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement