1
This commit is contained in:
89
Core/StorageProvider/FileProvider.cs
Normal file
89
Core/StorageProvider/FileProvider.cs
Normal file
@@ -0,0 +1,89 @@
|
||||
using Cysharp.Threading.Tasks;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace BITKit.IO
|
||||
{
|
||||
public class FileProvider : IStorageFile
|
||||
{
|
||||
readonly FileInfo fileInfo;
|
||||
public FileProvider(FileInfo fileInfo)
|
||||
{
|
||||
this.fileInfo = fileInfo;
|
||||
}
|
||||
public FileProvider(string path)
|
||||
{
|
||||
this.fileInfo = new FileInfo(path);
|
||||
}
|
||||
public Stream CreateFile()
|
||||
{
|
||||
return File.Create(fileInfo.FullName);
|
||||
}
|
||||
|
||||
public Task<Stream> CreateFileAsync()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public string GetFileType()
|
||||
{
|
||||
return fileInfo.Extension;
|
||||
}
|
||||
|
||||
public DateTime GetLastUpdated()
|
||||
{
|
||||
return fileInfo.LastWriteTime;
|
||||
}
|
||||
|
||||
public string GetName()
|
||||
{
|
||||
return fileInfo.Name;
|
||||
}
|
||||
|
||||
public string GetPath()
|
||||
{
|
||||
return fileInfo.FullName;
|
||||
}
|
||||
|
||||
public long GetSize()
|
||||
{
|
||||
return fileInfo.Length;
|
||||
}
|
||||
|
||||
public string GetText()
|
||||
{
|
||||
return File.ReadAllText(fileInfo.FullName);
|
||||
}
|
||||
|
||||
public async Task<string> GetTextAsync()
|
||||
{
|
||||
return await File.ReadAllTextAsync(fileInfo.FullName);
|
||||
}
|
||||
|
||||
public Stream OpenRead()
|
||||
{
|
||||
return File.OpenRead(fileInfo.FullName);
|
||||
}
|
||||
|
||||
public async Task<Stream> OpenReadAsync()
|
||||
{
|
||||
await UniTask.Yield();
|
||||
return OpenRead();
|
||||
}
|
||||
|
||||
public Stream OpenWrite()
|
||||
{
|
||||
return File.OpenWrite(fileInfo.FullName);
|
||||
}
|
||||
|
||||
public async Task<Stream> OpenWriteAsync()
|
||||
{
|
||||
await UniTask.Yield();
|
||||
return OpenWrite();
|
||||
}
|
||||
}
|
||||
}
|
63
Core/StorageProvider/FolderProvider.cs
Normal file
63
Core/StorageProvider/FolderProvider.cs
Normal file
@@ -0,0 +1,63 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace BITKit.IO
|
||||
{
|
||||
public class FolderProvider : IStorageFolder
|
||||
{
|
||||
readonly DirectoryInfo directoryInfo;
|
||||
public FolderProvider(DirectoryInfo directoryInfo)
|
||||
{
|
||||
this.directoryInfo = directoryInfo;
|
||||
}
|
||||
public FolderProvider(string path)
|
||||
{
|
||||
this.directoryInfo=new DirectoryInfo(path);
|
||||
}
|
||||
public DateTime GetLastUpdated()
|
||||
{
|
||||
return directoryInfo.LastWriteTime;
|
||||
}
|
||||
|
||||
public string GetName()
|
||||
{
|
||||
return directoryInfo.Name;
|
||||
}
|
||||
|
||||
public IStorageFolder GetParent()
|
||||
{
|
||||
return new FolderProvider(directoryInfo.Parent);
|
||||
}
|
||||
|
||||
public string GetPath()
|
||||
{
|
||||
return directoryInfo.FullName;
|
||||
}
|
||||
|
||||
public long GetSize()
|
||||
{
|
||||
return DirSize(directoryInfo);
|
||||
}
|
||||
long DirSize(DirectoryInfo d)
|
||||
{
|
||||
long size = 0;
|
||||
// Add file sizes.
|
||||
FileInfo[] fis = d.GetFiles();
|
||||
foreach (FileInfo fi in fis)
|
||||
{
|
||||
size += fi.Length;
|
||||
}
|
||||
// Add subdirectory sizes.
|
||||
DirectoryInfo[] dis = d.GetDirectories();
|
||||
foreach (DirectoryInfo di in dis)
|
||||
{
|
||||
size += DirSize(di);
|
||||
}
|
||||
return size;
|
||||
}
|
||||
}
|
||||
}
|
128
Core/StorageProvider/LocalStorageProvider.cs
Normal file
128
Core/StorageProvider/LocalStorageProvider.cs
Normal file
@@ -0,0 +1,128 @@
|
||||
using Cysharp.Threading.Tasks;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace BITKit.IO
|
||||
{
|
||||
[System.Serializable]
|
||||
public class LocalStorageProvider : StorageProvider
|
||||
{
|
||||
protected virtual string GetAdress(string path)
|
||||
{
|
||||
return path;
|
||||
}
|
||||
public override async Task<string> Combine(string path1, string path2)
|
||||
{
|
||||
await UniTask.Yield();
|
||||
return Path.Combine(GetAdress(path1), path2);
|
||||
}
|
||||
|
||||
public override async Task<IStorageFile> CreateFile(string path)
|
||||
{
|
||||
await UniTask.Yield();
|
||||
FileInfo fileInfo = new FileInfo(GetAdress(path));
|
||||
FileProvider file = new(fileInfo);
|
||||
return file;
|
||||
}
|
||||
|
||||
public override void CreateFolder(string path)
|
||||
{
|
||||
DirectoryInfo directoryInfo = new DirectoryInfo(GetAdress(path));
|
||||
directoryInfo.Create();
|
||||
}
|
||||
|
||||
public override void DeleteFile(string path)
|
||||
{
|
||||
FileInfo file = new FileInfo(GetAdress(path));
|
||||
file.Delete();
|
||||
}
|
||||
|
||||
public override void DeleteFolder(string path)
|
||||
{
|
||||
DirectoryInfo directoryInfo = new DirectoryInfo(GetAdress(path));
|
||||
directoryInfo.Delete();
|
||||
}
|
||||
|
||||
public override async Task<bool> FileExists(string path)
|
||||
{
|
||||
await UniTask.Yield();
|
||||
return File.Exists(GetAdress(path));
|
||||
}
|
||||
|
||||
public override async Task<IStorageFile> GetFile(string path)
|
||||
{
|
||||
await UniTask.Yield();
|
||||
return new FileProvider(new FileInfo(GetAdress(path)));
|
||||
}
|
||||
|
||||
public override async Task<IEnumerable<IStorageFile>> ListFiles(string path)
|
||||
{
|
||||
await UniTask.Yield();
|
||||
DirectoryInfo directoryInfo = new DirectoryInfo(GetAdress(path));
|
||||
directoryInfo.Create();
|
||||
return directoryInfo.GetFiles().Select(x => new FileProvider(x));
|
||||
}
|
||||
|
||||
public override async Task<IEnumerable<IStorageFolder>> ListFolders(string path)
|
||||
{
|
||||
await UniTask.Yield();
|
||||
DirectoryInfo directoryInfo = new DirectoryInfo(GetAdress(path));
|
||||
return directoryInfo.GetDirectories().Select(x => new FolderProvider(x));
|
||||
}
|
||||
|
||||
public override void RenameFile(string oldPath, string newPath)
|
||||
{
|
||||
File.Move(GetAdress(oldPath), GetAdress(newPath));
|
||||
}
|
||||
|
||||
public override void RenameFolder(string oldPath, string newPath)
|
||||
{
|
||||
DirectoryInfo directoryInfo = new DirectoryInfo(GetAdress(oldPath));
|
||||
directoryInfo.MoveTo(GetAdress(newPath));
|
||||
}
|
||||
|
||||
public override void SaveStream(string path, Stream inputStream)
|
||||
{
|
||||
using (var fs = File.Create(GetAdress(path)))
|
||||
{
|
||||
inputStream.CopyTo(fs);
|
||||
fs.Close();
|
||||
fs.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
public override async Task<bool> TryCreateFolder(string path)
|
||||
{
|
||||
await UniTask.Yield();
|
||||
DirectoryInfo dirInfo = new DirectoryInfo(GetAdress(path));
|
||||
if (dirInfo.Exists is false)
|
||||
{
|
||||
dirInfo.Create();
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public override async Task<bool> TrySaveStream(string path, Stream inputStream)
|
||||
{
|
||||
await UniTask.Yield();
|
||||
try
|
||||
{
|
||||
SaveStream(path, inputStream);
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
BIT4Log.LogException(ex);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user