Mini Kabibi Habibi
using System;
using System.Collections.Generic;
using System.Data.Services;
using System.Data.Services.Providers;
using System.IO;
using System.Linq;
using System.Web;
namespace DXSK8.Service {
public interface IImageSource {
byte[] Image { get; }
}
public class ImageStreamProvider : IDataServiceStreamProvider {
public ImageStreamProvider(SK8DataContainer context) {
}
public void DeleteStream(object entity, DataServiceOperationContext operationContext) {
throw new NotImplementedException();
}
public Stream GetReadStream(object entity, string etag, bool? checkETagForEquality, DataServiceOperationContext operationContext) {
if(checkETagForEquality != null) {
// This stream provider implementation does not support
// ETag headers for media resources. This means that we do not track
// concurrency for a media resource, and last-in wins on updates.
throw new DataServiceException(400,
"This sample service does not support the ETag header for a media resource.");
}
var imageSource = entity as IImageSource;
if(imageSource == null) {
throw new DataServiceException(500, "Internal Server Error.");
}
var image = imageSource.Image;
// Build the full path to the stored image file, which includes the entity key.
/*string fullImageFilePath = imageFilePath + "image" + image.PhotoId;
if(!File.Exists(fullImageFilePath)) {
throw new DataServiceException(500, "The image file could not be found.");
}
// Return a stream that contains the requested file.
return new FileStream(fullImageFilePath, FileMode.Open);*/
return new MemoryStream(image);
}
public Uri GetReadStreamUri(object entity, DataServiceOperationContext operationContext) {
return null;
}
public string GetStreamContentType(object entity, DataServiceOperationContext operationContext) {
return "image/png";
}
public string GetStreamETag(object entity, DataServiceOperationContext operationContext) {
return null;
}
public Stream GetWriteStream(object entity, string etag, bool? checkETagForEquality, DataServiceOperationContext operationContext) {
throw new NotImplementedException();
}
public string ResolveType(string entitySetName, DataServiceOperationContext operationContext) {
// We should only be handling PhotoInfo types.
return "api." + entitySetName;
//if(entitySetName == "Product") {
// return "api.Product";
//}
//else {
// // This will raise an DataServiceException.
// return null;
//}
}
public int StreamBufferSize {
get { return 64000; }
}
}
}