-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapperProfile.cs
More file actions
75 lines (59 loc) · 3.17 KB
/
Copy pathMapperProfile.cs
File metadata and controls
75 lines (59 loc) · 3.17 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
using AutoMapper;
using SplitzBackend.Models;
using SplitzBackend.Services;
namespace SplitzBackend;
public class MapperProfile : Profile
{
public MapperProfile()
{
CreateMap<SplitzUser, SplitzUserDto>()
.ForMember(d => d.Photo, opt => opt.MapFrom<SignedPhotoUrlResolver, string?>(s => s.Photo));
CreateMap<SplitzUser, SplitzUserReducedDto>()
.ForMember(d => d.Photo, opt => opt.MapFrom<SignedPhotoUrlResolver, string?>(s => s.Photo));
CreateMap<Friend, FriendDto>();
CreateMap<Group, GroupDto>()
.ForMember(d => d.Photo, opt => opt.MapFrom<SignedPhotoUrlResolver, string?>(s => s.Photo));
CreateMap<Group, GroupReducedDto>()
.ForMember(d => d.Photo, opt => opt.MapFrom<SignedPhotoUrlResolver, string?>(s => s.Photo));
CreateMap<GroupBalance, GroupBalanceDto>();
CreateMap<GroupJoinLink, GroupJoinLinkDto>();
CreateMap<Transaction, TransactionDto>()
.ForMember(d => d.Photo, opt => opt.MapFrom<SignedPhotoUrlResolver, string?>(s => s.Photo));
CreateMap<TransactionBalance, TransactionBalanceDto>();
CreateMap<TransactionDraft, TransactionDraftDto>()
.ForMember(d => d.Photo, opt => opt.MapFrom<SignedPhotoUrlResolver, string?>(s => s.Photo));
CreateMap<TransactionDraftBalance, TransactionDraftBalanceDto>();
CreateMap<Invoice, InvoiceDto>();
CreateMap<Invoice, InvoiceReducedDto>();
CreateMap<InvoiceDebt, InvoiceDebtDto>();
CreateMap<InvoiceSettlement, InvoiceSettlementDto>();
CreateMap<Notification, NotificationDto>()
.ForMember(d => d.Data, opt => opt.MapFrom(s => s.GetTypedData() ?? (object)s.Data));
CreateMap<GroupInputDto, Group>();
CreateMap<TransactionInputDto, Transaction>();
CreateMap<TransactionBalanceInputDto, TransactionBalance>();
CreateMap<TransactionDraftInputDto, TransactionDraft>();
CreateMap<TransactionDraftBalanceInputDto, TransactionDraftBalance>();
}
}
public sealed class SignedPhotoUrlResolver(IObjectStorage objectStorage)
: IMemberValueResolver<object, object, string?, string?>
{
private static readonly TimeSpan PublicRounding = TimeSpan.FromHours(1);
private const string PublicCacheControl = "public, max-age=3600";
private static readonly TimeSpan PrivateRounding = TimeSpan.FromMinutes(15);
private const string PrivateCacheControl = "private, max-age=900";
public string? Resolve(object source, object destination, string? sourceMember, string? destMember,
ResolutionContext context)
{
if (string.IsNullOrWhiteSpace(sourceMember))
return sourceMember;
if (!objectStorage.TryParseObjectKey(sourceMember, out var objectKey))
return sourceMember;
var isPublic = objectKey.StartsWith("users/", StringComparison.OrdinalIgnoreCase) ||
objectKey.StartsWith("groups/", StringComparison.OrdinalIgnoreCase);
return isPublic
? objectStorage.BuildRelativeUrl(objectKey, PublicRounding, PublicCacheControl)
: objectStorage.BuildRelativeUrl(objectKey, PrivateRounding, PrivateCacheControl);
}
}