From 572e4d6ed33c03897f03137d320b8bddf2980c50 Mon Sep 17 00:00:00 2001 From: =?utf8?q?V=C3=A1s=C3=A1ry=20D=C3=A1niel?= Date: Thu, 16 Nov 2017 21:41:12 +0000 Subject: [PATCH] git-tfs-id: [http://tfs.userrendszerhaz.hu:8080/tfs/DefaultCollection]$/MediaCube;C30736 --- client/Maestro/Targets/FTPTargetProcessor.cs | 26 +++--- client/Maestro/Targets/FXPTargetProcessor.cs | 8 +- client/Maestro/Targets/UNCTargetProcessor.cs | 91 +++++++++++++------- 3 files changed, 75 insertions(+), 50 deletions(-) diff --git a/client/Maestro/Targets/FTPTargetProcessor.cs b/client/Maestro/Targets/FTPTargetProcessor.cs index 5ff420f0..0263220c 100644 --- a/client/Maestro/Targets/FTPTargetProcessor.cs +++ b/client/Maestro/Targets/FTPTargetProcessor.cs @@ -1,5 +1,5 @@ using FluentFTP; -using Maestro.Configuration; +using MaestroShared.Configuration; using NLog; using System; using System.IO; @@ -10,6 +10,9 @@ using System.Windows.Forms; namespace Maestro.Targets { public class FTPTargetProcessor : UNCTargetProcessor { + private const string PATTERN_CONCAT = "{0}{1}"; + private const string _226 = "226"; + private const string UPLOAD_ERROR = "Feltöltési hiba."; private readonly Logger logger = LogManager.GetCurrentClassLogger(); protected FtpClient targetFTP; @@ -18,7 +21,7 @@ namespace Maestro.Targets { FtpTrace.LogFunctions = false; if (!(parameters.SourceConfig is UNCSource)) { inputFile = null; - Uri inputUri = new Uri(String.Format("{0}{1}", parameters.SourceConfig.Remote.Address, parameters.InputFileName)); + Uri inputUri = new Uri(String.Format(PATTERN_CONCAT, parameters.SourceConfig.Remote.Address, parameters.InputFileName)); Input = inputUri.ToString(); } } @@ -53,14 +56,9 @@ namespace Maestro.Targets { } } FtpReply reply = targetFTP.GetReply(); - if (!reply.Success || !"226".Equals(reply.Code)) { - throw new Exception("Feltöltési hiba."); + if (!reply.Success || !_226.Equals(reply.Code)) { + throw new Exception(UPLOAD_ERROR); } - //if ((reply.Success && "226".Equals(reply.Code) && !FileExists(OutputName)) || - // !reply.Success || - // (reply.Success && !"226".Equals(reply.Code))) { - // throw new Exception("Feltöltési hiba."); - //} } protected void reconnect() { @@ -83,8 +81,8 @@ namespace Maestro.Targets { } } FtpReply reply = targetFTP.GetReply(); - if (!reply.Success || !"226".Equals(reply.Code)) { - throw new Exception("Feltöltési hiba."); + if (!reply.Success || !_226.Equals(reply.Code)) { + throw new Exception(UPLOAD_ERROR); } } @@ -128,13 +126,13 @@ namespace Maestro.Targets { if (!String.IsNullOrEmpty(parameters.TargetConfig.SubFolderFormat)) { string name = GetDynamicName(parameters.TargetConfig.SubFolderFormat); //TODO ellenőrizni, hogy mi van placeholder esetén - if (parameters.TargetConfig.SubFolderFormat.Contains("%TEXT%")) { - string pattern = name.Split('-')[0] + "-"; + if (parameters.TargetConfig.SubFolderFormat.Contains(PATTERN_TEXT)) { + string pattern = name.Split(HYPHEN[0])[0] + HYPHEN; targetFTP.SetWorkingDirectory(connection.Address.LocalPath); FtpListItem item = targetFTP.GetListing()?.Where(i => i.Type.Equals(FtpFileSystemObjectType.Directory) && i.Name.StartsWith(pattern)).FirstOrDefault(); if (item != null) name = item.Name; - targetFTP.SetWorkingDirectory("/"); + targetFTP.SetWorkingDirectory(SLASH); } result = Path.Combine(connection.Address.LocalPath, name); } else diff --git a/client/Maestro/Targets/FXPTargetProcessor.cs b/client/Maestro/Targets/FXPTargetProcessor.cs index ab428659..48fa3894 100644 --- a/client/Maestro/Targets/FXPTargetProcessor.cs +++ b/client/Maestro/Targets/FXPTargetProcessor.cs @@ -1,5 +1,5 @@ using FluentFTP; -using Maestro.Configuration; +using MaestroShared.Configuration; using NLog; using System; using System.IO; @@ -10,6 +10,8 @@ using System.Windows.Forms; namespace Maestro.Targets { public class FXPTargetProcessor : FTPTargetProcessor { + private const string LITERAL_SPACE = "%20"; + private const string SPACE = " "; private Logger logger = LogManager.GetCurrentClassLogger(); private Source sourceConfig; Uri inputUri; @@ -29,7 +31,7 @@ namespace Maestro.Targets { try { sourceFTP = CreateClient(sourceConfig.Remote); - string input = inputUri.AbsolutePath.Replace("%20", " "); + string input = inputUri.AbsolutePath.Replace(LITERAL_SPACE, SPACE); long ilength = sourceFTP.GetFileSize(input); if (parameters.TargetConfig.NexioServer) ilength = ilength / 2; @@ -106,7 +108,7 @@ namespace Maestro.Targets { TerminateClient(sourceFTP); } - logger.Debug("Done"); + //logger.Debug("Done"); } } diff --git a/client/Maestro/Targets/UNCTargetProcessor.cs b/client/Maestro/Targets/UNCTargetProcessor.cs index f908c73b..6ee00b07 100644 --- a/client/Maestro/Targets/UNCTargetProcessor.cs +++ b/client/Maestro/Targets/UNCTargetProcessor.cs @@ -1,5 +1,4 @@ -using Maestro.Configuration; -using NLog; +using NLog; using System; using System.IO; using System.Windows.Forms; @@ -11,6 +10,7 @@ using Commons; using Maestro.Metadata; using System.Globalization; using System.Text.RegularExpressions; +using MaestroShared.Configuration; namespace Maestro.Targets { @@ -27,8 +27,28 @@ namespace Maestro.Targets { private const string METADATA_FILE = "{0}.json"; private const string DEFAULT_EXTENSION = ".MXF"; private const string NORMALIZE_TEXT_PATTERN = "[^0-9A-Za-z-._]"; - private const string NORMALIZE_TEXT_REPLACEMENT = "_"; - + protected const string UNDERSCORE = "_"; + protected const string HYPHEN = "-"; + private const string DATE_FORMAT = "yyyy.MM.dd"; + private const string PROGRAMME = "PROGRAMME"; + private const string COMMERCIAL = "COMMERCIAL"; + private const string JUNCTION = "JUNCTION"; + private const string XML_EXT = ".xml"; + private const string PATTERN_TARGETNAME = "%TARGETNAME%"; + protected const string PATTERN_TEXT = "%TEXT%"; + protected const string SLASH = "/"; + private const string INPROGRESS = "Folyamatban"; + private const string ERROR = "Hiba"; + private const string DOT = "."; + private const string HYPHENSTAR = "-*"; + private const string DATE_FORMAT_NODOTS = "yyyyMMdd"; + private const string READY = "Kész"; + private const string HYPHEN_SPACES = " - "; + private const string PATTERN_ID = "%ID%"; + private const string PATTERN_IDROOT = "%IDROOT%"; + private const string PATTERN_SOURCENAME = "%SOURCENAME%"; + private const string PATTERN_TIMESTAMP = "%TIMESTAMP%"; + private const string DATETIME_FORMAT = "yyyyMMddhhmmss"; protected FileInfo inputFile; protected string workingDir; protected TargetProcessorParameter parameters; @@ -42,7 +62,12 @@ namespace Maestro.Targets { Input = Path.Combine(parameters.SourceConfig.Local.Address.LocalPath, parameters.InputFileName); inputFile = new FileInfo(Input); ID = parameters.ID; - workFlowAction = new WorkflowAction() { houseId = ID, tag = parameters.TargetConfig.Tag, touched = DateTime.Now }; + workFlowAction = new WorkflowAction() { + houseId = ID, + tag = parameters.TargetConfig.Tag, + touched = DateTime.Now, + userName = TrayApplicationContext.UserName + }; Label = parameters.TargetConfig.Label; } @@ -64,7 +89,7 @@ namespace Maestro.Targets { Output = Output.Replace(@"\", "/"); workFlowAction.source = Input; workFlowAction.destination = Output; - Status = "Folyamatban"; + Status = INPROGRESS; //DateTime started = DateTime.Now; string currentFile = GetOutputFilePath(workingDir, OutputName); if (FileExists(currentFile) && parameters.TargetConfig.DisableFileVersioning) @@ -74,7 +99,7 @@ namespace Maestro.Targets { ExecuteCompleted(); } catch (Exception e) { - Status = "Hiba"; + Status = ERROR; Message = e.Message; workFlowAction.description = Message; logger.Error(e); @@ -96,8 +121,8 @@ namespace Maestro.Targets { if (String.IsNullOrEmpty(OutputName)) return null; string result = OutputName; - if (result.Contains(".")) - result = result.Substring(0, result.LastIndexOf(".")); + if (result.Contains(DOT)) + result = result.Substring(0, result.LastIndexOf(DOT)); return result; } @@ -121,7 +146,7 @@ namespace Maestro.Targets { if (parameters.TargetConfig.SendEmailOnSuccess && !String.IsNullOrEmpty(parameters.TargetConfig.SuccessEmailRecipient) && !String.IsNullOrEmpty(parameters.TargetConfig.SuccessEmailPattern)) SendEmail(parameters.TargetConfig.SuccessEmailRecipient, parameters.TargetConfig.SuccessEmailPattern); - Status = "Kész"; + Status = READY; Message = SUCCESS; } @@ -131,39 +156,39 @@ namespace Maestro.Targets { string description = parameters.ArchiveMetadata?.mediaTitle; if (parameters.TargetConfig.KillDateDays > 0) { DateTime date = GetKillDate(); - KillDate = date.ToString("yyyy.MM.dd"); + KillDate = date.ToString(DATE_FORMAT); xml = NexioXML.ToXML(name, date, description, parameters.TargetConfig.Agency); } else xml = NexioXML.ToXML(name, null, description, parameters.TargetConfig.Agency); byte[] content = Encoding.Unicode.GetBytes(xml); - UploadContent(name + ".xml", content); + UploadContent(name + XML_EXT, content); } private void UploadMorpheusMetadata() { string name = GetOutputName(); string title = parameters.ArchiveMetadata?.itemTitle; if (!title.Equals(parameters.ArchiveMetadata?.mediaTitle)) - title += " - " + parameters.ArchiveMetadata?.mediaTitle; + title += HYPHEN_SPACES + parameters.ArchiveMetadata?.mediaTitle; string type = null; switch (parameters.MetadataKind) { case MetadataType.TrafficMaterial: - type = "PROGRAMME"; + type = PROGRAMME; break; case MetadataType.TrafficPromo: - type = "COMMERCIAL"; + type = COMMERCIAL; break; case MetadataType.TrafficAD: - type = "JUNCTION"; + type = JUNCTION; break; } byte[] content = MorpheusXML.ToXML(title, name, parameters.TargetConfig.DeviceIDMorpheus, type, parameters.MovieSegments); EnsureDirectoryExistence(parameters.TargetConfig.PathMorpheusMetadata); - UploadContent(name + ".xml", content); + UploadContent(name + XML_EXT, content); } private void SendEmail(string to, string messagePattern) { try { - string message = messagePattern.Replace("%TARGETNAME%", OutputName); + string message = messagePattern.Replace(PATTERN_TARGETNAME, OutputName); parameters.MediaCubeApi.Notify(to, message); } catch (Exception e) { @@ -195,8 +220,8 @@ namespace Maestro.Targets { string result = null; if (!String.IsNullOrEmpty(parameters.TargetConfig.SubFolderFormat)) { string name = GetDynamicName(parameters.TargetConfig.SubFolderFormat); - if (parameters.TargetConfig.SubFolderFormat.Contains("%TEXT%")) { - string pattern = name.Split('-')[0] + "-*"; + if (parameters.TargetConfig.SubFolderFormat.Contains(PATTERN_TEXT)) { + string pattern = name.Split(HYPHEN[0])[0] + HYPHENSTAR; string[] dirs = Directory.GetDirectories(connection.Address.LocalPath, pattern); if (dirs.Length > 0) name = dirs[0]; @@ -218,11 +243,11 @@ namespace Maestro.Targets { string statusWorkDir = Path.Combine(workingDir, STATUS_FOLDER); EnsureDirectoryExistence(statusWorkDir); DateTime date = GetKillDate(); - string fileName = String.Format(KILLDATE_FILE, OutputName, date.ToString("yyyyMMdd")); - logger.Debug("Creating KILLDATE status file {0}", fileName); + string fileName = String.Format(KILLDATE_FILE, OutputName, date.ToString(DATE_FORMAT_NODOTS)); + //logger.Debug("Creating KILLDATE status file {0}", fileName); KillDatePath = GetOutputFilePath(statusWorkDir, fileName); UploadContent(KillDatePath, new byte[] { }); - KillDate = date.ToString("yyyy.MM.dd"); + KillDate = date.ToString(DATE_FORMAT); } private void CreateArchiveMetadata() { @@ -230,7 +255,7 @@ namespace Maestro.Targets { string statusWorkDir = Path.Combine(workingDir, STATUS_FOLDER); EnsureDirectoryExistence(statusWorkDir); string fileName = String.Format(METADATA_FILE, OutputName); - logger.Debug("Creating METADATA file {0}", fileName); + //logger.Debug("Creating METADATA file {0}", fileName); byte[] content = Encoding.UTF8.GetBytes(parameters.ArchiveMetadata.ToString()); string ouputPath = GetOutputFilePath(statusWorkDir, fileName); UploadContent(ouputPath, content); @@ -241,7 +266,7 @@ namespace Maestro.Targets { string statusWorkDir = Path.Combine(workingDir, STATUS_FOLDER); EnsureDirectoryExistence(statusWorkDir); string fileName = String.Format(METADATA_FILE, OutputName); - logger.Debug("Creating METADATA file {0}", fileName); + //logger.Debug("Creating METADATA file {0}", fileName); byte[] content = Encoding.UTF8.GetBytes(parameters.ArchiveMetadata.ToString()); string ouputPath = GetOutputFilePath(statusWorkDir, fileName); UploadContent(ouputPath, content); @@ -291,15 +316,15 @@ namespace Maestro.Targets { } protected string GetDynamicName(string text) { - string idRoot = ID.Contains(NORMALIZE_TEXT_REPLACEMENT) ? ID.Split(NORMALIZE_TEXT_REPLACEMENT[0])[0] : ID; + string idRoot = ID.Contains(UNDERSCORE) ? ID.Split(UNDERSCORE[0])[0] : ID; string result = text - .Replace("%ID%", ID) - .Replace("%IDROOT%", idRoot) - .Replace("%TEXT%", "-" + RemoveDiacritics(parameters.MetadataText)) - .Replace("%SOURCENAME%", Path.GetFileNameWithoutExtension(InputName)) - .Replace("%TIMESTAMP%", DateTime.Now.ToString("yyyyMMddhhmmss", CultureInfo.InvariantCulture)); + .Replace(PATTERN_ID, ID) + .Replace(PATTERN_IDROOT, idRoot) + .Replace(PATTERN_TEXT, HYPHEN + RemoveDiacritics(parameters.MetadataText)) + .Replace(PATTERN_SOURCENAME, Path.GetFileNameWithoutExtension(InputName)) + .Replace(PATTERN_TIMESTAMP, DateTime.Now.ToString(DATETIME_FORMAT, CultureInfo.InvariantCulture)); // replace hungarian and special characters - result = Regex.Replace(result, NORMALIZE_TEXT_PATTERN, NORMALIZE_TEXT_REPLACEMENT); + result = Regex.Replace(result, NORMALIZE_TEXT_PATTERN, UNDERSCORE); if (result.Length > 100) result = result.Substring(0, 100); return result; @@ -307,7 +332,7 @@ namespace Maestro.Targets { protected virtual string CreateOutputFileName() { string nameWithoutExtension = GetDynamicName(parameters.TargetConfig.OutputFormat); - string extension = InputName.Contains(".") ? InputName.Substring(InputName.LastIndexOf(".")) : DEFAULT_EXTENSION; + string extension = InputName.Contains(DOT) ? InputName.Substring(InputName.LastIndexOf(DOT)) : DEFAULT_EXTENSION; extension = extension.ToUpper(); string result = String.Format(FILENAME, nameWithoutExtension, extension); if (!parameters.TargetConfig.DisableFileVersioning) { -- 2.54.0