Files
newznab-tmux/app/Services/Releases/ReleaseManagementService.php
T
2026-04-10 15:20:12 +02:00

189 lines
5.8 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Services\Releases;
use App\Facades\Search;
use App\Models\Release;
use App\Services\Nzb\NzbService;
use App\Services\ReleaseImageService;
use App\Support\ReleaseSearchIndexSync;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\File;
/**
* Service for managing releases (delete, update, export).
*/
class ReleaseManagementService
{
public function __construct() {}
/**
* @param array<string, mixed> $list
*
* @throws \Exception
*/
public function deleteMultiple(int|array|string $list): void
{
$list = (array) $list;
$nzb = app(NzbService::class);
$releaseImage = new ReleaseImageService;
foreach ($list as $identifier) {
$this->deleteSingleWithService(['g' => $identifier, 'i' => false], $nzb, $releaseImage);
}
}
/**
* Deletes a single release by GUID, and all the corresponding files.
*
* @param array<string, mixed> $identifiers ['g' => Release GUID(mandatory), 'id => ReleaseID(optional, pass
* false)]
*
* @throws \Exception
*/
public function deleteSingle(array $identifiers, NzbService $nzb, ReleaseImageService $releaseImage): void
{
// Delete NZB from disk.
$nzbPath = $nzb->nzbPath($identifiers['g']);
if (! empty($nzbPath)) {
File::delete($nzbPath);
}
// Delete images.
$releaseImage->delete($identifiers['g']);
// Get release ID if not provided
if ($identifiers['i'] === false) {
$release = Release::query()->where('guid', $identifiers['g'])->first(['id']);
if ($release !== null) {
$identifiers['i'] = $release->id;
}
}
// Delete from search index
if (! empty($identifiers['i'])) {
Search::deleteRelease((int) $identifiers['i']);
}
// Delete from DB.
Release::whereGuid($identifiers['g'])->delete();
}
/**
* Alias for deleteSingle for backwards compatibility.
*
* @param array<string, mixed> $identifiers ['g' => Release GUID(mandatory), 'i => ReleaseID(optional, pass false)]
*
* @throws \Exception
*/
public function deleteSingleWithService(array $identifiers, NzbService $nzb, ReleaseImageService $releaseImage): void
{
$this->deleteSingle($identifiers, $nzb, $releaseImage);
}
/**
* @return bool|int
*/
public function updateMulti(mixed $guids, mixed $category, mixed $grabs, mixed $videoId, mixed $episodeId, mixed $anidbId, mixed $imdbId)
{
if (! \is_array($guids) || \count($guids) < 1) {
return false;
}
$update = [
'categories_id' => $category === -1 ? 'categories_id' : $category,
'grabs' => $grabs,
'videos_id' => $videoId,
'tv_episodes_id' => $episodeId,
'anidbid' => $anidbId,
'imdbid' => $imdbId,
];
$releaseIds = Release::query()->whereIn('guid', $guids)->pluck('id');
$updated = Release::query()->whereIn('guid', $guids)->update($update);
ReleaseSearchIndexSync::forIds($releaseIds);
return $updated;
}
/**
* @return Release[]|Builder[]|\Illuminate\Database\Eloquent\Collection<int, mixed>|\Illuminate\Database\Query\Builder[]|Collection<int, mixed>
*/
public function getForExport(string $postFrom = '', string $postTo = '', string $groupID = '') // @phpstan-ignore missingType.generics
{
$query = Release::query()
->select(['r.searchname', 'r.guid', 'g.name as gname', DB::raw("CONCAT(cp.title,'_',c.title) AS catName")])
->from('releases as r')
->leftJoin('categories as c', 'c.id', '=', 'r.categories_id')
->leftJoin('root_categories as cp', 'cp.id', '=', 'c.root_categories_id')
->leftJoin('usenet_groups as g', 'g.id', '=', 'r.groups_id');
if ($groupID !== '') {
$query->where('r.groups_id', $groupID);
}
if ($postFrom !== '') {
$dateParts = explode('/', $postFrom);
if (\count($dateParts) === 3) {
$query->where('r.postdate', '>', $dateParts[2].'-'.$dateParts[1].'-'.$dateParts[0].'00:00:00');
}
}
if ($postTo !== '') {
$dateParts = explode('/', $postTo);
if (\count($dateParts) === 3) {
$query->where('r.postdate', '<', $dateParts[2].'-'.$dateParts[1].'-'.$dateParts[0].'23:59:59');
}
}
return $query->get();
}
/**
* @return mixed|string
*/
public function getEarliestUsenetPostDate(): mixed
{
$row = Release::query()->selectRaw("DATE_FORMAT(min(postdate), '%d/%m/%Y') AS postdate")->first();
return $row === null ? '01/01/2014' : $row['postdate'];
}
/**
* @return mixed|string
*/
public function getLatestUsenetPostDate(): mixed
{
$row = Release::query()->selectRaw("DATE_FORMAT(max(postdate), '%d/%m/%Y') AS postdate")->first();
return $row === null ? '01/01/2014' : $row['postdate'];
}
/**
* @return array<string, mixed>
*/
public function getReleasedGroupsForSelect(bool $blnIncludeAll = true): array
{
$groups = Release::query()
->selectRaw('DISTINCT g.id, g.name')
->leftJoin('usenet_groups as g', 'g.id', '=', 'releases.groups_id')
->get();
$temp_array = [];
if ($blnIncludeAll) {
$temp_array[-1] = '--All Groups--';
}
foreach ($groups as $group) {
$temp_array[$group['id']] = $group['name'];
}
return $temp_array;
}
}