Add project files.
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
using PoweredSoft.Storage.Core;
|
||||
|
||||
namespace PoweredSoft.Storage.Physical
|
||||
{
|
||||
public class PhysicalDirectoryInfo : IDirectoryInfo
|
||||
{
|
||||
public PhysicalDirectoryInfo(string path)
|
||||
{
|
||||
Path = path;
|
||||
}
|
||||
|
||||
public string Path { get; }
|
||||
public bool IsDirectory => true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
using PoweredSoft.Storage.Core;
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace PoweredSoft.Storage.Physical
|
||||
{
|
||||
public class PhysicalFileInfo : IFileInfo
|
||||
{
|
||||
private readonly FileInfo fileInfo;
|
||||
|
||||
public PhysicalFileInfo(FileInfo fileInfo)
|
||||
{
|
||||
this.fileInfo = fileInfo;
|
||||
}
|
||||
|
||||
public string Path => fileInfo.FullName;
|
||||
public string FileName => fileInfo.Name;
|
||||
public string Extension => fileInfo.Extension;
|
||||
public long FileSize => fileInfo.Length;
|
||||
public DateTimeOffset? CreatedTime => fileInfo.CreationTime;
|
||||
public DateTimeOffset? LastModifiedTime => fileInfo.LastWriteTime;
|
||||
public DateTimeOffset? LastAccessTime => fileInfo.LastAccessTime;
|
||||
public DateTime? CreatedTimeUtc => fileInfo.CreationTimeUtc;
|
||||
public DateTime? LastModifiedTimeUtc => fileInfo.LastWriteTimeUtc;
|
||||
public DateTime? LastAccessTimeUtc => fileInfo.LastAccessTimeUtc;
|
||||
public bool IsDirectory => false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,149 @@
|
||||
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<IDirectoryInfo> CreateDirectoryAsync(string path)
|
||||
{
|
||||
var directoryInfo = System.IO.Directory.CreateDirectory(path);
|
||||
var result = new PhysicalDirectoryInfo(path);
|
||||
return Task.FromResult<IDirectoryInfo>(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<bool> FileExistsAsync(string path)
|
||||
{
|
||||
return Task.FromResult(File.Exists(path));
|
||||
}
|
||||
|
||||
public Task<List<IDirectoryInfo>> GetDirectories(string path)
|
||||
{
|
||||
var directoryInfo = new DirectoryInfo(path);
|
||||
var directories = directoryInfo.GetDirectories();
|
||||
var directoriesConverted = directories.Select(t => new PhysicalDirectoryInfo(t.FullName)).AsEnumerable<IDirectoryInfo>().ToList();
|
||||
return Task.FromResult(directoriesConverted);
|
||||
}
|
||||
|
||||
public async Task<byte[]> GetFileBytesAsync(string path)
|
||||
{
|
||||
await ThrowNotExistingAsync(path);
|
||||
return File.ReadAllBytes(path);
|
||||
}
|
||||
|
||||
public async Task<string> GetFileContentAsync(string path, Encoding encoding)
|
||||
{
|
||||
await ThrowNotExistingAsync(path);
|
||||
return File.ReadAllText(path, encoding);
|
||||
}
|
||||
|
||||
public Task<List<IFileInfo>> 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<IFileInfo>().ToList();
|
||||
return Task.FromResult(result);
|
||||
}
|
||||
|
||||
private async Task ThrowNotExistingAsync(string path)
|
||||
{
|
||||
if (false == await this.FileExistsAsync(path))
|
||||
throw new FileDoesNotExistException(path);
|
||||
}
|
||||
|
||||
public async Task<Stream> GetFileStreamAsync(string path)
|
||||
{
|
||||
await ThrowNotExistingAsync(path);
|
||||
return new FileStream(path, FileMode.Open, FileAccess.Read);
|
||||
}
|
||||
|
||||
public async Task<List<IDirectoryOrFile>> GetListAsync(string path)
|
||||
{
|
||||
var files = await this.GetFilesAsync(path);
|
||||
var directories = await this.GetDirectories(path);
|
||||
var result = files.AsEnumerable<IDirectoryOrFile>().Concat(directories.AsEnumerable<IDirectoryOrFile>()).ToList();
|
||||
return result;
|
||||
}
|
||||
|
||||
public async Task<IFileInfo> WriteFileAsync(string sourcePath, string path, bool overrideIfExists = true)
|
||||
{
|
||||
if (!overrideIfExists && await FileExistsAsync(path))
|
||||
throw new FileAlreadyExistsException(path);
|
||||
|
||||
CreateDirectoryIfNotExisting(path);
|
||||
|
||||
System.IO.File.Copy(sourcePath, path, overrideIfExists);
|
||||
var fileInfo = new FileInfo(path);
|
||||
var ret = new PhysicalFileInfo(fileInfo);
|
||||
return ret;
|
||||
}
|
||||
|
||||
public async Task<IFileInfo> WriteFileAsync(byte[] bytes, string path, bool overrideIfExists = true)
|
||||
{
|
||||
if (!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<IFileInfo> WriteFileAsync(Stream stream, string path, bool overrideIfExists = true)
|
||||
{
|
||||
if (!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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.0</TargetFramework>
|
||||
<Copyright>Powered Softwares Inc.</Copyright>
|
||||
<Version>1.1.0$(VersionSuffix)</Version>
|
||||
<PackageIconUrl>https://secure.gravatar.com/avatar/4e32f73820c16718909a06c2927f1f8b?s=512&amp;r=g&amp;d=retro</PackageIconUrl>
|
||||
<Product>PoweredSoft.Storage.Physical</Product>
|
||||
<Description>initial</Description>
|
||||
<PackageId>PoweredSoft.Storage.Physical</PackageId>
|
||||
<PackageReleaseNotes>initial</PackageReleaseNotes>
|
||||
<PackageRequireLicenseAcceptance>False</PackageRequireLicenseAcceptance>
|
||||
<Company>PoweredSoft</Company>
|
||||
<Authors>PoweredSoft</Authors>
|
||||
<Deterministic>False</Deterministic>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\PoweredSoft.Storage.Core\PoweredSoft.Storage.Core.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
Reference in New Issue
Block a user