Beispiel #1
0
        public void Should_AbleToSetMultiParameters()
        {
            var parameters = new List <Parameter>();

            parameters.Add(new Parameter {
                Name = "first parameter"
            });
            parameters.Add(new Parameter {
                Name = "second parameter"
            });

            var pathItem = new PathItemBuilder(HttpMethod.Get)
                           .Parameters(parameters)
                           .Build();

            Assert.Equal(parameters.Count, pathItem.Parameters.Count());
            Assert.Equal(parameters, pathItem.Parameters);
        }
Beispiel #2
0
        private async Task <IEnumerable <FileDescription> > ListContentsAsync(IOConnectionInfo ioc)
        {
            PathItemBuilder pathItemBuilder = await GetPathItemBuilder(ioc.Path);

            logDebug("listing files for " + ioc.Path);
            if (!pathItemBuilder.hasShare() && !pathItemBuilder.hasOneDrivePath())
            {
                logDebug("listing shares.");
                return(await ListShares(pathItemBuilder.itemLocation, pathItemBuilder.client));
            }

            logDebug("listing regular children.");
            List <FileDescription> result = new List <FileDescription>();

            /*logDebug("parent before:" + parentPath);
            *  parentPath = parentPath.substring(getProtocolPrefix().length());
            *  logDebug("parent after: " + parentPath);*/

            IDriveItemChildrenCollectionPage itemsPage = await pathItemBuilder.getPathItem()
                                                         .Children
                                                         .Request()
                                                         .GetAsync();

            while (true)
            {
                IList <DriveItem> items = itemsPage.CurrentPage;
                if (!items.Any())
                {
                    return(result);
                }

                foreach (DriveItem i in items)
                {
                    var e = GetFileDescription(pathItemBuilder.itemLocation.BuildLocalChildLocation(i.Name, i.Id, i.ParentReference?.DriveId), i);
                    result.Add(e);
                }
                var nextPageReqBuilder = itemsPage.NextPageRequest;
                if (nextPageReqBuilder == null)
                {
                    return(result);
                }
                itemsPage = Task.Run(async() => await nextPageReqBuilder.GetAsync()).Result;
            }
        }
        private PathItemBuilder GetPathItemBuilder(String path)
        {
            PathItemBuilder result = new PathItemBuilder(SpecialFolder);


            result.itemLocation = OneDrive2ItemLocation <OneDrive2PrefixContainerType> .FromString(path);

            if (string.IsNullOrEmpty(result.itemLocation.User?.Name))
            {
                throw new Exception("path does not contain user");
            }
            result.client = null;
            if (!mClientByUser.TryGetValue(result.itemLocation.User.Id, out result.client))
            {
                throw new Exception("failed to get client for " + result.itemLocation.User.Id);
            }

            return(result);
        }
 public Stream OpenFileForRead(IOConnectionInfo ioc)
 {
     try
     {
         string          path          = ioc.Path;
         PathItemBuilder clientAndpath = GetPathItemBuilder(path);
         logDebug("openFileForRead. Path=" + path);
         Stream result = Task.Run(async() => await clientAndpath
                                  .getPathItem()
                                  .Content
                                  .Request()
                                  .GetAsync()).Result;
         logDebug("ok");
         return(result);
     }
     catch (Exception e)
     {
         throw convertException(e);
     }
 }
 private void UploadFile(string path, MemoryStream stream)
 {
     try
     {
         Task.Run(async() =>
         {
             PathItemBuilder pathItemBuilder = await GetPathItemBuilder(path);
             return(await
                    pathItemBuilder
                    .getPathItem()
                    .Content
                    .Request()
                    .PutAsync <DriveItem>(stream));
         }).Wait();
     }
     catch (Exception e)
     {
         throw convertException(e);
     }
 }
Beispiel #6
0
        private async Task <FileDescription> GetFileDescriptionAsync(IOConnectionInfo ioc)
        {
            string          filename        = ioc.Path;
            PathItemBuilder pathItemBuilder = await GetPathItemBuilder(filename);

            if (!pathItemBuilder.itemLocation.LocalPath.Any() &&
                !pathItemBuilder.hasShare())
            {
                FileDescription rootEntry = new FileDescription();
                rootEntry.CanRead     = rootEntry.CanWrite = true;
                rootEntry.Path        = filename;
                rootEntry.DisplayName = pathItemBuilder.itemLocation.User.Name;
                rootEntry.IsDirectory = true;
                return(rootEntry);
            }

            IDriveItemRequestBuilder pathItem = pathItemBuilder.getPathItem();

            DriveItem item = await pathItem.Request().GetAsync();

            return(GetFileDescription(pathItemBuilder.itemLocation, item));
        }
Beispiel #7
0
        private async Task <PathItemBuilder> GetPathItemBuilder(String path)
        {
            PathItemBuilder result = new PathItemBuilder(SpecialFolder);


            result.itemLocation = OneDrive2ItemLocation <OneDrive2PrefixContainerType> .FromString(path);

            if (string.IsNullOrEmpty(result.itemLocation.User?.Name))
            {
                throw new Exception("path does not contain user");
            }

            result.client = await TryGetMsGraphClient(path, true);

            if (result.client == null)
            {
                throw new Exception("Failed to connect or authenticate to OneDrive!");
            }


            return(result);
        }
Beispiel #8
0
        public void CreateDirectory(IOConnectionInfo parentIoc, string newDirName)
        {
            try
            {
                DriveItem driveItem = new DriveItem();
                driveItem.Name   = newDirName;
                driveItem.Folder = new Folder();

                DriveItem res = Task.Run(async() =>
                {
                    PathItemBuilder pathItemBuilder = await GetPathItemBuilder(parentIoc.Path);

                    return(await pathItemBuilder.getPathItem()
                           .Children
                           .Request()
                           .AddAsync(driveItem));
                }).Result;
            }
            catch (Exception e)
            {
                throw convertException(e);
            }
        }
        public void CreateDirectory(IOConnectionInfo parentIoc, string newDirName)
        {
            try
            {
                DriveItem driveItem = new DriveItem();
                driveItem.Name   = newDirName;
                driveItem.Folder = new Folder();

                PathItemBuilder pathItemBuilder = GetPathItemBuilder(parentIoc.Path);


                logDebug("building request for " + pathItemBuilder.itemLocation);

                DriveItem res = Task.Run(async() => await pathItemBuilder.getPathItem()
                                         .Children
                                         .Request()
                                         .AddAsync(driveItem)).Result;
            }
            catch (Exception e)
            {
                throw convertException(e);
            }
        }
Beispiel #10
0
        private void UploadFile(string path, MemoryStream stream)
        {
            try
            {
                Task.Run(async() =>
                {
                    PathItemBuilder pathItemBuilder = await GetPathItemBuilder(path);
                    //for small files <2MB use the direct upload:
                    if (stream.Length < 2 * 1024 * 1024)
                    {
                        return(await
                               pathItemBuilder
                               .getPathItem()
                               .Content
                               .Request()
                               .PutAsync <DriveItem>(stream));
                    }

                    //for larger files use an upload session. This is required for 4MB and beyond, but as the docs are not very clear about this
                    //limit, let's use it a bit more often to be safe.

                    var uploadProps = new DriveItemUploadableProperties
                    {
                        ODataType      = null,
                        AdditionalData = new Dictionary <string, object>
                        {
                            { "@microsoft.graph.conflictBehavior", "replace" }
                        }
                    };


                    var uploadSession = await pathItemBuilder
                                        .getPathItem()
                                        .CreateUploadSession(uploadProps)
                                        .Request()
                                        .PostAsync();

                    // Max slice size must be a multiple of 320 KiB
                    int maxSliceSize   = 320 * 1024;
                    var fileUploadTask = new LargeFileUploadTask <DriveItem>(uploadSession, stream, maxSliceSize);
                    var uploadResult   = await fileUploadTask.UploadAsync();

                    if (!uploadResult.UploadSucceeded)
                    {
                        throw new Exception("Failed to upload data!");
                    }

                    return(uploadResult.ItemResponse);
                }).Wait();
            }
            catch (Exception e)
            {
                Task.Run(async() =>
                {
                    PathItemBuilder pathItemBuilder = await GetPathItemBuilder(path);
                    pathItemBuilder.verbose         = true;
                    pathItemBuilder.getPathItem();
                }).Wait();
                throw convertException(e);
            }
        }