private SyncResult CopyFiles(string srcRoot, HttpClient client, SyncDirDirectory nodeDir) { var message = new HttpRequestMessage(); var content = new MultipartSyncDirContent(); var clientDir = new DirectoryInfo(srcRoot); Logger.Verbose("Diffing server file structure with client..."); var diffs = nodeDir.Diff(clientDir); var files = diffs.MissingAndChangedPaths; foreach (var file in files) { if (file.IsDirectory) { continue; } Logger.Verbose(string.Format("File {0} is missing or changed on server. Adding to upload queue.", file.RelativePath)); var fileInfo = new FileInfo(file.Path); var fileStream = new FileStream(fileInfo.FullName, FileMode.Open, FileAccess.Read, FileShare.Read); content.Add(new StreamContent(fileStream), "file", file.RelativePath, fileInfo.Attributes, fileInfo.LastWriteTimeUtc); } var postProcContent = new SyncPostProcContent { DeletedFiles = diffs.DeletedFiles, DeletedDirectories = diffs.DeletedDirectories, ChangedDirectories = diffs.ChangedDirectories }; content.Add(new ObjectContent <SyncPostProcContent>(postProcContent, new JsonMediaTypeFormatter())); var link = nodeDir.Links.GetByRel("http://www.con-dep.net/rels/sync/directory"); Logger.Verbose(string.Format("Using url {0} to {1} missing or changed files.", link.Href, link.HttpMethod)); message.Method = link.HttpMethod; message.Content = content; message.RequestUri = new Uri(link.Href); Logger.Verbose("Copying files now..."); var result = client.SendAsync(message) .ContinueWith(task => { if (task.Result.IsSuccessStatusCode) { var syncResult = task.Result.Content.ReadAsAsync <SyncResult>().Result; return(syncResult); } return(null); }); result.Wait(); return(result.Result); }
public SyncDirDirectory GetSubDirInfo(string rootPath, string dirUrl, string fileUrl, DirectoryInfo dirInfo) { var dir = new SyncDirDirectory() { FullPath = dirInfo.FullName, RelativePath = MakePathRelative(rootPath, dirInfo.FullName), Attributes = dirInfo.Attributes.ToString() }; var link = string.Format(dirUrl, dirInfo.FullName); if (!dirInfo.Exists) { dir.Links.Add(new Link() { Rel = ApiRels.Directory, Method = "POST", Href = string.Format("{0}", link) }); return(dir); } dir.Links.Add(new Link() { Rel = ApiRels.Self, Href = string.Format("{0}", link), Method = "GET" }); dir.Links.Add(new Link() { Rel = ApiRels.Directory, Method = "PUT", Href = string.Format("{0}", link) }); foreach (var childDir in dirInfo.EnumerateDirectories()) { dir.Directories.Add(GetSubDirInfo(rootPath, dirUrl, fileUrl, childDir)); } foreach (var childFile in dirInfo.EnumerateFiles()) { //var secDescriptor = childFile.GetAccessControl().GetSecurityDescriptorSddlForm(AccessControlSections.); var file = new SyncDirFile() { FullPath = childFile.FullName, RelativePath = MakePathRelative(rootPath, childFile.FullName), Attributes = childFile.Attributes.ToString(), LastWriteTimeUtc = childFile.LastWriteTimeUtc, Size = childFile.Length, }; var fileLink = string.Format(fileUrl, childFile.FullName); file.Links.Add(new Link { Href = string.Format("{0}", fileLink), Rel = ApiRels.Self, Method = "GET" }); file.Links.Add(new Link { Href = string.Format("{0}", fileLink), Rel = ApiRels.File, Method = "PUT" }); file.Links.Add(new Link { Href = string.Format("{0}", link), Rel = ApiRels.Directory, Method = "PUT" }); dir.Files.Add(file); } return(dir); }
private SyncResult CopyFiles(string srcRoot, HttpClient client, SyncDirDirectory nodeDir) { var message = new HttpRequestMessage(); var content = new MultipartSyncDirContent(); var clientDir = new DirectoryInfo(srcRoot); Logger.Verbose("Diffing server file structure with client..."); var diffs = nodeDir.Diff(clientDir); var files = diffs.MissingAndChangedPaths; foreach (var file in files) { if (file.IsDirectory) continue; Logger.Verbose(string.Format("File {0} is missing or changed on server. Adding to upload queue.", file.RelativePath)); var fileInfo = new FileInfo(file.Path); var fileStream = new FileStream(fileInfo.FullName, FileMode.Open, FileAccess.Read, FileShare.Read); content.Add(new StreamContent(fileStream), "file", file.RelativePath, fileInfo.Attributes, fileInfo.LastWriteTimeUtc); } var postProcContent = new SyncPostProcContent { DeletedFiles = diffs.DeletedFiles, DeletedDirectories = diffs.DeletedDirectories, ChangedDirectories = diffs.ChangedDirectories }; content.Add(new ObjectContent<SyncPostProcContent>(postProcContent, new JsonMediaTypeFormatter())); var link = nodeDir.Links.GetByRel("http://www.con-dep.net/rels/sync/directory"); Logger.Verbose(string.Format("Using url {0} to {1} missing or changed files.", link.Href, link.HttpMethod)); message.Method = link.HttpMethod; message.Content = content; message.RequestUri = new Uri(link.Href); Logger.Verbose("Copying files now..."); var result = client.SendAsync(message) .ContinueWith(task => { if (task.Result.IsSuccessStatusCode) { var syncResult = task.Result.Content.ReadAsAsync<SyncResult>().Result; return syncResult; } return null; }); result.Wait(); return result.Result; }