public async Task <ActionResult <FileEntryModel> > Upload([FromForm] UploadFileModel model)
        {
            var fileEntry = new FileEntry
            {
                Name         = model.Name,
                Description  = model.Description,
                Size         = model.FormFile.Length,
                UploadedTime = DateTime.Now,
                FileName     = model.FormFile.FileName,
                Encrypted    = model.Encrypted,
            };

            await _dispatcher.DispatchAsync(new AddOrUpdateEntityCommand <FileEntry>(fileEntry));

            fileEntry.FileLocation = DateTime.Now.ToString("yyyy/MM/dd/") + fileEntry.Id;

            await _dispatcher.DispatchAsync(new AddOrUpdateEntityCommand <FileEntry>(fileEntry));

            var fileEntryDTO = fileEntry.ToModel();

            if (model.Encrypted)
            {
                var key = SymmetricCrypto.GenerateKey(32);
                var iv  = SymmetricCrypto.GenerateKey(16);
                using (var inputStream = model.FormFile.OpenReadStream())
                    using (var encryptedStream = new MemoryStream(inputStream
                                                                  .UseAES(key)
                                                                  .WithCipher(CipherMode.CBC)
                                                                  .WithIV(iv)
                                                                  .WithPadding(PaddingMode.PKCS7)
                                                                  .Encrypt()))
                    {
                        await _fileManager.CreateAsync(fileEntryDTO, encryptedStream);
                    }

                // TODO: EncryptionKey should be encrypted as well
                fileEntry.EncryptionKey = key.ToBase64String();
                fileEntry.EncryptionIV  = iv.ToBase64String();
            }
            else
            {
                using (var stream = model.FormFile.OpenReadStream())
                {
                    await _fileManager.CreateAsync(fileEntryDTO, stream);
                }
            }

            await _dispatcher.DispatchAsync(new AddOrUpdateEntityCommand <FileEntry>(fileEntry));

            return(Ok(fileEntry.ToModel()));
        }
Example #2
0
        public async Task <ActionResult <FileEntry> > Upload([FromForm] UploadFile model)
        {
            var fileEntry = new FileEntry
            {
                Name         = model.Name,
                Description  = model.Description,
                Size         = model.FormFile.Length,
                UploadedTime = DateTime.Now,
                FileName     = model.FormFile.FileName,
                Encrypted    = model.Encrypted,
            };

            await _dispatcher.DispatchAsync(new AddOrUpdateEntityCommand <FileEntry>(fileEntry));

            var fileEntryDTO = fileEntry.ToFileEntryDTO();

            if (model.Encrypted)
            {
                var key = SymmetricCrypto.GenerateKey(16);
                using (var inputStream = model.FormFile.OpenReadStream())
                    using (var encryptedStream = new MemoryStream(inputStream
                                                                  .UseAES(key)
                                                                  .WithCipher(CipherMode.ECB)
                                                                  .WithPadding(PaddingMode.PKCS7)
                                                                  .Encrypt()))
                    {
                        await _fileManager.CreateAsync(fileEntryDTO, encryptedStream);
                    }

                fileEntry.EncryptionKey = key.ToBase64String();
            }
            else
            {
                using (var stream = model.FormFile.OpenReadStream())
                {
                    await _fileManager.CreateAsync(fileEntryDTO, stream);
                }
            }

            fileEntry.FileLocation = fileEntryDTO.FileLocation;

            await _dispatcher.DispatchAsync(new AddOrUpdateEntityCommand <FileEntry>(fileEntry));

            return(Ok(fileEntry));
        }