-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStorage.cs
More file actions
50 lines (42 loc) · 1.24 KB
/
Copy pathStorage.cs
File metadata and controls
50 lines (42 loc) · 1.24 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
using System.Text;
namespace ResumableFunctions;
public interface IStorage
{
Task Save(string name, string content);
Task<string> Load(string name);
Task<bool> Delete(string name);
IEnumerable<string> EnumerateFiles();
}
public sealed class Storage : IStorage
{
private readonly string rootFolder;
public Storage(string rootFolder)
{
this.rootFolder = Path.GetFullPath(rootFolder);
Directory.CreateDirectory(this.rootFolder);
}
public Task Save(string name, string content)
{
return File.WriteAllTextAsync(Path.Join(rootFolder, name), content, Encoding.UTF8);
}
public Task<string> Load(string name)
{
return File.ReadAllTextAsync(Path.Join(rootFolder, name), Encoding.UTF8);
}
public Task<bool> Delete(string name)
{
if (!File.Exists(Path.Join(rootFolder, name)))
{
return Task.FromResult(false);
}
File.Delete(Path.Join(rootFolder, name));
return Task.FromResult(true);
}
public IEnumerable<string> EnumerateFiles()
{
return Directory
.EnumerateFiles(rootFolder, "*", SearchOption.TopDirectoryOnly)
.Select(Path.GetFileName)
.OfType<string>();
}
}