77 lines
2.4 KiB
C#
77 lines
2.4 KiB
C#
using System.Globalization;
|
||
using System.IO;
|
||
|
||
namespace EmbyToolbox.Services;
|
||
|
||
/// <summary>Единый каталог <c>extract\audio|subtitles|attachments</c> и имена без молчаливого перезаписывания.</summary>
|
||
public static class TrackExtractOutputPaths
|
||
{
|
||
/// <returns>Абсолютный путь к каталогу <c>…\extract</c> или null.</returns>
|
||
public static string? TryPrepareExtractDirectories(string destinationRootFolder)
|
||
{
|
||
if (string.IsNullOrWhiteSpace(destinationRootFolder))
|
||
{
|
||
return null;
|
||
}
|
||
|
||
try
|
||
{
|
||
var root = Path.GetFullPath(destinationRootFolder.Trim());
|
||
var extractRoot = Path.Combine(root, "extract");
|
||
Directory.CreateDirectory(Path.Combine(extractRoot, "audio"));
|
||
Directory.CreateDirectory(Path.Combine(extractRoot, "subtitles"));
|
||
Directory.CreateDirectory(Path.Combine(extractRoot, "attachments"));
|
||
return extractRoot;
|
||
}
|
||
catch (ArgumentException)
|
||
{
|
||
return null;
|
||
}
|
||
catch (IOException)
|
||
{
|
||
return null;
|
||
}
|
||
catch (UnauthorizedAccessException)
|
||
{
|
||
return null;
|
||
}
|
||
}
|
||
|
||
/// <summary>Если в <paramref name="parentDirectory"/> уже есть файл с таким именем — добавляет <c>_1</c>, <c>_2</c>… перед расширением.</summary>
|
||
public static string AllocateUniqueFilename(string parentDirectory, string desiredFileNameOnly)
|
||
{
|
||
var safe = Path.GetFileName(desiredFileNameOnly);
|
||
if (string.IsNullOrEmpty(safe))
|
||
{
|
||
safe = "output.bin";
|
||
}
|
||
|
||
if (!Exists(parentDirectory, safe))
|
||
{
|
||
return safe;
|
||
}
|
||
|
||
var stem = Path.GetFileNameWithoutExtension(safe);
|
||
var ext = Path.GetExtension(safe);
|
||
|
||
if (string.IsNullOrEmpty(stem))
|
||
{
|
||
stem = "file";
|
||
}
|
||
|
||
for (var i = 1; i < 10_000; i++)
|
||
{
|
||
var candidate = $"{stem}_{i.ToString(CultureInfo.InvariantCulture)}{ext}";
|
||
if (!Exists(parentDirectory, candidate))
|
||
{
|
||
return candidate;
|
||
}
|
||
}
|
||
|
||
return $"{stem}_{Guid.NewGuid():N}{ext}";
|
||
}
|
||
|
||
private static bool Exists(string parentDirectory, string fileNameOnly) =>
|
||
File.Exists(Path.Combine(parentDirectory, fileNameOnly));
|
||
}
|