Action script example (Validation phase)
This is an example of an Action script to be executed by a Trigger in the Validation phase (In process) when an asset is created or modified. It validates if an asset's filename has a valid Web extension.
Use case
- A user creates or modifies an asset.
- If the asset's filename does not have a valid Web extension, the application throws an exception.
Script
using System.Linq;
var webExtensions = new[] { ".jpg", ".jpeg", ".png", ".gif" };
var entity = Context.Target as IEntity;
var filename = await entity.GetPropertyValueAsync<string>("FileName");
if (string.IsNullOrEmpty(filename)) return;
var extension = GetExtension(filename)?.ToLowerInvariant();
if (string.IsNullOrEmpty(extension)) return;
if (!webExtensions.Contains(extension))
{
throw new ValidationException(
"The asset is not valid.",
new ValidationFailure("The file's extension must be the extension of a valid web filetype.", filename));
}
string GetExtension(string path)
{
var tokens = path.Split('.');
if (tokens.Length > 1)
{
return "." + tokens[tokens.Length - 1];
}
return null;
}
Script explanation
-
Include the libraries to be used in the script.
RequestResponseusing System.Linq;
TipTo know which libraries to include, please refer to the Context properties section (property types).
-
Define the list of Web extensions. If an asset's filename extension is not in the list, it is not a valid filename.
RequestResponsevar webExtensions = new [] { ".jpg", ".jpeg", ".png", ".gif" };
-
Retrieve the
Target
object from theContext
and cast it toIEntity
.Target
is the asset involved in the creation or modification event.RequestResponsevar entity = Context.Target as IEntity;
WarningThe
Target
object type depends on the Trigger's objectives (e.g. entity, entity definition, policy). In this case, the Trigger's objectives are Entity creation and Entity modification. Hence, we need to castTarget
toIEntity
.TipThe context property
TargetType
indicates the type ofTarget
. The possible values ofTargetType
areEntity
,EntityDefinition
,Policy
andDataSource
. For more information about context properties, please refer to the Context properties section. -
Retrieve the
Filename
property from theTarget
object usingGetPropertyValueAsync
.RequestResponsevar filename = await entity.GetPropertyValueAsync<string>("FileName");
NoteThe scripting API uses lazy loading.
GetPropertyValueAsync
checks whether the property is already loaded. If it's not, it will load it for the user. -
If the asset does not have a filename, the script exits.
RequestResponseif (string.IsNullOrEmpty(filename)) return;
-
Get the extension from the filename using
Path.GetExtension
(System.IO).RequestResponsevar extension = GetExtension(filename)?.ToLowerInvariant();
-
If the filename does not have an extension, the script exits.
RequestResponseif (string.IsNullOrEmpty(extension)) return;
-
If the extension is not a valid Web extensions, the script throws a Validation exception.
RequestResponseif (!webExtensions.Contains(extension)) { throw new ValidationException( "The asset is not valid.", new ValidationFailure("The file's extension must be the extension of a valid web filetype.", filename)); }
NoteFor more information about
ValidationException
andValidationFailure
, as well as a list of the available custom exceptions, please refer to the SDK API reference guide (Stylelabs.M.Sdk.Exceptions). -
This is an internal method used to get the extension of the filename.
RequestResponsestring GetExtension(string path) { var tokens = path.Split('.'); if (tokens.Length > 1) { return "." + tokens[tokens.Length - 1]; } return null; }
Setup
-
Create, publish and enable an Action script.
-
Create an action of type Action script and link it with the script.
-
Create a new trigger and set the trigger's objective to Entity creation and Entity modification.
- In the trigger conditions, add the entity definition Asset then add a new condition.
- In the trigger actions, add the action under Validation actions.
-
Save and enable the trigger.