Пример #1
0
        public FtpFailed(FtpPending ep)
            : base()
        {
            _AttachmentFileList = new List<string>();

            this.ID = ep.ID;
            this.CompanyID = ep.CompanyID;
            this.UserID = ep.UserID;

            this.IPAddress = ep.IPAddress;
            this.Port = ep.Port;
            this.Username = ep.Username;
            this.Password = ep.Password;
            this.PSK = ep.PSK;
            this.AttachmentFiles = ep.AttachmentFiles;
            this.DestinationPath = ep.DestinationPath;
            this.TimeToSend = ep.TimeToSend;
            this.Retry = ep.Retry;
            this.Status = "Failed";
            this.LastRetryAt = ep.LastRetryAt;
            this.Priority = ep.Priority;
        }
        //& IM-3927
        private void CheckFailedFtps()
        {
            try
            {
                var response = GetFtpFailedDueList(new IDRequest(Guid.Empty));
                if (response.List != null && response.List.Count > 0)
                {
                    SaveListRequest<FtpPending> request = new SaveListRequest<FtpPending>();
                    request.List = new List<FtpPending>();
                    foreach (FtpFailed ff in response.List)
                    {
                        //put all due failed Ftps to pending
                        FtpPending fp = new FtpPending(ff);

                        request.List.Add(fp);
                        DeleteFtpFailed(new IDRequest(ff.ID));
                    }
                    SaveFtpPendingList(request);//save to db pending table
                }
            }
            catch (Exception e)
            {
                ErrorHandler.HandleInternal(e);
                _Log.Info("NOTIFYError - CheckFailedFtps - " + (e.InnerException != null ? e.InnerException.Message : e.Message));
            }
        }
        //& IM-3927
        private void SendFtpInternal(
			string companyID,
			string userID,
			string server,
			string port,
			string username,
			string password,
			string psk,
			string destinationPath,
			string attachments,
			byte priority)
        {
            string attachmentFiles = SaveAttachmentData(_AttachmentFolder, attachments);
            FtpPending fp = new FtpPending();

            fp.ID = SequentialGuid.NewDbGuid();
            fp.CompanyID = new Guid(companyID);
            fp.UserID = new Guid(userID);

            fp.IPAddress = server;
            fp.Port = (byte) Int32.Parse(port);
            fp.Username = username;
            fp.Password = password;
            fp.PSK = psk;
            fp.DestinationPath = destinationPath;
            fp.AttachmentFiles = attachmentFiles;
            fp.Priority = priority;
            SaveFtpPending(new SaveRequest<FtpPending>(fp));		//save to db as pending ftp
        }
        //& 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);
            }
        }