Ejemplo n.º 1
0
 /// <summary>
 /// This Method takes in a parent directory as parameter.
 /// Lists all the sub-directories and adds them to the parent directory.
 /// Lists all the files and adds them to the parent directory.
 /// </summary>
 public void GetFiles(DirectoryBlock parentDirectory)
 {
     using (DriveService service = DriveClient.GetInfo())
     {
         try
         {
             FilesResource.ListRequest listRequest = service.Files.List();
             listRequest.Q       = "'" + parentDirectory.id + "' in parents and trashed = false";
             listRequest.OrderBy = "name";
             listRequest.Fields  = "nextPageToken, files" +
                                   "(id, name, mimeType, permissions, webContentLink, description, size, shared, owners, parents)";
             Google.Apis.Drive.v3.Data.FileList ListResponse = listRequest.Execute();
             while (ListResponse.Files != null && ListResponse.Files.Count != 0)
             {
                 List <Task> tasks = new List <Task>();
                 foreach (var item in ListResponse.Files)
                 {
                     tasks.Add(Task.Run(() => AddToParentDirectoryObject(item, parentDirectory)));
                 }
                 Task.WaitAll(tasks.ToArray());
                 if (ListResponse.NextPageToken == null)
                 {
                     break;
                 }
                 listRequest.PageToken = ListResponse.NextPageToken;
                 ListResponse          = listRequest.Execute();
             }
         }
         catch (Exception e)
         {
             throw new Exception(e.StackTrace);
         }
     }
 }
Ejemplo n.º 2
0
        public bool UploadCloudFile(FileBlock file, string destinationPath = null)
        {
            bool status = false;

            try
            {
                using (var client = DriveClient.GetInfo())
                {
                    using (var stream = new FileStream(file.path, FileMode.OpenOrCreate))
                    {
                        FilesResource.CreateMediaUpload request = new FilesResource.CreateMediaUpload(client,
                                                                                                      new Google.Apis.Drive.v3.Data.File
                        {
                            Name    = file.Name,
                            Parents = file.Parent,
                        },
                                                                                                      stream,
                                                                                                      file.mimeType);
                        request.ChunkSize = 1024 * 1024;
                        Task initiateSessionTask = Task.Run(() => request.InitiateSessionAsync());
                        initiateSessionTask.Wait();
                        Task <Google.Apis.Upload.IUploadProgress> uploadTask = Task.Run(() => request.UploadAsync());
                        uploadTask.Wait();
                        status = true;
                    }
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.Message + " " + e.StackTrace);
            }

            return(status);
        }
Ejemplo n.º 3
0
 public long GetUsedSpace()
 {
     using (var client = DriveClient.GetInfo())
     {
         Google.Apis.Drive.v3.AboutResource.GetRequest request = client.About.Get();
         request.Fields = "storageQuota";
         var response = request.Execute();
         return((long)response.StorageQuota.UsageInDrive);
     }
 }
Ejemplo n.º 4
0
 public void DownloadCloudFile(FileBlock file, string localPath)
 {
     try
     {
         using (var client = DriveClient.GetInfo())
         {
             string fileFullPath    = localPath + file.path;
             string ext             = GetExt(file.mimeType);
             var    downloadRequest = client.Files.Get(file.id);
             using (FileStream stream = new FileStream(fileFullPath, FileMode.OpenOrCreate))
             {
                 downloadRequest.Download(stream);
             }
         }
     }
     catch (Exception e)
     {
         throw e;
     }
 }
Ejemplo n.º 5
0
        //public bool UploadFile(Containers.File item)
        //{
        //    bool status = false;
        //    try
        //    {
        //        var fileMetadata = new Google.Apis.Drive.v3.Data.File()
        //        {
        //            Name = item.Name,
        //            Parents = item.Parent,
        //        };
        //        FilesResource.CreateMediaUpload request;
        //        using (var service = DriveClient.GetInfo())
        //        {
        //            request = service.Files.Create(
        //            fileMetadata, new FileStream(item.path, FileMode.Open), item.mimeType);
        //            request.Upload();
        //        }
        //        status = true;
        //    }
        //    catch (Exception e)
        //    {
        //        throw e;
        //    }
        //    return status;
        //}

        internal bool UploadDirectory(DirectoryBlock item)
        {
            bool status = false;

            try
            {
                using (var service = DriveClient.GetInfo())
                {
                    var fileMetadata = new Google.Apis.Drive.v3.Data.File()
                    {
                        Name     = item.Name,
                        MimeType = "application/vnd.google-apps.folder",
                        Parents  = item.Parent,
                    };
                    var request = service.Files.Create(fileMetadata).Execute();
                    status = true;
                }
            }
            catch (Exception e)
            {
                throw e;
            }
            return(status);
        }