2017-09-04 02:20:56 +00:00
|
|
|
using System.Collections.Generic;
|
2014-06-08 08:22:55 +00:00
|
|
|
using System.Linq;
|
|
|
|
using NLog;
|
2018-04-08 02:28:16 +00:00
|
|
|
using NzbDrone.Core.ImportLists;
|
2014-06-08 08:22:55 +00:00
|
|
|
using NzbDrone.Core.Lifecycle;
|
|
|
|
using NzbDrone.Core.Messaging.Events;
|
2017-06-03 01:25:34 +00:00
|
|
|
using NzbDrone.Core.Music;
|
2020-01-03 12:49:24 +00:00
|
|
|
using NzbDrone.Core.Qualities;
|
2014-06-08 08:22:55 +00:00
|
|
|
|
2017-09-04 02:20:56 +00:00
|
|
|
namespace NzbDrone.Core.Profiles.Qualities
|
2014-06-08 08:22:55 +00:00
|
|
|
{
|
|
|
|
public interface IProfileService
|
|
|
|
{
|
2019-02-23 22:39:11 +00:00
|
|
|
QualityProfile Add(QualityProfile profile);
|
|
|
|
void Update(QualityProfile profile);
|
2014-06-08 08:22:55 +00:00
|
|
|
void Delete(int id);
|
2019-02-23 22:39:11 +00:00
|
|
|
List<QualityProfile> All();
|
|
|
|
QualityProfile Get(int id);
|
2015-12-05 10:22:22 +00:00
|
|
|
bool Exists(int id);
|
2019-02-23 22:39:11 +00:00
|
|
|
QualityProfile GetDefaultProfile(string name, Quality cutoff = null, params Quality[] allowed);
|
2014-06-08 08:22:55 +00:00
|
|
|
}
|
|
|
|
|
2019-02-23 22:39:11 +00:00
|
|
|
public class QualityProfileService : IProfileService, IHandle<ApplicationStartedEvent>
|
2014-06-08 08:22:55 +00:00
|
|
|
{
|
|
|
|
private readonly IProfileRepository _profileRepository;
|
2017-06-03 01:25:34 +00:00
|
|
|
private readonly IArtistService _artistService;
|
2018-04-08 02:28:16 +00:00
|
|
|
private readonly IImportListFactory _importListFactory;
|
2014-06-08 08:22:55 +00:00
|
|
|
private readonly Logger _logger;
|
|
|
|
|
2019-02-23 22:39:11 +00:00
|
|
|
public QualityProfileService(IProfileRepository profileRepository, IArtistService artistService, IImportListFactory importListFactory, Logger logger)
|
2014-06-08 08:22:55 +00:00
|
|
|
{
|
|
|
|
_profileRepository = profileRepository;
|
2017-06-03 01:25:34 +00:00
|
|
|
_artistService = artistService;
|
2018-04-08 02:28:16 +00:00
|
|
|
_importListFactory = importListFactory;
|
2014-06-08 08:22:55 +00:00
|
|
|
_logger = logger;
|
|
|
|
}
|
|
|
|
|
2019-02-23 22:39:11 +00:00
|
|
|
public QualityProfile Add(QualityProfile profile)
|
2014-06-08 08:22:55 +00:00
|
|
|
{
|
|
|
|
return _profileRepository.Insert(profile);
|
|
|
|
}
|
|
|
|
|
2019-02-23 22:39:11 +00:00
|
|
|
public void Update(QualityProfile profile)
|
2014-06-08 08:22:55 +00:00
|
|
|
{
|
|
|
|
_profileRepository.Update(profile);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Delete(int id)
|
|
|
|
{
|
2019-02-23 22:39:11 +00:00
|
|
|
if (_artistService.GetAllArtists().Any(c => c.QualityProfileId == id) || _importListFactory.All().Any(c => c.ProfileId == id))
|
2014-06-08 08:22:55 +00:00
|
|
|
{
|
2018-02-16 03:03:24 +00:00
|
|
|
var profile = _profileRepository.Get(id);
|
2019-02-23 22:39:11 +00:00
|
|
|
throw new QualityProfileInUseException(profile.Name);
|
2014-06-08 08:22:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_profileRepository.Delete(id);
|
|
|
|
}
|
|
|
|
|
2019-02-23 22:39:11 +00:00
|
|
|
public List<QualityProfile> All()
|
2014-06-08 08:22:55 +00:00
|
|
|
{
|
|
|
|
return _profileRepository.All().ToList();
|
|
|
|
}
|
|
|
|
|
2019-02-23 22:39:11 +00:00
|
|
|
public QualityProfile Get(int id)
|
2014-06-08 08:22:55 +00:00
|
|
|
{
|
|
|
|
return _profileRepository.Get(id);
|
|
|
|
}
|
|
|
|
|
2015-12-05 10:22:22 +00:00
|
|
|
public bool Exists(int id)
|
|
|
|
{
|
|
|
|
return _profileRepository.Exists(id);
|
|
|
|
}
|
|
|
|
|
2014-06-08 08:22:55 +00:00
|
|
|
public void Handle(ApplicationStartedEvent message)
|
|
|
|
{
|
2020-01-03 12:49:24 +00:00
|
|
|
if (All().Any())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2014-06-08 08:22:55 +00:00
|
|
|
|
|
|
|
_logger.Info("Setting up default quality profiles");
|
|
|
|
|
2020-01-03 12:49:24 +00:00
|
|
|
AddDefaultProfile("Any",
|
|
|
|
Quality.Unknown,
|
2017-10-19 02:57:11 +00:00
|
|
|
Quality.Unknown,
|
2017-11-21 00:20:21 +00:00
|
|
|
Quality.MP3_008,
|
|
|
|
Quality.MP3_016,
|
|
|
|
Quality.MP3_024,
|
|
|
|
Quality.MP3_032,
|
|
|
|
Quality.MP3_040,
|
|
|
|
Quality.MP3_048,
|
|
|
|
Quality.MP3_056,
|
|
|
|
Quality.MP3_064,
|
|
|
|
Quality.MP3_080,
|
|
|
|
Quality.MP3_096,
|
|
|
|
Quality.MP3_112,
|
|
|
|
Quality.MP3_128,
|
|
|
|
Quality.MP3_160,
|
2017-07-03 18:39:06 +00:00
|
|
|
Quality.MP3_192,
|
2017-11-21 00:20:21 +00:00
|
|
|
Quality.MP3_224,
|
2017-07-03 18:39:06 +00:00
|
|
|
Quality.MP3_256,
|
|
|
|
Quality.MP3_320,
|
|
|
|
Quality.MP3_VBR,
|
2017-11-21 00:20:21 +00:00
|
|
|
Quality.MP3_VBR_V2,
|
|
|
|
Quality.AAC_192,
|
|
|
|
Quality.AAC_256,
|
|
|
|
Quality.AAC_320,
|
|
|
|
Quality.AAC_VBR,
|
|
|
|
Quality.VORBIS_Q5,
|
|
|
|
Quality.VORBIS_Q6,
|
|
|
|
Quality.VORBIS_Q7,
|
|
|
|
Quality.VORBIS_Q8,
|
|
|
|
Quality.VORBIS_Q9,
|
|
|
|
Quality.VORBIS_Q10,
|
|
|
|
Quality.WMA,
|
|
|
|
Quality.ALAC,
|
|
|
|
Quality.FLAC,
|
|
|
|
Quality.FLAC_24);
|
2017-05-08 02:07:19 +00:00
|
|
|
|
2020-01-03 12:49:24 +00:00
|
|
|
AddDefaultProfile("Lossless",
|
|
|
|
Quality.FLAC,
|
2017-10-19 02:57:11 +00:00
|
|
|
Quality.FLAC,
|
2017-11-21 00:20:21 +00:00
|
|
|
Quality.ALAC,
|
|
|
|
Quality.FLAC_24);
|
2017-05-08 02:07:19 +00:00
|
|
|
|
2020-01-03 12:49:24 +00:00
|
|
|
AddDefaultProfile("Standard",
|
|
|
|
Quality.MP3_192,
|
2017-07-03 18:39:06 +00:00
|
|
|
Quality.MP3_192,
|
|
|
|
Quality.MP3_256,
|
|
|
|
Quality.MP3_320);
|
2014-06-08 08:22:55 +00:00
|
|
|
}
|
2017-11-21 00:20:21 +00:00
|
|
|
|
2019-02-23 22:39:11 +00:00
|
|
|
public QualityProfile GetDefaultProfile(string name, Quality cutoff = null, params Quality[] allowed)
|
2017-11-21 00:20:21 +00:00
|
|
|
{
|
|
|
|
var groupedQualites = Quality.DefaultQualityDefinitions.GroupBy(q => q.GroupWeight);
|
2019-02-23 22:39:11 +00:00
|
|
|
var items = new List<QualityProfileQualityItem>();
|
2017-11-21 00:20:21 +00:00
|
|
|
var groupId = 1000;
|
|
|
|
var profileCutoff = cutoff == null ? Quality.Unknown.Id : cutoff.Id;
|
|
|
|
|
|
|
|
foreach (var group in groupedQualites)
|
|
|
|
{
|
|
|
|
if (group.Count() == 1)
|
|
|
|
{
|
|
|
|
var quality = group.First().Quality;
|
2019-02-23 22:39:11 +00:00
|
|
|
items.Add(new QualityProfileQualityItem { Quality = quality, Allowed = allowed.Contains(quality) });
|
2017-11-21 00:20:21 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
var groupAllowed = group.Any(g => allowed.Contains(g.Quality));
|
|
|
|
|
2019-02-23 22:39:11 +00:00
|
|
|
items.Add(new QualityProfileQualityItem
|
2017-11-21 00:20:21 +00:00
|
|
|
{
|
|
|
|
Id = groupId,
|
|
|
|
Name = group.First().GroupName,
|
2019-02-23 22:39:11 +00:00
|
|
|
Items = group.Select(g => new QualityProfileQualityItem
|
2017-11-21 00:20:21 +00:00
|
|
|
{
|
|
|
|
Quality = g.Quality,
|
|
|
|
Allowed = groupAllowed
|
|
|
|
}).ToList(),
|
|
|
|
Allowed = groupAllowed
|
|
|
|
});
|
|
|
|
|
|
|
|
if (group.Any(s => s.Quality.Id == profileCutoff))
|
|
|
|
{
|
|
|
|
profileCutoff = groupId;
|
|
|
|
}
|
|
|
|
|
|
|
|
groupId++;
|
|
|
|
}
|
|
|
|
|
2019-02-23 22:39:11 +00:00
|
|
|
var qualityProfile = new QualityProfile
|
2017-11-21 00:20:21 +00:00
|
|
|
{
|
|
|
|
Name = name,
|
|
|
|
Cutoff = profileCutoff,
|
|
|
|
Items = items
|
|
|
|
};
|
|
|
|
|
|
|
|
return qualityProfile;
|
|
|
|
}
|
|
|
|
|
2019-02-23 22:39:11 +00:00
|
|
|
private QualityProfile AddDefaultProfile(string name, Quality cutoff, params Quality[] allowed)
|
2017-11-21 00:20:21 +00:00
|
|
|
{
|
|
|
|
var profile = GetDefaultProfile(name, cutoff, allowed);
|
|
|
|
|
|
|
|
return Add(profile);
|
|
|
|
}
|
2014-06-08 08:22:55 +00:00
|
|
|
}
|
2017-09-04 02:20:56 +00:00
|
|
|
}
|