namespace MediaLibrary.Services
{
///
/// Implementation for a local file store, mainly used for local testing.
///
public class FileSystemStorageService : IStorageService
{
private string baseFileLocation = Path.PathSeparator + "mockstorage";
public FileSystemStorageService ()
{
if (!Directory.Exists (baseFileLocation))
{
Directory.CreateDirectory (baseFileLocation);
}
}
///
/// Write the file to primary storage, retruing the file storage location.
///
///
///
public string SaveFile(IFormFile file)
{
var filePath = Path.Combine(baseFileLocation, DateTime.Now.Ticks.ToString() + Path.GetExtension(file.FileName));
using (var stream = System.IO.File.Create(filePath))
{
file.CopyTo(stream);
return Path.Combine(baseFileLocation, filePath);
}
}
public Task GetFileList()
{
return new Task(() =>
{
return Directory.GetFiles(baseFileLocation);
});
}
public Task PurgeFiles()
{
return new Task(() =>
{
string[] FileList = Directory.GetFiles(baseFileLocation);
foreach (string file in FileList)
{
File.Delete(file);
}
return FileList.Length;
});
}
public async Task DeleteFile(string KeyName)
{
try
{
File.Delete(string.Format("{0}{1}", new[] { baseFileLocation, KeyName }));
return true;
}
catch (Exception ex)
{
string message = ex.Message;
return false;
}
}
}
}