view Agendas/trunk/src/Agendas.Web.Tests/Controllers/HttpPostedFileBaseModelBinder.cs @ 298:9bc60d166c8a

Se corrigieron los tests por el cambio de Patrocinador, para que no persista el logo en disco. Se comentó el código de PatrocinadorApiController, que no se utiliza.
author juanjose.montesdeocaarbos
date Sun, 19 Feb 2012 16:00:38 -0300
parents bf993f99cee3
children
line wrap: on
line source

using System;
using System.IO;
using System.Web;
using System.Web.Mvc;

namespace Agendas.Web.Tests.Controllers
{
	public class HttpPostedFileBaseModelBinder : IModelBinder
	{
		public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
		{
			var fileDummy = new HttpFileDummy(((FormCollection)bindingContext.ValueProvider)[bindingContext.ModelName], "image/jpg");
			if (string.IsNullOrEmpty(fileDummy.FileName))
			{
				fileDummy = null;
			}
			return fileDummy;
		}
	}

	public class HttpFileDummy : HttpPostedFileBase, IDisposable
	{
		public override int ContentLength
		{
			get { return (int)InputStream.Length; }
		}

		private readonly string _contentType;
		public override string ContentType
		{
			get { return _contentType; }
		}

		private readonly string _fileName;
		public override string FileName
		{
			get { return _fileName; }
		}

		private FileStream _stream;
		public override Stream InputStream
		{
			get
			{
				return _stream ?? (_stream = new FileStream(FileName, FileMode.Open,
				                                            FileAccess.Read, FileShare.Read));
			}
		}

		public HttpFileDummy(string fileName, string contentType)
		{
			_contentType = contentType;
			_fileName = fileName;
		}

		public void Dispose()
		{
			if (_stream != null)
			{
				try { _stream.Dispose(); }
				finally { _stream = null; }
			}
		}

		public override void SaveAs(string filename)
		{
			File.WriteAllBytes(filename, File.ReadAllBytes(FileName));
		}
	}
}