-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLocalStorage.cs
More file actions
59 lines (48 loc) · 2.17 KB
/
LocalStorage.cs
File metadata and controls
59 lines (48 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using ECommerceAPI.Application.Abstractions.Storage.Local;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
namespace ECommerceAPI.Infrastructure.Services.Storage.Local
{
public class LocalStorage : Storage, ILocalStorage
{
readonly IWebHostEnvironment _webHostEnvironment;
public LocalStorage(IWebHostEnvironment webHostEnvironment)
{
_webHostEnvironment = webHostEnvironment;
}
public async Task DeleteAsync(string path, string fileName)
=> File.Delete($"{Path.Combine(_webHostEnvironment.WebRootPath, path)}\\{fileName}");
public List<string> GetFiles(string path)
=> new DirectoryInfo(Path.Combine(_webHostEnvironment.WebRootPath, path)).GetFiles().Select(f => f.Name).ToList();
public bool HasFile(string path, string fileName)
=> File.Exists($"{Path.Combine(_webHostEnvironment.WebRootPath, path)}\\{fileName}");
async Task<bool> CopyFileAsync(string path, IFormFile file)
{
try
{
await using FileStream fileStream = new(path, FileMode.Create, FileAccess.Write, FileShare.None, 1024 * 1024, useAsync: false);
await file.CopyToAsync(fileStream);
await fileStream.FlushAsync();
return true;
}
catch (Exception)
{ //todo log
throw;
}
}
public async Task<List<(string fileName, string pathOrContainerName)>> UploadAsync(string path, IFormFileCollection files)
{
string uploadPath = Path.Combine(_webHostEnvironment.WebRootPath, path);
if (!Directory.Exists(uploadPath))
Directory.CreateDirectory(uploadPath);
List<(string fileName, string path)> datas = new();
foreach (IFormFile file in files)
{
string newFileName = await FileRenameAsync(path, file.Name, GetFiles, HasFile);
bool result = await CopyFileAsync($"{uploadPath}\\{newFileName}", file);
datas.Add((newFileName, $"{path}\\{newFileName}"));
}
return datas;
}
}
}