Rebel.Cms.Web.Mvc.Controllers.MediaProxyController.Proxy C# (CSharp) Method

Proxy() public method

Displays the image, ensuring the correct permissions are set.
public Proxy ( string propertyAlias, string mediaId, int size, string fileName ) : System.Web.Mvc.ActionResult
propertyAlias string
mediaId string
size int
fileName string
return System.Web.Mvc.ActionResult
        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();
        }
    }

Usage Example

        public void MediaProxyController_Proxy_Returns_404_For_Empty_Id()
        {
            //Arrange
            var controller = new MediaProxyController(_backOfficeRequestContext);
            controller.InjectDependencies(new Dictionary<string, string>(), new Dictionary<string, string>(), _backOfficeRequestContext, false);

            var result = controller.Proxy("rebelFile", "", 0, "test.jpg");

            //Assert
            Assert.IsTrue(result is HttpNotFoundResult);
        }
All Usage Examples Of Rebel.Cms.Web.Mvc.Controllers.MediaProxyController::Proxy
MediaProxyController