1
0
mirror of https://git.teknik.io/Teknikode/Teknik.git synced 2023-08-02 14:16:22 +02:00
Teknik/StorageService/LocalStorageService.cs

85 lines
2.7 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Teknik.Configuration;
using Teknik.Utilities;
using Teknik.Utilities.Cryptography;
2021-08-12 03:44:15 +02:00
namespace Teknik.StorageService
{
public class LocalStorageService : StorageService
{
public LocalStorageService(StorageConfig config) : base(config)
{
}
public override string GetUniqueFileName()
{
string filePath = FileHelper.GenerateRandomFileName(_config.LocalDirectory, _config.FileExtension, _config.FileNameLength);
return Path.GetFileName(filePath);
}
public override List<string> GetFileNames()
{
return Directory.GetFiles(_config.LocalDirectory, "*.*", SearchOption.AllDirectories).Select(f => Path.GetFileName(f).ToLower()).ToList();
}
public override Stream GetFile(string fileName)
{
if (string.IsNullOrEmpty(fileName))
return null;
string filePath = GetFilePath(fileName);
if (File.Exists(filePath))
return new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
return null;
}
2022-05-28 20:00:15 +02:00
public override async Task SaveEncryptedFile(string fileName, Stream file, PooledArray key, PooledArray iv)
{
if (!Directory.Exists(_config.LocalDirectory))
Directory.CreateDirectory(_config.LocalDirectory);
string filePath = GetFilePath(fileName);
2022-05-22 03:55:22 +02:00
await AesCounterManaged.EncryptToFile(file, filePath, key, iv);
}
2022-05-22 03:55:22 +02:00
public override async Task SaveFile(string fileName, Stream file)
{
if (!Directory.Exists(_config.LocalDirectory))
Directory.CreateDirectory(_config.LocalDirectory);
string filePath = GetFilePath(fileName);
// Just write the stream to the file
using (FileStream fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write))
{
file.Seek(0, SeekOrigin.Begin);
2022-05-22 03:55:22 +02:00
await file.CopyToAsync(fileStream);
}
}
public override void DeleteFile(string fileName)
{
string filePath = GetFilePath(fileName);
// Delete the File
if (File.Exists(filePath))
{
File.Delete(filePath);
}
}
private string GetFilePath(string fileName)
{
if (string.IsNullOrEmpty(fileName))
return null;
string subDir = fileName[0].ToString().ToLower();
return Path.Combine(_config.LocalDirectory, subDir, fileName);
}
}
}