protected override async Task <Action <AsyncCodeActivityContext> > ExecuteAsync(AsyncCodeActivityContext context, CancellationToken cancellationToken)
        {
            // Inputs
            var timeout        = TimeoutMS.Get(context);
            var imagefilepath  = ImageFilePath.Get(context);
            var outputfilepath = OutputFilePath.Get(context);

            if (!File.Exists(imagefilepath))
            {
                throw new FileNotFoundException(Resources.GenerateWallpaperWithImageFile_ImageFilePath_DisplayName);
            }

            if (!Directory.Exists(Path.GetDirectoryName(imagefilepath)))
            {
                throw new ArgumentException(string.Format(Resources.ValidationValueFullPath_Error, Resources.GenerateWallpaperWithImageFile_ImageFilePath_DisplayName));
            }

            if (!Directory.Exists(Path.GetDirectoryName(outputfilepath)))
            {
                throw new ArgumentException(string.Format(Resources.ValidationValueFullPath_Error, Resources.GenerateWallpaperWithImageFile_OutputFilePath_DisplayName));
            }

            // Set a timeout on the execution
            var task = ExecuteWithTimeout(context, cancellationToken);

            if (await Task.WhenAny(task, Task.Delay(timeout, cancellationToken)) != task)
            {
                throw new TimeoutException(Resources.Timeout_Error);
            }

            // Outputs
            return((ctx) => {
                Result.Set(ctx, task.Result);
            });
        }
Esempio n. 2
0
        protected override void Execute(CodeActivityContext context)
        {
            try
            {
                string   inputFilePath  = InputFilePath.Get(context);
                string   outputFilePath = OutputFilePath.Get(context);
                string   key            = Key.Get(context);
                Encoding keyEncoding    = KeyEncoding.Get(context);

                if (string.IsNullOrWhiteSpace(inputFilePath))
                {
                    throw new ArgumentNullException(Resources.InputFilePathDisplayName);
                }
                if (string.IsNullOrWhiteSpace(outputFilePath))
                {
                    throw new ArgumentNullException(Resources.OutputFilePathDisplayName);
                }
                if (string.IsNullOrWhiteSpace(key))
                {
                    throw new ArgumentNullException(Resources.Key);
                }
                if (keyEncoding == null)
                {
                    throw new ArgumentNullException(Resources.Encoding);
                }
                if (!File.Exists(inputFilePath))
                {
                    throw new ArgumentException(Resources.FileDoesNotExistsException, Resources.InputFilePathDisplayName);
                }
                // Because we use File.WriteAllText below, we don't need to delete the file now.
                if (File.Exists(outputFilePath) && !Overwrite)
                {
                    throw new ArgumentException(Resources.FileAlreadyExistsException, Resources.OutputFilePathDisplayName);
                }

                byte[] encrypted = File.ReadAllBytes(inputFilePath);

                byte[] decrypted = null;
                try
                {
                    decrypted = CryptographyHelper.DecryptData(Algorithm, encrypted, keyEncoding.GetBytes(key));
                }
                catch (CryptographicException ex)
                {
                    throw new InvalidOperationException(Resources.GenericCryptographicException, ex);
                }

                // This overwrites the file if it already exists.
                File.WriteAllBytes(outputFilePath, decrypted);
            }
            catch (Exception ex)
            {
                Trace.TraceError(ex.ToString());

                if (!ContinueOnError.Get(context))
                {
                    throw;
                }
            }
        }
        private async Task <bool> ExecuteWithTimeout(AsyncCodeActivityContext context, CancellationToken cancellationToken = default)
        {
            var text           = Text.Get(context);
            var fontsize       = FontSize.Get(context);
            var fontname       = FontName.Get(context);
            var outputfilepath = OutputFilePath.Get(context);

            return(await Task.FromResult(new WallpaperGenerater().GenerateWallPaperFromSolidColor(BackGroundColor,
                                                                                                  TextColor,
                                                                                                  text,
                                                                                                  fontsize,
                                                                                                  fontname,
                                                                                                  outputfilepath)));
        }