-
Notifications
You must be signed in to change notification settings - Fork 416
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #933 from peppy/storage-improvements
Storage improvements
- Loading branch information
Showing
6 changed files
with
111 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
// Copyright (c) 2007-2017 ppy Pty Ltd <[email protected]>. | ||
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu-framework/master/LICENCE | ||
|
||
using System; | ||
using System.IO; | ||
using osu.Framework.IO.File; | ||
using SQLite.Net; | ||
|
@@ -11,11 +12,35 @@ public abstract class Storage | |
{ | ||
protected string BaseName { get; set; } | ||
|
||
protected readonly string BasePath; | ||
|
||
/// <summary> | ||
/// An optional path to be added after <see cref="BaseName"/>. | ||
/// </summary> | ||
protected string SubDirectory { get; set; } = string.Empty; | ||
|
||
protected Storage(string baseName) | ||
{ | ||
BaseName = FileSafety.FilenameStrip(baseName); | ||
BasePath = LocateBasePath(); | ||
if (BasePath == null) | ||
throw new NullReferenceException(nameof(BasePath)); | ||
} | ||
|
||
/// <summary> | ||
/// Find the location which will be used as a root for this storage. | ||
/// This should usually be a platform-specific implementation. | ||
/// </summary> | ||
/// <returns></returns> | ||
protected abstract string LocateBasePath(); | ||
|
||
/// <summary> | ||
/// Get a Storage-usable path for the provided path. | ||
/// </summary> | ||
/// <param name="path">An incomplete path, usually provided as user input.</param> | ||
/// <returns></returns> | ||
protected string GetUsablePathFor(string path) => Path.Combine(BasePath, BaseName, SubDirectory, path); | ||
|
||
/// <summary> | ||
/// Check whether a file exists at the specified path. | ||
/// </summary> | ||
|
@@ -42,6 +67,25 @@ protected Storage(string baseName) | |
/// <param name="path">The path of the file to delete.</param> | ||
public abstract void Delete(string path); | ||
|
||
/// <summary> | ||
/// Retrieve a list of directories at the specified path. | ||
/// </summary> | ||
/// <param name="path">The path to list.</param> | ||
/// <returns>A list of directories in the path, relative to the path.</returns> | ||
public abstract string[] GetDirectories(string path); | ||
|
||
/// <summary> | ||
/// Retrieve a <see cref="Storage"/> for a contained directory. | ||
/// </summary> | ||
/// <param name="path">The subdirectory to use as a root.</param> | ||
/// <returns>A more specific storage.</returns> | ||
public Storage GetStorageForDirectory(string path) | ||
{ | ||
var clone = (Storage)MemberwiseClone(); | ||
clone.SubDirectory = path; | ||
return clone; | ||
} | ||
|
||
/// <summary> | ||
/// Retrieve a stream from an underlying file inside this storage. | ||
/// </summary> | ||
|