public async Task <UploadAudioCommandResult> Handle(UploadAudioCommand cmd, CancellationToken cancellationToken) { var request = cmd.Request; if (!MultipartRequestHelper.IsMultipartContentType(request.ContentType)) { throw new ArgumentException("The media file could not be processed."); } string mediaId = string.Empty; UploadAudioCommandResult cmdResult = new UploadAudioCommandResult(); try { var processResult = await ProcessFile(request); var form = processResult.Form; var file = processResult.File; var results = form.GetResults(); string instructorId = results["instructorId"]; string title = results["title"]; string firstName = results["firstName"]; string lastName = results["lastName"]; string duration = results["durationInMinutes"]; mediaId = await AddInstructorAudioMedia(instructorId, firstName, lastName, title, Convert.ToInt32(duration), DateTime.UtcNow, DateTime.UtcNow, file); cmdResult.MediaId = mediaId; cmdResult.ProfileId = instructorId; if (string.IsNullOrEmpty(mediaId)) { throw new Exception($"Could not add instructor media"); } } catch (Exception ex) { throw ex; } return(cmdResult); }
private async Task <(byte[] File, KeyValueAccumulator Form)> ProcessFile(HttpRequest request) { KeyValueAccumulator formAccumulator = new KeyValueAccumulator(); var streamedFileContent = new byte[0]; var boundary = MultipartRequestHelper.GetBoundary( MediaTypeHeaderValue.Parse(request.ContentType), _defaultFormOptions.MultipartBoundaryLengthLimit ); var reader = new MultipartReader(boundary, request.Body); var section = await reader.ReadNextSectionAsync(); while (section != null) { var hasContentDispositionHeader = ContentDispositionHeaderValue.TryParse( section.ContentDisposition, out var contentDisposition); if (hasContentDispositionHeader) { if (MultipartRequestHelper .HasFileContentDisposition(contentDisposition)) { streamedFileContent = await FileHelper.ProcessStreamedFile(section, contentDisposition, _permittedExtensions, _fileSizeLimit); } else if (MultipartRequestHelper .HasFormDataContentDisposition(contentDisposition)) { var key = HeaderUtilities.RemoveQuotes(contentDisposition.Name).Value; var encoding = FileHelper.GetEncoding(section); if (encoding == null) { throw new AddMediaException($"The request could not be processed: Bad Encoding"); } using (var streamReader = new StreamReader( section.Body, encoding, detectEncodingFromByteOrderMarks: true, bufferSize: 1024, leaveOpen: true)) { // The value length limit is enforced by // MultipartBodyLengthLimit var value = await streamReader.ReadToEndAsync(); if (string.Equals(value, "undefined", StringComparison.OrdinalIgnoreCase)) { value = string.Empty; } formAccumulator.Append(key, value); if (formAccumulator.ValueCount > _defaultFormOptions.ValueCountLimit) { throw new AddMediaException($"The request could not be processed: Key Count limit exceeded."); } } } } // Drain any remaining section body that hasn't been consumed and // read the headers for the next section. section = await reader.ReadNextSectionAsync(); } return(streamedFileContent, formAccumulator); }