/// <summary> /// Creates a data element by reading the first multipart element or body of the request. /// </summary> private async Task <(Stream, DataElement)> ReadRequestAndCreateDataElementAsync(HttpRequest request, string elementType, List <Guid> refs, Instance instance) { DateTime creationTime = DateTime.UtcNow; Stream theStream; string contentType; string contentFileName = null; long fileSize = 0; if (MultipartRequestHelper.IsMultipartContentType(request.ContentType)) { // Only read the first section of the mulitpart message. MediaTypeHeaderValue mediaType = MediaTypeHeaderValue.Parse(request.ContentType); string boundary = MultipartRequestHelper.GetBoundary(mediaType, _defaultFormOptions.MultipartBoundaryLengthLimit); MultipartReader reader = new MultipartReader(boundary, request.Body); MultipartSection section = await reader.ReadNextSectionAsync(); theStream = section.Body; contentType = section.ContentType; bool hasContentDisposition = ContentDispositionHeaderValue.TryParse(section.ContentDisposition, out ContentDispositionHeaderValue contentDisposition); if (hasContentDisposition) { contentFileName = contentDisposition.FileName.ToString(); fileSize = contentDisposition.Size ?? 0; } } else { theStream = request.Body; if (request.Headers.TryGetValue("Content-Disposition", out StringValues headerValues)) { string contentDisposition = headerValues.ToString(); List <string> contentDispositionValues = contentDisposition.Split(';').ToList(); string fileNameValue = contentDispositionValues.FirstOrDefault(x => x.Contains("filename", StringComparison.CurrentCultureIgnoreCase)); if (!string.IsNullOrEmpty(fileNameValue)) { string[] valueParts = fileNameValue.Split('='); if (valueParts.Length == 2) { contentFileName = valueParts[1]; } } } contentType = request.ContentType; } DataElement newData = DataElementHelper.CreateDataElement(elementType, refs, instance, creationTime, contentType, contentFileName, fileSize, null); return(theStream, newData); }
/// <summary> /// Creates a data element by reading the first multipart element or body of the request. /// </summary> private async Task <(Stream Stream, DataElement DataElement)> ReadRequestAndCreateDataElementAsync(HttpRequest request, string elementType, List <Guid> refs, Instance instance) { DateTime creationTime = DateTime.UtcNow; Stream theStream; string contentType; string contentFileName = null; long fileSize = 0; if (MultipartRequestHelper.IsMultipartContentType(request.ContentType)) { // Only read the first section of the Multipart message. MediaTypeHeaderValue mediaType = MediaTypeHeaderValue.Parse(request.ContentType); string boundary = MultipartRequestHelper.GetBoundary(mediaType, _defaultFormOptions.MultipartBoundaryLengthLimit); MultipartReader reader = new MultipartReader(boundary, request.Body); MultipartSection section = await reader.ReadNextSectionAsync(); theStream = section.Body; contentType = section.ContentType; bool hasContentDisposition = ContentDispositionHeaderValue.TryParse(section.ContentDisposition, out ContentDispositionHeaderValue contentDisposition); if (hasContentDisposition) { contentFileName = HttpUtility.UrlDecode(contentDisposition.GetFilename()); fileSize = contentDisposition.Size ?? 0; } } else { theStream = request.Body; if (request.Headers.TryGetValue("Content-Disposition", out StringValues headerValues)) { bool hasContentDisposition = ContentDispositionHeaderValue.TryParse(headerValues.ToString(), out ContentDispositionHeaderValue contentDisposition); if (hasContentDisposition) { contentFileName = HttpUtility.UrlDecode(contentDisposition.GetFilename()); fileSize = contentDisposition.Size ?? 0; } } contentType = request.ContentType; } string user = User.GetUserOrOrgId(); DataElement newData = DataElementHelper.CreateDataElement(elementType, refs, instance, creationTime, contentType, contentFileName, fileSize, user); return(theStream, newData); }
/// <summary> /// Creates a data element by reading the first multipart element or body of the request. /// </summary> private DataElement GetDataElementFromRequest(HttpRequest request, string elementType, Instance instance, out Stream theStream) { DateTime creationTime = DateTime.UtcNow; theStream = null; string contentType = null; string contentFileName = null; long fileSize = 0; if (MultipartRequestHelper.IsMultipartContentType(request.ContentType)) { // Only read the first section of the mulitpart message. MediaTypeHeaderValue mediaType = MediaTypeHeaderValue.Parse(request.ContentType); string boundary = MultipartRequestHelper.GetBoundary(mediaType, _defaultFormOptions.MultipartBoundaryLengthLimit); MultipartReader reader = new MultipartReader(boundary, request.Body); MultipartSection section = reader.ReadNextSectionAsync().Result; theStream = section.Body; contentType = section.ContentType; bool hasContentDisposition = ContentDispositionHeaderValue.TryParse(section.ContentDisposition, out ContentDispositionHeaderValue contentDisposition); if (hasContentDisposition) { contentFileName = contentDisposition.FileName.ToString(); fileSize = contentDisposition.Size ?? 0; } } else { theStream = request.Body; contentType = request.ContentType; } string user = null; DataElement newData = DataElementHelper.CreateDataElement(elementType, instance, creationTime, contentType, contentFileName, fileSize, user); return(newData); }
private async Task <Instance> SaveDataElementsAndUpdateInstance(List <Part> parts, Instance storedInstance, DateTime creationTime, string userId) { try { foreach (Part part in parts) { // Create a new DataElement to be stored in blob and added in the Data List of the Instance object. DataElement newDataElement = DataElementHelper.CreateDataElement(part.Name, storedInstance, creationTime, part.ContentType, part.FileName, part.Stream.Length, userId); // Store file as blob. newDataElement.FileSize = _dataRepository.WriteDataToStorage(part.Stream, newDataElement.StorageUrl).Result; if (newDataElement.FileSize > 0) { storedInstance.Data.Add(newDataElement); logger.LogInformation($"Data element '{newDataElement.ElementType} - {newDataElement.Id}' is stored at {newDataElement.StorageUrl}, file size {newDataElement.FileSize / 1024}KB"); } } // Update instance with the data element. storedInstance = _instanceRepository.Update(storedInstance).Result; } catch (Exception dataElementException) { // compensating action - delete blobs logger.LogError($"Creation of data elements failed. {dataElementException}"); foreach (DataElement dataElement in storedInstance.Data) { await _dataRepository.DeleteDataInStorage(dataElement.StorageUrl); logger.LogError($"Deleted data element '{dataElement.ElementType} - {dataElement.Id}' stored at {dataElement.StorageUrl}"); } throw; } return(storedInstance); }