private void copyFolderItems(FileManagerDirectoryContent item, string targetID) { DriveService service = GetService(); ChildrenResource.ListRequest request = service.Children.List(item.Id); ChildList children = request.Execute(); foreach (ChildReference child in children.Items) { var childDetails = service.Files.Get(child.Id).Execute(); if (childDetails.MimeType == "application/vnd.google-apps.folder") { File file = new File() { Title = childDetails.Title, Parents = new List <ParentReference> { new ParentReference() { Id = targetID } }, MimeType = "application/vnd.google-apps.folder" }; request.Fields = "id"; File SubFolder = (service.Files.Insert(file)).Execute(); FileManagerDirectoryContent FileDetail = new FileManagerDirectoryContent(); FileDetail.Name = childDetails.Title; FileDetail.Id = childDetails.Id; FileDetail.IsFile = false; FileDetail.Type = "folder"; FileDetail.FilterId = obtainFilterId(childDetails); FileDetail.HasChild = getChildrenById(childDetails.Id); FileDetail.DateCreated = Convert.ToDateTime(childDetails.ModifiedDate); FileDetail.DateModified = Convert.ToDateTime(childDetails.ModifiedDate); copyFolderItems(FileDetail, SubFolder.Id); } else { File SubFile = new File() { Title = childDetails.Title, Parents = new List <ParentReference> { new ParentReference() { Id = targetID } } }; FilesResource.CopyRequest subFilerequest = service.Files.Copy(SubFile, child.Id); subFilerequest.Execute(); } } }
private static void CopyFile(DriveService driveService, DriveFile sourceFile, string parentFolderId) { DriveFile fileCopy = new DriveFile { MimeType = sourceFile.MimeType, Name = sourceFile.Name, Parents = new[] { parentFolderId }, }; FilesResource.CopyRequest copyRequest = driveService.Files.Copy(fileCopy, sourceFile.Id); copyRequest.SupportsAllDrives = true; _ = copyRequest.Execute(); }
private static void UploadFile(DriveService service) { File body = new File(); body.Title = "test image ocr - " + DateTime.Now.ToString(" - yyyyMMdd - HHmmss"); body.Description = "test image ocr - " + DateTime.Now.ToString(" - yyyyMMdd - HHmmss"); //body.MimeType = "application/vnd.ms-excel"; body.MimeType = "image/jpeg"; // File's content. byte[] byteArray = System.IO.File.ReadAllBytes("1.jpg"); MemoryStream stream = new MemoryStream(byteArray); try { //FilesResource.InsertMediaUpload request = service.Files.Insert(body, stream, "application/vnd.google-apps.spreadsheet"); FilesResource.InsertMediaUpload request = service.Files.Insert(body, stream, "application/vnd.google-apps.photo"); request.Ocr = true; request.OcrLanguage = "vi"; request.Convert = true; request.Upload(); File imgFile = request.ResponseBody; // Copy image and paste as document var textMetadata = new File(); //textMetadata.Name = inputFile.Name; //textMetadata.Parents = new List<string> { folderId }; textMetadata.MimeType = "application/vnd.google-apps.document"; FilesResource.CopyRequest requestCopy = service.Files.Copy(textMetadata, imgFile.Id); requestCopy.Fields = "id"; requestCopy.OcrLanguage = "vi"; var textFile = requestCopy.Execute(); // Now we export document as plain text FilesResource.ExportRequest requestExport = service.Files.Export(textFile.Id, "text/plain"); string output = requestExport.Execute(); // Uncomment the following line to print the File ID. // Console.WriteLine("File ID: " + file.Id); Console.WriteLine(output); } catch (Exception e) { Console.WriteLine("An error occurred: " + e.Message); } }
void CopyFiles() { Google.Apis.Drive.v3.Data.File file1 = new Google.Apis.Drive.v3.Data.File(); file1.Name = $"{DateTime.Now.ToString("yyyy-MM-dd HH-mm")} - Sao Lưu"; FilesResource.CopyRequest updateRequest1 = DService.Files.Copy(file1, SpreadsheetId); updateRequest1.Fields = "id, parents, name"; file1 = updateRequest1.Execute(); FilesResource.UpdateRequest updateRequest = DService.Files.Update(new Google.Apis.Drive.v3.Data.File(), file1.Id); updateRequest.Fields = "id, parents"; updateRequest.AddParents = "1i05huRMast4E7XPi3twLeAcWvTkdakJO"; file1 = updateRequest.Execute(); }
public OcrImageInfo goo_ocr_uploadFile(OcrImageInfo ocr) { string file = Path.Combine(PATH_OCR_IMAGE, ocr.FileName); if (File.Exists(file) == false) { ocr.StateOcr = STATE_OCR.OCR_FAIL_MISS_FILE; return(ocr); } DataFile body = new DataFile() { Title = ocr.FileName, Description = ocr.Url, //body.MimeType = "application/vnd.ms-excel"; MimeType = "image/jpeg" }; byte[] byteArray = File.ReadAllBytes(file); using (MemoryStream stream = new MemoryStream(byteArray)) { try { //FilesResource.InsertMediaUpload request = service.Files.Insert(body, stream, "application/vnd.google-apps.spreadsheet"); FilesResource.InsertMediaUpload request = gooService.Files.Insert(body, stream, "application/vnd.google-apps.photo"); request.Ocr = true; request.OcrLanguage = "vi"; request.Convert = true; request.Upload(); DataFile imgFile = request.ResponseBody; string fileId = imgFile.Id; // Copy image and paste as document var textMetadata = new DataFile(); //textMetadata.Name = inputFile.Name; //textMetadata.Parents = new List<string> { folderId }; textMetadata.MimeType = "application/vnd.google-apps.document"; FilesResource.CopyRequest requestCopy = gooService.Files.Copy(textMetadata, fileId); requestCopy.Fields = "id"; requestCopy.OcrLanguage = "vi"; var textFile = requestCopy.Execute(); // Now we export document as plain text FilesResource.ExportRequest requestExport = gooService.Files.Export(textFile.Id, "text/plain"); string output = requestExport.Execute(); ocr.TextResult = output; ocr.StateOcr = STATE_OCR.OCR_SUCCESS; writeLogMessage("OK: " + ocr.FileName); if (ocr.WriteToFile) { if (!string.IsNullOrEmpty(ocr.TextResult)) { File.WriteAllText(PATH_OCR_IMAGE + @"log\" + ocr.FileName + ".txt", ocr.TextResult); } } } catch (Exception e) { ocr.TextError = e.Message; ocr.StateOcr = STATE_OCR.OCR_FAIL_THROW_ERROR; } } return(ocr); }
public FileManagerResponse Copy(string path, string targetPath, string[] names, string[] replacedItemNames, FileManagerDirectoryContent TargetData, params FileManagerDirectoryContent[] data) { FileManagerResponse copyResponse = new FileManagerResponse(); DriveService service = GetService(); List <FileManagerDirectoryContent> copyFiles = new List <FileManagerDirectoryContent>(); ChildrenResource.ListRequest childRequest = service.Children.List(data[0].Id); ChildList children = childRequest.Execute(); List <string> childFileList = children.Items.Select(x => x.Id).ToList(); if (childFileList.IndexOf(TargetData.Id) != -1) { ErrorDetails er = new ErrorDetails(); er.Code = "400"; er.Message = "The destination folder is the subfolder of the source folder."; copyResponse.Error = er; return(copyResponse); } foreach (FileManagerDirectoryContent item in data) { File copyFile; try { File file = new File() { Title = item.Name, Parents = new List <ParentReference> { new ParentReference() { Id = TargetData.Id } } }; if (item.IsFile) { // Copy the file FilesResource.CopyRequest request = service.Files.Copy(file, item.Id); copyFile = request.Execute(); } else { file.MimeType = "application/vnd.google-apps.folder"; FilesResource.InsertRequest request = service.Files.Insert(file); request.Fields = "id"; copyFile = request.Execute(); copyFolderItems(item, copyFile.Id); } } catch (Exception e) { Console.WriteLine("An error occurred: " + e.Message); return(null); } File filedetails = service.Files.Get(copyFile.Id).Execute(); FileManagerDirectoryContent FileDetail = new FileManagerDirectoryContent(); FileDetail.Name = filedetails.Title; FileDetail.Id = filedetails.Id; FileDetail.IsFile = filedetails.MimeType == "application/vnd.google-apps.folder" ? false : true; FileDetail.Type = filedetails.FileExtension == null ? "folder" : filedetails.FileExtension; FileDetail.HasChild = getChildrenById(filedetails.Id); FileDetail.FilterId = obtainFilterId(filedetails); FileDetail.Size = item.Size; FileDetail.FilterPath = targetPath.Replace("/", @"\"); FileDetail.DateCreated = Convert.ToDateTime(filedetails.ModifiedDate); FileDetail.DateModified = Convert.ToDateTime(filedetails.ModifiedDate); copyFiles.Add(FileDetail); } copyResponse.Files = copyFiles; return(copyResponse); }
public static FilesResource.CopyRequest AsFileSystem(this FilesResource.CopyRequest request) { request.Fields = "id,name,createdTime,modifiedTime,size,md5Checksum"; return(request); }
protected FileInfo CopyFile(string parentId, FileInfo fileInfo) { try { FileInfoStatus currentStatus = API.DriveService.GetFileInfoStatus(fileInfo); File file = fileInfo._file; if (file.Parents.Count > 0) { file.Parents.RemoveAt(0); } var parentReference = new ParentReference { Id = parentId }; if (file.Parents == null) { file.Parents = new List <ParentReference>(); } file.Parents.Insert(0, parentReference); using (API.DriveService.Connection connection = API.DriveService.Connection.Create()) { FilesResource.CopyRequest request = connection.Service.Files.Copy(file, file.Id); request.Fields = API.DriveService.RequestFields.FileFields; _CancellationTokenSource = new System.Threading.CancellationTokenSource(); file = request.Execute(); fileInfo = GetFileInfo(file); FileInfoStatus newStatus = API.DriveService.GetFileInfoStatus(fileInfo); if (currentStatus == FileInfoStatus.OnDisk) { DateTime modifiedDate = API.DriveService.GetDateTime(file.ModifiedDate); try { API.DriveService.SetFileLastWriteTime(fileInfo.FilePath, modifiedDate); } catch { } fileInfo = GetFileInfo(file); newStatus = API.DriveService.GetFileInfoStatus(fileInfo); } return(fileInfo); } } catch (Exception exception) { Log.Error(exception); return(null); } }