Beispiel #1
0
        /// <summary>
        /// Validates page, master page and file upload details before images can be prepared for a page.
        /// </summary>
        /// <param name="tenantId">Website that page belongs to.</param>
        /// <param name="masterPageId">Master page containing image upload rules.</param>
        /// <param name="model">Uploaded image.</param>
        /// <param name="keyPrefix">Validation key prefix.</param>
        /// <returns>Useful information retrieved during validation.</returns>
        public ValidatePrepareImagesResult ValidatePrepareImages(long tenantId, long masterPageId, CreateUploadModel model, string keyPrefix = null)
        {
            // Check that master page associated with page allows images
            MasterPage masterPage = _masterPageRepository.Read(tenantId, masterPageId);

            if (!masterPage.HasImage)
            {
                throw new ValidationErrorException(new ValidationError(PagePropertyNames.Image, PageResource.ImageNotAllowedMessage, keyPrefix));
            }

            // Check that content type identifies an image
            UploadType uploadType = _uploadService.GetUploadType(model.ContentType);

            if (uploadType != UploadType.Image)
            {
                throw new ValidationErrorException(new ValidationError(PagePropertyNames.Image, PageResource.ImageInvalidMessage, keyPrefix));
            }

            // Check that supplied upload is an image
            Size?size = _imageAnalysisService.GetImageDimensions(model.Content);

            if (size == null)
            {
                throw new ValidationErrorException(new ValidationError(PagePropertyNames.Image, PageResource.ImageInvalidMessage, keyPrefix));
            }

            // Check image dimension constraints (minimum width and height)
            if (size.Value.Width < masterPage.ImageMinWidth.Value || size.Value.Height < masterPage.ImageMinHeight.Value)
            {
                throw new ValidationErrorException(new ValidationError(PagePropertyNames.Image, string.Format(PageResource.ImageDimensionsInvalidMessage, masterPage.ImageMinWidth.Value, masterPage.ImageMinHeight.Value), keyPrefix));
            }

            // Return result
            return(new ValidatePrepareImagesResult {
                MasterPage = masterPage, Size = size.Value
            });
        }
Beispiel #2
0
        /// <summary>
        /// Creates a new page.
        /// </summary>
        /// <param name="tenantId">The website that created page will belong to.</param>
        /// <param name="parentPageId">Parent page (can be null if page has no parent - i.e. is home page, or if can be determined).</param>
        /// <param name="masterpageId">The master page that newly created page is based on.</param>
        /// <param name="pageInfo">If specified, used to override master page settings.</param>
        /// <param name="unitOfWork">Unit of work.</param>
        /// <returns>Newly allocated page identifier.</returns>
        public long Create(long tenantId, long?parentPageId, long masterPageId, PageInfo pageInfo, IUnitOfWork unitOfWork = null)
        {
            // Multiple actions are performed during page creation, so we need a unit of work to perform rollback if any failures occur
            IUnitOfWork localUnitOfWork = unitOfWork == null?_unitOfWorkFactory.CreateUnitOfWork() : null;

            try
            {
                // Get master page that will determine how new page is created
                MasterPage masterPage = _masterPageRepository.Read(tenantId, masterPageId, unitOfWork ?? localUnitOfWork);

                // Get valid parent pages
                List <Page> parentPages = null;
                if (masterPage.AncestorPageId.HasValue && masterPage.AncestorPageLevel.HasValue)
                {
                    parentPages = ListMasterPageParentPages(masterPage, unitOfWork ?? localUnitOfWork);
                }

                // Validate the page create request (including checking that all or none of the image upload properties are specified)
                _pageValidator.ValidateCreate(tenantId, parentPageId, pageInfo, parentPages);

                // Set parent page identifier?
                if (parentPageId == null && masterPage.AncestorPageId.HasValue && masterPage.AncestorPageLevel.HasValue)
                {
                    parentPageId = parentPages[0].PageId;
                }

                // Construct new page based on master page definition
                DateTime now  = DateTime.UtcNow;
                Page     page = new Page
                {
                    Created                = now,
                    Description            = pageInfo == null ? masterPage.PageDescription : pageInfo.Description,
                    MasterPageId           = masterPageId,
                    Name                   = pageInfo == null ? masterPage.PageName : pageInfo.Name,
                    Occurred               = masterPage.HasOccurred ? DateTime.UtcNow.Date : (DateTime?)null,
                    ParentPageId           = parentPageId,
                    Updated                = now,
                    TenantId               = tenantId,
                    PageZones              = new List <PageZone>(),
                    Tags                   = pageInfo == null ? new List <Tag>() : pageInfo.Tags,
                    ImageTenantId          = pageInfo == null ? null : pageInfo.ImageTenantId,
                    ThumbnailImageUploadId = pageInfo == null ? null : pageInfo.ThumbnailImageUploadId,
                    PreviewImageUploadId   = pageInfo == null ? null : pageInfo.PreviewImageUploadId,
                    ImageUploadId          = pageInfo == null ? null : pageInfo.ImageUploadId
                };

                // Construct page zones
                foreach (MasterPageZone masterPageZone in masterPage.MasterPageZones)
                {
                    if (masterPageZone.AdminType != MasterPageZoneAdminType.Static)
                    {
                        PageZone pageZone = new PageZone
                        {
                            TenantId         = tenantId,
                            MasterPageId     = masterPageId,
                            MasterPageZoneId = masterPageZone.MasterPageZoneId,
                            PageZoneElements = new List <PageZoneElement>()
                        };
                        foreach (MasterPageZoneElement masterPageZoneElement in masterPageZone.MasterPageZoneElements)
                        {
                            long            elementId       = _elementService.Copy(tenantId, masterPageZoneElement.ElementId, tenantId, masterPageZoneElement.Element.ElementTypeId, unitOfWork ?? localUnitOfWork);
                            PageZoneElement pageZoneElement = new PageZoneElement
                            {
                                TenantId      = tenantId,
                                ElementTypeId = masterPageZoneElement.Element.ElementTypeId,
                                ElementId     = elementId,
                                Parent        = pageZone
                            };
                            if (masterPageZone.AdminType == MasterPageZoneAdminType.Configurable)
                            {
                                pageZoneElement.SortOrder = masterPageZoneElement.SortOrder;
                            }
                            if (masterPageZone.AdminType == MasterPageZoneAdminType.Editable)
                            {
                                pageZoneElement.MasterPageId            = masterPageId;
                                pageZoneElement.MasterPageZoneId        = masterPageZone.MasterPageZoneId;
                                pageZoneElement.MasterPageZoneElementId = masterPageZoneElement.MasterPageZoneElementId;
                            }
                            pageZone.PageZoneElements.Add(pageZoneElement);
                        }
                        page.PageZones.Add(pageZone);
                    }
                }

                // Commit page images?
                if (page.ImageUploadId.HasValue)
                {
                    _uploadService.Commit(page.ImageTenantId.Value, page.ThumbnailImageUploadId.Value, GetPageImageStorageHierarchy(), unitOfWork ?? localUnitOfWork);
                    _uploadService.Commit(page.ImageTenantId.Value, page.PreviewImageUploadId.Value, GetPageImageStorageHierarchy(), unitOfWork ?? localUnitOfWork);
                    _uploadService.Commit(page.ImageTenantId.Value, page.ImageUploadId.Value, GetPageImageStorageHierarchy(), unitOfWork ?? localUnitOfWork);
                }

                // Create page and return newly allocated page identifier
                page.PageId = _pageRepository.Create(page, unitOfWork ?? localUnitOfWork);

                // Update page tags?
                if (masterPage.Taggable)
                {
                    _pageRepository.UpdateTags(page, unitOfWork ?? localUnitOfWork);
                }

                // Commit work if local unit of work in place, then return newly allocated page identifier
                if (localUnitOfWork != null)
                {
                    localUnitOfWork.Commit();
                }
                return(page.PageId);
            }
            catch (ValidationErrorException)
            {
                if (localUnitOfWork != null)
                {
                    localUnitOfWork.Rollback();
                }
                throw;
            }
            catch (Exception ex)
            {
                if (localUnitOfWork != null)
                {
                    localUnitOfWork.Rollback();
                }
                throw new ValidationErrorException(new ValidationError(null, ApplicationResource.UnexpectedErrorMessage), ex);
            }
            finally
            {
                if (localUnitOfWork != null)
                {
                    localUnitOfWork.Dispose();
                }
            }
        }
Beispiel #3
0
 /// <summary>
 /// Gets master page details.
 /// </summary>
 /// <param name="tenantId">Identifies website that master pages belong to.</param>
 /// <param name="masterPageId">Identifies the master page to return.</param>
 /// <param name="unitOfWork">Unit of work.</param>
 /// <returns>Master page details (or null if master page not found).</returns>
 public MasterPage Read(long tenantId, long masterPageId, IUnitOfWork unitOfWork = null)
 {
     return(_masterPageRepository.Read(tenantId, masterPageId, unitOfWork));
 }