Example #1
0
        private static void SyncFilesToCloud(CloudBlobContainer container, IFtpClient client, string directoryFullPath,
                                             DateTime beginTime)
        {
            var remoteFiles = client.GetListing(directoryFullPath).Where(p => p.Modified >= beginTime).ToList();

            foreach (var remoteFile in remoteFiles)
            {
                if (remoteFile.Size <= 0)
                {
                    continue;
                }

                if (!client.Download(out var buffer, remoteFile.FullName))
                {
                    continue;
                }

                var blockBlob = container.GetBlockBlobReference(remoteFile.FullName.Substring(1));
                if (blockBlob.Exists())
                {
                    blockBlob.FetchAttributes();
                    if (blockBlob.Properties.Length == remoteFile.Size)
                    {
                        Console.WriteLine($"File {remoteFile.FullName} exists on cloud.");
                    }
                    continue;
                }
                blockBlob.UploadFromByteArray(buffer, 0, buffer.Length);
                Console.WriteLine($"Upload {remoteFile.FullName} to cloud.");
            }
        }
Example #2
0
        private static IEnumerable <string> DownloadFiles(string localPath, IFtpClient client, string directoryFullPath,
                                                          DateTime beginTime)
        {
            if (!Directory.Exists(localPath))
            {
                Directory.CreateDirectory(localPath);
            }
            var localFiles = new DirectoryInfo(localPath).GetFiles("*.*");

            var remoteFiles    = client.GetListing(directoryFullPath).Where(p => p.Modified >= beginTime);
            var downloads      = new List <string>();
            var localFullPaths = new List <string>();

            foreach (var remoteFile in remoteFiles)
            {
                if (remoteFile.Size <= 0)
                {
                    continue;
                }
                if (localFiles.Any(p => p.Name == remoteFile.Name))
                {
                    var localFile = localFiles.Single(p => p.Name == remoteFile.Name);
                    if (localFile.Length == remoteFile.Size)
                    {
                        continue;
                    }
                }
                downloads.Add(remoteFile.FullName);
                localFullPaths.Add(Path.Combine(localPath, remoteFile.Name));
            }
            client.DownloadFiles(localPath, downloads);
            Console.WriteLine($"Download {downloads.Count} records from {directoryFullPath}...");
            return(localFullPaths);
        }
Example #3
0
        private void SyncDirectory(IFtpClient client, string remotePath, string localPath, bool isDel)
        {
            var localFolder = new DirectoryInfo(localPath);
            var infos       = localFolder.GetFileSystemInfos();

            foreach (var info in infos)
            {
                if (!client.IsConnected)
                {
                    client.Connect();
                }
                if (info is FileInfo)
                {
                    var size = (info as FileInfo).Length;

                    var remoteFile = Path.Combine(remotePath, info.Name);

                    if (!client.FileExists(remoteFile) || client.GetFileSize(remoteFile) != size)
                    {
                        client.UploadFile(info.FullName, remoteFile);

                        Logger.Info($"Uploaded==>{info.FullName}");
                    }
                }
                else if (info is DirectoryInfo)
                {
                    var remoteFile = Path.Combine(remotePath, info.Name);

                    if (!client.DirectoryExists(remoteFile))
                    {
                        client.CreateDirectory(remoteFile);

                        Logger.Info($"CreateFtpDirectory==>{remoteFile}");
                    }
                    SyncDirectory(client, Path.Combine(remotePath, info.Name), info.FullName, isDel);
                }
            }

            if (isDel)
            {
                var items = client.GetListing(remotePath);
                foreach (var item in items)
                {
                    if (infos.All(info => info.Name != item.Name))
                    {
                        if (item.Type == FtpFileSystemObjectType.File)
                        {
                            client.DeleteFile(item.FullName);
                        }
                        else if (item.Type == FtpFileSystemObjectType.Directory)
                        {
                            client.DeleteDirectory(item.FullName);
                        }

                        Logger.Info($"DeletedFtp==>{item.FullName}");
                    }
                }
            }
        }
Example #4
0
        private void BackupDirectory(IFtpClient client, string remotePath, string localPath, bool isDel)
        {
            var items = client.GetListing(remotePath);

            foreach (var item in items)
            {
                if (!client.IsConnected)
                {
                    client.Connect();
                }
                if (item.Type == FtpFileSystemObjectType.File)
                {
                    var size = client.GetFileSize(item.FullName);

                    var localFile = Path.Combine(localPath, item.Name);

                    if (!File.Exists(localFile) || new FileInfo(localFile).Length != size)
                    {
                        client.DownloadFile(localFile, item.FullName);

                        Logger.Info($"Downloaded==>{item.FullName}");
                    }
                }
                else if (item.Type == FtpFileSystemObjectType.Directory)
                {
                    if (!Directory.Exists(item.FullName))
                    {
                        Directory.CreateDirectory(item.FullName);

                        Logger.Info($"CreateDirectory==>{item.FullName}");
                    }
                    BackupDirectory(client, item.FullName, Path.Combine(localPath, item.Name), isDel);
                }
            }

            if (isDel)
            {
                var localFolder = new DirectoryInfo(localPath);
                var infos       = localFolder.GetFileSystemInfos();
                foreach (var info in infos)
                {
                    if (items.All(item => item.Name != info.Name))
                    {
                        if (info is DirectoryInfo)
                        {
                            (info as DirectoryInfo).Delete(true);
                        }
                        else
                        {
                            info.Delete();
                        }

                        Logger.Info($"Deleted==>{info.FullName}");
                    }
                }
            }
        }
Example #5
0
        /// <summary>
        /// Формирование списка файлов на ftp
        /// </summary>
        private FtpListItem[] GetFtpItemsList(string topDirectory, FtpListOption options)
        {
            List <FtpListItem> files = new List <FtpListItem>();

            FtpListItem[] items = ftpClient.GetListing(topDirectory);

            foreach (FtpListItem item in items)
            {
                if (item.Type != FtpFileSystemObjectType.Directory)
                {
                    files.Add(item);
                }
                else
                if (options == FtpListOption.Recursive)
                {
                    if (!item.Name.IsMatch(@"^\.+$"))
                    {
                        files.AddRange(GetFtpItemsList(topDirectory + Path.AltDirectorySeparatorChar + item.Name, options));
                    }
                }
            }

            return(files.ToArray());
        }
Example #6
0
        private IEnumerable <string> getSongListings(IFtpClient client, string path)
        {
            //check if user wants to cancel at this point
            if (backgroundWorker1.CancellationPending || CancelProcess)
            {
                yield break;
            }

            var listings = new List <FtpListItem>();
            var failed   = false;

            if (client.IsConnected)
            {
                for (var attempts = 0; attempts < (int)numTries.Value; attempts++)
                {
                    try
                    {
                        client.SetWorkingDirectory(path);
                        listings = client.GetListing(path).ToList();
                        failed   = false;
                        break;
                    }
                    catch
                    {
                        failed = true;
                    }
                    Thread.Sleep((int)numWait.Value);
                }
            }

            if (!client.IsConnected)
            {
                if (!ConnectToFTP(client))
                {
                    yield break;
                }
            }
            if (failed)
            {
                Log("Failed to read directory " + Path.GetDirectoryName(path));
                yield break;
            }

            if (listings.Exists(x => x.Name == "songs.dta"))
            {
                if (!client.IsConnected)
                {
                    yield break;
                }
                var item = listings.Find(x => x.Name == "songs.dta");
                yield return(item.FullName);
            }
            else if (listings.Exists(x => x.Type == FtpFileSystemObjectType.Directory))
            {
                var items = listings.FindAll(x => x.Type == FtpFileSystemObjectType.Directory);
                foreach (var listing in items.TakeWhile(listing => client.IsConnected).SelectMany(item => getSongListings(client, item.FullName)))
                {
                    yield return(listing);
                }
            }
        }