2026-02-04 15:44:23 +01:00
|
|
|
// Copyright (c) 2026 Duplicati Inc. All rights reserved.
|
|
|
|
|
|
|
|
|
|
using Duplicati.Library.Common.IO;
|
2026-02-13 14:42:55 +01:00
|
|
|
using Google.Apis.Drive.v3;
|
2026-02-04 15:44:23 +01:00
|
|
|
using Google.Apis.HangoutsChat.v1.Data;
|
2026-02-15 22:03:57 +01:00
|
|
|
using Google.Apis.HangoutsChat.v1;
|
2026-02-04 15:44:23 +01:00
|
|
|
|
|
|
|
|
namespace Duplicati.Proprietary.GoogleWorkspace.SourceItems;
|
|
|
|
|
|
2026-02-15 22:03:57 +01:00
|
|
|
internal class ChatAttachmentSourceEntry(string parentPath, Attachment attachment, HangoutsChatService chatService, DriveService driveService)
|
2026-02-04 15:44:23 +01:00
|
|
|
: StreamResourceEntryBase(SystemIO.IO_OS.PathCombine(parentPath, attachment.ContentName ?? attachment.Name.Split('/').Last()), DateTime.UnixEpoch, DateTime.UnixEpoch)
|
|
|
|
|
{
|
|
|
|
|
public override long Size => -1;
|
|
|
|
|
|
|
|
|
|
public override async Task<Stream> OpenRead(CancellationToken cancellationToken)
|
|
|
|
|
{
|
2026-02-13 14:42:55 +01:00
|
|
|
// Check if the attachment is stored in Google Chat (not Drive)
|
|
|
|
|
if (attachment.AttachmentDataRef != null)
|
2026-02-04 15:44:23 +01:00
|
|
|
{
|
2026-02-15 22:03:57 +01:00
|
|
|
var request = chatService.Media.Download(attachment.AttachmentDataRef.ResourceName);
|
2026-02-13 14:42:55 +01:00
|
|
|
return await request.ExecuteAsStreamAsync(cancellationToken);
|
2026-02-04 15:44:23 +01:00
|
|
|
}
|
|
|
|
|
|
2026-02-13 14:42:55 +01:00
|
|
|
// Check if the attachment is stored in Google Drive
|
|
|
|
|
if (attachment.DriveDataRef != null)
|
|
|
|
|
{
|
|
|
|
|
var driveRequest = driveService.Files.Get(attachment.DriveDataRef.DriveFileId);
|
|
|
|
|
driveRequest.Alt = FilesResource.GetRequest.AltEnum.Media;
|
|
|
|
|
return await driveRequest.ExecuteAsStreamAsync(cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw new FileNotFoundException("Attachment contains no valid data reference.");
|
2026-02-04 15:44:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override Task<Dictionary<string, string?>> GetMinorMetadata(CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
return Task.FromResult(new Dictionary<string, string?>
|
|
|
|
|
{
|
|
|
|
|
{ "gsuite:v", "1" },
|
|
|
|
|
{ "gsuite:Type", SourceItemType.ChatAttachment.ToString() },
|
|
|
|
|
{ "gsuite:Name", attachment.ContentName ?? attachment.Name.Split('/').Last() },
|
2026-02-11 16:23:27 +01:00
|
|
|
{ "gsuite:Id", attachment.Name },
|
2026-02-04 15:44:23 +01:00
|
|
|
{ "gsuite:ContentType", attachment.ContentType }
|
|
|
|
|
}
|
|
|
|
|
.Where(kv => !string.IsNullOrEmpty(kv.Value))
|
|
|
|
|
.ToDictionary(kv => kv.Key, kv => kv.Value));
|
|
|
|
|
}
|
|
|
|
|
}
|