1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-12-21 01:49:57 +02:00
Sonarr/src/NzbDrone.Common/PathExtensions.cs

199 lines
7.2 KiB
C#
Raw Normal View History

using System;
using System.IO;
using System.Text.RegularExpressions;
using NzbDrone.Common.EnsureThat;
using NzbDrone.Common.EnvironmentInfo;
namespace NzbDrone.Common
{
public static class PathExtensions
{
2013-07-16 03:46:41 +03:00
private const string APP_CONFIG_FILE = "config.xml";
private const string NZBDRONE_DB = "nzbdrone.db";
private const string NZBDRONE_LOG_DB = "logs.db";
private const string BACKUP_ZIP_FILE = "NzbDrone_Backup.zip";
2013-07-24 18:08:31 +03:00
private const string NLOG_CONFIG_FILE = "nlog.config";
2013-07-27 08:02:25 +03:00
private const string UPDATE_CLIENT_EXE = "nzbdrone.update.exe";
2013-04-30 03:04:14 +03:00
private static readonly string UPDATE_SANDBOX_FOLDER_NAME = "nzbdrone_update" + Path.DirectorySeparatorChar;
private static readonly string UPDATE_PACKAGE_FOLDER_NAME = "nzbdrone" + Path.DirectorySeparatorChar;
private static readonly string UPDATE_BACKUP_FOLDER_NAME = "nzbdrone_backup" + Path.DirectorySeparatorChar;
2014-02-07 05:13:24 +03:00
private static readonly string UPDATE_BACKUP_APPDATA_FOLDER_NAME = "nzbdrone_appdata_backup" + Path.DirectorySeparatorChar;
2013-04-30 03:04:14 +03:00
private static readonly string UPDATE_CLIENT_FOLDER_NAME = "NzbDrone.Update" + Path.DirectorySeparatorChar;
private static readonly string UPDATE_LOG_FOLDER_NAME = "UpdateLogs" + Path.DirectorySeparatorChar;
2013-07-26 08:22:22 +03:00
public static string CleanFilePath(this string path)
{
2013-12-01 02:53:07 +03:00
Ensure.That(path, () => path).IsNotNullOrWhiteSpace();
Ensure.That(path, () => path).IsValidPath();
2013-07-26 09:25:03 +03:00
var info = new FileInfo(path.Trim());
2013-07-26 09:11:55 +03:00
if (!OsInfo.IsLinux && info.FullName.StartsWith(@"\\")) //UNC
{
return info.FullName.TrimEnd('/', '\\', ' ');
}
2013-04-30 06:09:50 +03:00
return info.FullName.TrimEnd('/').Trim('\\', ' ');
}
public static bool PathEquals(this string firstPath, string secondPath)
{
2013-08-21 04:17:06 +03:00
if (OsInfo.IsLinux)
{
2013-12-01 03:33:59 +03:00
if (firstPath.Equals(secondPath)) return true;
2013-08-21 04:17:06 +03:00
return String.Equals(firstPath.CleanFilePath(), secondPath.CleanFilePath());
}
2013-12-01 03:33:59 +03:00
if (firstPath.Equals(secondPath, StringComparison.OrdinalIgnoreCase)) return true;
return String.Equals(firstPath.CleanFilePath(), secondPath.CleanFilePath(), StringComparison.OrdinalIgnoreCase);
}
2013-08-31 23:43:35 +03:00
private static readonly Regex WindowsPathWithDriveRegex = new Regex(@"^[a-zA-Z]:\\", RegexOptions.Compiled);
public static bool IsPathValid(this string path)
{
if (path.ContainsInvalidPathChars() || string.IsNullOrWhiteSpace(path))
{
return false;
}
2013-08-31 23:43:35 +03:00
if (OsInfo.IsLinux)
{
2013-08-31 23:43:35 +03:00
return path.StartsWith(Path.DirectorySeparatorChar.ToString());
}
2013-08-31 23:43:35 +03:00
if (path.StartsWith("\\") || WindowsPathWithDriveRegex.IsMatch(path))
{
2013-08-31 23:43:35 +03:00
return true;
}
2013-08-31 23:43:35 +03:00
return false;
}
2013-05-11 08:59:42 +03:00
public static bool ContainsInvalidPathChars(this string text)
{
return text.IndexOfAny(Path.GetInvalidPathChars()) >= 0;
}
private static string GetProperCapitalization(DirectoryInfo dirInfo)
2013-04-30 03:39:54 +03:00
{
var parentDirInfo = dirInfo.Parent;
if (parentDirInfo == null)
{
//Drive letter
return dirInfo.Name.ToUpper();
}
2013-08-30 05:10:43 +03:00
var folderName = dirInfo.Name;
if (dirInfo.Exists)
{
folderName = parentDirInfo.GetDirectories(dirInfo.Name)[0].Name;
}
return Path.Combine(GetProperCapitalization(parentDirInfo), folderName);
2013-04-30 03:39:54 +03:00
}
public static string GetActualCasing(this string path)
2013-04-30 03:39:54 +03:00
{
if (OsInfo.IsLinux || path.StartsWith("\\"))
{
return path;
}
if (Directory.Exists(path) && (File.GetAttributes(path) & FileAttributes.Directory) == FileAttributes.Directory)
{
return GetProperCapitalization(new DirectoryInfo(path));
}
var fileInfo = new FileInfo(path);
var dirInfo = fileInfo.Directory;
var fileName = fileInfo.Name;
if (dirInfo != null && fileInfo.Exists)
{
fileName = dirInfo.GetFiles(fileInfo.Name)[0].Name;
}
return Path.Combine(GetProperCapitalization(dirInfo), fileName);
2013-04-30 03:39:54 +03:00
}
public static string GetAppDataPath(this IAppFolderInfo appFolderInfo)
2011-11-13 10:27:16 +03:00
{
return appFolderInfo.AppDataFolder;
2013-05-20 03:30:02 +03:00
}
public static string GetLogFolder(this IAppFolderInfo appFolderInfo)
2013-05-20 03:30:02 +03:00
{
return Path.Combine(GetAppDataPath(appFolderInfo), "logs");
2011-11-13 10:27:16 +03:00
}
public static string GetConfigPath(this IAppFolderInfo appFolderInfo)
2011-11-13 10:27:16 +03:00
{
return Path.Combine(GetAppDataPath(appFolderInfo), APP_CONFIG_FILE);
2011-11-13 10:27:16 +03:00
}
public static string GetMediaCoverPath(this IAppFolderInfo appFolderInfo)
2011-12-02 10:07:18 +03:00
{
return Path.Combine(GetAppDataPath(appFolderInfo), "MediaCover");
}
public static string GetUpdateLogFolder(this IAppFolderInfo appFolderInfo)
{
return Path.Combine(GetAppDataPath(appFolderInfo), UPDATE_LOG_FOLDER_NAME);
}
public static string GetUpdateSandboxFolder(this IAppFolderInfo appFolderInfo)
{
return Path.Combine(appFolderInfo.TempFolder, UPDATE_SANDBOX_FOLDER_NAME);
}
public static string GetUpdateBackUpFolder(this IAppFolderInfo appFolderInfo)
{
return Path.Combine(GetUpdateSandboxFolder(appFolderInfo), UPDATE_BACKUP_FOLDER_NAME);
}
2014-02-07 05:13:24 +03:00
public static string GetUpdateBackUpAppDataFolder(this IAppFolderInfo appFolderInfo)
{
return Path.Combine(GetUpdateSandboxFolder(appFolderInfo), UPDATE_BACKUP_APPDATA_FOLDER_NAME);
}
public static string GetUpdatePackageFolder(this IAppFolderInfo appFolderInfo)
{
return Path.Combine(GetUpdateSandboxFolder(appFolderInfo), UPDATE_PACKAGE_FOLDER_NAME);
}
public static string GetUpdateClientFolder(this IAppFolderInfo appFolderInfo)
2011-11-14 04:27:11 +03:00
{
return Path.Combine(GetUpdatePackageFolder(appFolderInfo), UPDATE_CLIENT_FOLDER_NAME);
2011-11-14 04:27:11 +03:00
}
public static string GetUpdateClientExePath(this IAppFolderInfo appFolderInfo)
{
return Path.Combine(GetUpdateSandboxFolder(appFolderInfo), UPDATE_CLIENT_EXE);
}
public static string GetConfigBackupFile(this IAppFolderInfo appFolderInfo)
2012-01-27 08:05:09 +03:00
{
return Path.Combine(GetAppDataPath(appFolderInfo), BACKUP_ZIP_FILE);
2012-01-27 08:05:09 +03:00
}
public static string GetNzbDroneDatabase(this IAppFolderInfo appFolderInfo)
{
return Path.Combine(GetAppDataPath(appFolderInfo), NZBDRONE_DB);
}
2013-05-21 06:20:29 +03:00
public static string GetLogDatabase(this IAppFolderInfo appFolderInfo)
2013-05-21 06:20:29 +03:00
{
return Path.Combine(GetAppDataPath(appFolderInfo), NZBDRONE_LOG_DB);
2013-05-21 06:20:29 +03:00
}
2013-07-24 18:08:31 +03:00
public static string GetNlogConfigPath(this IAppFolderInfo appFolderInfo)
{
return Path.Combine(appFolderInfo.StartUpFolder, NLOG_CONFIG_FILE);
}
}
}