Beispiel #1
0
        protected override TaskOutput Execute(TaskInput input, ITaskContext context)
        {
            var xsltPath = context.CommonParams[_xsltFilePathKey];

            if (!File.Exists(xsltPath))
            {
                throw new ArgumentException(string.Format("Xslt file not found: {0}", xsltPath));
            }

            var outputFiles = new List <TaskFile>();

            foreach (var taskFile in input.Files)
            {
                using (var inputStream = new StreamReader(taskFile.Path, true))
                {
                    var source = new XPathDocument(inputStream);
                    var xslt   = new XslCompiledTransform();
                    xslt.Load(xsltPath);
                    using (var myWriter = new XmlTextWriter(taskFile.Path, Encoding.UTF8))
                    {
                        xslt.Transform(source, null, myWriter);
                    }
                }
                outputFiles.Add(new TaskFile(taskFile));
            }
            return(new TaskOutput(outputFiles, input.Params));
        }
Beispiel #2
0
        protected override TaskOutput Execute(TaskInput input, ITaskContext context)
        {
            foreach (var file in input.Files)
            {
                Rectangle cropRectangle;
                bool      cropped = false;
                if (TryGetCropRectangle(file, out cropRectangle))
                {
                    try
                    {
                        using (var readStream = new FileStream(file.Path, FileMode.Open))
                        {
                            using (var reader = FormatManager.CreateFormatReader(readStream))
                            {
                                using (var frame = reader.LoadFrame(0))
                                {
                                    if ((cropRectangle.X > 0 || cropRectangle.Y > 0) && (cropRectangle.Width < frame.Width || cropRectangle.Height < frame.Height))
                                    {
                                        using (var resultBitmap = new Bitmap(frame.Width, frame.Height, Aurigma.GraphicsMill.PixelFormat.Format32bppArgb))
                                        {
                                            frame.GetBitmap(resultBitmap);
                                            resultBitmap.Transforms.Crop(cropRectangle);

                                            if (reader.MediaFormat == FormatManager.JpegFormat)
                                            {
                                                resultBitmap.Save(file.Path + ".tmp", new JpegEncoderOptions(95, false));
                                            }
                                            else
                                            {
                                                resultBitmap.Save(file.Path + ".tmp", FormatManager.CreateEncoderOptions(reader.MediaFormat));
                                            }
                                        }
                                        cropped = true;
                                    }
                                }
                            }
                        }

                        if (cropped)
                        {
                            File.Delete(file.Path);
                            File.Move(file.Path + ".tmp", file.Path);
                        }
                    }
                    catch (Exception ex)
                    {
                        Logger.ErrorException(string.Format("Error acquired while cropping file {0}", file.Path), ex);
                    }
                }
            }
            return(new TaskOutput(input.Files, input.Params));
        }
Beispiel #3
0
 protected override void ChildrenExecuted(TaskInput childrenInput, IEnumerable <TaskOutput> childrenOutput)
 {
     if (_createdTempFiles == null)
     {
         return;
     }
     foreach (var file in _createdTempFiles)
     {
         var fileInfo = new FileInfo(file.Path);
         if (File.Exists(fileInfo.FullName))
         {
             File.Delete(fileInfo.FullName);
         }
         if (Directory.GetFiles(fileInfo.DirectoryName).Length == 0)
         {
             Directory.Delete(fileInfo.DirectoryName);
         }
     }
 }
Beispiel #4
0
        protected override TaskOutput Execute(TaskInput input, ITaskContext context)
        {
            if (input == null)
            {
                throw new ArgumentNullException("input");
            }

            _createdTempFiles = new List <TaskFile>();
            foreach (var taskFile in input.Files)
            {
                if (!File.Exists(taskFile.Path))
                {
                    throw new ArgumentException(string.Format("File not found: {0}", taskFile.Path));
                }

                var newPath = Path.Combine(context.WorkingDirectory, "_tmp", Path.GetRandomFileName());
                Directory.CreateDirectory(new FileInfo(newPath).DirectoryName);
                File.Copy(taskFile.Path, newPath);

                _createdTempFiles.Add(new TaskFile(newPath, input.Params));
            }
            return(new TaskOutput(_createdTempFiles));
        }
Beispiel #5
0
        protected override TaskOutput Execute(TaskInput input, ITaskContext context)
        {
            foreach (var file in input.Files)
            {
                try
                {
                    IEncoderOptions options = new JpegEncoderOptions(90, false);
                    switch (_convertFormat)
                    {
                    case ConvertFormat.Jpeg:
                        options = new JpegEncoderOptions(90, false);
                        break;

                    case ConvertFormat.Png:
                        options = new PngEncoderOptions();
                        break;

                    case ConvertFormat.Tiff:
                        options = new TiffEncoderOptions(CompressionType.Zip);
                        break;
                    }

                    var isConverted = false;
                    using (var readStream = new FileStream(file.Path, FileMode.Open))
                    {
                        using (var reader = FormatManager.CreateFormatReader(readStream))
                        {
                            if (reader.MediaFormat != options.MediaFormat)
                            {
                                using (var frame = reader.LoadFrame(0))
                                {
                                    using (var resultBitmap = new Bitmap(frame.Width, frame.Height, Aurigma.GraphicsMill.PixelFormat.Format24bppRgb))
                                    {
                                        frame.GetBitmap(resultBitmap);

                                        var conveter = new PixelFormatConverter {
                                            DestinationPixelFormat = Aurigma.GraphicsMill.PixelFormat.Format32bppArgb
                                        };
                                        conveter.ApplyTransform(resultBitmap);

                                        if (resultBitmap.HasAlpha)
                                        {
                                            resultBitmap.Channels.DiscardAlpha(RgbColor.White);
                                        }

                                        resultBitmap.Save(file.Path + ".tmp", options);
                                        isConverted = true;
                                    }
                                }
                            }
                        }
                    }

                    if (isConverted)
                    {
                        File.Delete(file.Path);
                        File.Move(file.Path + ".tmp", file.Path);
                    }
                }
                catch (System.Exception ex)
                {
                    Logger.ErrorException(string.Format("Error while converting file {1} to {0}", _convertFormat, file.Path), ex);
                }
            }
            return(new TaskOutput(input.Files, input.Params));
        }
Beispiel #6
0
 protected override void ChildrenExecuted(TaskInput childrenInput, IEnumerable <TaskOutput> childrenOutput)
 {
 }
Beispiel #7
0
        protected override TaskOutput Execute(TaskInput input, ITaskContext context)
        {
            var channelFilesDictionary = new Dictionary <string, List <TaskFile> >();

            foreach (var file in input.Files)
            {
                if (file.Params.ContainsKey(_printChannelKey))
                {
                    var channal = file.Params[_printChannelKey];
                    if (!channelFilesDictionary.ContainsKey(channal))
                    {
                        channelFilesDictionary[channal] = new List <TaskFile>();
                    }

                    channelFilesDictionary[channal].Add(file);
                }
            }

            var outputFiles = new List <TaskFile>();

            foreach (var channelFiles in channelFilesDictionary)
            {
                var channel = channelFiles.Key;
                Logger.Info("Found {0} files for {1} channel", channelFiles.Value.Count, channel);

                var @params = new Dictionary <string, string>(context.CommonParams);
                if ([email protected](_printChannelKey))
                {
                    @params.Add(_printChannelKey, channel);
                }

                var miscDirectoryPath  = GetMiscDirectoryPath(context.WorkingDirectory, @params);
                var filesDirectoryPath = GetFilesDirectoryPath(context.WorkingDirectory, @params);

                Logger.Info("dpof FILES directory path: {0}", Path.GetFullPath(filesDirectoryPath));
                Logger.Info("dpof MISC directory path: {0}", Path.GetFullPath(miscDirectoryPath));

                var printFiles = new List <PrintFileInfo>();
                foreach (var file in channelFiles.Value)
                {
                    var sourceFile = new FileInfo(file.Path);
                    var fileName   = string.Format("{0}{1}", Path.GetFileNameWithoutExtension(file.Path).Unidecode().Trim(), sourceFile.Extension).Replace(" ", "_");
                    var newPath    = Path.Combine(filesDirectoryPath, fileName);

                    if (File.Exists(newPath))
                    {
                        Logger.Warn("warning: file exists ({0})", Path.GetFullPath(newPath));

                        var newName = Path.GetFileNameWithoutExtension(newPath);
                        var fi      = new FileInfo(newPath);
                        var dirPath = fi.DirectoryName;
                        var i       = 1;
                        while (i < 99)
                        {
                            newName = string.Format("{0}_{1}{2}", newName, i, fi.Extension);
                            var path = Path.Combine(dirPath, newName);
                            if (!File.Exists(path))
                            {
                                newPath = path;
                                break;
                            }
                            i++;
                        }
                        Logger.Warn("new file path is: {0}", Path.GetFullPath(newPath));
                        File.Copy(file.Path, newPath, true);
                    }
                    else
                    {
                        File.Copy(sourceFile.FullName, newPath, false);
                    }

                    Logger.Info("file copy: {0} to {1}", Path.GetFullPath(sourceFile.FullName), newPath);
                    printFiles.Add(new PrintFileInfo()
                    {
                        CopyCount = int.Parse(file.Params[_copyCountKey]), FilePath = newPath
                    });

                    outputFiles.Add(new TaskFile(newPath, file.Params));
                }

                var paperSize = channelFiles.Value.First().Params[_paperSizeKey];
                var doc       = new NoritsuDpofDocument(miscDirectoryPath, printFiles, paperSize, int.Parse(channel));
                var result    = doc.GetRenderedFilesPath();
                foreach (var filePath in result)
                {
                    Logger.Info("dpof result file: {0}", Path.GetFullPath(filePath));
                    outputFiles.Add(new TaskFile(filePath, null));
                }
            }
            return(new TaskOutput(outputFiles));
        }
Beispiel #8
0
 protected abstract void ChildrenExecuted(TaskInput childrenInput, IEnumerable <TaskOutput> childrenOutput);
Beispiel #9
0
 protected abstract TaskOutput Execute(TaskInput input, ITaskContext context);
Beispiel #10
0
        protected override TaskOutput Execute(TaskInput input, ITaskContext context)
        {
            foreach (var taskFile in input.Files)
            {
                var textNote     = taskFile.Params[_textKey];
                var textPosition = taskFile.Params[_positionKey];

                using (var image = new Bitmap(taskFile.Path))
                {
                    if (!image.IsRgb || image.IsIndexed || image.IsGrayScale)
                    {
                        image.ColorManagement.ConvertToContinuous(ColorSpace.Rgb, false, false);
                    }

                    var renderingSize = new Size(image.Width, image.Height);

                    double materialWidth  = (double)renderingSize.Width / 100;
                    double materialHeight = (double)renderingSize.Height / 100;

                    double lineSize = 0;
                    if (textPosition == "Left" || textPosition == "Right")
                    {
                        lineSize       = materialWidth / 15;
                        materialWidth -= lineSize;
                    }
                    else
                    {
                        lineSize        = materialHeight / 15;
                        materialHeight -= lineSize;
                    }

                    int textWidth  = 0;
                    int textHeight = (int)Math.Round(lineSize * image.Height / materialHeight);

                    if (textPosition == "Left" || textPosition == "Right")
                    {
                        textWidth = image.Height;
                    }
                    else
                    {
                        textWidth = image.Width;
                    }

                    using (Bitmap text = new Bitmap(System.Drawing.Color.FromArgb(240, 240, 240), textWidth, textHeight, PixelFormat.Format32bppArgb))
                    {
                        using (Graphics graphics = text.GetGdiplusGraphics())
                        {
                            System.Drawing.Color sourceColor = System.Drawing.Color.White;
                            System.Drawing.Color destColor   = System.Drawing.Color.White;

                            if (textPosition == "Left" || textPosition == "Top")
                            {
                                sourceColor = System.Drawing.Color.FromArgb(200, 200, 200);
                            }
                            else
                            {
                                destColor = System.Drawing.Color.FromArgb(200, 200, 200);
                            }

                            var gradient = new LinearGradientBrush(new Rectangle(0, 0, text.Width, text.Height), sourceColor, destColor, LinearGradientMode.Vertical);
                            graphics.FillRectangle(gradient, 0, 0, text.Width, text.Height);

                            using (var drawFont = new Aurigma.GraphicsMill.Drawing.Font("Trebuchet MS", textHeight / 2))
                            {
                                drawFont.Antialiased = true;
                                drawFont.Unit        = Unit.Point;
                                drawFont.ClearType   = true;

                                using (var drawBrush = new SolidBrush(System.Drawing.Color.Black))
                                {
                                    var drawFormat = new StringFormat();
                                    drawFormat.Alignment     = StringAlignment.Center;
                                    drawFormat.LineAlignment = StringAlignment.Center;

                                    graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
                                    graphics.DrawString(textNote, drawFont, drawBrush, new RectangleF(0, 0, textWidth, textHeight), drawFormat);
                                }
                            }
                        }

                        int resultWidth  = image.Width;
                        int resultHeight = image.Height;

                        if (textPosition == "Left" || textPosition == "Right")
                        {
                            text.Transforms.RotateAndFlip(RotateFlipType.Rotate270FlipNone);
                            resultWidth += text.Width;
                        }
                        else
                        {
                            resultHeight += text.Height;
                        }

                        using (var result = new Bitmap(resultWidth, resultHeight, PixelFormat.Format32bppArgb))
                        {
                            switch (textPosition)
                            {
                            case "Left":
                                text.Draw(result, 0, 0, CombineMode.AlphaOverlapped, 1.0f);
                                image.Draw(result, text.Width, 0, CombineMode.AlphaOverlapped, 1.0f);
                                break;

                            case "Right":
                                text.Draw(result, image.Width, 0, CombineMode.AlphaOverlapped, 1.0f);
                                image.Draw(result, 0, 0, CombineMode.AlphaOverlapped, 1.0f);
                                break;

                            case "Top":
                                text.Draw(result, 0, 0, CombineMode.AlphaOverlapped, 1.0f);
                                image.Draw(result, 0, text.Height, CombineMode.AlphaOverlapped, 1.0f);
                                break;

                            default:
                                text.Draw(result, 0, image.Height, CombineMode.AlphaOverlapped, 1.0f);
                                image.Draw(result, 0, 0, CombineMode.AlphaOverlapped, 1.0f);
                                break;
                            }

                            var pngEncoder = new PngEncoderOptions();
                            result.Save(taskFile.Path, pngEncoder);
                        }
                    }
                }
            }
            return(new TaskOutput(input.Files, input.Params));
        }
Beispiel #11
0
        protected override TaskOutput Execute(TaskInput inputFiles, ITaskContext context)
        {
            var outputFiles = new List <TaskFile>();

            foreach (var file in inputFiles.Files)
            {
                var target = _target;

                var @params = new List <KeyValuePair <string, string> >();
                @params.AddRange(context.CommonParams);
                if (file.Params != null)
                {
                    @params.AddRange(file.Params);
                }

                foreach (var pair in @params)
                {
                    target = target.Replace(pair.Key, pair.Value);
                }

                target = target.Replace("\\\\", "\\");
                if (target.StartsWith("\\"))
                {
                    target = target.Substring(1);
                }

                var sourceFileInfo = new FileInfo(file.Path);
                var targetFilePath = Path.Combine(context.WorkingDirectory, target);

                targetFilePath = GetSafeFilePath(targetFilePath);

                var targetFileInfo = new FileInfo(targetFilePath);
                Directory.CreateDirectory(targetFileInfo.DirectoryName);

                if (string.IsNullOrEmpty(targetFileInfo.Name))
                {
                    targetFilePath = Path.Combine(targetFilePath, sourceFileInfo.Name);
                }

                if (!Path.GetFullPath(targetFilePath).StartsWith(Path.GetFullPath(context.WorkingDirectory), StringComparison.InvariantCultureIgnoreCase))
                {
                    Logger.Error("target file path is outside working directory:", Path.GetFullPath(targetFilePath));
                    throw new ArgumentException("Target file path is outside working directory.");
                }

                Logger.Info("copy from {0} to {1}",
                            Path.GetFullPath(file.Path),
                            Path.GetFullPath(targetFilePath));

                if (File.Exists(targetFilePath))
                {
                    Logger.Warn("warning: file exists ({0}), conflict resolve mode: {1}", Path.GetFullPath(targetFilePath), OverrideMode);
                    switch (OverrideMode)
                    {
                    case OverwriteMode.Replace:
                        File.Copy(file.Path, targetFilePath, true);
                        break;

                    case OverwriteMode.Rename:
                        var newName = Path.GetFileNameWithoutExtension(targetFilePath);
                        var fi      = new FileInfo(targetFilePath);
                        var dirPath = fi.DirectoryName;
                        var i       = 1;
                        while (i < 99)
                        {
                            newName = string.Format("{0}_{1}{2}", newName, i, fi.Extension);
                            var newPath = Path.Combine(dirPath, newName);
                            if (!File.Exists(newPath))
                            {
                                targetFilePath = newPath;
                                break;
                            }
                            i++;
                        }
                        Logger.Warn("new file path is: {0}", Path.GetFullPath(targetFilePath));
                        File.Copy(file.Path, targetFilePath, true);
                        break;

                    case OverwriteMode.Skip:
                        break;
                    }
                }
                else
                {
                    File.Copy(file.Path, targetFilePath, true);
                }

                var targetFile = new TaskFile(targetFilePath, file.Params);
                outputFiles.Add(targetFile);
            }
            return(new TaskOutput(outputFiles));
        }