예제 #1
0
        private static IEnumerable <ValidationRule> GetValidationRules(IObjectElementDescriptor descriptor)
        {
            switch (descriptor.Type)
            {
            case ElementDescriptorType.PlainText:
            case ElementDescriptorType.FasComment:
                return(new ValidationRule[]
                {
                    PlainTextValidator.CheckLength,
                    PlainTextValidator.CheckWordsLength,
                    PlainTextValidator.CheckLinesCount,
                    PlainTextValidator.CheckRestrictedSymbols
                });

            case ElementDescriptorType.FormattedText:
                return(new ValidationRule[]
                {
                    FormattedTextValidator.CheckLength,
                    FormattedTextValidator.CheckWordsLength,
                    FormattedTextValidator.CheckLinesCount,
                    FormattedTextValidator.CheckRestrictedSymbols,
                    FormattedTextValidator.CheckValidHtml,
                    FormattedTextValidator.CheckSupportedHtmlTags,
                    FormattedTextValidator.CheckAttributesAbsence,
                    FormattedTextValidator.CheckEmptyList,
                    FormattedTextValidator.CheckNestedList,
                    FormattedTextValidator.CheckUnsupportedListElements
                });

            case ElementDescriptorType.Link:
            case ElementDescriptorType.VideoLink:
                return(new ValidationRule[]
                {
                    LinkValidator.CheckLink,
                    PlainTextValidator.CheckLength,
                    PlainTextValidator.CheckRestrictedSymbols
                });

            case ElementDescriptorType.BitmapImage:
            case ElementDescriptorType.VectorImage:
            case ElementDescriptorType.ScalableBitmapImage:
            case ElementDescriptorType.Article:
            case ElementDescriptorType.Phone:
                return(new ValidationRule[] { });

            case ElementDescriptorType.Color:
                return(new ValidationRule[] { ColorValidator.CheckValidColor });

            case ElementDescriptorType.CompositeBitmapImage:
                return(new ValidationRule[] { CompositeBitmapImageValidator.CheckValidCompositeBitmapImage });

            default:
                throw new ArgumentOutOfRangeException(nameof(descriptor.Type), descriptor.Type, $"Unsupported element descriptor type for descriptor {descriptor.Id}");
            }
        }
        public async Task <ObjectDescriptor> GetObjectDescriptor(long id, string versionId)
        {
            string objectVersionId;

            if (string.IsNullOrEmpty(versionId))
            {
                var objectVersions = await GetObjectLatestVersions(id);

                objectVersionId = objectVersions.Where(x => x.Id.EndsWith(Tokens.ObjectPostfix))
                                  .Select(x => x.VersionId)
                                  .SingleOrDefault();

                if (objectVersionId == null)
                {
                    throw new ObjectNotFoundException($"Object '{id}' not found.");
                }
            }
            else
            {
                objectVersionId = versionId;
            }

            var(persistenceDescriptor, objectAuthorInfo, objectLastModified, modifiedElements) =
                await GetObjectFromS3 <ObjectPersistenceDescriptor>(id.AsS3ObjectKey(Tokens.ObjectPostfix), objectVersionId);

            var elements = new IObjectElementDescriptor[persistenceDescriptor.Elements.Count];
            var tasks    = persistenceDescriptor.Elements.Select(
                async(x, index) =>
            {
                var(elementPersistenceDescriptor, _, elementLastModified, _) =
                    await GetObjectFromS3 <ObjectElementPersistenceDescriptor>(x.Id, x.VersionId);

                if (elementPersistenceDescriptor.Value is IBinaryElementValue binaryElementValue &&
                    !string.IsNullOrEmpty(binaryElementValue.Raw))
                {
                    binaryElementValue.DownloadUri = new Uri(_fileStorageEndpoint, binaryElementValue.Raw);

                    if (binaryElementValue is IImageElementValue imageElementValue)
                    {
                        imageElementValue.PreviewUri = new Uri(_fileStorageEndpoint, imageElementValue.Raw);
                    }
                }

                elements[index] = new ObjectElementDescriptor
                {
                    Id           = x.Id.AsSubObjectId(),
                    VersionId    = x.VersionId,
                    LastModified = elementLastModified,
                    Type         = elementPersistenceDescriptor.Type,
                    TemplateCode = elementPersistenceDescriptor.TemplateCode,
                    Properties   = elementPersistenceDescriptor.Properties,
                    Constraints  = elementPersistenceDescriptor.Constraints,
                    Value        = elementPersistenceDescriptor.Value
                };
            });
            await Task.WhenAll(tasks);

            var descriptor = new ObjectDescriptor
            {
                Id                = id,
                VersionId         = objectVersionId,
                LastModified      = objectLastModified,
                TemplateId        = persistenceDescriptor.TemplateId,
                TemplateVersionId = persistenceDescriptor.TemplateVersionId,
                Language          = persistenceDescriptor.Language,
                Properties        = persistenceDescriptor.Properties,
                Elements          = elements,
                Metadata          = new ObjectDescriptor.ObjectMetadata
                {
                    Author           = objectAuthorInfo.Author,
                    AuthorLogin      = objectAuthorInfo.AuthorLogin,
                    AuthorName       = objectAuthorInfo.AuthorName,
                    ModifiedElements = modifiedElements
                }
            };

            return(descriptor);
        }