123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232 |
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Data.Objects;
- using System.Data.Services;
- using System.Data.Services.Providers;
- using System.IO;
- using System.Linq;
- using System.Security.Cryptography;
- using System.Text;
- using System.Web;
- using System.Web.Hosting;
- using DevExpress.XtraPrinting.Native;
- using iBemsDataService.Model;
- using iBemsDataService.Util;
- namespace iBemsDataService
- {
- public interface IImageSource
- {
- byte[] Image { get; }
- }
-
- public class ImageStreamProvider : IDataServiceStreamProvider, IDisposable
- {
- private string _tempFile;
- private string _filePath;
- private CmFile _cachedEntity;
- private iBemsEntities _context;
- private static Dictionary<string , string> _fileCategories = new Dictionary<string , string>();
- public ImageStreamProvider( iBemsEntities context )
- {
- _context = context;
-
- _filePath = HostingEnvironment.MapPath( "~/App_Data/" );
-
- _tempFile = Path.GetTempFileName();
- }
- public void DeleteStream( object entity , DataServiceOperationContext operationContext )
- {
- var file = entity as CmFile;
- if( file == null )
- {
- throw new DataServiceException( 500 , "Internal Server Error." );
- }
- try
- {
-
- File.Delete( FileCategoryManager.GetInstance().GetFilePath( file ) );
- }
- catch( IOException ex )
- {
- throw new DataServiceException( "The file could not be found." , ex );
- }
- }
- public Stream GetReadStream( object entity , string etag , bool? checkETagForEquality , DataServiceOperationContext operationContext )
- {
- if( checkETagForEquality != null )
- {
- throw new DataServiceException( 400 , "This service does not support the ETag header for a media resource." );
- }
-
- var file = entity as CmFile;
- if( file == null )
- {
- throw new DataServiceException( 500 , "Internal Server Error." );
- }
-
- string filePath = FileCategoryManager.GetInstance().GetFilePath( file );
- if( !File.Exists( filePath ) )
- {
- throw new DataServiceException( 500 , "The file could not be found." );
- }
- if ( IsImageType( file ) == false )
- {
-
-
-
-
- string strDispos = "attachment; filename= " + Uri.EscapeDataString(file.Name);
-
- operationContext.ResponseHeaders["Content-Disposition"] = strDispos;
- }
-
- return new FileStream( filePath, FileMode.Open );
- }
- protected bool IsImageType( CmFile file )
- {
- var types = file.ContentType.Split( '/' );
- return ( types.Length > 0 && types[0].ToLower() == "image" );
- }
- public Uri GetReadStreamUri( object entity , DataServiceOperationContext operationContext )
- {
- return null;
- }
- public string GetStreamContentType( object entity , DataServiceOperationContext operationContext )
- {
- var file = entity as CmFile;
- if( file == null )
- {
- throw new DataServiceException( 500 , "Internal Server Error." );
- }
- return file.ContentType;
- }
- public string GetStreamETag( object entity , DataServiceOperationContext operationContext )
- {
- return null;
- }
- public Stream GetWriteStream( object entity , string etag , bool? checkETagForEquality , DataServiceOperationContext operationContext )
- {
- if( checkETagForEquality != null )
- {
- throw new DataServiceException( 400 , "This service does not support ETags associated with BLOBs" );
- }
- var file = entity as CmFile;
- if( file == null )
- {
- throw new DataServiceException( 500 , "Internal Server Error: " + "the Media Link Entry could not be determined." );
- }
-
- if( operationContext.RequestMethod == "POST" )
- {
-
-
-
- file.Name = operationContext.RequestHeaders["Slug"] ?? "newFile";
-
- file.CreatedDate = DateTime.Today;
-
- file.ContentType = operationContext.RequestHeaders["Content-Type"];
-
-
- _cachedEntity = file;
- return new FileStream( _tempFile , FileMode.Open );
- }
-
- else
- {
-
- return new FileStream( FileCategoryManager.GetInstance().GetFilePath( file ) , FileMode.Open , FileAccess.Write );
- }
- }
- public string GetFileCategoryPath( string fileCategoryName )
- {
- var sb = new StringBuilder();
- sb.Append( _filePath );
- sb.Append( "files/" );
- sb.Append( fileCategoryName );
- return sb.ToString();
- }
- public string ResolveType( string entitySetName , DataServiceOperationContext operationContext )
- {
- if ( entitySetName == "CmFile" )
- {
- return "BemsDataService." + entitySetName;
- }
- return null;
- }
- public int StreamBufferSize
- {
- get { return 64000; }
- }
- public void Dispose()
- {
-
- if( _cachedEntity != null )
- {
-
- var state = _context.Entry( _cachedEntity ).State;
- if( state == EntityState.Unchanged )
- {
-
-
- File.Move( _tempFile , FileCategoryManager.GetInstance().GetFilePath( _cachedEntity ) );
-
- File.Delete( _tempFile );
- }
- else
- {
-
-
- _context.CmFile.Remove( _cachedEntity );
- _context.SaveChanges();
- File.Delete( _tempFile );
- throw new DataServiceException( "An error occurred. The file could not be saved." );
- }
- }
- }
- }
- }
|