NonInventPurchasingSystem/CPRNIMS.Domain/Services/GoogleDriveService.cs
2026-01-20 07:44:30 +08:00

133 lines
4.4 KiB
C#

using Google.Apis.Auth.OAuth2;
using Google.Apis.Drive.v3;
using Google.Apis.Services;
using Google.Apis.Util.Store;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
namespace CPRNIMS.Domain.Services
{
public class GoogleDriveService
{
private DriveService _driveService;
public GoogleDriveService(string serviceAccountKeyFilePath)
{
serviceAccountKeyFilePath = "wwwroot/GoogleSecurity/credentials.json";
InitializeDriveService(serviceAccountKeyFilePath);
}
private void InitializeDriveService(string serviceAccountKeyFilePath)
{
var credential = GoogleCredential.FromFile(serviceAccountKeyFilePath)
.CreateScoped(new[] { DriveService.ScopeConstants.Drive });
_driveService = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential
});
}
public async Task<string> UploadFileAsync(string filePath, string fileName)
{
var fileMetadata = new Google.Apis.Drive.v3.Data.File()
{
Name = fileName
};
FilesResource.CreateMediaUpload request;
using (var stream = new FileStream(filePath, FileMode.Open))
{
request = _driveService.Files.Create(fileMetadata, stream, "image/jpeg");
request.Fields = "id";
await request.UploadAsync();
}
var file = request.ResponseBody;
return file.Id;
}
public async Task UpdateFileAsync(string fileId, string newFilePath)
{
var fileMetadata = new Google.Apis.Drive.v3.Data.File();
using (var stream = new FileStream(newFilePath, FileMode.Open))
{
await _driveService.Files.Update(fileMetadata, fileId, stream, "image/jpeg").UploadAsync();
}
}
public async Task DeleteFileAsync(string fileId)
{
await _driveService.Files.Delete(fileId).ExecuteAsync();
}
public async Task<string> GetFileIdByNameAsync(string fileName)
{
//14uzoQM4pagf7F_CUZmwRxmaCAJ_8Nayg
fileName = "1.jpg";
var request = _driveService.Files.List();
request.Q = $"name='{fileName}'";
var response = await request.ExecuteAsync();
if (response.Files.Count > 0)
return response.Files[0].Id;
else
return null;
}
public async Task<string> GetFileIdByIdAsync(string fileName)
{
//14uzoQM4pagf7F_CUZmwRxmaCAJ_8Nayg
fileName = "1";
var request = _driveService.Files.List();
request.Q = $"name='{fileName}'";
var response = await request.ExecuteAsync();
if (response.Files.Count > 0)
return response.Files[0].Id;
else
return null;
}
public async Task<string> DownloadFileByNameAsync(string fileName, string downloadDirectory)
{
var fileId = await GetFileIdByNameAsync(fileName);
if (fileId != null)
{
var downloadPath = Path.Combine(downloadDirectory, fileName);
await DownloadFileByIdAsync(fileId, downloadPath);
return downloadPath;
}
else
{
throw new FileNotFoundException($"File with name '{fileName}' not found on Google Drive.");
}
}
public async Task DownloadFileByIdAsync(string fileId, string downloadPath)
{
var request = _driveService.Files.Get(fileId);
using (var stream = new FileStream(downloadPath, FileMode.Create))
{
await request.DownloadAsync(stream);
}
}
/* public async Task DownloadFileByNameAsync(string fileName, string downloadPath)
{
var fileId = await GetFileIdByNameAsync(fileName);
if (fileId != null)
{
await DownloadFileByIdAsync(fileId, downloadPath);
}
else
{
throw new FileNotFoundException($"File with name '{fileName}' not found on Google Drive.");
}
}
*/
}
}