Exemplo n.º 1
0
        internal static async Task <File> StopSharing(this DriveService Service, File File)
        {
            var Perm     = (from x in File.Permissions where x.Type == "anyone" select x).Single();
            var PRequest = Service.Permissions.Delete(File.Id, Perm.Id);

            PRequest.SupportsAllDrives = true;
            PRequest.Fields            = "*";
            await PRequest.ExecuteAsync();

            while (true)
            {
                try
                {
                    await PRequest.ExecuteAsync();

                    break;
                }
                catch (Exception ex)
                {
                    if (ex.IsRemoteException())
                    {
                        await Task.Delay(1000 * 10);//10 sec

                        continue;
                    }
                    throw;
                }
            }

            return(await Service.GetFileById(File.Id));
        }
Exemplo n.º 2
0
        internal static async Task <File> CopyFile(this DriveService Service, string FromId, string OriginalName = null, File NewDirectory = null)
        {
            File SaveAs = new File();

            SaveAs.Name = OriginalName ?? (await Service.GetFileById(FromId)).Name;

            if (NewDirectory != null)
            {
                SaveAs.Parents = new List <string>();
                SaveAs.Parents.Add(NewDirectory.Id);
            }

            var Request = Service.Files.Copy(SaveAs, FromId);

            Request.SupportsAllDrives = true;
            Request.Fields            = "*";

            while (true)
            {
                try
                {
                    return(await Request.ExecuteAsync());
                }
                catch (Exception ex)
                {
                    if (ex.IsRemoteException())
                    {
                        await Task.Delay(1000 * 10);//10 sec

                        continue;
                    }
                    throw;
                }
            }
        }
Exemplo n.º 3
0
        internal static async Task <File> CreateDirectory(this DriveService Service, string DirectoryPath, Drive Drive = null)
        {
            DirectoryPath = DirectoryPath.TrimEnd(PathSplitChars);
            string DirName = DirectoryPath.Split(PathSplitChars).Last();

            DirectoryPath = DirectoryPath.Substring(0, DirectoryPath.LastIndexOfAny(PathSplitChars) + 1);

            var Parent = await Service.TranslatePath(DirectoryPath, Drive);

            if (Parent == null)
            {
                Parent = await Service.CreateDirectory(DirectoryPath, Drive);
            }

            File NewDir = new File();

            NewDir.Name     = DirName;
            NewDir.MimeType = GoogleFolderMimeType;
            NewDir.Parents  = new List <string>();
            NewDir.Parents.Add(Parent.Id);

            var Request = Service.Files.Create(NewDir);

            Request.Fields            = "id";
            Request.SupportsAllDrives = true;
            File Rst;

            while (true)
            {
                try
                {
                    Rst = await Request.ExecuteAsync();

                    break;
                }
                catch (Exception ex)
                {
                    if (ex.IsRemoteException())
                    {
                        await Task.Delay(1000 * 10);//10 sec

                        continue;
                    }
                    throw;
                }
            }

            return(await Service.GetFileById(Rst.Id));
        }
Exemplo n.º 4
0
    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);
                }
            }
        }
    }
Exemplo n.º 5
0
        internal static async Task <File> TranslatePath(this DriveService Service, string fPath, Drive Drive = null, bool Metadata = false)
        {
            var Queries = (from x in QueryCache where x.Service == Service && x.Drive == Drive && x.Path == fPath select x).ToArray();

            if (Queries.Length == 1)
            {
                return(Queries.Single().Result);
            }

            string BaseID    = Drive?.Id ?? "root";
            var    CurParent = await Service.GetFileById(BaseID);

            string FullPath = fPath.Trim(System.IO.Path.GetInvalidPathChars());

            string[] Path = FullPath.Trim(PathSplitChars).Split(PathSplitChars);

            bool IsDir = FullPath.EndsWith("\\") || FullPath.EndsWith("/");

            if (Path.Length == 1 && Path[0] == string.Empty)
            {
                return(CurParent);
            }

            int Begin = 0;

            for (int i = Path.Length; i >= 0; i--)
            {
                string PartialPath = string.Join("/", Path.Take(i));
                var    Rst         = (from x in QueryCache where x.Service == Service && x.Drive == Drive && x.Path.Trim(PathSplitChars) == PartialPath select x).ToArray();
                if (Rst.Length == 1)
                {
                    Begin     = i;
                    CurParent = Rst.Single().Result;
                    break;
                }
            }

            for (int i = Begin; i < Path.Length; i++)
            {
                string Name   = Path[i];
                bool   IsLast = i + 1 >= Path.Length;
                if (!IsLast || IsDir)
                {
                    var Dirs = await Service.EnumFolders(CurParent, Drive).ToListAsync();

                    CurParent = null;
                    foreach (var Dir in Dirs)
                    {
                        if (Dir.Name == Name)
                        {
                            CurParent = Dir;
                            break;
                        }
                    }

                    if (CurParent == null)
                    {
                        return(null);
                    }
                    continue;
                }
                var Files = await Service.EnumFiles(CurParent, Drive, Metadata).ToListAsync();

                CurParent = null;
                foreach (var File in Files)
                {
                    if (File.Name == Name)
                    {
                        CurParent = File;
                        break;
                    }
                }
                if (CurParent == null)
                {
                    return(null);
                }

                break;
            }

            QueryCache.Add(new QueryInfo()
            {
                Service = Service,
                Drive   = Drive,
                Path    = fPath,
                Result  = CurParent
            });

            return(CurParent);
        }