コード例 #1
0
        /// <summary>
        /// Sync
        /// </summary>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public override Task <IEnumerable <FolderItemSyncResult> > SyncFolderAsync(CancellationToken cancellationToken)
        {
            var fileInfos = JsonConvert.DeserializeObject <Dictionary <string, FolderItemSyncResult> >(File.Exists(SyncFilePath) ? File.ReadAllText(SyncFilePath) : "{}");

            var files = Directory.GetFiles(Folder, "*", SearchOption.AllDirectories);

            var blobSyncResults = new List <FolderItemSyncResult>();

            foreach (var file in files)
            {
                var result = new FolderItemSyncResult()
                {
                    Path = file
                };
                var path         = Path.Combine(Folder, file);
                var lastModified = File.GetLastWriteTimeUtc(path);
                if (!fileInfos.TryGetValue(file, out var fileInfo))
                {
                    fileInfo = new FolderItemSyncResult()
                    {
                        LastModified = DateTimeOffset.MinValue,
                        Result       = FolderItemSyncResultEnum.Skip,
                        Path         = file
                    };
                }

                if (lastModified > fileInfo.LastModified)
                {
                    fileInfo.LastModified = lastModified;
                    fileInfos[path]       = fileInfo;
                    result.Result         = FolderItemSyncResultEnum.UpdateSuccess;
                }
                else
                {
                    result.Result = FolderItemSyncResultEnum.Skip;
                }
                result.LastModified = lastModified;
                blobSyncResults.Add(result);
            }
            var newFileInfos = blobSyncResults.ToDictionary(x => x.Path, y => y);

            File.WriteAllText(SyncFilePath, JsonConvert.SerializeObject(newFileInfos));
            return(Task.FromResult <IEnumerable <FolderItemSyncResult> >(blobSyncResults));
        }
コード例 #2
0
        /// <summary>
        /// Sync
        /// </summary>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public override Task <IEnumerable <FolderItemSyncResult> > SyncFolderAsync(CancellationToken cancellationToken)
        {
            var files = LocalFileUtils.ListFiles(SourceFolder);

            var blobSyncResults = new List <FolderItemSyncResult>();

            foreach (var file in files)
            {
                var result = new FolderItemSyncResult()
                {
                    Path = file
                };
                try
                {
                    var sourcePath         = Path.Combine(SourceFolder, file);
                    var sourceLastModified = File.GetLastWriteTimeUtc(sourcePath);

                    var targetPath = Path.Combine(TargetFolder, file);
                    if (!File.Exists(targetPath))
                    {
                        Directory.CreateDirectory(Path.GetDirectoryName(targetPath));
                    }
                    var targetLastModified = File.GetLastWriteTimeUtc(targetPath);

                    result.LastModified = sourceLastModified > targetLastModified ? sourceLastModified : targetLastModified;
                    if (sourceLastModified > targetLastModified)
                    {
                        File.Copy(sourcePath, targetPath, true);
                        result.Result = FolderItemSyncResultEnum.UpdateSuccess;
                    }
                    else
                    {
                        result.Result = FolderItemSyncResultEnum.Skip;
                    }
                    blobSyncResults.Add(result);
                }
                catch (Exception ex)
                {
                    result.Result = FolderItemSyncResultEnum.UpdateFailure;
                    result.Ex     = ex;
                }
            }
            return(Task.FromResult <IEnumerable <FolderItemSyncResult> >(blobSyncResults));
        }