コード例 #1
0
        public async Task <IActionResult> UploadFile(
            [FromForm] UploadFileRequest fileData,
            [FromRoute] string directoryId,
            [FromQuery(Name = "revisionNr")] uint?revisionNumber)
        {
            #region - Input data validation

            if (!IdGenerator.TypeFromId(directoryId, out var typOfId) || typOfId != IdType.Directory)
            {
                return(BadRequest());
            }

            if (fileData.File == null ||
                fileData.File.Length <= 0 ||
                fileData.File.Length > int.MaxValue ||
                fileData.File.Length > config.Value.ValidationRules.MaxFileSizeInBytes ||
                string.IsNullOrWhiteSpace(fileData.Name) ||
                string.IsNullOrWhiteSpace(fileData.EncryptedKey) ||
                string.IsNullOrWhiteSpace(fileData.PlaintextKey))
            {
                return(BadRequest());
            }

            var fileSize = (int)fileData.File.Length;

            var plaintextKeyBytes = Convert.FromBase64String(fileData.PlaintextKey);
            // Make sure the iv is 16 Bytes long and the key has exactly 32 Byte.
            if (plaintextKeyBytes.Length != 16 + 32)
            {
                return(BadRequest());
            }

            #endregion

            var fileId = IdGenerator.NewId(IdType.File);

            try
            {
                // Upload file to bucket
                var successful = await bucket.UploadFileEncrypted(
                    config.Value.ExternalServices.MinioBucketName,
                    fileId,
                    fileSize,
                    plaintextKeyBytes,
                    fileData.File.OpenReadStream());

                if (!successful)
                {
                    return(BadRequest());
                }

                // Add item to database
                var quickNumber = await guardApi.AddFile(
                    directoryId,
                    fileId,
                    fileSize,
                    fileData.EncryptedKey,
                    DocumentLanguage.German,
                    ExtensionType.Docx);


                // Add item to queue
                var queueRequest = new QueueFileMetadataDto
                {
                    FileId           = fileId,
                    DirectoryId      = directoryId,
                    DocumentLanguage = fileData.DocumentLanguage,
                    EncryptedKey     = fileData.EncryptedKey,
                    FileName         = fileData.Name,
                    PlaintextKey     = fileData.PlaintextKey
                };
                await queueSender.Send(queueRequest);


                // Build response
                // TODO Remove casts and add uint return values in sub-apis
                var response = new UploadFileResponse(
                    queueRequest.FileName,
                    fileId,
                    (uint)fileSize,
                    DateTime.UtcNow,
                    DateTime.MinValue,
                    (uint)quickNumber);

                return(Ok(response));
            }
            catch (Exception e)
            {
                // TODO Remove file if something failed
                Console.Write(e);
                return(BadRequest());
            }
        }
コード例 #2
0
 public async Task <bool> UploadFileEncrypted(string bucketName, string fileId, int fileSize, byte[] plaintextKey, Stream fileStream)
 {
     return(await writableBucket.UploadFileEncrypted(bucketName, fileId, fileSize, plaintextKey, fileStream));
 }