Files
newznab-tmux-NNTmux/app/Services/Releases/ReleaseManagementService.php
T

191 lines
5.9 KiB
PHP

<?php
namespace App\Services\Releases;
use App\Models\Release;
use App\Services\Search\ManticoreSearchService;
use Blacklight\NZB;
use Blacklight\ReleaseImage;
use Elasticsearch;
use Elasticsearch\Common\Exceptions\Missing404Exception;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\File;
/**
* Service for managing releases (delete, update, export).
*/
class ReleaseManagementService
{
private ManticoreSearchService $manticoreSearch;
public function __construct(
ManticoreSearchService $manticoreSearch
) {
$this->manticoreSearch = $manticoreSearch;
}
/**
* @throws \Exception
*/
public function deleteMultiple(int|array|string $list): void
{
$list = (array) $list;
$nzb = new NZB;
$releaseImage = new ReleaseImage;
foreach ($list as $identifier) {
$this->deleteSingle(['g' => $identifier, 'i' => false], $nzb, $releaseImage);
}
}
/**
* Deletes a single release by GUID, and all the corresponding files.
*
* @param array $identifiers ['g' => Release GUID(mandatory), 'id => ReleaseID(optional, pass
* false)]
*
* @throws \Exception
*/
public function deleteSingle(array $identifiers, NZB $nzb, ReleaseImage $releaseImage): void
{
// Delete NZB from disk.
$nzbPath = $nzb->NZBPath($identifiers['g']);
if (! empty($nzbPath)) {
File::delete($nzbPath);
}
// Delete images.
$releaseImage->delete($identifiers['g']);
if (config('nntmux.elasticsearch_enabled') === true) {
if ($identifiers['i'] === false) {
$identifiers['i'] = Release::query()->where('guid', $identifiers['g'])->first(['id']);
if ($identifiers['i'] !== null) {
$identifiers['i'] = $identifiers['i']['id'];
}
}
if ($identifiers['i'] !== null) {
$params = [
'index' => 'releases',
'id' => $identifiers['i'],
];
try {
Elasticsearch::delete($params);
} catch (Missing404Exception $e) {
// we do nothing here just catch the error, we don't care if release is missing from ES, we are deleting it anyway
}
}
} else {
// Delete from Manticore
if ($identifiers['i'] === false) {
$release = Release::query()->where('guid', $identifiers['g'])->first(['id']);
if ($release !== null) {
$identifiers['i'] = $release->id;
}
}
if (!empty($identifiers['i'])) {
$this->manticoreSearch->deleteRelease((int) $identifiers['i']);
}
}
// Delete from DB.
Release::whereGuid($identifiers['g'])->delete();
}
/**
* @return bool|int
*/
public function updateMulti($guids, $category, $grabs, $videoId, $episodeId, $anidbId, $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,
];
return Release::query()->whereIn('guid', $guids)->update($update);
}
/**
* @return Release[]|\Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Query\Builder[]|\Illuminate\Support\Collection
*/
public function getForExport(string $postFrom = '', string $postTo = '', string $groupID = '')
{
$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'];
}
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;
}
}