forked from ddrilling/AsbCloudServer
139 lines
5.6 KiB
C#
139 lines
5.6 KiB
C#
using Google.Apis.Auth.OAuth2;
|
|
using Google.Apis.Auth.OAuth2.Flows;
|
|
using Google.Apis.Auth.OAuth2.Responses;
|
|
using Google.Apis.Drive.v3;
|
|
using Google.Apis.Services;
|
|
using Google.Apis.Util.Store;
|
|
using Google.Apis.Drive.v3.Data;
|
|
using System.IO;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using AsbCloudApp.Services;
|
|
|
|
namespace AsbCloudInfrastructure.Services
|
|
{
|
|
public class GoogleDriveService : IGoogleDriveService
|
|
{
|
|
private static DriveService GetService()
|
|
{ // ключи для почты asbautodrilling@gmail.com.
|
|
var tokenResponse = new TokenResponse
|
|
{
|
|
AccessToken = "ya29.a0ARrdaM-lM7q0TIC_DXixR4oW63QUftjSPHl-8nIdvZwtqA8Z1bXtlYpDrQXj9UFTjW8FW8uqPMrdamUSp4kO4a9JX7FddkBWxaJ_omSJpqzDfnHTHA_7-zGMUohaAsmPLsQtFz_GUmB5ZoVLmA8xWdbJxVxU",
|
|
RefreshToken = "1//04FeDguc19IfgCgYIARAAGAQSNwF-L9Ir8U7wX2seanUzsxXXGgFzOYQqjbtN9O27ZZybbOobZjVAo_4_eFNLMX1ElPKOFVWsrJQ"
|
|
};
|
|
|
|
var applicationName = "Files";
|
|
var username = "asbautodrilling@gmail.com";
|
|
|
|
var apiCodeFlow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
|
|
{
|
|
ClientSecrets = new ClientSecrets
|
|
{
|
|
ClientId = "1020584579240-f7amqg35qg7j94ta1ntgitajq27cgh49.apps.googleusercontent.com",
|
|
ClientSecret = "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"
|
|
},
|
|
Scopes = new[] {DriveService.Scope.Drive},
|
|
DataStore = new FileDataStore(applicationName)
|
|
});
|
|
|
|
var credential = new UserCredential(apiCodeFlow, username, tokenResponse);
|
|
|
|
var newService = new DriveService(new BaseClientService.Initializer
|
|
{
|
|
HttpClientInitializer = credential,
|
|
ApplicationName = applicationName
|
|
});
|
|
return newService;
|
|
}
|
|
|
|
public IEnumerable<Google.Apis.Drive.v3.Data.File> 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";
|
|
//fileList.Fields = "nextPageToken, files(id, name, size, mimeType)";
|
|
|
|
var result = new List<Google.Apis.Drive.v3.Data.File>();
|
|
string pageToken = null;
|
|
do
|
|
{
|
|
fileList.PageToken = pageToken;
|
|
var filesResult = fileList.Execute();
|
|
var files = filesResult.Files;
|
|
pageToken = filesResult.NextPageToken;
|
|
result.AddRange(files);
|
|
} while (pageToken != null);
|
|
|
|
return result;
|
|
}
|
|
|
|
public async Task<string> 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)
|
|
.ConfigureAwait(false);
|
|
var file = filesResult.Files.FirstOrDefault(f => f.Id == idFile);
|
|
return file?.WebViewLink ?? string.Empty;
|
|
}
|
|
|
|
public async Task<string> 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";
|
|
//driveFolder.Parents = new string[] { parent };
|
|
var command = service.Files.Create(driveFolder);
|
|
var file = await command.ExecuteAsync(token)
|
|
.ConfigureAwait(false);
|
|
return file.Id;
|
|
}
|
|
|
|
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)
|
|
.ConfigureAwait(false);
|
|
}
|
|
|
|
public async Task<string> 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;
|
|
driveFile.MimeType = fileMime;
|
|
//driveFile.Parents = new [] {folder};
|
|
|
|
var request = service.Files.Create(driveFile, file, fileMime);
|
|
request.Fields = "id";
|
|
|
|
var response = await request.UploadAsync(token)
|
|
.ConfigureAwait(false);
|
|
if (response.Status != Google.Apis.Upload.UploadStatus.Completed)
|
|
throw response.Exception;
|
|
|
|
return request.ResponseBody.Id;
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
} |