Beispiel #1
0
 public UserController(
     IUserRepository userRepo,
     ICommentRepository commentRepo,
     IArticleRepository articleRepo,
     ICategoryRepository categoryRepo,
     IConfigurationKeyRepository configRepo,
     IAccount accnt,
     IFtp ft,
     ITextBuilder txtBuilder,
     IImageModifier imgModifier,
     ISessionHelper sessionRepo,
     IImageHelper imgHelper)
 {
     repoUser               = userRepo;
     repoComment            = commentRepo;
     repoArticle            = articleRepo;
     repoCategory           = categoryRepo;
     repoConfig             = configRepo;
     account                = accnt;
     ftp                    = ft;
     textBuilder            = txtBuilder;
     imageModifier          = imgModifier;
     repoSession            = sessionRepo;
     imageHelper            = imgHelper;
     repoSession.Controller = this;
 }
        public static string DoWorkIgen(ILoggar logger, IFtp ftpService, string msgIn)
        {
            logger.Logga("Nu hände någonting");

            ftpService.GetFileAsStringValue("filnamn");

            logger.Logga("Nu tog det s**t");

            return msgIn + "tjoho";
        }
Beispiel #3
0
 public ImageHelper(
     IConfigurationKeyRepository configRepo,
     IImageModifier imgModifier,
     IFtp ft,
     ITextBuilder txtBuilder)
 {
     repoConfig    = configRepo;
     imageModifier = imgModifier;
     ftp           = ft;
     textBuilder   = txtBuilder;
 }
Beispiel #4
0
        protected override IEnumerable <CommandParameters> Execute(IEnumerable <CommandParameters> inParametersList)
        {
            foreach (var inParameters in inParametersList)
            {
                //inParameters = GetCurrentInParameters();
                string   host            = inParameters.GetValue <string>("Host");
                string   passWord        = inParameters.GetValue <string>("Password");
                string   user            = inParameters.GetValue <string>("User");
                string   remoteDirectory = inParameters.GetValueOrDefault <string>("RemoteDirectory", "\\*.*");
                string   localDirectory  = inParameters.GetValue <string>("LocalDirectory");
                FtpTypes ftpType         = inParameters.GetValue <FtpTypes>("FtpType");

                if (this.ftp == null)
                {
                    switch (ftpType)
                    {
                    case FtpTypes.FTP:
                        this.ftp = new Ftp();
                        break;

                    case FtpTypes.FTPS:
                        this.ftp = new FtpS();
                        break;

                    case FtpTypes.SFTP:
                        this.ftp = new SFtp();
                        break;
                    }
                }

                this.ftp.SetConnectionInfos(host, user, passWord);

                //this.ftp.SetConnectionInfos(this.ConnectionInfo.FtpServer, this.ConnectionInfo.UserName, this.ConnectionInfo.DecryptedPassword);

                this.LogDebugFormat("Start reading files from Host='{0}', RemoteDirectory='{1}'", host, remoteDirectory);

                int fileIdx = 0;
                foreach (var localFileName in this.ReadData(remoteDirectory, localDirectory))
                {
                    fileIdx++;

                    var outParameters = this.GetCurrentOutParameters();
                    outParameters.SetOrAddValue("File", localFileName);

                    yield return(outParameters);
                }

                this.LogDebugFormat("End reading files from Host='{0}', RemoteDirectory='{1}': FilesCount={2}", host,
                                    remoteDirectory, fileIdx);
            }
        }
Beispiel #5
0
        public async Task <XchangeFile> Handle(XchangeFile xchangeFile)
        {
            Rebex.Licensing.Key = Runner.StartupValueOf(CommonProperties.LicenseKey);

            switch (Runner.StartupValueOf(CommonProperties.Protocol).ToLower())
            {
            case "sftp":

                var sftp     = new Sftp();
                int sftpPort = string.IsNullOrWhiteSpace(Runner.StartupValueOf(CommonProperties.Port)) ? 22 : Convert.ToInt32(Runner.StartupValueOf(CommonProperties.Port));
                await sftp.ConnectAsync(Runner.StartupValueOf(CommonProperties.Host), sftpPort);

                ftpOrSftp = sftp;
                break;

            case "ftp":

                var ftp     = new Rebex.Net.Ftp();
                int ftpPort = string.IsNullOrWhiteSpace(Runner.StartupValueOf(CommonProperties.Port)) ? 21 : Convert.ToInt32(Runner.StartupValueOf(CommonProperties.Port));
                await ftp.ConnectAsync(Runner.StartupValueOf(CommonProperties.Host), ftpPort);

                ftpOrSftp = ftp;
                break;

            default:
                throw new ArgumentException($"Unknown protocol '{Runner.StartupValueOf(CommonProperties.Protocol)}'");
            }

            await ftpOrSftp.LoginAsync(Runner.StartupValueOf(CommonProperties.Username), Runner.StartupValueOf(CommonProperties.Password));

            using var stream = new MemoryStream(Encoding.UTF8.GetBytes(xchangeFile.Data));
            //Stream str = stream;

            var filename = xchangeFile.Filename;

            if (string.IsNullOrWhiteSpace(filename))
            {
                var currentDate = DateTime.UtcNow;
                filename = $"{currentDate.Year:0000}{currentDate.Month:00}{currentDate.Day:00}{currentDate.Hour:00}{currentDate.Minute:00}{currentDate.Second:00}{currentDate.Millisecond:000}";
            }

            //if (!string.IsNullOrWhiteSpace(Runner.StartupValueOf(CommonProperties.FileNamePrefix)))
            //    filename += Runner.StartupValueOf(CommonProperties.FileNamePrefix) + "_";
            //filename += Runner.StartupValueOf(CommonProperties.FileNamePrefix) + "_" + DateTime.UtcNow.Day + DateTime.UtcNow.Month + DateTime.UtcNow.Year + DateTime.UtcNow.Hour + DateTime.UtcNow.Minute + DateTime.UtcNow.Second + DateTime.UtcNow.Millisecond;

            await ftpOrSftp.PutFileAsync(stream, Runner.StartupValueOf($"{CommonProperties.TargetPath}/{filename}"));

            await ftpOrSftp.DisconnectAsync();

            return(new XchangeFile(string.Empty));
        }
Beispiel #6
0
        static void Main(string[] args)
        {
            InitLogging();

            if (args.Length != 1)
            {
                Logger.Fatal("No project number argument provided");
                Environment.Exit(1);
            }

            var projectConfigs = LoadProjectConfigs();

            var projectConfig = projectConfigs.Single(x => x.projectNumber.ToString() == args[0]);

            Logger.Info($"Project number: {args[0]}");

            Debug.Assert(projectConfig.sources.Count > 0);
            Logger.Info($"{projectConfig.sources.Count} FTP source(s) found");

            foreach (var source in projectConfig.sources)
            {
                // login to project ftp
                IFtp ftp = FtpFactory.Create(source.ftpType);
                ftp.Login();

                List <ICompany> companies = ftp.GetCompanyListing();
                ICompany        company   = companies.Where(x => x.Id == source.companyId).Single();
                Logger.Info($"Found company");
                Logger.Info($"Name: {company.Name}");
                Logger.Info($"ID: {company.Id}");

                List <IProject> projects = ftp.GetProjectListing(company);
                IProject        project  = projects.Where(x => x.Id == source.projectId).Single();
                Logger.Info($"Found project");
                Logger.Info($"Name: {project.Name}");
                Logger.Info($"ID: {project.Id}");


                // get mappings from projectConfig
                List <Mapping> fileMappings   = source.mappings.Where(x => x.mappingType == MappingType.FILE).ToList();
                List <Mapping> folderMappings = source.mappings.Where(x => x.mappingType == MappingType.FOLDER).ToList();

                Logger.Info($"{fileMappings.Count} file mappings");
                Logger.Info($"{folderMappings.Count} folder mappings");

                int filesAdded           = 0;
                int filesUpdated         = 0;
                int filesIgnored         = 0;
                int filesAlreadyUpToDate = 0;


                void UpdateFtpFile(IFile ftpFile)
                {
                    // need to re-query files in database to avoid duplicates
                    // because new records aren't set by Entity Framework
                    var internalFileQuery = from file in ais_dev.VDC_FILES
                                            where file.FILE_PROJECT_NUMBER == projectConfig.projectNumber &&
                                            file.FILE_EXTERNAL_NAME.ToUpper() == ftpFile.BaseName.ToUpper() && // string.Equals() case insensitive doesnt work here
                                            file.FILE_EXTENSION.ToUpper() == ftpFile.FileExt.ToUpper()
                                            select new {
                        file.FILE_ACTION,
                        file.FILE_EXTENSION,
                        file.FILE_EXTERNAL_NAME,
                        file.FILE_VERSION
                    };

                    var internalFiles = internalFileQuery.ToList();


                    if (internalFiles.Count > 1)
                    {
                        Logger.Error($"Duplicate external names found for {ftpFile.Name}");
                        throw new Exception($"Duplicate external names found for {ftpFile.Name}");
                    }


                    var internalFile = internalFiles.SingleOrDefault();

                    // if file not in database
                    if (internalFile == null)
                    {
                        Logger.Warn($"File not in database");

                        ftp.DownloadFile(ftpFile, projectConfig.downloadDirectory);

                        InsertNewInternalFileRecord(ftpFile, projectConfig);

                        filesAdded++;
                        Logger.Info("New file added to database");
                    }

                    // else if no local file found
                    else if (!File.Exists(Path.Combine(projectConfig.downloadDirectory, ftpFile.Name)))
                    {
                        Logger.Warn("No local file found, re-downloading");

                        ftp.DownloadFile(ftpFile, projectConfig.downloadDirectory);

                        UpdateInternalFileVersion(ftpFile, projectConfig.projectNumber);

                        filesUpdated++;
                        Logger.Info($"Re-downloaded file version {ftpFile.Version}");
                    }

                    // else if new version found on FTP
                    else if (ftpFile.Version > (internalFile.FILE_VERSION ?? 0))
                    {
                        Logger.Info("New version found");

                        ftp.DownloadFile(ftpFile, projectConfig.downloadDirectory);

                        UpdateInternalFileVersion(ftpFile, projectConfig.projectNumber);

                        filesUpdated++;
                        Logger.Info($"Updated internal file to version {ftpFile.Version}");
                    }

                    // else if internal file is more recent than FTP file (most likely duplicate filenames on FTP)
                    else if (ftpFile.Version < (internalFile.FILE_VERSION ?? 0))
                    {
                        filesIgnored++;
                        Logger.Warn("Internal file version greater than FTP file version, ignoring FTP file");
                    }

                    else
                    {
                        filesAlreadyUpToDate++;
                        Logger.Info("File up to date");
                    }
                }

                // check and download single file mappings and merged models (glue only)
                foreach (var fileMapping in fileMappings)
                {
                    Logger.Info($"Processing mapping");
                    Logger.Info($"Type: FILE");
                    Logger.Info($"ID: {fileMapping.id}");

                    IFile ftpFile = ftp.GetFileInfo(project, fileMapping.id);

                    Logger.Info($"Name: {ftpFile.Name}");

                    UpdateFtpFile(ftpFile);
                }


                // check and download files in folder mappings
                foreach (var folderMapping in folderMappings)
                {
                    Logger.Info($"Processing mapping");
                    Logger.Info($"Type: FOLDER");
                    Logger.Info($"ID: {folderMapping.id}");

                    IFolder onlineFolder = ftp.GetFolderInfo(project, folderMapping.id);

                    Logger.Info($"Name: {onlineFolder.Name}");

                    List <IFile> filesInFolder = onlineFolder.Contents.OfType <IFile>().ToList();
                    Logger.Info($"{filesInFolder.Count} files in folder");


                    foreach (IFile ftpFile in filesInFolder)
                    {
                        Logger.Info($"Processing mapping");
                        Logger.Info($"Type: FILE");
                        Logger.Info($"ID: {ftpFile.Id}");
                        Logger.Info($"Name: {ftpFile.Name}");

                        UpdateFtpFile(ftpFile);
                    }
                }


                // summary
                Logger.Info($"Processing of source complete");
                Logger.Info($"{filesAdded} files added");
                Logger.Info($"{filesUpdated} files updated");
                Logger.Info($"{filesIgnored} files ignored");
                Logger.Info($"{filesAlreadyUpToDate} files already up to date");
            }

            Logger.Info($"Processing of project complete");
        }