private static Task <UserCredential> GetUserCredential(IConfiguration configuration) { return(GoogleWebAuthorizationBroker.AuthorizeAsync( new ClientSecrets { ClientId = configuration["client_id"], ClientSecret = configuration["client_secret"] }, new[] { Oauth2Service.Scope.UserinfoEmail, DriveService.Scope.DriveReadonly }, "user", CancellationToken.None, new LiteDbDataStore(LiteDbHelper.GetFilePath(@"MonoDrive.db")))); }
public LiteDbFixture() { Database = new LiteDatabase(LiteDbHelper.GetFilePath(@"MonoDrive.db")); }
/// <summary> /// Baixa estrutura de diretórios e a adiciona ao caminho passado como parâmetro /// </summary> /// <param name="parentDirectoryPath">Caminho no qual será reproduzida a estrutura de diretórios remotos</param> /// <returns></returns> public async Task DownloadAndCreateFolders(string parentDirectoryPath) { //TODO: Tratar possíveis excessões relacionadas à construção do DirectoryInfo antes de baixar as informações //var parentDirectoryInfo = new DirectoryInfo(parentDirectoryPath); var folders = await DownloadFoldersStructure(); var stopwatch = new Stopwatch(); stopwatch.Start(); //Pastas compartilhadas são retornadas como órfãs folders = folders.Where(x => x.Parents != null).ToArray(); //TODO: Verificar se é interessante salvar o id da pasta raíz //A pasta raíz não é retornada pela consulta de diretórios var rootFolder = await _driveService.Files.Get("root").ExecuteAsync(); var rootChildren = folders.Where(x => x.Parents.Contains(rootFolder.Id)); var directoriesInfo = new List <LocalDirectoryInfo>(); void CreateFolder(File remoteFolder, string localPath) { var localFolderPath = Path.Combine(localPath, remoteFolder.Name); var directoryInfo = new DirectoryInfo(localFolderPath); directoryInfo.Create(); directoriesInfo.Add(new LocalDirectoryInfo { RemoteId = remoteFolder.Id, Attributes = directoryInfo.Attributes, Exists = directoryInfo.Exists, FullName = directoryInfo.FullName, CreationTimeUtc = directoryInfo.CreationTimeUtc, LastWriteTimeUtc = directoryInfo.LastWriteTimeUtc, LastAccessTimeUtc = directoryInfo.LastAccessTimeUtc, ParentRemoteId = remoteFolder.Parents[0] }); var children = folders.Where(y => y.Parents.Contains(remoteFolder.Id)); foreach (var childrenFolder in children) { CreateFolder(childrenFolder, localFolderPath); } } foreach (var folder in rootChildren) { CreateFolder(folder, parentDirectoryPath); } LocalDirectoryInfo[] newDirectories; using (var db = new LiteDatabase(LiteDbHelper.GetFilePath(@"MonoDrive.db"))) { var collection = db.GetCollection <LocalDirectoryInfo>(); var localDirectoriesInfo = collection.FindAll().ToArray(); newDirectories = directoriesInfo.Where(x => localDirectoriesInfo.All(y => y.RemoteId != x.RemoteId)).ToArray(); //TODO: Remover diretórios excluídos do servidor remoto collection.EnsureIndex(x => x.RemoteId, true); collection.InsertBulk(newDirectories); } //var directoriesInfo = parentDirectoryInfo.GetDirectories("*.*", SearchOption.AllDirectories); _logger.LogInformation( $"{newDirectories.Length} new directories successfully created. Elapsed time: {stopwatch.Elapsed.Milliseconds} milliseconds."); }