Last active
March 3, 2021 10:07
-
-
Save SirRufo/6c31d6d688542bf0a139452087235163 to your computer and use it in GitHub Desktop.
IFileClient
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AzureBlobFileClient : IFileClient | |
{ | |
private CloudBlobClient _blobClient; | |
public AzureBlobFileClient(string connectionString) | |
{ | |
var account = CloudStorageAccount.Parse(connectionString); | |
_blobClient = account.CreateCloudBlobClient(); | |
} | |
public Task DeleteFile(string storeName, string filePath) | |
{ | |
var container = _blobClient.GetContainerReference(storeName); | |
var blob = container.GetBlockBlobReference(filePath.ToLower()); | |
return blob.DeleteIfExistsAsync(); | |
} | |
public Task<bool> FileExists(string storeName, string filePath) | |
{ | |
var container = _blobClient.GetContainerReference(storeName); | |
var blob = container.GetBlockBlobReference(filePath.ToLower()); | |
return blob.ExistsAsync(); | |
} | |
public async Task<Stream> GetFile(string storeName, string filePath) | |
{ | |
var container = _blobClient.GetContainerReference(storeName); | |
var blob = container.GetBlockBlobReference(filePath.ToLower()); | |
var mem = new MemoryStream(); | |
await blob.DownloadToStreamAsync(mem).ConfigureAwait(false); | |
mem.Seek(0, SeekOrigin.Begin); | |
return mem; | |
} | |
public async Task<string> GetFileUrl(string storeName, string filePath) | |
{ | |
var container = _blobClient.GetContainerReference(storeName); | |
var blob = container.GetBlockBlobReference(filePath.ToLower()); | |
string url = null; | |
if(await blob.ExistsAsync().ConfigureAwait(false)) | |
{ | |
url = blob.Uri.AbsoluteUri; | |
} | |
return url; | |
} | |
public Task SaveFile(string storeName, string filePath, Stream fileStream) | |
{ | |
var container = _blobClient.GetContainerReference(storeName); | |
var blob = container.GetBlockBlobReference(filePath.ToLower()); | |
return blob.UploadFromStreamAsync(fileStream); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class LocalFileClient : IFileClient | |
{ | |
private string _fileRoot; | |
public LocalFileClient(string fileRoot) | |
{ | |
_fileRoot = fileRoot; | |
} | |
public Task DeleteFile(string storeName, string filePath) | |
{ | |
var path = Path.Combine(_fileRoot, storeName, filePath); | |
if(File.Exists(path)) | |
{ | |
File.Delete(path); | |
} | |
return Task.CompletedTask; | |
} | |
public Task<bool> FileExists(string storeName, string filePath) | |
{ | |
var path = Path.Combine(_fileRoot, storeName, filePath); | |
return Task.FromResult(File.Exists(path)); | |
} | |
public Task<Stream> GetFile(string storeName, string filePath) | |
{ | |
var path = Path.Combine(_fileRoot, storeName, filePath); | |
Stream stream = null; | |
if(File.Exists(path)) | |
{ | |
stream = File.OpenRead(path); | |
} | |
return Task.FromResult(stream); | |
} | |
public Task<string> GetFileUrl(string storeName, string filePath) | |
{ | |
return Task.FromResult((string)null); | |
} | |
public async Task SaveFile(string storeName, string filePath, Stream fileStream) | |
{ | |
var path = Path.Combine(_fileRoot, storeName, filePath); | |
if(File.Exists(path)) | |
{ | |
File.Delete(path); | |
} | |
using (var file = new FileStream(path, FileMode.CreateNew)) | |
{ | |
await fileStream.CopyToAsync(file).ConfigureAwait(false); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment