This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public async Task<string> GetUri() | |
{ | |
var info = new | |
{ | |
Id = 1234, | |
Name = "Jack" | |
} | |
// 直接 JSON 序列化丟進去就好嗎? | |
var uri = $"https://localhost:8080/?info={JsonSerializer.Serialize(info)}"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Delete drive item (移到垃圾桶) | |
/// </summary> | |
/// <param name="request"></param> | |
/// <param name="client"></param> | |
/// <returns></returns> | |
public async Task DeleteItem(string driveId, string driveItemId, GraphServiceClient client) | |
{ | |
await client.Drives[driveId].Items[driveItemId].DeleteAsync(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ===== | |
// 其他程式碼 | |
// ===== | |
builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory()); | |
ConfigurationManager configuration = builder.Configuration; | |
IWebHostEnvironment environment = builder.Environment; | |
configuration.SetBasePath(environment.ContentRootPath) | |
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[HttpPost("File/{folderId}")] | |
public async Task<IActionResult> UploadFile(IFormFile file, string folderId) | |
{ | |
var credential = GoogleService.GetGoogleCredential(); | |
var uploadRequest = new UploadFileRequest | |
{ | |
FileStream = file.OpenReadStream(), | |
FileName = file.FileName, | |
FileFormat = file.ContentType, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace CloudBackup.Dtos | |
{ | |
public class UploadFileRequest | |
{ | |
public Stream FileStream { get; set; } | |
public string FileName { get; set; } | |
public string FolderId { get; set; } | |
public string FileFormat { get; set; } | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public async Task<Google.Apis.Drive.v3.Data.File> UploadToFolder (UploadFileRequest uploadRequest, IConfigurableHttpClientInitializer credential) | |
{ | |
try | |
{ | |
var service = GetDriveService(credential); | |
var fileMetadata = new Google.Apis.Drive.v3.Data.File() | |
{ | |
Name = uploadRequest.FileName, | |
//若填入多個資料夾 ID 則上傳的檔案都會出現到這些資料夾中 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public async Task DeleteFile(string fileId, UserCredential credential) | |
{ | |
var service = GetDriveService(credential); | |
try | |
{ | |
// 將檔案完全刪除 | |
var deleteRequest = service.Files.Delete(fileId); | |
await deleteRequest.ExecuteAsync(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public async Task EmptyTrash(UserCredential credential) | |
{ | |
var service = GetDriveService(credential); | |
try | |
{ | |
// 清空垃圾桶 | |
var deleteRequest = service.Files.EmptyTrash(); | |
await deleteRequest.ExecuteAsync(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public async Task MoveToTrash(string fileId, UserCredential credential) | |
{ | |
var service = GetDriveService(credential); | |
try | |
{ | |
var fileMetadata = new Google.Apis.Drive.v3.Data.File() | |
{ | |
Trashed = true | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public async Task MoveFile(string targetFileId, string newParentId, UserCredential credential) | |
{ | |
var service = GetDriveService(credential); | |
try | |
{ | |
// 取得檔案的當前父資料夾 | |
var getRequest = service.Files.Get(targetFileId); | |
getRequest.Fields = "parents"; | |
var file = await getRequest.ExecuteAsync(); |
NewerOlder