Пример #1
0
        private async Task <string> CreateFilePathAsync(string parent, string newFilename)
        {
            PathItemBuilder pathItemBuilder = await GetPathItemBuilder(parent);

            //see if such a file exists already:
            var item = TryFindFile(pathItemBuilder, newFilename);

            if (item != null)
            {
                return(pathItemBuilder.itemLocation.BuildLocalChildLocation(item.Name, item.Id, item.ParentReference?.DriveId)
                       .ToString());
            }
            //doesn't exist. Create:
            logDebug("building request for " + pathItemBuilder.itemLocation);

            DriveItem res = await pathItemBuilder.getPathItem()

                            .ItemWithPath(newFilename)
                            .Content
                            .Request()
                            .PutAsync <DriveItem>(new MemoryStream());

            return(pathItemBuilder.itemLocation.BuildLocalChildLocation(res.Name, res.Id, res.ParentReference?.DriveId)
                   .ToString());
        }
Пример #2
0
 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)
     {
         Task.Run(async() =>
         {
             PathItemBuilder pathItemBuilder = await GetPathItemBuilder(path);
             pathItemBuilder.verbose         = true;
             pathItemBuilder.getPathItem();
         }).Wait();
         throw convertException(e);
     }
 }
        public FileDescription GetFileDescription(IOConnectionInfo ioc)
        {
            try
            {
                string          filename        = ioc.Path;
                PathItemBuilder pathItemBuilder = 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 = Task.Run(async() => await pathItem.Request().GetAsync()).Result;
                return(GetFileDescription(pathItemBuilder.itemLocation, item));
            }
            catch (Exception e)
            {
                throw convertException(e);
            }
        }
Пример #4
0
        DriveItem TryFindFile(PathItemBuilder parent, string filename)
        {
            IDriveItemChildrenCollectionPage itemsPage = Task.Run(async() => await parent.getPathItem()
                                                                  .Children
                                                                  .Request()
                                                                  .GetAsync()).Result;

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

                foreach (DriveItem i in items)
                {
                    if (i.Name == filename)
                    {
                        return(i);
                    }
                }
                var nextPageReqBuilder = itemsPage.NextPageRequest;
                if (nextPageReqBuilder == null)
                {
                    return(null);
                }
                itemsPage = Task.Run(async() => await nextPageReqBuilder.GetAsync()).Result;
            }
        }
        public string CreateFilePath(string parent, string newFilename)
        {
            try
            {
                DriveItem driveItem = new DriveItem();
                driveItem.Name = newFilename;
                driveItem.File = new File();
                PathItemBuilder pathItemBuilder = GetPathItemBuilder(parent);

                //see if such a file exists already:
                var item = TryFindFile(pathItemBuilder, newFilename);
                if (item != null)
                {
                    return(pathItemBuilder.itemLocation.BuildLocalChildLocation(item.Name, item.Id, item.ParentReference?.DriveId).ToString());
                }
                //doesn't exist. Create:
                logDebug("building request for " + pathItemBuilder.itemLocation);

                DriveItem res = Task.Run(async() => await pathItemBuilder.getPathItem()
                                         .Children
                                         .Request()
                                         .AddAsync(driveItem)).Result;

                return(pathItemBuilder.itemLocation.BuildLocalChildLocation(res.Name, res.Id, res.ParentReference?.DriveId).ToString());
            }
            catch (Exception e)
            {
                throw convertException(e);
            }
        }
Пример #6
0
        private async Task <string> CreateFilePathAsync(string parent, string newFilename)
        {
            DriveItem driveItem = new DriveItem();

            driveItem.Name = newFilename;
            driveItem.File = new File();
            PathItemBuilder pathItemBuilder = await GetPathItemBuilder(parent);

            //see if such a file exists already:
            var item = TryFindFile(pathItemBuilder, newFilename);

            if (item != null)
            {
                return(pathItemBuilder.itemLocation.BuildLocalChildLocation(item.Name, item.Id, item.ParentReference?.DriveId)
                       .ToString());
            }
            //doesn't exist. Create:
            logDebug("building request for " + pathItemBuilder.itemLocation);

            DriveItem res = await pathItemBuilder.getPathItem()
                            .Children
                            .Request()
                            .AddAsync(driveItem);

            return(pathItemBuilder.itemLocation.BuildLocalChildLocation(res.Name, res.Id, res.ParentReference?.DriveId)
                   .ToString());
        }
 public void Delete(IOConnectionInfo ioc)
 {
     try
     {
         PathItemBuilder pathItemBuilder = GetPathItemBuilder(ioc.Path);
         Task.Run(async() => await pathItemBuilder.getPathItem()
                  .Request()
                  .DeleteAsync()).Wait();
     }
     catch (Exception e)
     {
         throw convertException(e);
     }
 }
 private void UploadFile(string path, MemoryStream stream)
 {
     try
     {
         PathItemBuilder pathItemBuilder = GetPathItemBuilder(path);
         Task.Run(async() => await
                  pathItemBuilder
                  .getPathItem()
                  .Content
                  .Request()
                  .PutAsync <DriveItem>(stream)).Wait();
     }
     catch (Exception e)
     {
         throw convertException(e);
     }
 }
Пример #9
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;
            }
        }
 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);
     }
 }
Пример #11
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));
        }
Пример #12
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);
            }
        }
Пример #14
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);
            }
        }