} // END METHOD /// <summary> /// ECGridOSDownload - Downloads Files using ECGridOS ParcelInBox() or ParcelInBoxEx() /// </summary> /// <param name="APIKey">string GUID</param> /// <param name="FileDir">string</param> /// <param name="ECGridIDFrom">integer</param> /// <param name="ECGridIDTo">integer</param> private static void ECGridOSDownload(string APIKey, string FileDir, int ECGridIDFrom, int ECGridIDTo) { // ECGRIDOS: Set up an reference for the Web Service this requires a Web Reference to https://ecgridos.net/v3.2/prod/ecgridos.asmx net.ecgridos.ECGridOSAPIv3 ecgridos = new net.ecgridos.ECGridOSAPIv3(); ecgridos.EnableDecompression = true; long ParcelID; // The FileInfo object holds all the info and payload of the downloaded file ECGridOS_API.FileInfo FileInfo = new ECGridOS_API.FileInfo(); ECGridOS_API.ParcelIDInfoCollection Parcels = new ECGridOS_API.ParcelIDInfoCollection(); // Add trailing slash onto directory if (!FileDir.Right(1).Equals(@"\")) { FileDir += "\\"; } ECGridOSConsole("WhoAmI(\"{0}\")", APIKey); ECGridOS_API.SessionInfo whoami = ecgridos.WhoAmI(APIKey); if (ECGridIDFrom > 0) { ECGridOSConsole("ParcelInBoxEx(\"{0}\",{1},{2},{3},{4})", APIKey, whoami.NetworkID, whoami.MailboxID, ECGridIDFrom, ECGridIDTo); Parcels = ecgridos.ParcelInBoxEx(APIKey, whoami.NetworkID, whoami.MailboxID, ECGridIDFrom, ECGridIDTo); } else { ECGridOSConsole("ParcelInBox(\"{0}\")", APIKey); Parcels = ecgridos.ParcelInBox(APIKey); } // END IF/ELSE // Try to download each Parcel from the Inbox foreach (ECGridOS_API.ParcelIDInfo Parcel in Parcels.ParcelIDInfoList) { Console.WriteLine("Downloading: {0} ParcelID: {1} Bytes: {2}", Parcel.FileName, Parcel.ParcelID, Parcel.ParcelBytes); ParcelID = Parcel.ParcelID; // Check the Size of this file and use GZip decompression if it is 100 KB or over if (Parcel.ParcelBytes <= 100000) { // ECGRIDOS: ParcelDownloadGZip() is used to download the File info & content, it returns the FileInfo object ECGridOSConsole("ParcelDownload(\"{0}\",{1})", APIKey, ParcelID); FileInfo = ecgridos.ParcelDownload(APIKey, ParcelID); } else { // ECGRIDOS: ParcelDownloadGZip() is used to download the File info & content, it returns the FileInfo object ECGridOSConsole("ParcelDownloadGZip(\"{0}\",{1})", APIKey, ParcelID); FileInfo = ecgridos.ParcelDownloadGZip(APIKey, ParcelID); } string errorMessage = ""; try { if (IsGzipCompressed(FileInfo.Content)) { byte[] decompressedContent = GzipDecompress(FileInfo.Content); // Write to File to Drive File.WriteAllBytes(Path.Combine(FileDir, FileInfo.FileName), decompressedContent); } else { // Write to File to Drive File.WriteAllBytes(Path.Combine(FileDir, FileInfo.FileName), FileInfo.Content); } ECGridOSConsole("ParcelDownloadConfirm(\"{0}\",{1})", APIKey, ParcelID); ecgridos.ParcelDownloadConfirm(APIKey, ParcelID); Console.WriteLine("Complete."); if (targetAnalytics && whoami.NetworkID == 506) { TargetConfirm(APIKey, Parcel); } } catch (SoapException SoapEx) { // There is good data in the InnerXML which can be parsed and used to processes specific exceptions errorMessage = ShowSoapError(SoapEx); returnValue = 5; } catch (Exception ex) { errorMessage = "ERROR: " + ex.ToString(); Console.WriteLine(errorMessage); returnValue = 2; } // Chck if there are any error messsages if (errorMessage.Length > 0) { SendError(ErrorContact, errorMessage); ECGridOSConsole("ParcelDownloadReset(\"{0}\",{1})", APIKey, ParcelID); try { // Reset the Download if error ecgridos.ParcelDownloadReset(APIKey, ParcelID); Console.WriteLine("error: download reset."); } catch (SoapException SoapEx) { errorMessage = ShowSoapError(SoapEx); } } // END IF } // END FOREACH } // END METHOD
/// <summary> /// Main - Calling Method for the CommandLine to Execute /// </summary> /// <param name="args">string[]</param> /// <returns>integer</returns> static int Main(string[] args) { #region Setup Console Window Console.Title = "ECGridOS Mailbox IO"; Console.WindowHeight = Console.WindowHeight = 60; Console.WindowWidth = Console.WindowWidth = 100; #endregion // Write to Console Window Product Name, Company, and Copyright Console.WriteLine("{0} {1:g}", assemblyProduct, DateTime.Now); Console.WriteLine("{0} {1}", assemblyCompany, assemblyCopyright); Console.WriteLine(""); // Check for valid argument counts if (args.Count() < 3 || args.Count() > 4) { #region SMTP Arguments if (args.Count() == 1) { if (args[0].ToLower().StartsWith("-smtpfromname")) { if (args[0].Contains(":")) { string smtpfromname = (args[0].Split(':'))[1]; SetAppSetting("SMTPFromName", smtpfromname); Console.WriteLine("Set SMTP From Name: {0}", ConfigurationManager.AppSettings["SMTPFromName"].ToString()); } } else if (args[0].ToLower().StartsWith("-smtpfrom")) { if (args[0].Contains(":")) { string smtpfrom = (args[0].Split(':'))[1]; SetAppSetting("SMTPFrom", smtpfrom); Console.WriteLine("Set SMTP From: {0}", ConfigurationManager.AppSettings["SMTPFrom"].ToString()); } } else if (args[0].ToLower().StartsWith("-smtphost")) { if (args[0].Contains(":")) { string smtphost = (args[0].Split(':'))[1]; SetAppSetting("SMTPHost", smtphost); Console.WriteLine("Set SMTP Host: {0}", ConfigurationManager.AppSettings["SMTPHost"].ToString()); } } else if (args[0].ToLower().StartsWith("-smtpport")) { if (args[0].Contains(":")) { string smtpport = (args[0].Split(':'))[1]; SetAppSetting("SMTPPort", smtpport); Console.WriteLine("Set SMTP Port: {0}", ConfigurationManager.AppSettings["SMTPPort"].ToString()); } } else if (args[0].ToLower().StartsWith("-smtpuser")) { if (args[0].Contains(":")) { string smtpuser = (args[0].Split(':'))[1]; SetAppSetting("SMTPUserName", smtpuser); Console.WriteLine("Set SMTP User: {0}", ConfigurationManager.AppSettings["SMTPUserName"].ToString()); } } else if (args[0].ToLower().StartsWith("-smtppassword")) { if (args[0].Contains(":")) { string smtppassword = (args[0].Split(':'))[1]; SetAppSetting("SMTPPassword", smtppassword); Console.WriteLine("Set SMTP Password: {0}", ConfigurationManager.AppSettings["SMTPPassword"].ToString()); } } else if (args[0].ToLower().StartsWith("-smtpssl")) { if (args[0].Contains(":")) { string smtppassword = (args[0].Split(':'))[1]; SetAppSetting("SMTPSSL", smtppassword); Console.WriteLine("Set SMTP SSL: {0}", ConfigurationManager.AppSettings["SMTPSSL"].ToString()); } } else if (args[0].ToLower().StartsWith("-smtpauth")) { if (args[0].Contains(":")) { string smtpauth = (args[0].Split(':'))[1]; SetAppSetting("SMTPAuth", smtpauth); Console.WriteLine("Set SMTP Auth: {0}", ConfigurationManager.AppSettings["SMTPAuth"].ToString()); } } else if (args[0].ToLower().StartsWith("-timeout")) { if (args[0].Contains(":")) { string smtpauth = (args[0].Split(':'))[1]; SetAppSetting("TimeOut", smtpauth); Console.WriteLine("Set Time Out: {0}", ConfigurationManager.AppSettings["TimeOut"].ToString()); } } else if (args[0].ToLower().StartsWith("-smtpconfig")) { // Display the current -smtp configuration SMTPConfig(); returnValue = 1; } else if (args[0].ToLower().StartsWith("-smtphelp")) { // Display the smtp syntax usage SMTPSyntax(); returnValue = 1; } } else { // Invalid argument count displays argument definition and returns true/1 Syntax(); returnValue = 1; } #endregion } else { // Parse arguments into variables string APIKey = args[0]; if (!IsValidGUID(APIKey)) { throw new Exception("Invalid GUID Format."); } string FileDir = args[1]; bool Upload = false; int ECGridIDFrom = -1; int ECGridIDTo = -1; string errorMessage = ""; #region Arguments // Check the Arguments for options for (int i = 2; i <= args.Count() - 1; i++) { string argument = args[i]; argument = argument.Replace("–", "-"); // Replace em-dash if (argument.ToLower().StartsWith("-upload")) { // Get the optional ECGridIDs for an upload Upload = true; if (argument.Contains(":")) { if (argument.Contains(",")) { string ids = (argument.Split(':'))[1]; ECGridIDFrom = Convert.ToInt32((ids.Split(','))[0]); ECGridIDTo = Convert.ToInt32((ids.Split(','))[1]); } else { // This is a fix for old code format - allows old : separator ECGridIDFrom = Convert.ToInt32((argument.Split(':'))[1]); ECGridIDTo = Convert.ToInt32((argument.Split(':'))[2]); } } } else if (argument.ToLower().StartsWith("-download")) { // Get the optional ECGridIDs for a download if (argument.Contains(":")) { if (argument.Contains(",")) { string ids = (argument.Split(':'))[1]; ECGridIDFrom = Convert.ToInt32((ids.Split(','))[0]); ECGridIDTo = Convert.ToInt32((ids.Split(','))[1]); } else { // This is a fix for old code format - allows old : separator ECGridIDFrom = Convert.ToInt32((argument.Split(':'))[1]); ECGridIDTo = Convert.ToInt32((argument.Split(':'))[2]); } } } else if (argument.ToLower().StartsWith("-e:")) { // Get the Error Contact Email ErrorContact = argument.Substring(3).Trim(); } } // END FOR #endregion // Try the API Calls and catch and exceptions try { using (net.ecgridos.ECGridOSAPIv3 ecgridos = new net.ecgridos.ECGridOSAPIv3()) { Console.WriteLine("ECGridOS {0}{1}", ecgridos.Version(), Environment.NewLine); } if (Upload) { ECGridOSUpload(APIKey, FileDir, ECGridIDFrom, ECGridIDTo); } else { ECGridOSDownload(APIKey, FileDir, ECGridIDFrom, ECGridIDTo); } } catch (SoapException SoapEx) { //There is good data in the InnerXML which can be parsed and used to processes specific exceptions errorMessage = ShowSoapError(SoapEx); } catch (Exception ex) { if (returnValue == 0) { returnValue = 2; } errorMessage = String.Format("ERROR: {0} {1}{1} {2}", ex.Message, Environment.NewLine, ex.StackTrace); Console.WriteLine(ex.Message); } // Send the Email to the Error contact if there are Errors and a Error Contact Set if (errorMessage.Length > 0 && !String.IsNullOrEmpty(ErrorContact)) { SendError(ErrorContact, errorMessage); } } // END IF/ELSE return(returnValue); } // END MAIN
/// <summary> /// ECGridOSUpload - Uploads a File using ECGridOS ParcelUpload()/ParcelUploadGZip() or ParcelUploadDirected()/ParcelUploadDirectedGZip() /// </summary> /// <param name="APIKey">string GUID</param> /// <param name="FileDir">string</param> /// <param name="ECGridIDFrom">integer</param> /// <param name="ECGridIDTo">integer</param> private static void ECGridOSUpload(string APIKey, string FileDir, int ECGridIDFrom, int ECGridIDTo) { // Declare variables IReadOnlyCollection <string> files; int ParcelID; // Check if this File or Directory Exists if (File.Exists(FileDir) || Directory.Exists(FileDir)) { // Find out if this is a file or directory FileAttributes attributes = File.GetAttributes(FileDir); if ((attributes & FileAttributes.Directory) == FileAttributes.Directory) { // This is a directory files = Directory.GetFiles(FileDir, "*.*", SearchOption.TopDirectoryOnly); } else { // This is a single file files = Directory.GetFiles(Path.GetDirectoryName(FileDir), Path.GetFileName(FileDir), SearchOption.TopDirectoryOnly); } } else { returnValue = 2; throw new IOException("File/Path not found"); } // Check if there are ECGridIDs and Get the SessionInfo if True bool isDirected = false; ECGridOS_API.SessionInfo whoami = new ECGridOS_API.SessionInfo(); if (ECGridIDFrom > 0) { // ECGRIDOS: Set up an reference for the Web Service this requires a Web Reference to https://ecgridos.net/v3.2/prod/ecgridos.asmx using (net.ecgridos.ECGridOSAPIv3 ecgridos = new net.ecgridos.ECGridOSAPIv3()) { ecgridos.Timeout = Convert.ToInt32(ConfigurationManager.AppSettings["TimeOut"].ToString()); // ECGRIDOS: ParcelUploadDirected() posts a file to ECGrid wrapped with specified From and To IDs. // The ParcelID can be used as a handle later to get more information about the specific files as it transits the system. ECGridOSConsole("WhoAmI(\"{0}\")", APIKey); whoami = ecgridos.WhoAmI(APIKey); isDirected = true; } } // Upload File(s) foreach (string fileName in files) { //Load the entire file into a string buffer byte[] buffer = File.ReadAllBytes(fileName); // Check that the file is not empty if (buffer.Length <= 0) { Console.WriteLine("Deleting Empty File: {0} ....", fileName); File.Delete(fileName); continue; } Console.WriteLine("Uploading: {0} ....", fileName); if (isDirected) { // ECGRIDOS: Set up an reference for the Web Service this requires a Web Reference to https://ecgridos.net/v3.2/prod/ecgridos.asmx using (net.ecgridos.ECGridOSAPIv3 ecgridos = new net.ecgridos.ECGridOSAPIv3()) { ecgridos.Timeout = Convert.ToInt32(ConfigurationManager.AppSettings["TimeOut"].ToString()); ecgridos.EnableDecompression = true; // Check the Size of this file and use GZip compression if it is 1 MB or over /* * if (buffer.Length >= 100000) * { * buffer = GzipCompress(buffer); * Console.WriteLine("GZip Compressing File: {0} ....", fileName); * * // ECGRIDOS: ParcelUploadDirectedGZip() posts a GZip file to ECGrid * // The ParcelID can be used as a handle later to get more information about the specific files as it transits the system. * ECGridOSConsole("ParcelUploadDirectedGZip(\"{0}\",{1},{2},\"{3}\",{4},{5},{6},{7})", APIKey, whoami.NetworkID, whoami.MailboxID, Path.GetFileName(fileName), buffer.Length, "byte[]", ECGridIDFrom, ECGridIDTo); * ParcelID = ecgridos.ParcelUploadDirectedGZip(APIKey, whoami.NetworkID, whoami.MailboxID, Path.GetFileName(fileName), buffer.Length, buffer, ECGridIDFrom, ECGridIDTo); * } * else * { */ // ECGRIDOS: ParcelUploadDirected() posts a GZip file to ECGrid // The ParcelID can be used as a handle later to get more information about the specific files as it transits the system. ECGridOSConsole("ParcelUploadDirected(\"{0}\",{1},{2},\"{3}\",{4},{5},{6},{7})", APIKey, whoami.NetworkID, whoami.MailboxID, Path.GetFileName(fileName), buffer.Length, "byte[]", ECGridIDFrom, ECGridIDTo); ParcelID = ecgridos.ParcelUploadDirected(APIKey, whoami.NetworkID, whoami.MailboxID, Path.GetFileName(fileName), buffer.Length, buffer, ECGridIDFrom, ECGridIDTo); //} } } else { // ECGRIDOS: Set up an reference for the Web Service this requires a Web Reference to https://ecgridos.net/v3.2/prod/ecgridos.asmx using (net.ecgridos.ECGridOSAPIv3 ecgridos = new net.ecgridos.ECGridOSAPIv3()) { ecgridos.Timeout = Convert.ToInt32(ConfigurationManager.AppSettings["TimeOut"].ToString()); ecgridos.EnableDecompression = true; // Check the Size of this file and use GZip compression if it is 1 MB or over if (buffer.Length >= 1000000) { buffer = GzipCompress(buffer); Console.WriteLine("GZip Compressing File: {0} ....", fileName); // ECGRIDOS: ParcelUploadGZip() posts a GZip EDI file to ECGrid // The ParcelID can be used as a handle later to get more information about the specific files as it transits the system. ECGridOSConsole("ParcelUploadGZip(\"{0}\",\"{1}\",{2},{3})", APIKey, Path.GetFileName(fileName), buffer.Length, "byte[]"); ParcelID = ecgridos.ParcelUploadGZip(APIKey, Path.GetFileName(fileName), buffer.Length, buffer); } else { // ECGRIDOS: ParcelUpload() posts a EDI file to ECGrid // The ParcelID can be used as a handle later to get more information about the specific files as it transits the system. ECGridOSConsole("ParcelUpload(\"{0}\",\"{1}\",{2},{3})", APIKey, Path.GetFileName(fileName), buffer.Length, "byte[]"); ParcelID = ecgridos.ParcelUpload(APIKey, Path.GetFileName(fileName), buffer.Length, buffer); } } } // END IF/ELSE Console.WriteLine("ParcelID = {0}", ParcelID); // Delete the file after a successful upload. // Note that the Try/Catch from the calling routine will handle any errors and not delete the file if it doesn't upload File.Delete(fileName); } // END FOREACH(File) } // END METHOD