Example #1
0
        private static async Task ExecuteScriptAsync(AssetOperation operation, string script, AssetScriptVars vars)
        {
            var snapshot = operation.Snapshot;

            var parentPath = await GetPathAsync(operation, snapshot.ParentId);

            // Script vars are just wrappers over dictionaries for better performance.
            var asset = new AssetEntityScriptVars
            {
                Metadata    = snapshot.Metadata,
                FileHash    = snapshot.FileHash,
                FileName    = snapshot.FileName,
                FileSize    = snapshot.FileSize,
                FileSlug    = snapshot.Slug,
                FileVersion = snapshot.FileVersion,
                IsProtected = snapshot.IsProtected,
                MimeType    = snapshot.MimeType,
                ParentId    = snapshot.ParentId,
                ParentPath  = parentPath,
                Tags        = snapshot.Tags
            };

            vars.AppId   = operation.App.Id;
            vars.AppName = operation.App.Name;
            vars.AssetId = operation.CommandId;
            vars.Asset   = asset;
            vars.User    = operation.User;

            var scriptEngine = operation.Resolve <IScriptEngine>();

            await scriptEngine.ExecuteAsync(vars, script, Options);
        }
Example #2
0
        private static async Task ExecuteScriptAsync(AssetOperation operation, string script, ScriptVars vars)
        {
            var snapshot = operation.Snapshot;

            var parentPath = await GetPathAsync(operation, snapshot.ParentId);

            // Use a dictionary for better performance, because no reflection is involved.
            var asset = new Dictionary <string, object?>
            {
                [ScriptKeys.Metadata]    = snapshot.ReadonlyMetadata(),
                [ScriptKeys.FileHash]    = snapshot.FileHash,
                [ScriptKeys.FileName]    = snapshot.FileName,
                [ScriptKeys.FileSize]    = snapshot.FileSize,
                [ScriptKeys.FileSlug]    = snapshot.Slug,
                [ScriptKeys.FileVersion] = snapshot.FileVersion,
                [ScriptKeys.IsProtected] = snapshot.IsProtected,
                [ScriptKeys.MimeType]    = snapshot.MimeType,
                [ScriptKeys.ParentId]    = snapshot.ParentId,
                [ScriptKeys.ParentPath]  = parentPath,
                [ScriptKeys.Tags]        = snapshot.ReadonlyTags()
            };

            vars[ScriptKeys.AppId]   = operation.App.Id;
            vars[ScriptKeys.AppName] = operation.App.Name;
            vars[ScriptKeys.AssetId] = operation.CommandId;
            vars[ScriptKeys.Asset]   = asset;
            vars[ScriptKeys.User]    = operation.User;

            var scriptEngine = operation.Resolve <IScriptEngine>();

            await scriptEngine.ExecuteAsync(vars, script, Options);
        }
        public static async Task <HashSet <string> > NormalizeTags(this AssetOperation operation, HashSet <string> tags)
        {
            var tagService = operation.Resolve <ITagService>();

            var normalized = await tagService.NormalizeTagsAsync(operation.App.Id, TagGroups.Assets, tags, operation.Snapshot.Tags);

            return(new HashSet <string>(normalized.Values));
        }
Example #4
0
        public static async Task CheckReferrersAsync(this AssetOperation operation)
        {
            var contentRepository = operation.Resolve <IContentRepository>();

            var hasReferrer = await contentRepository.HasReferrersAsync(operation.App.Id, operation.CommandId, SearchScope.All, default);

            if (hasReferrer)
            {
                throw new DomainException(T.Get("assets.referenced"), "OBJECT_REFERENCED");
            }
        }
Example #5
0
        private static async Task <Array> GetPathAsync(AssetOperation operation, DomainId parentId)
        {
            if (parentId == default)
            {
                return(Array.Empty <object>());
            }

            var assetQuery = operation.Resolve <IAssetQueryService>();
            var assetPath  = await assetQuery.FindAssetFolderAsync(operation.App.Id, parentId);

            return(assetPath.Select(x => new { id = x.Id, folderName = x.FolderName }).ToArray());
        }
Example #6
0
        public static async Task MustMoveToValidFolder(this AssetOperation operation, DomainId parentId)
        {
            // If moved to root folder or not moved at all, we can just skip the validation.
            if (parentId == DomainId.Empty || parentId == operation.Snapshot.ParentId)
            {
                return;
            }

            var assetQuery = operation.Resolve <IAssetQueryService>();

            var path = await assetQuery.FindAssetFolderAsync(operation.App.Id, parentId);

            if (path.Count == 0)
            {
                operation.AddError(T.Get("assets.folderNotFound"), nameof(MoveAsset.ParentId)).ThrowOnErrors();
            }

            operation.ThrowOnErrors();
        }
        public static async Task UnsetTags(this AssetOperation operation)
        {
            var tagService = operation.Resolve <ITagService>();

            await tagService.NormalizeTagsAsync(operation.App.Id, TagGroups.Assets, null, operation.Snapshot.Tags);
        }