Files
duplicati/proprietary/GoogleWorkspace/SourceItems/KeepNoteAttachmentSourceEntry.cs
T

48 lines
2.0 KiB
C#
Raw Normal View History

// Copyright (c) 2026 Duplicati Inc. All rights reserved.
using Duplicati.Library.Common.IO;
using Google.Apis.Keep.v1.Data;
namespace Duplicati.Proprietary.GoogleWorkspace.SourceItems;
2026-02-13 13:46:00 +01:00
internal class KeepNoteAttachmentSourceEntry(SourceProvider provider, string parentPath, string userId, Attachment attachment)
: StreamResourceEntryBase(SystemIO.IO_OS.PathCombine(parentPath, attachment.Name.Split('/').Last()), DateTime.UnixEpoch, DateTime.UnixEpoch)
{
public override long Size => -1;
public override async Task<Stream> OpenRead(CancellationToken cancellationToken)
{
// Keep API attachments are downloaded via media URL?
// attachment.Name is like "notes/123/attachments/456".
// Docs say: GET https://keep.googleapis.com/v1/notes/{noteId}/attachments/{attachmentId}?alt=media
2026-02-13 13:46:00 +01:00
var service = provider.ApiHelper.GetKeepService(userId);
var url = $"https://keep.googleapis.com/v1/{attachment.Name}?alt=media";
var response = await service.HttpClient.GetAsync(url, cancellationToken);
if (response.IsSuccessStatusCode)
{
var stream = new MemoryStream();
await response.Content.CopyToAsync(stream, cancellationToken);
stream.Seek(0, SeekOrigin.Begin);
return stream;
}
throw new FileNotFoundException($"Cannot download attachment: {response.StatusCode}");
}
public override Task<Dictionary<string, string?>> GetMinorMetadata(CancellationToken cancellationToken)
{
return Task.FromResult(new Dictionary<string, string?>
{
{ "gsuite:v", "1" },
{ "gsuite:Type", SourceItemType.KeepNoteAttachment.ToString() },
{ "gsuite:Name", attachment.Name.Split('/').Last() },
{ "gsuite:Id", attachment.Name },
{ "gsuite:MimeType", attachment.MimeType != null ? string.Join(",", attachment.MimeType) : null }
}
.Where(kv => !string.IsNullOrEmpty(kv.Value))
.ToDictionary(kv => kv.Key, kv => kv.Value));
}
}