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