// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. using System; using System.Collections.Generic; using System.IO; namespace osu.Framework.Platform { public abstract class Storage { protected string BasePath { get; } protected Storage(string path, string subfolder = null) { static string filenameStrip(string entry) { foreach (char c in Path.GetInvalidFileNameChars()) entry = entry.Replace(c.ToString(), string.Empty); return entry; } BasePath = path; if (BasePath == null) throw new InvalidOperationException($"{nameof(BasePath)} not correctly initialized!"); if (!string.IsNullOrEmpty(subfolder)) BasePath = Path.Combine(BasePath, filenameStrip(subfolder)); } /// /// Get a usable filesystem path for the provided incomplete path. /// /// An incomplete path, usually provided as user input. /// Create the path if it doesn't already exist. /// A usable filesystem path. public abstract string GetFullPath(string path, bool createIfNotExisting = false); /// /// Check whether a file exists at the specified path. /// /// The path to check. /// Whether a file exists. public abstract bool Exists(string path); /// /// Check whether a directory exists at the specified path. /// /// The path to check. /// Whether a directory exists. public abstract bool ExistsDirectory(string path); /// /// Delete a directory and all its contents recursively. /// /// The path of the directory to delete. public abstract void DeleteDirectory(string path); /// /// Delete a file. /// /// The path of the file to delete. public abstract void Delete(string path); /// /// Retrieve a list of directories at the specified path. /// /// The path to list. /// A list of directories in the path, relative to the path of this storage. public abstract IEnumerable GetDirectories(string path); /// /// Retrieve a list of files at the specified path. /// /// The path to list. /// An optional search pattern. Accepts "*" wildcard. /// A list of files in the path, relative to the path of this storage. public abstract IEnumerable GetFiles(string path, string pattern = "*"); /// /// Retrieve a for a contained directory. /// Creates the path if not existing. /// /// The subdirectory to use as a root. /// A more specific storage. public virtual Storage GetStorageForDirectory(string path) { if (string.IsNullOrEmpty(path)) throw new ArgumentException("Must be non-null and not empty string", nameof(path)); if (!path.EndsWith(Path.DirectorySeparatorChar)) path += Path.DirectorySeparatorChar; // create non-existing path. var fullPath = GetFullPath(path, true); return (Storage)Activator.CreateInstance(GetType(), fullPath); } /// /// Retrieve a stream from an underlying file inside this storage. /// /// The path of the file. /// The access requirements. /// The mode in which the file should be opened. /// A stream associated with the requested path. public abstract Stream GetStream(string path, FileAccess access = FileAccess.Read, FileMode mode = FileMode.OpenOrCreate); /// /// Retrieve an SQLite database connection string from within this storage. /// /// The name of the database. /// An SQLite connection string. public abstract string GetDatabaseConnectionString(string name); /// /// Delete an SQLite database from within this storage. /// /// The name of the database to delete. public abstract void DeleteDatabase(string name); /// /// Opens a native file browser window to the root path of this storage. /// public void OpenInNativeExplorer() => OpenPathInNativeExplorer(string.Empty); /// /// Opens a native file browser window to the specified relative path. /// public abstract void OpenPathInNativeExplorer(string path); } }