Пример #1
0
        private bool IsValidPath(string path)
        {
            if (path == null)
            {
                throw new ArgumentNullException(nameof(path));
            }

            var validName = new ValidName();

            return(validName.IsValidFtpPath(path));
        }
Пример #2
0
        public override ActionResult Check(ConversionProfile profile, Accounts accounts, CheckLevel checkLevel)
        {
            var actionResult = new ActionResult();

            if (!IsEnabled(profile))
            {
                return(actionResult);
            }

            var isFinal = checkLevel == CheckLevel.Job;

            var ftpAccount = accounts.GetFtpAccount(profile);

            if (ftpAccount == null)
            {
                Logger.Error($"The specified FTP account with ID '{profile.Ftp.AccountId}' is not configured.");
                actionResult.Add(ErrorCode.Ftp_NoAccount);

                return(actionResult);
            }

            if (string.IsNullOrEmpty(ftpAccount.Server))
            {
                Logger.Error("No FTP server specified.");
                actionResult.Add(ErrorCode.Ftp_NoServer);
            }

            if (string.IsNullOrEmpty(ftpAccount.UserName))
            {
                Logger.Error("No FTP username specified.");
                actionResult.Add(ErrorCode.Ftp_NoUser);
            }

            if (profile.AutoSave.Enabled && string.IsNullOrEmpty(ftpAccount.Password))
            {
                Logger.Error("Automatic saving without ftp password.");
                actionResult.Add(ErrorCode.Ftp_AutoSaveWithoutPassword);
            }

            if (!isFinal && TokenIdentifier.ContainsTokens(profile.Ftp.Directory))
            {
                return(actionResult);
            }

            if (!ValidName.IsValidFtpPath(profile.Ftp.Directory))
            {
                actionResult.Add(ErrorCode.FtpDirectory_InvalidFtpPath);
            }

            return(actionResult);
        }
Пример #3
0
        public override ActionResult Check(ConversionProfile profile, Accounts accounts, CheckLevel checkLevel)
        {
            var actionResult = new ActionResult();

            if (!IsEnabled(profile))
            {
                return(actionResult);
            }

            var isFinal = checkLevel == CheckLevel.Job;

            var ftpAccount = accounts.GetFtpAccount(profile);

            if (ftpAccount == null)
            {
                Logger.Error($"The specified FTP account with ID '{profile.Ftp.AccountId}' is not configured.");
                actionResult.Add(ErrorCode.Ftp_NoAccount);

                return(actionResult);
            }

            if (string.IsNullOrEmpty(ftpAccount.Server))
            {
                Logger.Error("No FTP server specified.");
                actionResult.Add(ErrorCode.Ftp_NoServer);
            }

            if (string.IsNullOrEmpty(ftpAccount.UserName))
            {
                Logger.Error("No FTP username specified.");
                actionResult.Add(ErrorCode.Ftp_NoUser);
            }

            if (ftpAccount.AuthenticationType == AuthenticationType.KeyFileAuthentication)
            {
                var pathUtilStatus = _pathUtil.IsValidRootedPathWithResponse(ftpAccount.PrivateKeyFile);
                switch (pathUtilStatus)
                {
                case PathUtilStatus.InvalidRootedPath:
                    return(new ActionResult(ErrorCode.FtpKeyFilePath_InvalidRootedPath));

                case PathUtilStatus.PathTooLongEx:
                    return(new ActionResult(ErrorCode.FtpKeyFilePath_PathTooLong));

                case PathUtilStatus.NotSupportedEx:
                    return(new ActionResult(ErrorCode.FtpKeyFilePath_InvalidRootedPath));

                case PathUtilStatus.ArgumentEx:
                    return(new ActionResult(ErrorCode.FtpKeyFilePath_IllegalCharacters));
                }

                if (!isFinal && ftpAccount.PrivateKeyFile.StartsWith(@"\\"))
                {
                    return(new ActionResult());
                }

                if (!_file.Exists(ftpAccount.PrivateKeyFile))
                {
                    Logger.Error("The private key file \"" + ftpAccount.PrivateKeyFile + "\" does not exist.");
                    return(new ActionResult(ErrorCode.FtpKeyFilePath_FileDoesNotExist));
                }
            }

            if (profile.AutoSave.Enabled && string.IsNullOrEmpty(ftpAccount.Password) || KeyFilePasswordIsRequired(ftpAccount))
            {
                Logger.Error("Automatic saving without ftp password.");
                actionResult.Add(ErrorCode.Ftp_AutoSaveWithoutPassword);
            }

            if (!isFinal && TokenIdentifier.ContainsTokens(profile.Ftp.Directory))
            {
                return(actionResult);
            }

            if (!ValidName.IsValidFtpPath(profile.Ftp.Directory))
            {
                actionResult.Add(ErrorCode.FtpDirectory_InvalidFtpPath);
            }

            return(actionResult);
        }
Пример #4
0
        protected override ActionResult DoActionProcessing(Job job)
        {
            var ftpAccount = job.Accounts.GetFtpAccount(job.Profile);

            Logger.Debug("Creating ftp connection.\r\nServer: " + ftpAccount.Server + "\r\nUsername: "******"Exception while login to ftp server: ");
                ftpClient.Disconnect();
                return(new ActionResult(ErrorCode.Ftp_LoginError));
            }

            var fullDirectory = job.TokenReplacer.ReplaceTokens(job.Profile.Ftp.Directory).Trim();

            if (!ValidName.IsValidFtpPath(fullDirectory))
            {
                Logger.Warn("Directory contains invalid characters \"" + fullDirectory + "\"");
                fullDirectory = ValidName.MakeValidFtpPath(fullDirectory);
            }

            Logger.Debug("Directory on ftp server: " + fullDirectory);

            try
            {
                if (!ftpClient.DirectoryExists(fullDirectory))
                {
                    ftpClient.CreateDirectory(fullDirectory);
                }
            }
            catch (Exception ex)
            {
                Logger.Error(ex, "Exception while setting directory on ftp server: ");
                ftpClient.Disconnect();
                return(new ActionResult(ErrorCode.Ftp_DirectoryError));
            }

            foreach (var file in job.OutputFiles)
            {
                var targetFile = PathSafe.GetFileName(file);
                targetFile = ValidName.MakeValidFtpPath(targetFile);
                if (job.Profile.Ftp.EnsureUniqueFilenames)
                {
                    Logger.Debug("Make unique filename for " + targetFile);
                    try
                    {
                        var uf = new UniqueFilenameForFtp(targetFile, ftpClient, _pathUtil);
                        targetFile = uf.CreateUniqueFileName();
                        Logger.Debug("-> The unique filename is \"" + targetFile + "\"");
                    }
                    catch (Exception ex)
                    {
                        Logger.Error(ex, "Exception while generating unique filename: ");
                        ftpClient.Disconnect();
                        return(new ActionResult(ErrorCode.Ftp_DirectoryReadError));
                    }
                }

                try
                {
                    ftpClient.UploadFile(file, fullDirectory + "/" + targetFile);
                }
                catch (Exception ex)
                {
                    Logger.Error(ex, "Exception while uploading the file \"" + file);
                    ftpClient.Disconnect();
                    return(new ActionResult(ErrorCode.Ftp_UploadError));
                }
            }

            ftpClient.Disconnect();
            return(new ActionResult());
        }
Пример #5
0
        protected override ActionResult DoActionProcessing(Job job)
        {
            var ftpAccount = job.Accounts.GetFtpAccount(job.Profile);

            Logger.Debug("Creating ftp connection.\r\nServer: " + ftpAccount.Server + "\r\nUsername: "******"Can not connect to the internet for login to ftp. Win32Exception Message:\r\n" + ex.Message);
                    ftpConnection.Close();
                    return(new ActionResult(ErrorCode.Ftp_ConnectionError));
                }
                if (ex.NativeErrorCode.Equals(12014))
                {
                    Logger.Error("Can not login to ftp because the password is incorrect. Win32Exception Message:\r\n" + ex.Message);
                    ftpConnection.Close();
                    return(new ActionResult(ErrorCode.PasswordAction_Login_Error));
                }

                Logger.Error("Win32Exception while login to ftp server:\r\n" + ex.Message);
                ftpConnection.Close();
                return(new ActionResult(ErrorCode.Ftp_LoginError));
            }
            catch (Exception ex)
            {
                Logger.Error("Exception while login to ftp server:\r\n" + ex.Message);
                ftpConnection.Close();
                return(new ActionResult(ErrorCode.Ftp_LoginError));
            }

            var fullDirectory = job.TokenReplacer.ReplaceTokens(job.Profile.Ftp.Directory).Trim();

            if (!ValidName.IsValidFtpPath(fullDirectory))
            {
                Logger.Warn("Directory contains invalid characters \"" + fullDirectory + "\"");
                fullDirectory = ValidName.MakeValidFtpPath(fullDirectory);
            }

            Logger.Debug("Directory on ftp server: " + fullDirectory);

            var directories = fullDirectory.Split(new[] { "/" }, StringSplitOptions.RemoveEmptyEntries);

            try
            {
                foreach (var directory in directories)
                {
                    if (!ftpConnection.DirectoryExists(directory))
                    {
                        Logger.Debug("Create folder: " + directory);
                        ftpConnection.CreateDirectory(directory);
                    }
                    Logger.Debug("Move to: " + directory);
                    ftpConnection.SetCurrentDirectory(directory);
                }
            }
            catch (Exception ex)
            {
                Logger.Error("Exception while setting directory on ftp server\r\n:" + ex.Message);
                ftpConnection.Close();
                return(new ActionResult(ErrorCode.Ftp_DirectoryError));
            }

            foreach (var file in job.OutputFiles)
            {
                var targetFile = Path.GetFileName(file);
                targetFile = ValidName.MakeValidFtpPath(targetFile);
                if (job.Profile.Ftp.EnsureUniqueFilenames)
                {
                    Logger.Debug("Make unique filename for " + targetFile);
                    try
                    {
                        var uf = new UniqueFilenameForFtp(targetFile, ftpConnection, _pathUtil);
                        targetFile = uf.CreateUniqueFileName();
                        Logger.Debug("-> The unique filename is \"" + targetFile + "\"");
                    }
                    catch (Exception ex)
                    {
                        Logger.Error("Exception while generating unique filename\r\n:" + ex.Message);
                        ftpConnection.Close();
                        return(new ActionResult(ErrorCode.Ftp_DirectoryReadError));
                    }
                }

                try
                {
                    ftpConnection.PutFile(file, targetFile);
                }
                catch (Exception ex)
                {
                    Logger.Error("Exception while uploading the file \"" + file + "\": \r\n" + ex.Message);
                    ftpConnection.Close();
                    return(new ActionResult(ErrorCode.Ftp_UploadError));
                }
            }

            ftpConnection.Close();
            return(new ActionResult());
        }