/// <summary> /// Creates the files for the very first time on Google Drive /// </summary> /// <param name="title">Title of the Document.</param> /// <param name="contents">Document's body.</param> /// <returns></returns> public static bool createFile(string title, string contents) { try { Google.Apis.Drive.v2.Data.File body = new Google.Apis.Drive.v2.Data.File(); body.Title = title; body.Description = "Uploaded from Google Docs Desktop Utility."; body.MimeType = "text/plain"; //byte[] byteArray = System.IO.File.ReadAllBytes("document.txt"); byte[] byteArray = System.Text.Encoding.Unicode.GetBytes(contents); System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray); FilesResource.InsertMediaUpload request = service.Files.Insert(body, stream, "text/plain"); FilesResource filesResource = service.Files; FilesResource.ListRequest listRequest = filesResource.List(); request.Upload(); currentJobFile = request.ResponseBody; return(true); } catch (Exception) { return(false); } }
/// <summary> /// Client for making requests to the API. /// </summary> /// <param name="authHandler">The authentication handler.</param> /// <param name="ctx">The HTTP context to use for this session.</param> private Client(Func <CancellationToken, Task <string> > authHandler, HttpContext ctx) { // Setup resources. Assets = new AssetsResource(authHandler, ctx); TimeSeries = new TimeSeriesResource(authHandler, ctx); DataPoints = new DataPointsResource(authHandler, ctx); Events = new EventsResource(authHandler, ctx); Sequences = new SequencesResource(authHandler, ctx); Raw = new RawResource(authHandler, ctx); Relationships = new RelationshipResource(authHandler, ctx); DataSets = new DataSetsResource(authHandler, ctx); ThreeDModels = new ThreeDModelsResource(authHandler, ctx); ThreeDRevisions = new ThreeDRevisionsResource(authHandler, ctx); ThreeDAssetMappings = new ThreeDAssetMappingsResource(authHandler, ctx); Files = new FilesResource(authHandler, ctx); Login = new LoginResource(authHandler, ctx); Token = new TokenResource(authHandler, ctx); ExtPipes = new ExtPipesResource(authHandler, ctx); Labels = new LabelsResource(authHandler, ctx); Groups = new GroupsResource(authHandler, ctx); // Playground features (experimental) Playground = new PlaygroundResource(authHandler, ctx); // Beta features (experimental) Beta = new BetaResource(authHandler, ctx); }
public void StreamtoFile(Google.Apis.Drive.v3.Data.File x) { FilesResource bung = new FilesResource(driver); MemoryStream output = new MemoryStream(); var stream = new System.IO.MemoryStream(); bung.Get(x.Id).Download(stream); DependencyService.Get <ISave>().Save(x.Name, "application/vnd.ms-excel", stream); Application.Current.Properties["Boff"] += "\nDownloaded: " + x.Name; }
public void FiletoStream(Google.Apis.Drive.v3.Data.File x, string name) { bool doesExist = System.IO.File.Exists(DependencyService.Get <ISave>().GetFileName() + "/" + name); if (doesExist) { FileStream inputStream = new FileStream(DependencyService.Get <ISave>().GetFileName() + "/" + name, FileMode.Open); FilesResource bung = new FilesResource(driver); bung.Update(x, x.Id, inputStream, "application/vnd.ms-excel").Upload(); //bung.Create(x,inputStream, "application/msexcel").Upload(); Application.Current.Properties["Boff"] += "Uploaded: " + x.Name; inputStream.Close(); } }
public static async Task <File> FetchGoogleFile(string fileName) { File f = null; FilesResource resource = _service.Files; FileList files = null; try { FilesResource.ListRequest req = resource.List(); req.Q = $"name='{_filePrefix + fileName}'"; files = await req.ExecuteAsync(); } catch (Exception) { return(null); } if (files == null) { return(null); } if (files.Files.Count <= 0) { f = new File(); f.Name = _filePrefix + fileName; f.MimeType = "application/json"; try { FilesResource.CreateRequest create = resource.Create(f); await create.ExecuteAsync(); } catch (Exception) { } } else { f = files.Files[0]; } await DownloadGoogleFile(f, fileName); return(f); }
public void FilesList() { Xamarin.Forms.Application.Current.Properties["Boff"] = "Downloading Files, Please Wait."; FilesResource bung = new FilesResource(driver); FileList jonny = bung.List().Execute(); bool pr = false; bool tr = false; bool pl = false; // try{ foreach (Google.Apis.Drive.v3.Data.File x in jonny.Files) { if (x.MimeType == "application/vnd.ms-excel") { if (x.Name == "Pricings.xls") { Xamarin.Forms.Application.Current.Properties["Boff"] += "\nDownloading: " + x.Name; pr = true; StreamtoFile(x); } else if (x.Name == "Plots.xls") { Xamarin.Forms.Application.Current.Properties["Boff"] += "\nDownloading: " + x.Name; tr = true; StreamtoFile(x); } else if (x.Name == "trees.xls") { Xamarin.Forms.Application.Current.Properties["Boff"] += "\nDownloading: " + x.Name; pl = true; StreamtoFile(x); } } } if (!pl || !pr || !tr) { Xamarin.Forms.Application.Current.Properties["Boff"] = (pr ? "" : "Pricings.xls, ") + (pl ? "" : "Plots.xls, ") + (tr ? "" : "trees.xls, ") + "not found."; } // } // catch(Exception e) // { // Xamarin.Forms.Application.Current.Properties["Boff"] = e.GetBaseException().ToString(); // } }
/// <summary> /// Elimina los documentos que haya en la carpeta indicada. /// Los nombres de los documentos a eliminar deben empezar con el nombre de la carpeta. /// folderName_nombre.doc /// </summary> /// <param name="folderName">La carpeta con los documentos a eliminar.</param> /// <returns>Devuelve el número de documentos eliminados.</returns> public static int EliminarDocumentos(string folderName) { var t = 0; var fr = new FilesResource(driveService); fr.List().Q = "application/vnd.google-apps.folder"; var folders = fr.List().Execute(); var prefix = folderName + "_"; foreach (var f in folders.Files) { if (f.MimeType == "application/vnd.google-apps.document" && f.Name.StartsWith(prefix)) { if (deleteFile(f.Id)) { t++; } } } return(t); }
/// <summary> /// Comprueba si existe la carpeta indicada. /// Si existe devuelve el ID, si no, una cadena vacía. /// </summary> /// <param name="folderName">El nombre de la carpeta a buscar.</param> /// <returns>El ID de la carpeta o una cadena vacía si no existe.</returns> public static string ExisteFolder(string folderName) { var fr = new FilesResource(driveService); fr.List().Q = "application/vnd.google-apps.folder"; var folders = fr.List().Execute(); foreach (var f in folders.Files) { // Saltarse los documentos if (f.MimeType == "application/vnd.google-apps.document") { continue; } if (f.Name == folderName) { return(f.Id); } } return(""); }
/// <summary> /// Creates the files for the very first time on Google Drive /// </summary> /// <param name="title">Title of the Document.</param> /// <param name="contents">Document's body.</param> /// <returns></returns> public static bool createFile(string title, Stream stream) { try { Google.Apis.Drive.v2.Data.File body = new Google.Apis.Drive.v2.Data.File(); body.Title = title; body.Description = "Uploaded from Google Docs Desktop Utility."; body.MimeType = "text/rtf";//"text/plain"; FilesResource.InsertMediaUpload request = service.Files.Insert(body, stream, "text/rtf"); FilesResource filesResource = service.Files; FilesResource.ListRequest listRequest = filesResource.List(); request.Upload(); currentJobFile = request.ResponseBody; return(true); } catch (Exception) { return(false); } }
public void UpList() { Xamarin.Forms.Application.Current.Properties["Boff"] = "Uploading Files, Please Wait."; FilesResource bung = new FilesResource(driver); ListRequest req = bung.List(); req.Q = "'root' in parents and trashed=false"; FileList jonny = req.Execute(); // try{ (bool, bool, bool)set = (false, false, false); foreach (Google.Apis.Drive.v3.Data.File x in jonny.Files) { if (x.MimeType == "application/vnd.ms-excel") { if (x.Name == "Pricings.xls") { Xamarin.Forms.Application.Current.Properties["Boff"] += "\nUploading: " + x.Name; set.Item1 = true; FiletoStream(x, "Pricings.xls"); } else if (x.Name == "Plots.xls") { Xamarin.Forms.Application.Current.Properties["Boff"] += "\nUploading: " + x.Name; set.Item2 = true; FiletoStream(x, "Plots.xls"); } else if (x.Name == "trees.xls") { Xamarin.Forms.Application.Current.Properties["Boff"] += "\nUploading: " + x.Name; set.Item3 = true; FiletoStream(x, "trees.xls"); } } } CreateFiles(set); }
/// <summary>Constructs a new service.</summary> /// <param name="initializer">The service initializer.</param> public DriveService(Google.Apis.Services.BaseClientService.Initializer initializer) : base(initializer) { files = new FilesResource(this); }
private static async Task<IEnumerable<File>> GetFilesPage(FilesResource.ListRequest request, CancellationToken cancellationToken) { var files = await request.ExecuteAsync(cancellationToken); request.PageToken = files.NextPageToken; return files.Items; }
private static async Task<List<File>> GetAllFiles(FilesResource.ListRequest request, CancellationToken cancellationToken) { var result = new List<File>(); do { result.AddRange(await GetFilesPage(request, cancellationToken)); } while (!string.IsNullOrEmpty(request.PageToken)); return result; }