DD.WellWorkover.Cloud/AsbCloudInfrastructure/Repository/FileStorageRepository.cs

60 lines
1.7 KiB
C#

using AsbCloudApp.Exceptions;
using AsbCloudApp.Services;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
namespace AsbCloudInfrastructure.Repository
{
public class FileStorageRepository : IFileStorageRepository
{
public string RootPath { get; private set; }
public FileStorageRepository()
{
RootPath = "files";
}
public async Task CopyFileAsync(string filePath, Stream fileStream, CancellationToken token)
{
CreateDirectory(filePath);
using var newfileStream = new FileStream(filePath, FileMode.Create);
await fileStream.CopyToAsync(newfileStream, token).ConfigureAwait(false);
}
public void DeleteFile(string fileName)
{
if (File.Exists(fileName))
File.Delete(fileName);
}
public long GetLengthFile(string srcFilePath)
{
if (!File.Exists(srcFilePath))
throw new ArgumentInvalidException($"file {srcFilePath} doesn't exist", nameof(srcFilePath));
var sysFileInfo = new FileInfo(srcFilePath);
return sysFileInfo.Length;
}
public void MoveFile(string srcFilePath, string filePath)
{
CreateDirectory(filePath);
File.Move(srcFilePath, filePath);
}
public bool FileExists(string fullPath, string fileName)
{
if (!File.Exists(fullPath))
throw new FileNotFoundException("not found", fileName);
return true;
}
private void CreateDirectory(string filePath)
{
Directory.CreateDirectory(Path.GetDirectoryName(filePath));
}
}
}