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

Added in memory storage service

This commit is contained in:
Uncled1023 2021-08-06 23:21:24 -07:00
parent 2331d1dd9f
commit 3d7fbd6054
6 changed files with 105 additions and 10 deletions

View File

@ -9,6 +9,7 @@ namespace Teknik.Configuration
public enum StorageType
{
Local,
InMemory,
S3
}
}

View File

@ -44,7 +44,7 @@ namespace StorageService
Directory.CreateDirectory(_config.LocalDirectory);
string filePath = GetFilePath(fileName);
AesCounterManaged.EncryptToFile(filePath, file, chunkSize, key, iv);
AesCounterManaged.EncryptToFile(file, filePath, chunkSize, key, iv);
}
public override void SaveFile(string fileName, Stream file)

View File

@ -0,0 +1,92 @@
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;
namespace StorageService
{
public class MemoryStorageService : StorageService
{
private static Dictionary<string, byte[]> _files;
private Dictionary<string, byte[]> Files
{
get
{
if (_files == null)
_files = new Dictionary<string, byte[]>();
return _files;
}
set
{
_files = value;
}
}
public MemoryStorageService(StorageConfig config) : base(config)
{
}
public override string GetUniqueFileName()
{
string filename = StringHelper.RandomString(_config.FileNameLength);
while (Files.ContainsKey(string.Format("{0}.{1}", filename, _config.FileExtension)))
{
filename = StringHelper.RandomString(_config.FileNameLength);
}
return filename;
}
public override List<string> GetFileNames()
{
return Files.Keys.ToList();
}
public override Stream GetFile(string fileName)
{
if (string.IsNullOrEmpty(fileName))
return null;
if (!Files.ContainsKey(fileName))
return null;
return new MemoryStream(Files[fileName]);
}
public override void SaveEncryptedFile(string fileName, Stream file, int chunkSize, byte[] key, byte[] iv)
{
if (file == null ||
Files.ContainsKey(fileName))
return;
using (var ms = new MemoryStream())
{
AesCounterManaged.EncryptToStream(file, ms, chunkSize, key, iv);
Files.Add(fileName, ms.ToArray());
}
}
public override void SaveFile(string fileName, Stream file)
{
if (file == null ||
Files.ContainsKey(fileName))
return;
using (var ms = new MemoryStream())
{
file.Seek(0, SeekOrigin.Begin);
file.CopyTo(ms);
Files.Add(fileName, ms.ToArray());
}
}
public override void DeleteFile(string fileName)
{
if (Files.ContainsKey(fileName))
Files.Remove(fileName);
}
}
}

View File

@ -13,6 +13,8 @@ namespace StorageService
{
switch (config.Type)
{
case StorageType.InMemory:
return new MemoryStorageService(config);
case StorageType.Local:
return new LocalStorageService(config);
case StorageType.S3:

View File

@ -76,7 +76,7 @@ namespace Teknik.Utilities.Cryptography
return output;
}
public static void EncryptToFile(string filePath, Stream input, int chunkSize, byte[] key, byte[] iv)
public static void EncryptToFile(Stream input, string filePath, int chunkSize, byte[] key, byte[] iv)
{
using (FileStream fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write))

View File

@ -15,23 +15,23 @@ namespace Teknik.Utilities
{
string filename = StringHelper.RandomString(length);
string subDir = filename[0].ToString();
path = Path.Combine(path, subDir);
if (!Directory.Exists(path))
var fullPath = Path.Combine(path, subDir);
if (!Directory.Exists(fullPath))
{
Directory.CreateDirectory(path);
Directory.CreateDirectory(fullPath);
}
while (File.Exists(Path.Combine(path, string.Format("{0}.{1}", filename, extension))))
while (File.Exists(Path.Combine(fullPath, string.Format("{0}.{1}", filename, extension))))
{
filename = StringHelper.RandomString(length);
subDir = filename[0].ToString();
path = Path.Combine(path, subDir);
if (!Directory.Exists(path))
fullPath = Path.Combine(path, subDir);
if (!Directory.Exists(fullPath))
{
Directory.CreateDirectory(path);
Directory.CreateDirectory(fullPath);
}
}
return Path.Combine(path, string.Format("{0}.{1}", filename, extension));
return Path.Combine(fullPath, string.Format("{0}.{1}", filename, extension));
}
return string.Empty;