Skip to content

Commit 4f1e803

Browse files
committed
interface
1 parent a8a7884 commit 4f1e803

File tree

6 files changed

+190
-5
lines changed

6 files changed

+190
-5
lines changed

ManagedCode.Storage.Aws/ManagedCode.Storage.Aws.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<PackageProjectUrl>https://github.com/managed-code-hub/Storage</PackageProjectUrl>
1212
<PackageLicenseUrl>https://github.com/managed-code-hub/Storage/blob/main/LICENSE</PackageLicenseUrl>
1313
<RepositoryUrl>https://github.com/managed-code-hub/Storage</RepositoryUrl>
14-
<PackageVersion>0.0.4</PackageVersion>
14+
<PackageVersion>0.0.1</PackageVersion>
1515
<Description>Storage for AWS</Description>
1616
<PackageIcon>logo.png</PackageIcon>
1717
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>

ManagedCode.Storage.Azure/ManagedCode.Storage.Azure.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
<PackageProjectUrl>https://github.com/managed-code-hub/Storage</PackageProjectUrl>
1212
<PackageLicenseUrl>https://github.com/managed-code-hub/Storage/blob/main/LICENSE</PackageLicenseUrl>
1313
<RepositoryUrl>https://github.com/managed-code-hub/Storage</RepositoryUrl>
14-
<PackageVersion>0.0.4</PackageVersion>
15-
<Description>Storage for Azurey</Description>
14+
<PackageVersion>0.0.1</PackageVersion>
15+
<Description>Storage for Azure</Description>
1616
<PackageIcon>logo.png</PackageIcon>
1717
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
1818
</PropertyGroup>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Threading;
5+
using System.Threading.Tasks;
6+
7+
namespace ManagedCode.Storage.Core
8+
{
9+
public interface IStorage : IDisposable
10+
{
11+
IAsyncEnumerable<Blob> GetBlobListAsync(CancellationToken cancellationToken = default);
12+
IAsyncEnumerable<Blob> GetBlob(string blob, CancellationToken cancellationToken = default);
13+
IAsyncEnumerable<Blob> GetBlob(Blob blob, CancellationToken cancellationToken = default);
14+
IAsyncEnumerable<Blob> GetBlob(IEnumerable<string> blobs, CancellationToken cancellationToken = default);
15+
IAsyncEnumerable<Blob> GetBlob(IEnumerable<Blob> blobs, CancellationToken cancellationToken = default);
16+
17+
Task UploadAsync(string blob, Stream dataStream, bool append = false, CancellationToken cancellationToken = default);
18+
Task UploadAsync(string blob, string pathToFile, bool append = false, CancellationToken cancellationToken = default);
19+
Task UploadAsync(Blob blob, Stream dataStream, bool append = false, CancellationToken cancellationToken = default);
20+
Task UploadAsync(Blob blob, string pathToFile, bool append = false, CancellationToken cancellationToken = default);
21+
22+
Task<Stream> DownloadAsStreamAsync(string blob, CancellationToken cancellationToken = default);
23+
Task<Stream> DownloadAsStreamAsync(Blob blob, CancellationToken cancellationToken = default);
24+
Task<LocalFile> DownloadAsync(string blob, CancellationToken cancellationToken = default);
25+
Task<LocalFile> DownloadAsync(Blob blob, CancellationToken cancellationToken = default);
26+
27+
Task DeleteAsync(string blob, CancellationToken cancellationToken = default);
28+
Task DeleteAsync(Blob blob, CancellationToken cancellationToken = default);
29+
Task DeleteAsync(IEnumerable<string> blobs, CancellationToken cancellationToken = default);
30+
Task DeleteAsync(IEnumerable<Blob> blobs, CancellationToken cancellationToken = default);
31+
32+
Task<bool> ExistsAsync(string blob, CancellationToken cancellationToken = default);
33+
Task<bool> ExistsAsync(Blob blob, CancellationToken cancellationToken = default);
34+
IAsyncEnumerable<bool> ExistsAsync(IEnumerable<string> blobs, CancellationToken cancellationToken = default);
35+
IAsyncEnumerable<bool> ExistsAsync(IEnumerable<Blob> blobs, CancellationToken cancellationToken = default);
36+
37+
}
38+
39+
public class Blob
40+
{
41+
public string Path { get; set; }
42+
}
43+
}
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
using System;
2+
using System.IO;
3+
using System.Threading.Tasks;
4+
5+
namespace ManagedCode.Storage.Core
6+
{
7+
public class LocalFile : IDisposable, IAsyncDisposable
8+
{
9+
private readonly object _lockObject = new();
10+
private bool _disposed;
11+
private FileStream _stream;
12+
13+
public LocalFile(bool keepAlive = false) : this(Path.GetTempFileName(), keepAlive)
14+
{
15+
}
16+
17+
public LocalFile(string path, bool keepAlive = false)
18+
{
19+
string directory;
20+
KeepAlive = keepAlive;
21+
22+
if (string.IsNullOrEmpty(Path.GetExtension(path)))
23+
{
24+
directory = path;
25+
var tempName = Guid.NewGuid().ToString("N");
26+
FilePath = Path.Combine(path, $"{tempName}.tmp");
27+
}
28+
else
29+
{
30+
directory = Path.GetDirectoryName(path);
31+
FilePath = path;
32+
}
33+
34+
if (!Directory.Exists(directory))
35+
{
36+
Directory.CreateDirectory(directory ?? throw new InvalidOperationException());
37+
}
38+
39+
if (!File.Exists(FilePath))
40+
{
41+
File.Create(FilePath);
42+
}
43+
44+
FileName = FileInfo.Name;
45+
}
46+
47+
public string FilePath { get; }
48+
49+
public string FileName { get; }
50+
51+
public bool KeepAlive { get; set; }
52+
53+
public FileInfo FileInfo => new(FilePath);
54+
55+
public FileStream FileStream
56+
{
57+
get
58+
{
59+
lock (_lockObject)
60+
{
61+
CloseFileStream();
62+
63+
if (_disposed)
64+
{
65+
throw new ObjectDisposedException(FilePath);
66+
}
67+
68+
if (!FileInfo.Exists)
69+
{
70+
throw new FileNotFoundException(FilePath);
71+
}
72+
73+
_stream = new FileStream(FilePath, FileMode.Open, FileAccess.ReadWrite);
74+
return _stream;
75+
}
76+
}
77+
}
78+
79+
public ValueTask DisposeAsync()
80+
{
81+
return new(Task.Run(Dispose));
82+
}
83+
84+
public void Dispose()
85+
{
86+
if (_disposed)
87+
{
88+
return;
89+
}
90+
91+
lock (_lockObject)
92+
{
93+
try
94+
{
95+
CloseFileStream();
96+
97+
if (KeepAlive)
98+
{
99+
return;
100+
}
101+
102+
if (File.Exists(FilePath))
103+
{
104+
File.Delete(FilePath);
105+
}
106+
}
107+
finally
108+
{
109+
_disposed = true;
110+
}
111+
}
112+
}
113+
114+
~LocalFile()
115+
{
116+
Dispose();
117+
}
118+
119+
private void CloseFileStream()
120+
{
121+
_stream?.Dispose();
122+
_stream = null;
123+
}
124+
125+
public void Close()
126+
{
127+
lock (_lockObject)
128+
{
129+
CloseFileStream();
130+
}
131+
}
132+
133+
public static async Task<LocalFile> FromStream(Stream stream)
134+
{
135+
var file = new LocalFile();
136+
await stream.CopyToAsync(file.FileStream);
137+
await file.FileStream.DisposeAsync();
138+
file.Close();
139+
return file;
140+
}
141+
}
142+
}

ManagedCode.Storage.Core/ManagedCode.Storage.Core.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<PackageProjectUrl>https://github.com/managed-code-hub/Storage</PackageProjectUrl>
1212
<PackageLicenseUrl>https://github.com/managed-code-hub/Storage/blob/main/LICENSE</PackageLicenseUrl>
1313
<RepositoryUrl>https://github.com/managed-code-hub/Storage</RepositoryUrl>
14-
<PackageVersion>0.0.4</PackageVersion>
14+
<PackageVersion>0.0.1</PackageVersion>
1515
<Description>Base implementation Storage, Include FTP and Local File system</Description>
1616
<PackageIcon>logo.png</PackageIcon>
1717
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>

ManagedCode.Storage.Gcp/ManagedCode.Storage.Gcp.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<PackageProjectUrl>https://github.com/managed-code-hub/Storage</PackageProjectUrl>
1212
<PackageLicenseUrl>https://github.com/managed-code-hub/Storage/blob/main/LICENSE</PackageLicenseUrl>
1313
<RepositoryUrl>https://github.com/managed-code-hub/Storage</RepositoryUrl>
14-
<PackageVersion>0.0.4</PackageVersion>
14+
<PackageVersion>0.0.1</PackageVersion>
1515
<Description>Storage for GCP</Description>
1616
<PackageIcon>logo.png</PackageIcon>
1717
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>

0 commit comments

Comments
 (0)