Exemple #1
0
        public async Task <IHttpActionResult> Post()
        {
            var errors = new List <FFErrorCode>();

            // Parse Request Content
            var provider = new MultipartFormDataStreamProvider(Path.GetTempPath());
            var parts    = await Request.Content.ReadAsMultipartAsync(provider).ConfigureAwait(false);

            // Validate the request body
            if (string.IsNullOrEmpty(parts.FormData["uploadTransactionType"]))
            {
                errors.Add(GeneralErrorCodes.FormDataFieldMissing("uploadTransactionType"));
            }

            var tenantIdString = parts.FormData["tenantId"];

            if (string.IsNullOrEmpty(tenantIdString))
            {
                errors.Add(GeneralErrorCodes.FormDataFieldMissing("tenantId"));
            }

            var tenantIdGuid = Guid.Empty;

            if (!Guid.TryParse(tenantIdString, out tenantIdGuid) || tenantIdGuid == Guid.Empty)
            {
                errors.Add(GeneralErrorCodes.FormDataFieldInvalid("tenantId"));
            }

            // Get files
            var files           = parts.FileData.Select(x => x.LocalFileName);
            var enumeratedFiles = files as IList <string> ?? files.ToList();

            if (!enumeratedFiles.Any())
            {
                errors.Add(GeneralErrorCodes.FormDataFilesMissing());
            }

            if (errors.Any())
            {
                return(Request.CreateApiResponse(NoDtoHelpers.CreateCommandResult(errors)));
            }

            var fileToUpload     = enumeratedFiles.First();
            var originalFileName = parts.FileData[0].Headers.ContentDisposition.FileName.Replace("\"", string.Empty);

            var fileUploadMetadata = new FileUploadMetadataDto
            {
                SavedFileName    = fileToUpload,
                OriginalFileName = originalFileName,
                TransactionType  = parts.FormData["uploadTransactionType"].Replace("\"", string.Empty)
            };

            var authHeader = Request.Headers.Authorization.ToString();
            var result     = await _facade.Upload(fileUploadMetadata, authHeader, tenantIdGuid);

            foreach (var file in enumeratedFiles)
            {
                File.Delete(file);
            }

            return(Request.CreateApiResponse(result));
        }