diff options
Diffstat (limited to 'lib/Utils')
-rw-r--r-- | lib/Utils/src/Extensions/IoExtensions.cs | 44 | ||||
-rw-r--r-- | lib/Utils/src/IO/ISimpleFilesystem.cs | 72 |
2 files changed, 116 insertions, 0 deletions
diff --git a/lib/Utils/src/Extensions/IoExtensions.cs b/lib/Utils/src/Extensions/IoExtensions.cs index bcbe810..d727fed 100644 --- a/lib/Utils/src/Extensions/IoExtensions.cs +++ b/lib/Utils/src/Extensions/IoExtensions.cs @@ -371,6 +371,7 @@ namespace VNLib.Utils.Extensions string fullPath = Path.Combine(dir.FullName, fileName); return new FileStream(fullPath, mode, access, share, bufferSize, options); } + /// <summary> /// Deletes the speicifed file from the current directory /// </summary> @@ -383,6 +384,7 @@ namespace VNLib.Utils.Extensions string fullPath = Path.Combine(dir.FullName, fileName); File.Delete(fullPath); } + /// <summary> /// Determines if a file exists within the current directory /// </summary> @@ -396,5 +398,47 @@ namespace VNLib.Utils.Extensions string fullPath = Path.Combine(dir.FullName, fileName); return FileOperations.FileExists(fullPath); } + + + /// <summary> + /// Creates a new scope for the given filesystem. All operations will be offset by the given path + /// within the parent filesystem. + /// </summary> + /// <param name="fs"></param> + /// <param name="offsetPath">The base path to prepend to all requests</param> + /// <returns>A new <see cref="ISimpleFilesystem"/> with a new filesystem directory scope</returns> + public static ISimpleFilesystem CreateNewScope(this ISimpleFilesystem fs, string offsetPath) => new FsScope(fs, offsetPath); + + private sealed record class FsScope(ISimpleFilesystem Parent, string OffsetPath) : ISimpleFilesystem + { + + ///<inheritdoc/> + public Task DeleteFileAsync(string filePath, CancellationToken cancellation) + { + string path = Path.Combine(OffsetPath, filePath); + return Parent.DeleteFileAsync(path, cancellation); + } + + ///<inheritdoc/> + public string GetExternalFilePath(string filePath) + { + string path = Path.Combine(OffsetPath, filePath); + return Parent.GetExternalFilePath(path); + } + + ///<inheritdoc/> + public Task<long> ReadFileAsync(string filePath, Stream output, CancellationToken cancellation) + { + string path = Path.Combine(OffsetPath, filePath); + return Parent.ReadFileAsync(path, output, cancellation); + } + + ///<inheritdoc/> + public Task WriteFileAsync(string filePath, Stream data, string contentType, CancellationToken cancellation) + { + string path = Path.Combine(OffsetPath, filePath); + return Parent.WriteFileAsync(path, data, contentType, cancellation); + } + } } }
\ No newline at end of file diff --git a/lib/Utils/src/IO/ISimpleFilesystem.cs b/lib/Utils/src/IO/ISimpleFilesystem.cs new file mode 100644 index 0000000..f730a46 --- /dev/null +++ b/lib/Utils/src/IO/ISimpleFilesystem.cs @@ -0,0 +1,72 @@ +/* +* Copyright (c) 2023 Vaughn Nugent +* +* Library: VNLib +* Package: VNLib.Utils +* File: ISimpleFilesystem.cs +* +* ISimpleFilesystem.cs is part of VNLib.Utils which is part +* of the larger VNLib collection of libraries and utilities. +* +* VNLib.Utils is free software: you can redistribute it and/or modify +* it under the terms of the GNU Affero General Public License as +* published by the Free Software Foundation, either version 3 of the +* License, or (at your option) any later version. +* +* VNLib.Utils is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU Affero General Public License for more details. +* +* You should have received a copy of the GNU Affero General Public License +* along with this program. If not, see https://www.gnu.org/licenses/. +*/ + +using System.IO; +using System.Threading; +using System.Threading.Tasks; + +namespace VNLib.Utils.IO +{ + + /// <summary> + /// Represents an opaque storage interface that abstracts simple storage operations + /// ignorant of the underlying storage system. + /// </summary> + public interface ISimpleFilesystem + { + /// <summary> + /// Gets the full public file path for the given relative file path + /// </summary> + /// <param name="filePath">The relative file path of the item to get the full path for</param> + /// <returns>The full relative file path</returns> + string GetExternalFilePath(string filePath); + + /// <summary> + /// Deletes a file from the storage system asynchronously + /// </summary> + /// <param name="filePath">The path to the file to delete</param> + /// <param name="cancellation">A token to cancel the operation</param> + /// <returns>A task that represents and asynchronous work</returns> + Task DeleteFileAsync(string filePath, CancellationToken cancellation); + + /// <summary> + /// Writes a file from the stream to the given file location + /// </summary> + /// <param name="filePath">The path to the file to write to</param> + /// <param name="data">The file data to stream</param> + /// <param name="contentType">The content type of the file to write</param> + /// <param name="cancellation">A token to cancel the operation</param> + /// <returns>A task that represents and asynchronous work</returns> + Task WriteFileAsync(string filePath, Stream data, string contentType, CancellationToken cancellation); + + /// <summary> + /// Reads a file from the storage system at the given path asynchronously + /// </summary> + /// <param name="filePath">The file to read</param> + /// <param name="output">The stream to write the file output to</param> + /// <param name="cancellation">A token to cancel the operation</param> + /// <returns>The number of bytes read, -1 if the operation failed</returns> + Task<long> ReadFileAsync(string filePath, Stream output, CancellationToken cancellation); + } +} |