Files
duplicati/proprietary/GoogleWorkspace/SourceItems/GmailLabelSourceEntry.cs
T
Kenneth Skovhede 910c56b3c4 Reworked the use of scopes.
Instead of requesting all scopes up front, each sub-section of the backup/restore will request the permissions that are relevant for the operation.

The effect is that it is not required to grant all permissions to get a backup running. If permissions are failing, that part of the backup will generate warnings for items that are excluded.
2026-02-15 22:03:57 +01:00

81 lines
2.9 KiB
C#

// Copyright (c) 2026 Duplicati Inc. All rights reserved.
using Duplicati.Library.Common.IO;
using Duplicati.Library.Interface;
using Google.Apis.Gmail.v1.Data;
using Google.Apis.Gmail.v1;
using Google.Apis.Requests;
using System.Runtime.CompilerServices;
namespace Duplicati.Proprietary.GoogleWorkspace.SourceItems;
internal class GmailLabelSourceEntry(string userId, string parentPath, Label label, GmailService service)
: MetaEntryBase(Util.AppendDirSeparator(SystemIO.IO_OS.PathCombine(parentPath, label.Name)), null, null)
{
public override IAsyncEnumerable<ISourceProviderEntry> Enumerate(CancellationToken cancellationToken)
{
return EnumerateMessages(cancellationToken);
}
private async IAsyncEnumerable<ISourceProviderEntry> EnumerateMessages([EnumeratorCancellation] CancellationToken cancellationToken)
{
var request = service.Users.Messages.List(userId);
request.LabelIds = new[] { label.Id };
do
{
if (cancellationToken.IsCancellationRequested)
yield break;
var response = await request.ExecuteAsync(cancellationToken);
if (response.Messages != null)
{
var batch = new BatchRequest(service);
var messages = new List<Message>();
foreach (var msg in response.Messages)
{
var msgRequest = service.Users.Messages.Get(userId, msg.Id);
msgRequest.Format = UsersResource.MessagesResource.GetRequest.FormatEnum.Metadata;
batch.Queue<Message>(msgRequest, (message, error, index, httpResponse) =>
{
if (error == null)
{
lock (messages)
{
messages.Add(message);
}
}
});
}
await batch.ExecuteAsync(cancellationToken);
foreach (var message in messages)
{
if (cancellationToken.IsCancellationRequested)
yield break;
yield return new GmailMessageSourceEntry(userId, this.Path, message, service);
}
}
request.PageToken = response.NextPageToken;
} while (!string.IsNullOrEmpty(request.PageToken));
}
public override Task<Dictionary<string, string?>> GetMinorMetadata(CancellationToken cancellationToken)
{
return Task.FromResult(new Dictionary<string, string?>
{
{ "gsuite:v", "1" },
{ "gsuite:Type", SourceItemType.GmailLabel.ToString() },
{ "gsuite:Name", label.Name },
{ "gsuite:Id", label.Id },
{ "gsuite:LabelType", label.Type }
}
.Where(kv => !string.IsNullOrEmpty(kv.Value))
.ToDictionary(kv => kv.Key, kv => kv.Value));
}
}