diff --git a/Configuration/StorageType.cs b/Configuration/StorageType.cs index 1ec76af..9766630 100644 --- a/Configuration/StorageType.cs +++ b/Configuration/StorageType.cs @@ -9,6 +9,7 @@ namespace Teknik.Configuration public enum StorageType { Local, + InMemory, S3 } } diff --git a/StorageService/LocalStorageService.cs b/StorageService/LocalStorageService.cs index 5cb42b7..d567ced 100644 --- a/StorageService/LocalStorageService.cs +++ b/StorageService/LocalStorageService.cs @@ -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) diff --git a/StorageService/MemoryStorageService.cs b/StorageService/MemoryStorageService.cs new file mode 100644 index 0000000..cb2939e --- /dev/null +++ b/StorageService/MemoryStorageService.cs @@ -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 _files; + private Dictionary Files + { + get + { + if (_files == null) + _files = new Dictionary(); + 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 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); + } + } +} diff --git a/StorageService/StorageServiceFactory.cs b/StorageService/StorageServiceFactory.cs index efec36b..df042e7 100644 --- a/StorageService/StorageServiceFactory.cs +++ b/StorageService/StorageServiceFactory.cs @@ -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: diff --git a/Utilities/Cryptography/AesCounterManaged.cs b/Utilities/Cryptography/AesCounterManaged.cs index 91e73e0..435349e 100644 --- a/Utilities/Cryptography/AesCounterManaged.cs +++ b/Utilities/Cryptography/AesCounterManaged.cs @@ -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)) diff --git a/Utilities/FileHelper.cs b/Utilities/FileHelper.cs index 68fd797..c2f7e00 100644 --- a/Utilities/FileHelper.cs +++ b/Utilities/FileHelper.cs @@ -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;