public static void Main(string[] args) { try { Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine(Assembly.GetExecutingAssembly().FullName); Console.WriteLine(); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("Start integration"); string rootDirectoryPath = Environment.CurrentDirectory; Console.WriteLine("Root folder: " + rootDirectoryPath); string integrationFilePath = WorkerHelpers.GetIntegrationTextFile(rootDirectoryPath); Worker.CreatePreviewHtmlFile(rootDirectoryPath, integrationFilePath); List <string> imageTags = WorkerHelpers.GetYandexFotkiImageTags(rootDirectoryPath); Worker.IntergatePhotosInTextFile(rootDirectoryPath, integrationFilePath, imageTags); Console.WriteLine(); } catch (Exception e) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(); Console.WriteLine(e.Message); Console.WriteLine("Press any key to exit..."); Console.ReadKey(); } }
public static void CreatePreviewHtmlFile(string rootPath, string integrationFilePath) { try { if (string.IsNullOrEmpty(rootPath) || string.IsNullOrEmpty(integrationFilePath) || !File.Exists(integrationFilePath)) { throw new Exception("Integration text file not found."); } Console.WriteLine(); string htmlFilePath = integrationFilePath.Replace(".txt", ".html"); if (File.Exists(htmlFilePath)) { File.Delete(htmlFilePath); Console.WriteLine("Old .HTML file deleted."); } List <string> textContent = File.ReadAllLines(integrationFilePath, Encoding.GetEncoding("Windows-1251")).ToList(); List <string> photos = WorkerHelpers.GetImages(rootPath, integrationFilePath); StringBuilder page = new StringBuilder(); page.AppendLine("<!DOCTYPE html>"); page.AppendLine("<html>"); page.AppendLine("<head>"); page.AppendLine("</head>"); page.AppendLine("<body>"); foreach (string line in textContent) { if (!Regex.IsMatch(line, "\\([\\w\\d]+\\)")) { page.AppendLine(line + "<br>"); continue; } string imageName = line.Trim('(', ')'); string photoPath = photos.FirstOrDefault(p => StringHelper.GetFileNameWithoutExtension(p).Equals(imageName, StringComparison.OrdinalIgnoreCase)); if (string.IsNullOrEmpty(photoPath)) { page.AppendLine(line + "<br>"); continue; } string relativePath = photoPath.Replace("/", "\\").Replace(rootPath.Replace("/", "\\"), string.Empty).Trim('\\'); string imageLine = "<img src=\"" + relativePath + "\" title=\"\" alt=\"" + Constants.NoPhotoMessage + "\" border=\"0\"/>"; page.AppendLine(imageLine + "<br>"); } page.AppendLine("</body>"); page.AppendLine("</html>"); File.WriteAllText(htmlFilePath, page.ToString(), Encoding.UTF8); Console.WriteLine(".HTML file saved by path " + htmlFilePath); } catch (Exception e) { throw new Exception("Failed to create preview .HTML file. " + e.Message); } }
static TaskResult DoAction(string message) { var args = new MessageArgs(message); var result = args.A == args.B ? "=" : args.A > args.B ? ">" : "<"; Thread.Sleep(1000); var printedResult = WorkerHelpers.PrintResult(args.TaskId, string.Format("{0} {1} {2}", args.A, result, args.B)); return(new TaskResult() { TaskId = args.TaskId, SenderRoute = args.SenderRoute + ".w3", Result = printedResult, }); }
static TaskResult DoAction(string message) { var args = new MessageArgs(message); var result = args.A + args.B; Thread.Sleep(result % 2 == 0 ? 1000 : 5000); var printedResult = WorkerHelpers.PrintResult(args.TaskId, string.Format("{0}+{1}={2}", args.A, args.B, result)); return(new TaskResult() { TaskId = args.TaskId, SenderRoute = args.SenderRoute + ".w1", Result = printedResult, }); }
public static void IntergatePhotosInTextFile(string rootPath, string integrationFilePath, List <string> imgTags) { try { string integratingFileName = Path.GetFileName(integrationFilePath); if (string.IsNullOrEmpty(integratingFileName)) { throw new Exception("Integration file name is empty."); } List <string> photos = WorkerHelpers.GetImages(rootPath, integrationFilePath); Console.WriteLine(photos.Count() + " .JPG files found in imaging folder."); if (imgTags.Count != photos.Count()) { throw new Exception("Failed. Counts are different"); } List <string> photoFileNames = photos.Select(f => "(" + Path.GetFileNameWithoutExtension(f) + ")").ToList(); string mainReportText = File.ReadAllText(integrationFilePath, Encoding.GetEncoding("WINDOWS-1251")); Console.WriteLine("Integrating file \"" + integratingFileName + "\" read"); foreach (string photoFileName in photoFileNames) { int inArrayPosition = photoFileNames.IndexOf(photoFileName); string currentImgTag = imgTags[inArrayPosition]; mainReportText = mainReportText.Replace(photoFileName, currentImgTag); } Console.WriteLine(photoFileNames.Count + " entries changed in \"" + integratingFileName + "\" file"); File.WriteAllText(integrationFilePath, mainReportText, Encoding.GetEncoding("WINDOWS-1251")); Console.WriteLine(integratingFileName + "\" file saved"); } catch (Exception e) { throw new Exception("Failed to integrate photos in text file. " + e.Message); } }
public void Execute(Func <string, TaskResult> callbackAction) { var factory = new ConnectionFactory() { HostName = hostName }; using (var connection = factory.CreateConnection()) { using (var channel = connection.CreateModel()) { using (var logChannel = connection.CreateModel()) { using (var resChannel = connection.CreateModel()) { channel.QueueDeclare(queue: queueName, durable: true, exclusive: false, autoDelete: false, arguments: null); channel.BasicQos(prefetchSize: 0, prefetchCount: 1, global: false); logChannel.ExchangeDeclare(exchange: LoggerExchangeName, type: ExchangeType.Fanout); resChannel.ExchangeDeclare(exchange: ResultExchangeName, type: ExchangeType.Topic); var consumer = new EventingBasicConsumer(channel); consumer.Received += (model, ea) => { var body = ea.Body.ToArray(); var message = Encoding.UTF8.GetString(body); try { var taskResult = callbackAction(message); channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false); resChannel.BasicPublish(exchange: ResultExchangeName, routingKey: taskResult.SenderRoute, basicProperties: null, body: WorkerHelpers.GetResultBody(taskResult.Result)); logChannel.BasicPublish(exchange: LoggerExchangeName, routingKey: taskResult.SenderRoute, basicProperties: null, body: WorkerHelpers.GetFinishedLogBody(taskResult.TaskId)); } catch (Exception e) { WorkerHelpers.PrintError(e); } }; channel.BasicConsume(queue: queueName, autoAck: false, consumer: consumer); Console.WriteLine(" Press [enter] to exit."); Console.ReadLine(); } } } } }