internal static DriveAPIHttprequestv2 GetAPIv2(string Email, GD_LimitExceededDelegate LimitExceeded = null) { DriveAPIHttprequestv2 gdclient = new DriveAPIHttprequestv2(JsonConvert.DeserializeObject <TokenGoogleDrive>(AppSetting.settings.GetToken(Email, CloudType.GoogleDrive)), LimitExceeded); if (string.IsNullOrEmpty(gdclient.Token.Email) || gdclient.Token.Email != Email) { gdclient.Token.Email = Email; } gdclient.TokenRenewEvent += Gdclient_TokenRenewEvent; return(gdclient); }
public static GD_Files_list Search(string query, string Email, string pageToken = null) { DriveAPIHttprequestv2 gdclient = GetAPIv2(Email); GD_Files_list list = JsonConvert.DeserializeObject <GD_Files_list>(gdclient.Files.List(en, query, CorpusEnum.DEFAULT, ProjectionEnum.BASIC, pageToken)); if (!string.IsNullOrEmpty(list.nextPageToken)) { list.items.AddRange(Search(query, Email, list.nextPageToken).items); } list.nextPageToken = null; return(list); }
public static bool ReNameItem(ExplorerNode node, string newname) { DriveAPIHttprequestv2 gdclient = GetAPIv2(node.GetRoot.NodeType.Email); string json = "{\"title\": \"" + newname + "\"}"; string response = gdclient.Files.Patch(node.Info.ID, json); dynamic json_ = JsonConvert.DeserializeObject(response); string name = json_.title; if (name == newname) { return(true); } else { return(false); } }
public static void CreateFolder(ExplorerNode node) { string Email = node.GetRoot.NodeType.Email; DriveAPIHttprequestv2 gdclient = GetAPIv2(Email); string parent_id = ""; try { List <ExplorerNode> listnode = node.GetFullPath(); Monitor.Enter(sync_createfolder); int i; for (i = listnode.Count - 1; i > 0; i--) { if (!string.IsNullOrEmpty(listnode[i].Info.ID)) { parent_id = listnode[i].Info.ID; break; } } i++; bool create = false; for (; i < listnode.Count; i++) { if (!create) { List <ExplorerNode> listsearchnode = Search("'" + parent_id + "' in parents" + " and trashed=false" + " and title='" + listnode[i].Info.Name.Replace("'", "\\'") + "'" + " and mimeType = 'application/vnd.google-apps.folder'", Email).Convert(node); if (listsearchnode.Count == 0) { create = true; } else { parent_id = listsearchnode[0].Info.ID; } } if (create) { gdclient.Extend.CreateFolder(listnode[i].Info.Name, parent_id); } } } finally { Monitor.Exit(sync_createfolder); } }
public static GD_item MoveItem(ExplorerNode nodemove, ExplorerNode newparent, string newname = null, bool copy = false) { //Same account if (nodemove.GetRoot.NodeType.Email != newparent.GetRoot.NodeType.Email) { throw new Exception("Email not match."); } if (nodemove.GetRoot.NodeType.Type != newparent.GetRoot.NodeType.Type) { throw new Exception("TypeCloud not match."); } DriveAPIHttprequestv2 gdclient = GetAPIv2(nodemove.GetRoot.NodeType.Email); GD_item item_metadata = JsonConvert.DeserializeObject <GD_item>(gdclient.Files.Patch(nodemove.Info.ID)); if (nodemove.Parent != newparent) { if (!copy) { foreach (GD_parent parent in item_metadata.parents) { if (parent.id == nodemove.Parent.Info.ID) { item_metadata.parents.Remove(parent); break; } } } bool isroot = false; if (AppSetting.settings.GetCloudRootNode(gdclient.Token.Email, CloudType.GoogleDrive).Info.ID == newparent.Parent.Info.ID) { isroot = true; } item_metadata.parents.Add(new GD_parent() { id = newparent.Parent.Info.ID, isRoot = isroot }); } if (newname != null) { item_metadata.title = newname; } return(JsonConvert.DeserializeObject <GD_item>(gdclient.Files.Patch(nodemove.Info.ID, JsonConvert.SerializeObject(item_metadata)))); }
//trash/delete public static bool File_trash(ExplorerNode node, bool Permanently) { DriveAPIHttprequestv2 gdclient = GetAPIv2(node.GetRoot.NodeType.Email); if (node == node.GetRoot) { throw new Exception("Can't delete root."); } if (Permanently) { gdclient.Files.Delete(node.Info.ID); return(true); } else { dynamic json = JsonConvert.DeserializeObject(gdclient.Files.Trash(node.Info.ID)); return(true); } }
private void Oauth_gd_TokenCallBack(string token_) { if (!string.IsNullOrEmpty(token_)) { TokenGoogleDrive token = JsonConvert.DeserializeObject <TokenGoogleDrive>(token_); if (token.IsError) { throw new Exception("Accesstoken:" + token.access_token + ",RefreshToken:" + token.refresh_token); } string token_text = JsonConvert.SerializeObject(token); DriveAPIHttprequestv2 client = new DriveAPIHttprequestv2(token); dynamic about = JsonConvert.DeserializeObject(client.About.Get()); string email = about.user.emailAddress; SaveToken(email, token_text, CloudType.GoogleDrive); } else { throw new Exception("Oauth token GD failed."); } }
void Transfer(TransferItem item) { #if DEBUG Console.WriteLine("Transfer items:" + item.From.node.GetFullPathString()); #endif int buffer_length = 32; //default int.TryParse(AppSetting.settings.GetSettingsAsString(SettingsKey.BufferSize), out buffer_length); //get buffer_length from setting item.buffer = item.From.node.GetRoot.NodeType.Type == CloudType.Mega ? new byte[buffer_length * 2048] : new byte[buffer_length * 1024]; //create buffer ExplorerNode rootnodeto = item.To.node.GetRoot; item.byteread = 0; //this.group.items[x].UploadID = "";//resume item.SizeWasTransfer = item.OldTransfer = item.SaveSizeTransferSuccess; //resume item.ErrorMsg = ""; //clear error item.TimeStamp = CurrentMillis.Millis; if (GroupData.status != StatusTransfer.Running) { return; } switch (rootnodeto.NodeType.Type) { case CloudType.LocalDisk: #region LocalDisk ItemsTransferWork.Add(new TransferBytes(item, this)); return; #endregion case CloudType.Dropbox: #region Dropbox int chunksizedb = 25; //default 25Mb int.TryParse(AppSetting.settings.GetSettingsAsString(SettingsKey.Dropbox_ChunksSize), out chunksizedb); item.ChunkUploadSize = chunksizedb * 1024 * 1024; DropboxRequestAPIv2 DropboxClient = Dropbox.GetAPIv2(rootnodeto.NodeType.Email); if (string.IsNullOrEmpty(item.UploadID)) //create upload id { item.byteread = item.From.stream.Read(item.buffer, 0, item.buffer.Length); IDropbox_Request_UploadSessionAppend session = DropboxClient.upload_session_start(item.buffer, item.byteread); item.UploadID = session.session_id; item.SizeWasTransfer += item.byteread; } ItemsTransferWork.Add(new TransferBytes(item, this, DropboxClient)); return; #endregion case CloudType.GoogleDrive: #region GoogleDrive DriveAPIHttprequestv2 gdclient = GoogleDrive.GetAPIv2(rootnodeto.NodeType.Email); GoogleDrive.CreateFolder(item.To.node.Parent); int chunksizeGD = 5; //default int.TryParse(AppSetting.settings.GetSettingsAsString(SettingsKey.GD_ChunksSize), out chunksizeGD); item.ChunkUploadSize = chunksizeGD * 1024 * 1024; if (string.IsNullOrEmpty(item.UploadID)) //create upload id { if (string.IsNullOrEmpty(item.To.node.Parent.Info.ID)) { throw new Exception("Can't get root id."); } string parentid = item.To.node.Parent.Info.ID; string mimeType = Get_mimeType.Get_mimeType_From_FileExtension(item.To.node.GetExtension()); string jsondata = "{\"title\": \"" + item.From.node.Info.Name + "\", \"mimeType\": \"" + mimeType + "\", \"parents\": [{\"id\": \"" + parentid + "\"}]}"; item.UploadID = gdclient.Files.Insert_ResumableGetUploadID(jsondata, mimeType, item.From.node.Info.Size); } ItemsTransferWork.Add(new TransferBytes(item, this, gdclient)); return; #endregion case CloudType.Mega: #region Mega MegaApiClient MegaClient = MegaNz.GetClient(rootnodeto.NodeType.Email); item.buffer = new byte[128 * 1024]; if (string.IsNullOrEmpty(item.UploadID)) //create upload id { MegaNz.AutoCreateFolder(item.To.node.Parent); //auto create folder item.UploadID = MegaClient.RequestUrlUpload(item.From.node.Info.Size); //Make Upload url } item.From.stream = MegaApiClient.MakeEncryptStreamForUpload(item.From.stream, item.From.node.Info.Size, item.dataCryptoMega); //make encrypt stream from file ItemsTransferWork.Add(new TransferBytes(item, this, MegaClient)); return; #endregion } }
public static GD_item GetMetadataItem(ExplorerNode node) { DriveAPIHttprequestv2 client = GetAPIv2(node.GetRoot.NodeType.Email); return(JsonConvert.DeserializeObject <GD_item>(client.Files.Patch(node.Info.ID, null))); }
public static Stream GetFileStream(ExplorerNode node, long Startpos = -1, long endpos = -1) { DriveAPIHttprequestv2 gdclient = GetAPIv2(node.GetRoot.NodeType.Email); return(gdclient.Files.Get(node.Info.ID, Startpos, endpos)); }