Example #1
0
        /// <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);
            }
        }
Example #2
0
        /// <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);
        }
Example #3
0
        /// <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("");
        }
        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);
        }
Example #5
0
        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();
            // }
        }
Example #6
0
        /// <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);
            }
        }
Example #7
0
        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);
        }