public virtual ActionResult Proxy(string propertyAlias, string mediaId, int size, string fileName)
{
if (mediaId.IsNullOrWhiteSpace())
return HttpNotFound();
var app = RoutableRequestContext.Application;
using (var uow = app.Hive.OpenReader<IContentStore>())
{
// Lookup a TypedEntity with an Upload field that has a MediaId of the mediaId property
var entity =
uow.Repositories.SingleOrDefault(
x => x.InnerAttribute<string>(propertyAlias, "MediaId") == mediaId);
if (entity == null)
return HttpNotFound();
var member = app.Security.Members.GetCurrent();
if (member == null || !app.Security.PublicAccess.GetPublicAccessStatus(member.Id, entity.Id).CanAccess)
{
// Member can't access, but check to see if logged in identiy is a User, if so, just allow access
var ticket = HttpContext.GetRebelAuthTicket();
if (ticket == null || ticket.Expired)
{
return HttpNotFound();
}
}
// Find the upload property
var property = entity.Attributes.SingleOrDefault(x => x.AttributeDefinition.AttributeType.RenderTypeProvider.Equals(CorePluginConstants.FileUploadPropertyEditorId, StringComparison.InvariantCultureIgnoreCase) && x.Values["MediaId"].ToString() == mediaId);
if (property == null)
return HttpNotFound();
// Get the file
var fileId = new HiveId(property.DynamicValue);
using (var uow2 = app.Hive.OpenReader<IFileStore>(fileId.ToUri()))
{
var file = uow2.Repositories.Get<File>(fileId);
if (size > 0)
{
// Look for thubnail file
var relation = uow2.Repositories.GetLazyChildRelations(fileId, FixedRelationTypes.ThumbnailRelationType)
.SingleOrDefault(x => x.MetaData.Single(y => y.Key == "size").Value == size.ToString());
if (relation != null && relation.Destination != null)
{
var thumbnail = (File)relation.Destination;
return File(thumbnail.ContentBytes, thumbnail.GetMimeType());
}
return HttpNotFound();
}
if (file != null)
return File(file.ContentBytes, file.GetMimeType());
}
}
return HttpNotFound();
}
}