internal static async Task <File> ShareFile(this DriveService Service, File File, DriveService AllowTo = null) { var Request = Service.Permissions.Create(new Permission() { Role = "reader", Type = AllowTo == null ? "anyone" : "user", EmailAddress = AllowTo == null ? null : (await AllowTo.GetUserInfo()).EmailAddress }, File.Id); Request.SupportsAllDrives = true; Permission Rst; while (true) { try { Rst = await Request.ExecuteAsync(); File.Permissions = new List <Permission>(); File.Permissions.Add(Rst); return(File); } catch (Exception ex) { Console.WriteLine(ex); if (ex.IsRemoteException()) { await Task.Delay(1000 * 10);//10 sec continue; } throw; } } }
public async static Task OpenServer() { ApiCredentials = new ClientSecrets(); string CustomCredentials = Path.Combine(AppDataDirectory, "credentials.json"); if (System.IO.File.Exists(CustomCredentials)) { using (var Stream = new MemoryStream(System.IO.File.ReadAllBytes(CustomCredentials))) ApiCredentials = GoogleClientSecrets.Load(Stream).Secrets; } else { ApiCredentials.ClientId = "1028112365169-3iss4ffrp626a31lc96pn3c3cujkbdg2.apps.googleusercontent.com"; ApiCredentials.ClientSecret = "lDb5KUCJt6gLrOtHNScZ6QYm"; } if (ServerInstance == null) { Message("Null server instance", "dbg", MessageType.Other, ButtonsType.Ok); return; } using (NamedPipeServerStream Server = new NamedPipeServerStream(ServerName + (ServerInstance ?? "GLOBAL"))) { while (true) { try { if (!Server.IsConnected) { await Server.WaitForConnectionAsync(); } var Cmd = (Commands)await Server.ReadU16(); switch (Cmd) { case Commands.PING: await Server.WriteBool(true); break; case Commands.InterAccounts: await Server.WriteBool(UserInfoA.EmailAddress != UserInfoB.EmailAddress); break; case Commands.CloseServer: Server.Close(); return; case Commands.ConnectA: CredentialsA = await GoogleWebAuthorizationBroker.AuthorizeAsync(ApiCredentials, Scopes, UserA, System.Threading.CancellationToken.None); ServiceA = new DriveService(new BaseClientService.Initializer() { HttpClientInitializer = CredentialsA, ApplicationName = "DriveMirror" }); UserInfoA = await ServiceA.GetUserInfo(); break; case Commands.ConnectB: CredentialsB = await GoogleWebAuthorizationBroker.AuthorizeAsync(ApiCredentials, Scopes, UserB, System.Threading.CancellationToken.None); ServiceB = new DriveService(new BaseClientService.Initializer() { HttpClientInitializer = CredentialsB, ApplicationName = "DriveMirror" }); UserInfoB = await ServiceB.GetUserInfo(); break; case Commands.Disconnect: await CredentialsA.RevokeTokenAsync(CancellationToken.None); await CredentialsB.RevokeTokenAsync(CancellationToken.None); break; case Commands.EnumDrivesA: var DrivesA = await ServiceA.EnumDrives().ToListAsync(); await Server.WriteU32((uint)DrivesA.Count); foreach (var Drive in DrivesA) { await Server.WriteString(Drive.Name); await Server.WriteString(Drive.Id); } break; case Commands.EnumDrivesB: var DrivesB = await ServiceB.EnumDrives().ToListAsync(); await Server.WriteU32((uint)DrivesB.Count); foreach (var Drive in DrivesB) { await Server.WriteString(Drive.Name); await Server.WriteString(Drive.Id); } break; case Commands.GetFileByIdA: var FileAID = await Server.ReadString(); var AInfo = await ServiceA.GetFileById(FileAID); await Server.WriteFileInfo(AInfo); break; case Commands.GetFileByIdB: var FileBID = await Server.ReadString(); var BInfo = await ServiceB.GetFileById(FileBID); await Server.WriteFileInfo(BInfo); break; case Commands.ParsePathA: var APath = await Server.ReadString(); var AFInfo = await ServiceA.TranslatePath(APath, DriveA, true); await Server.WriteFileInfo(AFInfo); break; case Commands.ParsePathB: var BPath = await Server.ReadString(); var BFInfo = await ServiceB.TranslatePath(BPath, DriveB, true); await Server.WriteFileInfo(BFInfo); break; case Commands.EnumFilesA: var AEFID = await Server.ReadString(); var ATrashed = await Server.ReadBool(); if (AEFID.StartsWith("/")) { AEFID = (await ServiceA.TranslatePath(AEFID)).Id; } await ServiceA.EnumFiles(AEFID, DriveA, true, ATrashed).ForEachAsync(async x => { await Server.WriteBool(true); await Server.WriteFileInfo(x); }); await Server.WriteBool(false); break; case Commands.EnumFilesB: var BEFID = await Server.ReadString(); var BTrashed = await Server.ReadBool(); if (BEFID.StartsWith("/")) { BEFID = (await ServiceB.TranslatePath(BEFID)).Id; } await ServiceB.EnumFiles(BEFID, DriveB, true, BTrashed).ForEachAsync(async x => { await Server.WriteBool(true); await Server.WriteFileInfo(x); }); await Server.WriteBool(false); break; case Commands.EnumFoldersA: var AEDID = await Server.ReadString(); var ADTrashed = await Server.ReadBool(); if (AEDID.StartsWith("/")) { AEDID = (await ServiceA.TranslatePath(AEDID)).Id; } await ServiceA.EnumFolders(AEDID, DriveA, ADTrashed).ForEachAsync(async x => { await Server.WriteBool(true); await Server.WriteFileInfo(x); }); await Server.WriteBool(false); break; case Commands.EnumFoldersB: var BEDID = await Server.ReadString(); var BDTrashed = await Server.ReadBool(); if (BEDID.StartsWith("/")) { BEDID = (await ServiceB.TranslatePath(BEDID)).Id; } await ServiceB.EnumFolders(BEDID, DriveB, BDTrashed).ForEachAsync(async x => { await Server.WriteBool(true); await Server.WriteFileInfo(x); }); await Server.WriteBool(false); break; case Commands.CopyFileA: string ACPYID = await Server.ReadString(); string ACPYNM = await Server.ReadString(); string ACPYDR = await Server.ReadString(); var ACPYNF = await ServiceA.CopyFile(ACPYID, ACPYNM, ACPYDR); await Server.WriteFileInfo(ACPYNF); break; case Commands.CopyFileB: string BCPYID = await Server.ReadString(); string BCPYNM = await Server.ReadString(); string BCPYDR = await Server.ReadString(); var BCPYNF = await ServiceB.CopyFile(BCPYID, BCPYNM, BCPYDR); await Server.WriteFileInfo(BCPYNF); break; case Commands.DeleteFileA: string ADELID = await Server.ReadString(); await ServiceA.Delete(await ServiceA.GetFileById(ADELID)); break; case Commands.DeleteFileB: string BDELID = await Server.ReadString(); await ServiceB.Delete(await ServiceB.GetFileById(BDELID)); break; case Commands.CreateDirectoryA: string NewDirPathA = await Server.ReadString(); var NDirA = await ServiceA.CreateDirectory(NewDirPathA, DriveA); await Server.WriteFileInfo(NDirA); break; case Commands.CreateDirectoryB: string NewDirPathB = await Server.ReadString(); var NDirB = await ServiceB.CreateDirectory(NewDirPathB, DriveB); await Server.WriteFileInfo(NDirB); break; case Commands.SelectDriveA: string ADriveID = await Server.ReadString(); DriveA = new Drive() { Id = ADriveID }; break; case Commands.SelectDriveB: string BDriveID = await Server.ReadString(); DriveB = new Drive() { Id = BDriveID }; break; case Commands.ShareFileA: string ASFID = await Server.ReadString(); var ASFInfo = await ServiceA.ShareFile(ASFID, ServiceB); await Server.WriteFileInfo(ASFInfo); break; case Commands.ShareFileB: string BSFID = await Server.ReadString(); var BSFInfo = await ServiceB.ShareFile(BSFID, ServiceA); await Server.WriteFileInfo(BSFInfo); break; case Commands.StopShareA: var StpFileInfA = await Server.ReadFileInfo(); var NWFileInfA = await ServiceA.StopSharing(StpFileInfA); await Server.WriteFileInfo(NWFileInfA); break; case Commands.StopShareB: var StpFileInfB = await Server.ReadFileInfo(); var NWFileInfB = await ServiceB.StopSharing(StpFileInfB); await Server.WriteFileInfo(NWFileInfB); break; } await Server.FlushAsync(); } catch (Exception ex) { if (!Client.IsConnected) { return; } Message(ex.ToString(), "DriveMirror Service", MessageType.Error, ButtonsType.Ok); } } } }