public static T GetGFile <T>(this File file) where T : AFileInfo { AFileInfo gfile = null; if (typeof(T) == typeof(AFolderInfo)) { gfile = new AFolderInfo(); } else { gfile = new AFileInfo(); } gfile.Id = file.Id; gfile.MimeType = file.MimeType; gfile.FileName = file.Title; gfile.DisplayName = file.Title; gfile.Description = file.Title; if (file.Parents != null && file.Parents.Count > 0) { gfile.ParentId = file.Parents.First().Id; } return((T)gfile); }
private Task <FileList> getFileMetadata(AFileInfo file, DriveService service) { if (service != null) { var request = service.Files.List(); if (file == null) { request.Q = string.Format( "mimeType!='{0}' and trashed=false", Constants.GFolderIdentifier); return(request.ExecuteAsync()); } if (file.IsFolder) { request.Q = string.Format( "mimeType='{0}' and trashed=false and title='{1}'", file.MimeType, file.FileName); } else { request.Q = string.Format("trashed=false and title='{0}'", file.FileName); } if (!string.IsNullOrWhiteSpace(file.ParentId)) { request.Q += string.Format(" and '{0}' in parents", file.ParentId); } return(request.ExecuteAsync()); } return(null); }
public File CreateFile(AFileInfo file, DriveService service, IO.Stream stream = null) { if (string.IsNullOrWhiteSpace(file.MimeType)) { file.MimeType = GetMimeType(file.FileName); } var body = new File { Title = file.FileName, MimeType = file.MimeType }; if (!string.IsNullOrWhiteSpace(file.ParentId)) { if (body.Parents == null) { body.Parents = new List <ParentReference>(); } body.Parents.Add(new ParentReference { Id = file.ParentId }); } if (stream == null) { return(service.Files.Insert(body).Execute()); } else { var request = service.Files.Insert(body, stream, ""); request.Upload(); return(request.ResponseBody); } }
private void UpdateLocaDbFromGDrive(DriveService service) { var rootDir = GetDriveFileMetadata(new AFolderInfo { FileName = AppConfig.APP_DRIVE_FOLDER }, service, CreateIfNotExist: true); if (rootDir != null) { var dir = rootDir.GetGFile <AFolderInfo>(); dir.DisplayName = "Drive"; userConfig.DB.AssistMeDrive = dir; HttpContext.Current.Session["UserConfig"] = userConfig; var gFile = new AFileInfo { FileName = userConfig.DB_NAME, ParentId = userConfig.DB.AssistMeDrive.Id }; var dbFile = GetDriveFileMetadata(gFile, service); if (dbFile != null)//db file exists in GDrive { if (!IO.File.Exists(LocalDbPath)) { var request = service.HttpClient.GetByteArrayAsync(dbFile.DownloadUrl); var content = Encoding.ASCII.GetString(request.Result); IO.File.WriteAllText(LocalDbPath, content); userConfig.DB = JsonConvert.DeserializeObject <AssistMeDb>(content); HttpContext.Current.Session["UserConfig"] = userConfig; } else//Local file exist, remove file exist { //Let it be there, will think about it. //have to consider the one with recent changes } } else if (IO.File.Exists(LocalDbPath))//db file not in GDrive but exist localy, upload local to Drive { } else//File not available anywhere, create new and upload to gdrive as well as local { userConfig.DB.AssistMeDrive.Children.Add(gFile); byte[] byteArray = Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(userConfig.DB)); using (var stream = new IO.MemoryStream(byteArray)) { var uploadedFile = CreateFile(gFile, service, stream); userConfig.DB.AssistMeDrive.Children.Remove(gFile); var aUploadedFile = uploadedFile.GetGFile <AFileInfo>(); aUploadedFile.IsSystemFile = true; aUploadedFile.DisplayName = "DB"; userConfig.DB.AssistMeDrive.Children.Add(aUploadedFile); } HttpContext.Current.Session["UserConfig"] = userConfig; WriteDBDataToLocalDB(); } } }
public File GetDriveFileMetadata(AFileInfo file, DriveService service, bool CreateIfNotExist = false) { if (file != null && !string.IsNullOrWhiteSpace(file.Id)) { return(service.Files.Get(file.Id).ExecuteAsync().Result); } var result = getFileMetadata(file, service).Result; if (result != null && result.Items.Count > 0) { return(result.Items.First()); } else if (CreateIfNotExist) { return(CreateFile(file, service)); } return(null); }
public ActionResult Create(FormCollection collection) { var userConfig = (UserConfig)Session["UserConfig"]; try { var fileInfo = new AFileInfo { Id = collection["Id"], Description = collection["Description"], DetailedInfo = collection["DetailedInfo"], DisplayName = collection["DisplayName"], FolderName = collection["FolderName"], }; var folder = new AFolderInfo { ParentId = userConfig.DB.AssistMeDrive.Id, FileName = fileInfo.FolderName, DisplayName = fileInfo.FolderName, }; var service = HttpContext.Session["DriveService"] as DriveService; var ghelper = new GDriveHelper(); var serverFolder = ghelper.GetDriveFileMetadata(folder, service, CreateIfNotExist: true); folder.Id = serverFolder.Id; //check folder already exist var existingFolder = userConfig.DB.AssistMeDrive.GetChild(folder.Id); if (existingFolder == null) { var parentofFolder = userConfig.DB.AssistMeDrive.GetChild(folder.ParentId); parentofFolder.Children.Add(folder); } //TODO: If folder changed while editing, Remove the file from older folder fileInfo.ParentId = folder.Id; Google.Apis.Drive.v2.Data.File driveFile = null; if (Request.Files.Count > 0 && Request.Files[0].ContentLength > 0) { var file = Request.Files[0]; fileInfo.FileName = file.FileName; driveFile = ghelper.CreateFile(fileInfo, service, file.InputStream); } else { driveFile = ghelper.GetDriveFileMetadata(fileInfo, service); } fileInfo.Id = driveFile.Id; fileInfo.DownloadUrl = driveFile.DownloadUrl; fileInfo.ThumbnailUrl = driveFile.ThumbnailLink; var existingFile = userConfig.DB.AssistMeDrive.GetChild(fileInfo.Id); if (existingFile != null && existingFile.ParentId != null) { var folderOfFile = userConfig.DB.AssistMeDrive.GetChild(existingFile.ParentId); folderOfFile.Children.Remove(folderOfFile.Children.First(f => f.Id == existingFile.Id)); } var parentFolderInDb = userConfig.DB.AssistMeDrive.GetChild(fileInfo.ParentId); parentFolderInDb.Children.Add(fileInfo); var dbFile = userConfig.DB.AssistMeDrive.Children.First(f => f.FileName != null && f.FileName == userConfig.DB_NAME); if (string.IsNullOrWhiteSpace(dbFile.Id)) { var serverDbFile = ghelper.GetDriveFileMetadata(new AFileInfo { ParentId = userConfig.DB.AssistMeDrive.Id, FileName = userConfig.DB_NAME, }, service); dbFile.Id = serverDbFile.Id; dbFile.IsSystemFile = true; } ghelper.WriteDBDataToLocalDB(); if (log.IsDebugEnabled) { log.DebugFormat("Document add/update completed without error. User:{0}", userConfig.User_Id); } return(RedirectToAction("Index", "Home")); } catch (Exception exp) { log.Error(string.Format("User:{0}, Message:{1}", userConfig.User_Id, exp.Message), exp); return(RedirectToAction("Index", "Home")); } }