예제 #1
0
        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);
        }
예제 #2
0
        public static DriveItem MemberwiseClone(this DriveItem driveItem)
        {
            Dictionary <string, object> additionalData = null;
            ItemReference parentReference = null;

            Microsoft.Graph.FileSystemInfo fileSystemInfo = null;
            Hashes hashes = null;

            if (driveItem.AdditionalData != null)
            {
                additionalData = new Dictionary <string, object>()
                {
                    [CryptoDriveConstants.DownloadUrl] = driveItem.AdditionalData[CryptoDriveConstants.DownloadUrl]
                }
            }
            ;

            if (driveItem.ParentReference != null)
            {
                parentReference = new ItemReference()
                {
                    Path = driveItem.ParentReference.Path
                }
            }
            ;

            if (driveItem.FileSystemInfo != null)
            {
                fileSystemInfo = new Microsoft.Graph.FileSystemInfo()
                {
                    CreatedDateTime      = driveItem.FileSystemInfo.CreatedDateTime,
                    LastAccessedDateTime = driveItem.FileSystemInfo.LastAccessedDateTime,
                    LastModifiedDateTime = driveItem.FileSystemInfo.LastModifiedDateTime
                };
            }

            if (driveItem.File?.Hashes != null)
            {
                hashes = new Hashes()
                {
                    QuickXorHash = driveItem.QuickXorHash()
                };
            }

            return(new DriveItem()
            {
                AdditionalData = additionalData,
                Content = driveItem.Content,
                Deleted = driveItem.Deleted == null ? null : new Deleted(),
                File = driveItem.File == null ? null : new Microsoft.Graph.File()
                {
                    Hashes = hashes
                },
                Folder = driveItem.Folder == null ? null : new Folder(),
                Id = driveItem.Id,
                Name = driveItem.Name,
                ParentReference = parentReference,
                RemoteItem = driveItem.RemoteItem == null ? null : new RemoteItem(),
                Size = driveItem.Size,
                FileSystemInfo = fileSystemInfo,
            });
        }