/// <summary> /// Returns the content type of the stream that is associated with the specified entity. /// </summary> /// <param name="entity">The entity that has the associated binary data stream.</param> /// <param name="operationContext">The <see cref="T:System.Data.Services.DataServiceOperationContext"/> instance used by the data service to process the request.</param> /// <returns>A valid Content-Type of the binary data.</returns> public string GetStreamContentType(object entity, DataServiceOperationContext operationContext) { // Check if the entity is a File resource if (entity != null && entity is FlattenedFile) { FlattenedFile fileRes = entity as FlattenedFile; // Check if the File resource holds a MIME content type. if (!string.IsNullOrWhiteSpace(fileRes.MimeType)) { return(fileRes.MimeType); } } return(ZentityFileStreamProvider.DefaultContentType); }
/// <summary> /// Returns the default stream that is associated with an entity that has a binary property. /// </summary> /// <param name="entity">The entity that has the associated binary stream.</param> /// <param name="etag">The eTag value sent as part of the HTTP request that is sent to the data service.</param> /// <param name="checkETagForEquality">A nullable <see cref="T:System.Boolean"/> value that determines the type of eTag that is used.</param> /// <param name="operationContext">The <see cref="T:System.Data.Services.DataServiceOperationContext"/> instance used by the data service to process the request.</param> /// <returns> /// The data <see cref="T:System.IO.Stream"/> that contains the binary property data of the <paramref name="entity"/>. /// </returns> public Stream GetReadStream(object entity, string etag, bool?checkETagForEquality, DataServiceOperationContext operationContext) { if (checkETagForEquality != null) { // This implementation does not support eTag associated with BLOBs throw new DataServiceException((int)HttpStatusCode.NotImplemented, CoreResources.FileETagsNotSupported); } // Set the file resource instance. fileResource = entity as FlattenedFile; if (fileResource == null) { throw new DataServiceException((int)HttpStatusCode.NotFound, CoreResources.FileNotFound); } try { // Convert the FlattenedFile resource into a File resource File zenFileResource = this.zentityContext.Files.Where <File>(zenFile => zenFile.Id == fileResource.Id).FirstOrDefault(); if (zenFileResource == null) { throw new DataServiceException((int)HttpStatusCode.NotFound, CoreResources.FileNotFound); } // Create a memory stream for File resource content download using (MemoryStream memStream = new MemoryStream()) { // Download the File content from SQL Server this.zentityContext.DownloadFileContent(zenFileResource, memStream); // Reset the position to the start for reading operations. memStream.Seek(0, SeekOrigin.Begin); return(memStream); } } catch (Exception ex) { throw new DataServiceException((int)HttpStatusCode.InternalServerError, string.Format(CoreResources.FileContentNotStreamed, fileResource.Id, ex.Message)); } }
/// <summary> /// Returns the stream that the data service uses to write the contents of a binary property that is associated with an entity. /// </summary> /// <param name="entity">The entity that has the associated binary stream.</param> /// <param name="etag">The eTag value that is sent as part of the HTTP request that is sent to the data service.</param> /// <param name="checkETagForEquality">A nullable <see cref="T:System.Boolean"/> value that determines the type of eTag is used.</param> /// <param name="operationContext">The <see cref="T:System.Data.Services.DataServiceOperationContext"/> instance that is used by the data service to process the request.</param> /// <returns> /// A valid <see cref="T:System.Stream"/> the data service uses to write the contents of a binary property that is associated with the <paramref name="entity"/>. /// </returns> public Stream GetWriteStream(object entity, string etag, bool?checkETagForEquality, DataServiceOperationContext operationContext) { if (checkETagForEquality != null) { // This implementation does not support eTag associated with BLOBs throw new DataServiceException((int)HttpStatusCode.NotImplemented, CoreResources.FileETagsNotSupported); } // Set the file resource instance. fileResource = entity as FlattenedFile; if (fileResource == null) { throw new DataServiceException((int)HttpStatusCode.NotFound, CoreResources.FileNotFound); } string slugHeader = operationContext.RequestHeaders.Get(CoreResources.HttpHeaderSlug); if (string.IsNullOrWhiteSpace(slugHeader)) { throw new DataServiceException((int)HttpStatusCode.BadRequest, CoreResources.FileIdNotSent); } Guid fileResourceId = Guid.Empty; if (Guid.TryParse(slugHeader, out fileResourceId)) { if (fileResourceId == Guid.Empty) { throw new DataServiceException((int)HttpStatusCode.BadRequest, CoreResources.FileIdIsGuidEmpty); } fileResource.Id = fileResourceId; } else { throw new DataServiceException((int)HttpStatusCode.BadRequest, CoreResources.FileIdIsInvalid); } // Return the filestream that the data service uses to // write the requested binary data stream to a temp file. return(new FileStream(tempFileName, FileMode.Create, FileAccess.ReadWrite)); }