static List <FileSystemInfos> ListFiles(string RemotePath) { List <FileSystemInfos> fsis = new List <FileSystemInfos>(); DriveItem root; try { if (string.IsNullOrWhiteSpace(RemotePath) == true || RemotePath == "/") { root = QuerySync <DriveItem>(graphClient.Drive.Root.Request().Expand("children").GetAsync()); } else { root = QuerySync <DriveItem>(graphClient.Drive.Root.ItemWithPath(RemotePath.Replace("#", "%23")).Request().Expand("children").GetAsync()); } if (root == null) { return(null); } } catch (Exception ee) { Debug.WriteLine(ee.ToString()); return(null); } IDriveItemChildrenCollectionPage currentlist = root.Children; do { foreach (DriveItem item in currentlist.CurrentPage) { FileSystemInfos fsie = new FileSystemInfos(); fsie.Created = item.FileSystemInfo.CreatedDateTime.Value.DateTime; fsie.Modified = item.FileSystemInfo.LastModifiedDateTime.Value.DateTime; fsie.OneDriveID = item.Id; fsie.Name = item.Name; if (item.Folder == null) { fsie.SZ = item.Size.Value; fsie.IsDir = false; } else { fsie.SZ = 0; fsie.IsDir = true; } fsis.Add(fsie); } if (currentlist.NextPageRequest == null) { break; } currentlist = QuerySync <IDriveItemChildrenCollectionPage>(currentlist.NextPageRequest.GetAsync()); } while (true); return(fsis); }
static bool RecurseUploadData(string LocalPath, string RemotePath, bool SkipFailed) { if (TestLogin() == false) { return(false); } List <FileSystemInfos> RemoteFiles = ListFiles(RemotePath); if (RemoteFiles == null) { return(false); } StatNumDirProcessed++; foreach (string LPath in System.IO.Directory.EnumerateDirectories(LocalPath, "*.*", System.IO.SearchOption.TopDirectoryOnly)) { string RP = RemotePath; RP += "/"; string PathOnly = LPath.Substring(LPath.LastIndexOf("\\") + 1); RP += PathOnly; if (PathOnly.StartsWith(" ") == true) { continue; } MarkAsProcessed(RemoteFiles, PathOnly, true); //Create dir in AZ string RemotePathDotDot = RP.Substring(0, RP.LastIndexOf("/")); Drive drv = QuerySync <Drive>(graphClient.Drive.Request().GetAsync()); DriveItem newcreateddir; DriveItem newfolder = new DriveItem() { Name = RP.Substring(RP.LastIndexOf("/") + 1), Folder = new Folder() }; if (TestLogin() == false) { return(false); } if (string.IsNullOrWhiteSpace(RemotePathDotDot) == true || RemotePath == "/") { newcreateddir = QuerySync <DriveItem>(graphClient.Drive.Root.Children.Request().AddAsync(newfolder)); } else { newcreateddir = QuerySync <DriveItem>(graphClient.Drive.Root.ItemWithPath(RemotePathDotDot).Children.Request().AddAsync(newfolder)); } if (newcreateddir == null) { Console.WriteLine("Cannot create directory"); return(false); } if (RecurseUploadData(LPath, RP, SkipFailed) == false) { return(false); } } foreach (string Filename in System.IO.Directory.EnumerateFiles(LocalPath, "*.*", System.IO.SearchOption.TopDirectoryOnly)) { if (System.IO.Path.GetFileName(Filename).StartsWith(" ") == true) { continue; } string RemoteFullName = RemotePath.Replace("#", "%23") + "/" + Uri.EscapeUriString(System.IO.Path.GetFileName(Filename)).Replace("#", "%23"); System.IO.FileInfo fi = new System.IO.FileInfo(Filename); DateTime DTCreated = fi.CreationTimeUtc; DateTime DTModified = fi.LastWriteTimeUtc; Int64 FSZ = fi.Length; Console.Write(Filename + " -> " + RemoteFullName + " (" + NiceSize(FSZ) + ") ... "); if (FSZ == 0) { StatCopyNumSkipped++; Console.WriteLine("\b\b\b\b\bBlank"); continue; } MarkAsProcessed(RemoteFiles, System.IO.Path.GetFileName(Filename), false); FileSystemInfos fsitest = GetFSI(RemoteFiles, System.IO.Path.GetFileName(Filename), false); if (fsitest != null) { if (DTLightTest(fsitest.Modified, DTModified) && DTLightTest(fsitest.Created, DTCreated) && fsitest.SZ == FSZ) { StatCopyNumSkipped++; StatCopySZSkipped += FSZ; Console.WriteLine("\b\b\b\b\bSkipped"); continue; } else { if (TestLogin() == false) { return(false); } QuerySync(graphClient.Drive.Items[fsitest.OneDriveID].Request().DeleteAsync()); } } try { using (System.IO.FileStream fss = System.IO.File.Open(Filename, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read)) { FileSystemInfo fsi = new Microsoft.Graph.FileSystemInfo(); fsi.CreatedDateTime = new DateTimeOffset(DTCreated); fsi.LastModifiedDateTime = new DateTimeOffset(DTModified); DriveItemUploadableProperties uplprop = new DriveItemUploadableProperties(); uplprop.FileSystemInfo = fsi; if (TestLogin() == false) { return(false); } UploadSession UploadSess = QuerySync <UploadSession>(graphClient.Drive.Root.ItemWithPath(RemoteFullName).CreateUploadSession(uplprop).Request().PostAsync()); const int MaxChunk = 320 * 10 * 1024; ChunkedUploadProvider provider = new ChunkedUploadProvider(UploadSess, graphClient, fss, MaxChunk); IEnumerable <UploadChunkRequest> chunckRequests = provider.GetUploadChunkRequests(); byte[] readBuffer = new byte[MaxChunk]; List <Exception> exceptions = new List <Exception>(); bool Res = false; foreach (UploadChunkRequest request in chunckRequests) { Int64 Perc = (Int64)(((decimal)request.RangeBegin / (decimal)request.TotalSessionLength) * 100m); Console.Write("\b\b\b\b\b" + Perc.ToString().PadLeft(4) + "%"); if (TestLogin() == false) { return(false); } UploadChunkResult result = QuerySync <UploadChunkResult>(provider.GetChunkRequestResponseAsync(request, readBuffer, exceptions)); if (result.UploadSucceeded) { Res = true; } } if (Res == false) { Console.WriteLine("\b\b\b\b\bFAILED"); if (SkipFailed == false) { return(false); } else { continue; } } } } catch (Exception ee) { Debug.WriteLine(ee.ToString()); Console.WriteLine("\b\b\b\b\bFAILED"); StatCopyNumFailed++; StatCopySZFailed += FSZ; if (FailedDetails == true) { Console.WriteLine("---> " + ee.ToString()); WriteEventLog("Copy failed:\nLocal" + Filename + "\nRemote: " + RemoteFullName + "\n\n" + ee.ToString(), EventLogEntryType.Error); } if (SkipFailed == false) { return(false); } else { continue; } } StatCopyNumSuccess++; StatCopySZSuccess += FSZ; Console.WriteLine("\b\b\b\b\bOK "); } foreach (FileSystemInfos fsis in RemoteFiles) { if (fsis.Processed == true) { continue; } string RemoteFullName = RemotePath + "/" + fsis.Name + (fsis.IsDir == true ? " (DIR)" : ""); //deco only Console.Write("Deleting " + RemoteFullName + " ... "); if (TestLogin() == false) { return(false); } QuerySync(graphClient.Drive.Items[fsis.OneDriveID].Request().DeleteAsync()); StatNumDeleted++; Console.WriteLine("OK"); } return(true); }