-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGoogleCloud.cs
More file actions
110 lines (89 loc) · 3.41 KB
/
GoogleCloud.cs
File metadata and controls
110 lines (89 loc) · 3.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
using Google.Apis.Drive.v3;
using Google.Apis.Drive.v3.Data;
using Google.Apis.Upload;
using Microsoft.Extensions.Caching.Memory;
using TopicTalks.Domain.Common;
using TopicTalks.Domain.Interfaces.Core;
using File = Google.Apis.Drive.v3.Data.File;
namespace TopicTalks.Infrastructure.Services.Cloud;
internal class GoogleCloud(IMemoryCache cache, DriveService driveService) : IGoogleCloud
{
private readonly IMemoryCache _cache = cache;
private readonly DriveService _driveService = driveService;
public async Task<GoogleFile> UploadAsync(string fileName, Stream stream, string contentType)
{
var metaData = CreateMetaData(fileName);
var request = _driveService.Files.Create(metaData, stream, contentType);
request.Fields = "id, name, mimeType, size, webContentLink, webViewLink, createdTime";
var result = await request.UploadAsync();
if (result.Status == UploadStatus.Failed)
throw new Exception("Failed to upload file");
SetPermissions(request.ResponseBody.Id);
return MapToGoogleFile(request.ResponseBody);
}
private void SetPermissions(string fileId)
{
var permission = new Permission {
Type = "anyone",
Role = "reader"
};
_driveService.Permissions
.Create(permission, fileId)
.ExecuteAsync();
}
public async Task<GoogleFile> InfoAsync(string fileId)
{
var request = _driveService.Files.Get(fileId);
request.Fields = "id, name, mimeType, size, webContentLink, webViewLink, createdTime";
var response = await request.ExecuteAsync();
return MapToGoogleFile(response);
}
public async Task<GoogleFileDownload> DownloadAsync(string fileId)
{
var request = _driveService.Files.Get(fileId);
request.Fields = "id, name, mimeType, size, createdTime";
var fileInfo = await request.ExecuteAsync();
using var stream = new MemoryStream();
await request.DownloadAsync(stream);
return new GoogleFileDownload(
fileInfo.Id,
fileInfo.Name,
fileInfo.MimeType,
fileInfo.Size ?? 0,
stream.ToArray(),
fileInfo.CreatedTimeDateTimeOffset?.UtcDateTime ?? DateTime.UtcNow);
}
public async Task<GoogleFile> UpdateAsync(string fileId, string fileName, Stream stream, string contentType)
{
Delete(fileId);
return await UploadAsync(fileName, stream, contentType);
}
public void Delete(string fileId)
{
_driveService.Files.Delete(fileId).ExecuteAsync();
}
private File CreateMetaData(string fileName)
{
const string cacheKey = "TT_FolderId";
var metaData = new File { Name = fileName, };
if (_cache.TryGetValue(cacheKey, out string? fileId) && fileId is not null)
{
metaData.Parents = new List<string> { fileId };
}
return metaData;
}
private static string GetDirectLink(string fileId) => "https://lh3.googleusercontent.com/d/" + fileId;
private static GoogleFile MapToGoogleFile(File file)
{
return new GoogleFile(
file.Id,
file.Name,
file.MimeType,
file.Size ?? 0,
file.WebContentLink,
file.WebViewLink,
GetDirectLink(file.Id),
file.CreatedTimeDateTimeOffset?.UtcDateTime ?? DateTime.Now
);
}
}