// Copyright (c) Arlo Godfrey. All Rights Reserved. // Licensed under the GNU Lesser General Public License, Version 3.0 with additional terms. // See the LICENSE, LICENSE.LESSER and LICENSE.ADDITIONAL files in the project root for more information. using System; using System.Collections.Generic; using System.IO; using System.Linq; using OwlCore.AbstractStorage; namespace StrixMusic.Sdk.FileMetadata.Scanners { public partial class PlaylistMetadataScanner { private static string? TryGetHashFromExistingTracks(Uri path, IEnumerable files) { return files.FirstOrDefault(c => c?.TrackMetadata?.Url == path.OriginalString)?.Id; } /// /// Determines whether a given path is full or relative. /// private static bool IsFullPath(string path) { // FIXME: http:// paths are not recognized as absolute paths if (string.IsNullOrWhiteSpace(path) || path.IndexOfAny(Path.GetInvalidPathChars()) != -1 || !Path.IsPathRooted(path)) return false; var pathRoot = Path.GetPathRoot(path); if (pathRoot.Length <= 2 && pathRoot != "/") // Accepts X:\ and \\UNC\PATH, rejects empty string, \ and X:, but accepts / to support Linux return false; if (pathRoot[0] != '\\' || pathRoot[1] != '\\') return true; // Rooted and not a UNC path return pathRoot.Trim('\\').IndexOf('\\') != -1; // A UNC server name without a share name (e.g "\\NAME" or "\\NAME\") is invalid } /// /// Resolves a potentially relative file path to an absolute path. /// /// The path to resolve. /// The path to append to /// if it's relative. /// /// This method is safe to use on absolute paths as well. /// Does not work for Unix paths. /// /// An absolute path. public static string ResolveFilePath(string path, string currentPath) { // Check if the path is absolute if (IsFullPath(path)) { // Path is absolute return path; } else { // Path is relative string fullPath; if (path.StartsWith("~", StringComparison.InvariantCulture)) { // Unix relative file path fullPath = Path.GetFullPath(Path.GetDirectoryName(currentPath) + path.Substring(1)); } else { fullPath = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(currentPath) ?? string.Empty, path)); } return fullPath; } } /// /// Resolves a potentially relative file path to an absolute path. /// /// The path to resolve. /// The file to resolve paths relative to. /// This method is safe to use on absolute paths as well. /// An absolute path. public static string ResolveFilePath(string path, IFileData fileData) { return ResolveFilePath(path, Path.GetDirectoryName(fileData.Path) ?? string.Empty); } } }