From 96fdd02ca774f09c411f9a158e33b91d40f904f1 Mon Sep 17 00:00:00 2001 From: cult Date: Fri, 29 Oct 2021 15:01:55 +0500 Subject: [PATCH] CS2-106: Made GoogleDriveService disposable --- .../Services/GoogleDriveService.cs | 22 ++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) 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