Exemple #1
0
        public FtpPending(FtpFailed ff)
            : base()
        {
            this.ID = ff.ID;
            this.CompanyID = ff.CompanyID;
            this.UserID = ff.UserID;

            this.IPAddress = ff.IPAddress;
            this.Port = ff.Port;
            this.Username = ff.Username;
            this.Password = ff.Password;
            this.PSK = ff.PSK;
            this.AttachmentFiles = ff.AttachmentFiles;
            this.DestinationPath = ff.DestinationPath;

            this.LastRetryAt = ff.LastRetryAt;
            this.Retry = ff.Retry;
            this.Status = "Pending";
            this.TimeToSend = ff.TimeToSend;

            this.Priority = ff.Priority;
        }
        //& IM-3927
        private void SendSingleFtp(FtpPending fp)
        {
            bool succeeded = false;
            if (fp == null) return;
            string destinationFilename = "";
            try
            {
                if (fp.Retry > 1) _Log.Debug("Retry ftp attachmentFiles: " + fp.AttachmentFiles);

                string destinationPath = "";

                if (fp.DestinationPath != null)
                {
                    // Ensure destination path is in Windows format i.e. "\" between folders
                    string validWindowsPath = fp.DestinationPath;
                    validWindowsPath = validWindowsPath.Replace("/","\\");

                    destinationPath = Path.GetDirectoryName(validWindowsPath);
                    if (destinationPath != "" && !destinationPath.StartsWith("C:") && !destinationPath.StartsWith("\\"))		//& IM-4227
                        destinationPath = "\\" + destinationPath;																//& IM-4227

                    destinationFilename = Path.GetFileName(validWindowsPath);
                }

                if (destinationFilename.Contains("YYMMDD_HHMMSS"))
                {
                    string tmp = destinationFilename.Replace("YYMMDD_HHMMSS", DateTime.UtcNow.ToString("yyyyMMdd_HHmmss"));		//# IM-4227
                    destinationFilename = tmp.Replace(":", "");
                }

                if (destinationFilename.EndsWith("YYYYMMDD")) //PP-206
                {
                    string tmp = destinationFilename.Replace("YYYYMMDD", DateTime.UtcNow.ToString("yyyyMMdd"));	//PP-206
                    destinationFilename = tmp.Replace(":", "");
                }

                if (destinationFilename != "")
                {
                    // User has a custom filename they want to use - so just add the appropriate extension from the source report name
                    destinationFilename += Path.GetExtension(fp.AttachmentFiles);		// use extension from report file generated e.g. CSV	//# IM-4227
                }
                else
                {
                    // use the default report name that the report writer assigned when creating the report
                    destinationFilename = Path.GetFileName(fp.AttachmentFiles);
                }

                // Unencrypt username and password	// TODO

                var sftp = new SftpClient(fp.IPAddress, fp.Port, fp.Username, fp.Password); //# PP-223 ---Added Port Number
                sftp.Connect();

                using (var file = File.OpenRead(fp.AttachmentFiles))
                {
                    if (destinationPath != "")
                    {
                        destinationPath = FormatPathForOSTalkingTo(destinationPath, sftp.WorkingDirectory);
                        sftp.ChangeDirectory(destinationPath);
                    }
                    sftp.UploadFile(file, destinationFilename);
                }

                sftp.Disconnect();
                succeeded = true;
            }
            catch (Exception ex)
            {
                var msg = string.Format("Ftp ID={0} file=[{1}] server=[{2}]: error {3} : Stack {4} destination :{5}",
                    fp.ID, Path.GetFileName(fp.AttachmentFiles).Truncate(30), fp.IPAddress, ex.Message, ex.StackTrace, destinationFilename);
                AttentionUtils.Attention(new Guid("63dd8220-d6a8-badd-8158-bed1aa10d130"), msg);
                _Log.Warn(msg);
                succeeded = false;
            }
            //if ftp'ed successfully, save to FtpSent and delete from FtpPending
            if (succeeded)
            {
                DeleteFtpPending(new IDRequest(fp.ID));
                var req = new SaveRequest<FtpSent>();
                var fs = new FtpSent(fp);
                fs.LastRetryAt = fp.DateModified;
                fs.Retry = fp.Retry + 1;
                fs.TimeToSend = DateTime.MaxValue;//never to send again
                req.Item = fs;
                SaveFtpSent(req);

            }
            //if failed, save to FtpFailed and delete from FtpPending
            else
            {
                DeleteFtpPending(new IDRequest(fp.ID));
                var request = new SaveRequest<FtpFailed>();
                var fs = new FtpFailed(fp);
                fs.LastRetryAt = fp.DateModified;
                if (!string.IsNullOrEmpty(fs.DestinationPath) && fs.Retry < _Retries.Length) //TODO check for path valid syntax
                {
                    fs.TimeToSend = DateTime.UtcNow.AddMinutes(_Retries[fs.Retry]);
                    fs.Retry++;
                }
                else
                {
                    fs.TimeToSend = DateTime.MaxValue; // don't send again
                    fs.Deleted = true;
                }
                request.Item = fs;
                SaveFtpFailed(request);
            }
        }