forked from ddrilling/AsbCloudServer
CS2-106: Made GoogleDriveService disposable
This commit is contained in:
parent
3e00dac818
commit
96fdd02ca7
@ -1,3 +1,4 @@
|
|||||||
|
using System;
|
||||||
using Google.Apis.Auth.OAuth2;
|
using Google.Apis.Auth.OAuth2;
|
||||||
using Google.Apis.Auth.OAuth2.Flows;
|
using Google.Apis.Auth.OAuth2.Flows;
|
||||||
using Google.Apis.Auth.OAuth2.Responses;
|
using Google.Apis.Auth.OAuth2.Responses;
|
||||||
@ -14,9 +15,11 @@ using AsbCloudApp.Services;
|
|||||||
|
|
||||||
namespace AsbCloudInfrastructure.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.
|
{ // ключи для почты asbautodrilling@gmail.com.
|
||||||
var tokenResponse = new TokenResponse
|
var tokenResponse = new TokenResponse
|
||||||
{
|
{
|
||||||
@ -45,12 +48,11 @@ namespace AsbCloudInfrastructure.Services
|
|||||||
HttpClientInitializer = credential,
|
HttpClientInitializer = credential,
|
||||||
ApplicationName = applicationName
|
ApplicationName = applicationName
|
||||||
});
|
});
|
||||||
return newService;
|
service = newService;
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<Google.Apis.Drive.v3.Data.File> GetAllFiles()
|
public IEnumerable<Google.Apis.Drive.v3.Data.File> GetAllFiles()
|
||||||
{
|
{
|
||||||
using var service = GetService();
|
|
||||||
var fileList = service.Files.List();
|
var fileList = service.Files.List();
|
||||||
fileList.Fields = "files(id, webViewLink, size)";
|
fileList.Fields = "files(id, webViewLink, size)";
|
||||||
//fileList.Q =$"mimeType!='application/vnd.google-apps.folder' and '{folder}' in parents";
|
//fileList.Q =$"mimeType!='application/vnd.google-apps.folder' and '{folder}' in parents";
|
||||||
@ -73,7 +75,6 @@ namespace AsbCloudInfrastructure.Services
|
|||||||
public async Task<string> GetFileWebLinkAsync(string idFile,
|
public async Task<string> GetFileWebLinkAsync(string idFile,
|
||||||
CancellationToken token = default)
|
CancellationToken token = default)
|
||||||
{
|
{
|
||||||
using var service = GetService();
|
|
||||||
var fileList = service.Files.List();
|
var fileList = service.Files.List();
|
||||||
fileList.Fields = "files(id, webViewLink, size)";
|
fileList.Fields = "files(id, webViewLink, size)";
|
||||||
var filesResult = await fileList.ExecuteAsync(token)
|
var filesResult = await fileList.ExecuteAsync(token)
|
||||||
@ -84,8 +85,7 @@ namespace AsbCloudInfrastructure.Services
|
|||||||
|
|
||||||
public async Task<string> CreateFolderAsync(string folderName,
|
public async Task<string> CreateFolderAsync(string folderName,
|
||||||
CancellationToken token = default)
|
CancellationToken token = default)
|
||||||
{
|
{
|
||||||
using var service = GetService();
|
|
||||||
var driveFolder = new Google.Apis.Drive.v3.Data.File();
|
var driveFolder = new Google.Apis.Drive.v3.Data.File();
|
||||||
driveFolder.Name = folderName;
|
driveFolder.Name = folderName;
|
||||||
driveFolder.MimeType = "application/vnd.google-apps.folder";
|
driveFolder.MimeType = "application/vnd.google-apps.folder";
|
||||||
@ -99,7 +99,6 @@ namespace AsbCloudInfrastructure.Services
|
|||||||
public async Task CreatePublicPermissionForFileAsync(string idFile,
|
public async Task CreatePublicPermissionForFileAsync(string idFile,
|
||||||
CancellationToken token = default)
|
CancellationToken token = default)
|
||||||
{
|
{
|
||||||
using var service = GetService();
|
|
||||||
var permission = new Permission() { Type = "anyone", Role = "reader"};
|
var permission = new Permission() { Type = "anyone", Role = "reader"};
|
||||||
var addPermissionRequest = service.Permissions.Create(permission, idFile);
|
var addPermissionRequest = service.Permissions.Create(permission, idFile);
|
||||||
await addPermissionRequest.ExecuteAsync(token)
|
await addPermissionRequest.ExecuteAsync(token)
|
||||||
@ -109,7 +108,6 @@ namespace AsbCloudInfrastructure.Services
|
|||||||
public async Task<string> UploadFileAsync(Stream file, string fileName, string fileMime,
|
public async Task<string> UploadFileAsync(Stream file, string fileName, string fileMime,
|
||||||
string fileDescription, CancellationToken token = default)
|
string fileDescription, CancellationToken token = default)
|
||||||
{
|
{
|
||||||
using var service = GetService();
|
|
||||||
var driveFile = new Google.Apis.Drive.v3.Data.File();
|
var driveFile = new Google.Apis.Drive.v3.Data.File();
|
||||||
driveFile.Name = fileName;
|
driveFile.Name = fileName;
|
||||||
driveFile.Description = fileDescription;
|
driveFile.Description = fileDescription;
|
||||||
@ -130,10 +128,14 @@ namespace AsbCloudInfrastructure.Services
|
|||||||
public async Task DeleteFileAsync(string fileId,
|
public async Task DeleteFileAsync(string fileId,
|
||||||
CancellationToken token = default)
|
CancellationToken token = default)
|
||||||
{
|
{
|
||||||
using var service = GetService();
|
|
||||||
var command = service.Files.Delete(fileId);
|
var command = service.Files.Delete(fileId);
|
||||||
var result = await command.ExecuteAsync(token)
|
var result = await command.ExecuteAsync(token)
|
||||||
.ConfigureAwait(false);
|
.ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
service.Dispose();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user