-
Notifications
You must be signed in to change notification settings - Fork 8
/
pixel-image-block-transformer.service.ts
79 lines (71 loc) · 2.79 KB
/
pixel-image-block-transformer.service.ts
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
76
77
78
79
import { BlockContext, BlockTransformerServiceInterface } from "@comet/blocks-api";
import { Injectable } from "@nestjs/common";
import { FilesService } from "../files/files.service";
import { ImageCropArea } from "../images/entities/image-crop-area.entity";
import { ImagesService } from "../images/images.service";
import { DamScopeInterface } from "../types";
import { PixelImageBlockData } from "./pixel-image.block";
type TransformResponse = {
damFile?: {
id: string;
name: string;
size: number;
mimetype: string;
contentHash: string;
title?: string;
altText?: string;
archived: boolean;
scope?: DamScopeInterface;
importSourceId?: string;
importSourceType?: string;
image?: {
width: number;
height: number;
cropArea: ImageCropArea;
dominantColor?: string;
};
fileUrl?: string;
};
cropArea?: ImageCropArea;
urlTemplate?: string;
};
@Injectable()
export class PixelImageBlockTransformerService implements BlockTransformerServiceInterface<PixelImageBlockData, TransformResponse> {
constructor(private readonly filesService: FilesService, private readonly imagesService: ImagesService) {}
async transformToPlain(block: PixelImageBlockData, { includeInvisibleContent, previewDamUrls, relativeDamUrls }: BlockContext) {
if (!block.damFileId) {
return {};
}
const file = await this.filesService.findOneById(block.damFileId);
if (!file) {
return {};
}
const fileUrl = includeInvisibleContent ? await this.filesService.createFileUrl(file, { previewDamUrls, relativeDamUrls }) : undefined;
return {
damFile: {
id: file.id,
name: file.name,
size: file.size,
mimetype: file.mimetype,
contentHash: file.contentHash,
title: file.title,
altText: file.altText,
archived: file.archived,
scope: file.scope,
importSourceId: file.importSourceId,
importSourceType: file.importSourceType,
image: file.image
? {
width: file.image.width,
height: file.image.height,
cropArea: { ...file.image.cropArea },
dominantColor: file.image.dominantColor,
}
: undefined,
fileUrl,
},
cropArea: block.cropArea ? { ...block.cropArea } : undefined,
urlTemplate: this.imagesService.createUrlTemplate({ file, cropArea: block.cropArea }, { previewDamUrls, relativeDamUrls }),
};
}
}