diff --git a/AsbCloudInfrastructure/Services/GoogleDriveService.cs b/AsbCloudInfrastructure/Services/GoogleDriveService.cs index 8a26609c..a19ef137 100644 --- a/AsbCloudInfrastructure/Services/GoogleDriveService.cs +++ b/AsbCloudInfrastructure/Services/GoogleDriveService.cs @@ -1,3 +1,4 @@ +using System; using Google.Apis.Auth.OAuth2; using Google.Apis.Auth.OAuth2.Flows; using Google.Apis.Auth.OAuth2.Responses; @@ -14,9 +15,11 @@ using AsbCloudApp.Services; namespace AsbCloudInfrastructure.Services { - public class GoogleDriveService : IGoogleDriveService + public class GoogleDriveService : IGoogleDriveService, IDisposable { - private static DriveService GetService() + private readonly DriveService service; + + public GoogleDriveService() { // ключи для почты asbautodrilling@gmail.com. var tokenResponse = new TokenResponse { @@ -45,12 +48,11 @@ namespace AsbCloudInfrastructure.Services HttpClientInitializer = credential, ApplicationName = applicationName }); - return newService; + service = newService; } public IEnumerable GetAllFiles() { - using var service = GetService(); var fileList = service.Files.List(); fileList.Fields = "files(id, webViewLink, size)"; //fileList.Q =$"mimeType!='application/vnd.google-apps.folder' and '{folder}' in parents"; @@ -73,7 +75,6 @@ namespace AsbCloudInfrastructure.Services public async Task GetFileWebLinkAsync(string idFile, CancellationToken token = default) { - using var service = GetService(); var fileList = service.Files.List(); fileList.Fields = "files(id, webViewLink, size)"; var filesResult = await fileList.ExecuteAsync(token) @@ -84,8 +85,7 @@ namespace AsbCloudInfrastructure.Services public async Task CreateFolderAsync(string folderName, CancellationToken token = default) - { - using var service = GetService(); + { var driveFolder = new Google.Apis.Drive.v3.Data.File(); driveFolder.Name = folderName; driveFolder.MimeType = "application/vnd.google-apps.folder"; @@ -99,7 +99,6 @@ namespace AsbCloudInfrastructure.Services public async Task CreatePublicPermissionForFileAsync(string idFile, CancellationToken token = default) { - using var service = GetService(); var permission = new Permission() { Type = "anyone", Role = "reader"}; var addPermissionRequest = service.Permissions.Create(permission, idFile); await addPermissionRequest.ExecuteAsync(token) @@ -109,7 +108,6 @@ namespace AsbCloudInfrastructure.Services public async Task UploadFileAsync(Stream file, string fileName, string fileMime, string fileDescription, CancellationToken token = default) { - using var service = GetService(); var driveFile = new Google.Apis.Drive.v3.Data.File(); driveFile.Name = fileName; driveFile.Description = fileDescription; @@ -130,10 +128,14 @@ namespace AsbCloudInfrastructure.Services public async Task DeleteFileAsync(string fileId, CancellationToken token = default) { - using var service = GetService(); var command = service.Files.Delete(fileId); var result = await command.ExecuteAsync(token) .ConfigureAwait(false); } + + public void Dispose() + { + service.Dispose(); + } } } \ No newline at end of file