async Task<MergeResult> MergeImpl(IOwinContext context) { var content = new StreamContent(context.Request.Body); content.Headers.ContentType = MediaTypeHeaderValue.Parse(context.Request.ContentType); var result = new MergeResult(); result.Graph = new Graph(); var provider = await content.ReadAsMultipartAsync(); foreach (var httpContent in provider.Contents) { var fileName = httpContent.Headers.ContentDisposition.FileName; if (string.IsNullOrWhiteSpace(fileName)) { continue; } using (Stream fileContent = await httpContent.ReadAsStreamAsync()) { using (TextReader reader = new StreamReader(fileContent)) { string data = await reader.ReadToEndAsync(); JToken jsonLd = JToken.Parse(data); if (result.JsonLdContext == null) { result.JsonLdContext = (JObject)jsonLd["@context"]; result.JsonLdFrame = (string)jsonLd["@type"]; } IGraph graph = Common.GraphFromJson(jsonLd); result.Graph.Merge(graph, false); } } } return result; }
public void ReadAsMultipartAsync_ThrowsOnPrematureEndOfStream() { HttpContent content = new StreamContent(Stream.Null); string mediaType = String.Format("multipart/form-data; boundary=\"{0}\"", ValidBoundary); content.Headers.ContentType = MediaTypeHeaderValue.Parse(mediaType); Assert.Throws <IOException>(() => content.ReadAsMultipartAsync().Result); }
public void ReadAsMultipartAsync_ThrowsOnReadError() { HttpContent content = new StreamContent(new ReadErrorStream()); string mediaType = String.Format("multipart/form-data; boundary=\"{0}\"", ValidBoundary); content.Headers.ContentType = MediaTypeHeaderValue.Parse(mediaType); IOException exception = Assert.Throws <IOException>(() => content.ReadAsMultipartAsync().Result); Assert.NotNull(exception.InnerException); Assert.Equal(ExceptionAsyncStreamMessage, exception.InnerException.Message); }
public void ReadAsMultipartAsync_PrematureEndOfStream_Throws() { HttpContent content = new StreamContent(Stream.Null); var contentType = new MediaTypeHeaderValue("multipart/form-data"); contentType.Parameters.Add(new NameValueHeaderValue("boundary", "\"{--\"")); content.Headers.ContentType = contentType; Assert.Throws <IOException>( () => content.ReadAsMultipartAsync().Result, "Unexpected end of MIME multipart stream. MIME multipart message is not complete." ); }
public void ReadAsMultipartAsync_ReadErrorOnStream_Throws() { HttpContent content = new StreamContent(new ReadErrorStream()); var contentType = new MediaTypeHeaderValue("multipart/form-data"); contentType.Parameters.Add(new NameValueHeaderValue("boundary", "\"--\"")); content.Headers.ContentType = contentType; var ioException = Assert.Throws <IOException>( () => content.ReadAsMultipartAsync().Result, "Error reading MIME multipart body part." ); Assert.NotNull(ioException.InnerException); Assert.Equal(ExceptionAsyncStreamMessage, ioException.InnerException.Message); }
public void ReadAsMultipartAsync_ThrowsOnReadError() { HttpContent content = new StreamContent(new ReadErrorStream()); string mediaType = String.Format("multipart/form-data; boundary=\"{0}\"", ValidBoundary); content.Headers.ContentType = MediaTypeHeaderValue.Parse(mediaType); IOException exception = Assert.Throws<IOException>(() => content.ReadAsMultipartAsync().Result); Assert.NotNull(exception.InnerException); Assert.Equal(ExceptionAsyncStreamMessage, exception.InnerException.Message); }
public void ReadAsMultipartAsync_PrematureEndOfStream_Throws() { HttpContent content = new StreamContent(Stream.Null); var contentType = new MediaTypeHeaderValue("multipart/form-data"); contentType.Parameters.Add(new NameValueHeaderValue("boundary", "\"{--\"")); content.Headers.ContentType = contentType; Assert.Throws<IOException>( () => content.ReadAsMultipartAsync().Result, "Unexpected end of MIME multipart stream. MIME multipart message is not complete." ); }
public void ReadAsMultipartAsync_ThrowsOnPrematureEndOfStream() { HttpContent content = new StreamContent(Stream.Null); string mediaType = String.Format("multipart/form-data; boundary=\"{0}\"", ValidBoundary); content.Headers.ContentType = MediaTypeHeaderValue.Parse(mediaType); Assert.Throws<IOException>(() => content.ReadAsMultipartAsync().Result); }
public void ReadAsMultipartAsync_ReadErrorOnStream_Throws() { HttpContent content = new StreamContent(new ReadErrorStream()); var contentType = new MediaTypeHeaderValue("multipart/form-data"); contentType.Parameters.Add(new NameValueHeaderValue("boundary", "\"--\"")); content.Headers.ContentType = contentType; var ioException = Assert.Throws<IOException>( () => content.ReadAsMultipartAsync().Result, "Error reading MIME multipart body part." ); Assert.NotNull(ioException.InnerException); Assert.Equal(ExceptionAsyncStreamMessage, ioException.InnerException.Message); }
public HttpResponseMessage Post() { Stream reqStream = Request.Content.ReadAsStreamAsync().Result; MemoryStream tempStream = new MemoryStream(); reqStream.CopyTo(tempStream); tempStream.Seek(0, SeekOrigin.End); StreamWriter writer = new StreamWriter(tempStream); writer.WriteLine(); writer.Flush(); tempStream.Position = 0; StreamContent streamContent = new StreamContent(tempStream); foreach (var header in Request.Content.Headers) { streamContent.Headers.Add(header.Key, header.Value); } var result = new HttpResponseMessage(HttpStatusCode.OK); if (Request.Content.IsMimeMultipartContent()) { var task = streamContent.ReadAsMultipartAsync<MultipartMemoryStreamProvider>(new MultipartMemoryStreamProvider()); task.Wait(); MultipartMemoryStreamProvider provider = task.Result; using (var _repo = new WebsiteFileRepository(UnitOfWork)) { foreach (HttpContent content in provider.Contents) { WebsiteFile newFile = new WebsiteFile(); newFile.Id = Guid.NewGuid(); Stream stream = content.ReadAsStreamAsync().Result; string filePath = HostingEnvironment.MapPath("~/Images/"); string fileName = content.Headers.ContentDisposition.FileName.Replace("\"", ""); string fullPath = Path.Combine(filePath, fileName); using (var fileStream = File.Create(fullPath)) { stream.Seek(0, SeekOrigin.Begin); stream.CopyTo(fileStream); } //Add To DB newFile.Filename = fileName; _repo.InsertOrUpdate(newFile); } UnitOfWork.Save(); } return result; } else { throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotAcceptable, "This request is not properly formatted")); } }
public static async Task<MultipartStreamProvider> awaitReadAsync(StreamContent stream) { return await stream.ReadAsMultipartAsync(); }