Ejemplo n.º 1
0
        private static async Task <DriveItem> UploadlargeFile(SharepointIds sp, string path)
        {
            DriveItem uploadedFile = null;

            using (FileStream fileStream = new FileStream(largeFilePath, FileMode.Open))
            {
                UploadSession uploadSession = await graphClient.Sites["root"]
                                              .Drive.Root.ItemWithPath($"{path}/LargeFile.txt")
                                              .CreateUploadSession().Request()
                                              .PostAsync();

                if (uploadSession != null)
                {
                    // Chunk size must be divisible by 320KiB, our chunk size will be slightly more than 1MB
                    int maxSizeChunk = (320 * 1024) * 4;
                    ChunkedUploadProvider uploadProvider = new ChunkedUploadProvider(uploadSession, graphClient, fileStream, maxSizeChunk);
                    var chunkRequests = uploadProvider.GetUploadChunkRequests();
                    var exceptions    = new List <Exception>();
                    var readBuffer    = new byte[maxSizeChunk];
                    foreach (var request in chunkRequests)
                    {
                        var result = await uploadProvider.GetChunkRequestResponseAsync(request, readBuffer, exceptions);

                        if (result.UploadSucceeded)
                        {
                            uploadedFile = result.ItemResponse;
                        }
                    }
                }
            }
            return(uploadedFile);
        }
Ejemplo n.º 2
0
        private static async Task UploadSmallFileMT(SharepointIds sp, string path, int i)
        {
            try
            {
                using (var stream = new MemoryStream())
                {
                    var writer = new StreamWriter(stream);
                    writer.Write("Test document");
                    writer.Flush();
                    stream.Position = 0;

                    var createdFile = await graphClient.Sites[sp.SiteId]
                                      .Drive.Root
                                      .ItemWithPath($"{path}/SmallFile{i:D2}.txt")
                                      .Content.Request()
                                      .PutAsync <DriveItem>(stream);
                    var source = new Source()
                    {
                        ActType       = currentActivity,
                        ResType       = Source.ResourceType.File,
                        OrgActionDate = createdFile.CreatedDateTime.Value.UtcDateTime,
                        Message       = $"New File Created {createdFile.WebUrl} on {createdFile.CreatedDateTime.Value.UtcDateTime}"
                    };
                    //await DbOperations.UpdateSourcesAsync(source);
                    if (showOnConsole)
                    {
                        Console.WriteLine(source.Message);
                    }
                }
            }catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Ejemplo n.º 3
0
        private static async Task CheckOutInFile(SharepointIds sp, string path, int i)
        {
            using (FileStream fileStream = new FileStream(smallFilePath, FileMode.Open))
            {
                await graphClient.Sites[sp.SiteId]
                .Drive.Root
                .ItemWithPath($"{path}/SmallFile{i:D2}.txt")
                .Checkout()
                .Request()
                .PostAsync();

                await graphClient.Sites[sp.SiteId]
                .Drive.Root
                .ItemWithPath($"{path}/SmallFile{i:D2}.txt")
                .Checkin()
                .Request()
                .PostAsync();

                var source = new Source()
                {
                    ActType       = Source.Activity.VersionAdded,
                    ResType       = Source.ResourceType.File,
                    OrgActionDate = DateTime.UtcNow,
                    Message       = $"New Version Created {$"{path}/SmallFile{i:D2}.txt"} on {DateTime.UtcNow}"
                };
                await DbOperations.UpdateSourcesAsync(source);

                if (showOnConsole)
                {
                    Console.WriteLine(source.Message);
                }
            }
        }
Ejemplo n.º 4
0
        private static async Task RenameFile(SharepointIds sp, string path, int i)
        {
            var driveItem = new DriveItem
            {
                Name = $"RenameSmallFile{i:D2}.txt"
            };

            await graphClient.Sites[sp.SiteId]
            .Drive.Root
            .ItemWithPath($"{path}/SmallFile{i:D2}.txt")
            .Request()
            .UpdateAsync(driveItem);

            var source = new Source()
            {
                ActType       = Source.Activity.Renamed,
                ResType       = Source.ResourceType.File,
                OrgActionDate = DateTime.UtcNow,
                Message       = $"Rename file {$"{path}/SmallFile{i:D2}.txt"} on {DateTime.UtcNow}"
            };
            await DbOperations.UpdateSourcesAsync(source);

            if (showOnConsole)
            {
                Console.WriteLine(source.Message);
            }
        }
Ejemplo n.º 5
0
 private static async Task <string> CreateSingleFileAsync(SharepointIds sp, string path, int l1, int l2)
 {
     try
     {
         DriveItem createdFolder;
         var       driveItem = new DriveItem
         {
             Name   = (l1 == 1 && l2 == 1) ? $"Folder{l1:D2}" : $"Folder{l1:D2}L{l2:D2}",
             Folder = new Folder
             {
             },
             AdditionalData = new Dictionary <string, object>()
             {
                 { "@microsoft.graph.conflictBehavior", "replace" }
             }
         };
         if (string.IsNullOrEmpty(path))
         {
             driveItem.Name = "General";
             createdFolder  = await graphClient.Sites[sp.SiteId]
                              .Drive.Root.Children.Request()
                              .AddAsync(driveItem);
         }
         else
         {
             createdFolder = await graphClient.Sites[sp.SiteId]
                             .Drive.Root.ItemWithPath(path)
                             .Children
                             .Request()
                             .AddAsync(driveItem);
         }
         path = $"{createdFolder.ParentReference.Path.Split(':')[1]}/{createdFolder.Name}";
         await UploadSmallFile(sp, path, 1);
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.Message);
     }
     return(path);
 }
Ejemplo n.º 6
0
        private static async Task UploadSmallFile(SharepointIds sp, string path, int i)
        {
            using (FileStream fileStream = new FileStream(smallFilePath, FileMode.Open))
            {
                var createdFile = await graphClient.Sites[sp.SiteId]
                                  .Drive.Root
                                  .ItemWithPath($"{path}/SmallFile{i:D2}.txt")
                                  .Content.Request()
                                  .PutAsync <DriveItem>(fileStream);
                var source = new Source()
                {
                    ActType       = currentActivity,
                    ResType       = Source.ResourceType.File,
                    OrgActionDate = createdFile.CreatedDateTime.Value.UtcDateTime,
                    Message       = $"New File Created {createdFile.WebUrl} on {createdFile.CreatedDateTime.Value.UtcDateTime}"
                };
                await DbOperations.UpdateSourcesAsync(source);

                if (showOnConsole)
                {
                    Console.WriteLine(source.Message);
                }
            }
        }
Ejemplo n.º 7
0
        private static async Task <string> CreateFolderLevelAsync(SharepointIds sp, string path, int l1, int l2)
        {
            try
            {
                DriveItem createdFolder;
                var       driveItem = new DriveItem
                {
                    Name   = (l1 == 1 && l2 == 1) ? $"Folder{l1:D2}" : $"Folder{l1:D2}L{l2:D2}",
                    Folder = new Folder
                    {
                    },
                    AdditionalData = new Dictionary <string, object>()
                    {
                        { "@microsoft.graph.conflictBehavior", "replace" }
                    }
                };
                if (string.IsNullOrEmpty(path))
                {
                    driveItem.Name = "General";
                    createdFolder  = await graphClient.Sites[sp.SiteId]
                                     .Drive.Root.Children.Request()
                                     .AddAsync(driveItem);
                }
                else
                {
                    createdFolder = await graphClient.Sites[sp.SiteId]
                                    .Drive.Root.ItemWithPath(path)
                                    .Children
                                    .Request()
                                    .AddAsync(driveItem);
                }
                path = $"{createdFolder.ParentReference.Path.Split(':')[1]}/{createdFolder.Name}";

                var source = new Source()
                {
                    ActType       = currentActivity,
                    ResType       = Source.ResourceType.Folder,
                    OrgActionDate = createdFolder.CreatedDateTime.Value.UtcDateTime,
                    Message       = $"New Folder Created {createdFolder.WebUrl} on {createdFolder.CreatedDateTime.Value.UtcDateTime}"
                };
                await DbOperations.UpdateSourcesAsync(source);

                if (showOnConsole)
                {
                    Console.WriteLine(source.Message);
                }

                await Task.Delay(100);

                for (int i = 1; i <= filesCount; i++)
                {
                    await UploadSmallFile(sp, path, i);
                }

                await Task.Delay(100);

                for (int i = 1; i <= filesCount; i++)
                {
                    await CheckOutInFile(sp, path, i);
                }

                await Task.Delay(100);

                for (int i = 1; i <= filesCount; i++)
                {
                    await RenameFile(sp, path, i);
                }

                await Task.Delay(5000);

                for (int i = 1; i <= filesCount; i++)
                {
                    await DeleteFile(sp, path, i);
                }
            }catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return(path);
        }