public async Task <BusinessEntities.File> UploadFile(BusinessEntities.Folder targetFolder, string fileNameWithExtension, System.IO.Stream fileStream)
        {
            // Get the targetFolder driveItem and the destination path (strip /drive/root: (12 characters) from the parent path string)
            var targetFolderDriveItem = await this.graphAPI.GetDriveItemAsync(targetFolder.ID);

            string destinationFolderPath = (targetFolderDriveItem.Name == "root" && targetFolderDriveItem.ParentReference.Name == null)
                ? ""
                : targetFolderDriveItem.ParentReference.Path.Remove(0, 12) + "/" + Uri.EscapeUriString(targetFolderDriveItem.Name);
            var uploadPath = destinationFolderPath + "/" + fileNameWithExtension;


            // Attempt to upload the file
            BusinessEntities.File uploadedFile = null;
            try
            {
                var uploadedItem = await this.graphAPI.UploadDriveItemAsync(uploadPath, fileStream);

                uploadedFile = new BusinessEntities.File(uploadedItem.Id, uploadedItem.Name, uploadedItem.Size, null);
            }
            catch (Exception exception)
            {
                throw new Exception(exception.Message);
            }

            return(uploadedFile);
        }
        // Private methods
        private async void RequestAndGenerateChildrenEntitiesRecursively(BusinessEntities.Folder parentFolder, DriveItem expandedFolderDriveItem)
        {
            // Generate entities for its children
            foreach (var childDriveItem in expandedFolderDriveItem.Children.CurrentPage)
            {
                // Folder
                if (childDriveItem.Folder != null)
                {
                    var newChildFolder = new BusinessEntities.Folder(childDriveItem.Id, childDriveItem.Name, childDriveItem.Size, parentFolder);
                    parentFolder.Folders.Add(newChildFolder);

                    // Generate its children when it is a Folder
                    if (expandedFolderDriveItem.Children != null && expandedFolderDriveItem.Children.CurrentPage != null)
                    {
                        var expandedChildDriveItem = await graphAPI.GetDriveItemAsync(childDriveItem.Id);

                        RequestAndGenerateChildrenEntitiesRecursively(newChildFolder, expandedChildDriveItem);
                    }
                }
                // File
                else if (childDriveItem.File != null)
                {
                    var newFile = new BusinessEntities.File(childDriveItem.Id, childDriveItem.Name, childDriveItem.Size, parentFolder);
                    parentFolder.Files.Add(newFile);
                }
            }
        }
        public async Task <System.IO.Stream> DownloadFile(BusinessEntities.File targetFile)
        {
            System.IO.Stream fileStream = null;
            try
            {
                fileStream = await this.graphAPI.GetDriveItemContentAsync(targetFile.ID);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

            return(fileStream);
        }
Esempio n. 4
0
 public void AddAttachment(BusinessEntities.File file, bool specificVersion = true)
 {
     Attachments.Add(new Attachment {
         File = file, SpecificVersion = true
     });
 }