Skip to content

Commit

Permalink
Only detect the .cue file encoding for the manual import service.
Browse files Browse the repository at this point in the history
(cherry picked from commit a3ccf87)
  • Loading branch information
zhangdoa committed Jan 6, 2024
1 parent e78237d commit 804a6a8
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 10 deletions.
27 changes: 17 additions & 10 deletions src/NzbDrone.Core/MediaFiles/CueSheetService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public List<ImportDecision<LocalTrack>> GetImportDecisions(ref List<IFileInfo> m
var cueSheetInfos = new List<CueSheetInfo>();
foreach (var cueFile in cueFiles)
{
var cueSheetInfo = GetCueSheetInfo(cueFile, mediaFileList);
var cueSheetInfo = GetCueSheetInfo(cueFile, mediaFileList, itemInfo.DetectCueFileEncoding);
if (idOverrides != null)
{
cueSheetInfo.IdOverrides = idOverrides;
Expand Down Expand Up @@ -246,19 +246,26 @@ private static string NormalizeTitle(PunctuationReplacer replacer, string title)
return title;
}

private CueSheet LoadCueSheet(IFileInfo fileInfo)
private CueSheet LoadCueSheet(IFileInfo fileInfo, bool detectCueFileEncoding)
{
using (var fs = fileInfo.OpenRead())
{
var bytes = new byte[fileInfo.Length];
var result = CharsetDetector.DetectFromFile(fileInfo.FullName); // or pass FileInfo
var encoding = result.Detected.Encoding;
_logger.Debug("Detected encoding {0} for {1}", encoding.WebName, fileInfo.FullName);

string content;
while (fs.Read(bytes, 0, bytes.Length) > 0)
{
content = encoding.GetString(bytes);
string content;
if (detectCueFileEncoding)
{
var result = CharsetDetector.DetectFromFile(fileInfo.FullName); // or pass FileInfo
var encoding = result.Detected.Encoding;
_logger.Debug("Detected encoding {0} for {1}", encoding.WebName, fileInfo.FullName);
content = encoding.GetString(bytes);
}
else
{
content = Encoding.UTF8.GetString(bytes);
}

var lines = content.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
var cueSheet = ParseLines(lines);

Expand Down Expand Up @@ -425,10 +432,10 @@ private Artist GetArtist(List<string> performers)
return null;
}

private CueSheetInfo GetCueSheetInfo(IFileInfo cueFile, List<IFileInfo> musicFiles)
private CueSheetInfo GetCueSheetInfo(IFileInfo cueFile, List<IFileInfo> musicFiles, bool detectCueFileEncoding)
{
var cueSheetInfo = new CueSheetInfo();
var cueSheet = LoadCueSheet(cueFile);
var cueSheet = LoadCueSheet(cueFile, detectCueFileEncoding);
if (cueSheet == null)
{
return cueSheetInfo;
Expand Down
1 change: 1 addition & 0 deletions src/NzbDrone.Core/MediaFiles/DiskScanService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ public void Scan(List<string> folders = null, FilterFilesType filter = FilterFil

var decisions = new List<ImportDecision<LocalTrack>>();

itemInfo.DetectCueFileEncoding = false;
decisions.AddRange(_cueSheetService.GetImportDecisions(ref mediaFileList, null, itemInfo, config));
decisions.AddRange(_importDecisionMaker.GetImportDecisions(mediaFileList, null, itemInfo, config));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class ImportDecisionMakerInfo
public DownloadClientItem DownloadClientItem { get; set; }
public ParsedAlbumInfo ParsedAlbumInfo { get; set; }
public List<CueSheetInfo> CueSheetInfos { get; set; } = new List<CueSheetInfo>();
public bool DetectCueFileEncoding { get; set; }
}

public class ImportDecisionMakerConfig
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ private List<ManualImportItem> ProcessFolder(string downloadId, IdentificationOv
{
DownloadClientItem = downloadClientItem,
ParsedAlbumInfo = Parser.Parser.ParseAlbumTitle(albumTitle),
DetectCueFileEncoding = true,
};

var config = new ImportDecisionMakerConfig
Expand Down Expand Up @@ -251,6 +252,7 @@ public List<ManualImportItem> UpdateItems(List<ManualImportItem> items)
AlbumRelease = group.First().Release
};

itemInfo.DetectCueFileEncoding = true;
var decisions = _cueSheetService.GetImportDecisions(ref audioFiles, idOverride, itemInfo, config);
if (audioFiles.Count > 0)
{
Expand Down

0 comments on commit 804a6a8

Please sign in to comment.