using PoweredSoft.Storage.Core; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace PoweredSoft.Storage.Physical { public class PhysicalStorageProvider : IStorageProvider { public Task CreateDirectoryAsync(string path) { var directoryInfo = System.IO.Directory.CreateDirectory(path); var result = new PhysicalDirectoryInfo(path); return Task.FromResult(result); } public Task DeleteDirectoryAsync(string path, bool force = false) { if (force) Directory.Delete(path, true); else Directory.Delete(path); return Task.CompletedTask; } public Task DeleteFileAsync(string path) { System.IO.File.Delete(path); return Task.CompletedTask; } public Task FileExistsAsync(string path) { return Task.FromResult(File.Exists(path)); } public Task> GetDirectories(string path) { var directoryInfo = new DirectoryInfo(path); var directories = directoryInfo.GetDirectories(); var directoriesConverted = directories.Select(t => new PhysicalDirectoryInfo(t.FullName)).AsEnumerable().ToList(); return Task.FromResult(directoriesConverted); } public async Task GetFileBytesAsync(string path) { await ThrowNotExistingAsync(path); return File.ReadAllBytes(path); } public async Task GetFileContentAsync(string path, Encoding encoding) { await ThrowNotExistingAsync(path); return File.ReadAllText(path, encoding); } public Task> GetFilesAsync(string path, string pattern = null, SearchOption searchOption = SearchOption.TopDirectoryOnly) { var directoryInfo = new DirectoryInfo(path); FileInfo[] files; if (string.IsNullOrWhiteSpace(pattern)) files = directoryInfo.GetFiles(); else files = directoryInfo.GetFiles(pattern, searchOption); var result = files.Select(fileInfo => new PhysicalFileInfo(fileInfo)).AsEnumerable().ToList(); return Task.FromResult(result); } private async Task ThrowNotExistingAsync(string path) { if (false == await this.FileExistsAsync(path)) throw new FileDoesNotExistException(path); } public async Task GetFileStreamAsync(string path) { await ThrowNotExistingAsync(path); return new FileStream(path, FileMode.Open, FileAccess.Read); } public async Task> GetListAsync(string path) { var files = await this.GetFilesAsync(path); var directories = await this.GetDirectories(path); var result = files.AsEnumerable().Concat(directories.AsEnumerable()).ToList(); return result; } public async Task WriteFileAsync(string sourcePath, string path, IWriteFileOptions options) { if (options is null) { throw new ArgumentNullException(nameof(options)); } if (!options.OverrideIfExists && await FileExistsAsync(path)) throw new FileAlreadyExistsException(path); CreateDirectoryIfNotExisting(path); System.IO.File.Copy(sourcePath, path, options.OverrideIfExists); var fileInfo = new FileInfo(path); var ret = new PhysicalFileInfo(fileInfo); return ret; } public async Task WriteFileAsync(byte[] bytes, string path, IWriteFileOptions options) { if (options is null) { throw new ArgumentNullException(nameof(options)); } if (!options.OverrideIfExists && await FileExistsAsync(path)) throw new FileAlreadyExistsException(path); CreateDirectoryIfNotExisting(path); File.WriteAllBytes(path, bytes); var fileInfo = new FileInfo(path); var physicalinfo = new PhysicalFileInfo(fileInfo); return physicalinfo; } public async Task WriteFileAsync(Stream stream, string path, IWriteFileOptions options) { if (options is null) { throw new ArgumentNullException(nameof(options)); } if (!options.OverrideIfExists && await FileExistsAsync(path)) throw new FileAlreadyExistsException(path); CreateDirectoryIfNotExisting(path); if (stream.CanSeek && stream.Position != 0) stream.Seek(0, SeekOrigin.Begin); using (var fileStream = new FileStream(path, FileMode.CreateNew, FileAccess.Write)) { await stream.CopyToAsync(fileStream); fileStream.Close(); } var fileInfo = new FileInfo(path); var physicalinfo = new PhysicalFileInfo(fileInfo); return physicalinfo; } private void CreateDirectoryIfNotExisting(string path) { var directoryPath = Path.GetDirectoryName(path); if (!Directory.Exists(directoryPath)) Directory.CreateDirectory(directoryPath); } public bool IsFileNameAllowed(string fileName) { return true; } public string SanitizeFileName(string key, string replacement) { return key; } public Task WriteFileAsync(string sourcePath, string path, bool overrideIfExists = true) { return WriteFileAsync(sourcePath, path, new DefaultWriteOptions { OverrideIfExists = overrideIfExists }); } public Task WriteFileAsync(byte[] bytes, string path, bool overrideIfExists = true) { return WriteFileAsync(bytes, path, new DefaultWriteOptions { OverrideIfExists = overrideIfExists }); } public Task WriteFileAsync(Stream stream, string path, bool overrideIfExists = true) { return WriteFileAsync(stream, path, new DefaultWriteOptions { OverrideIfExists = overrideIfExists }); } } }