Skip to content

Commit 2619f16

Browse files
committed
Added media permalink resolver test.
1 parent 3564bfc commit 2619f16

File tree

4 files changed

+85
-5
lines changed

4 files changed

+85
-5
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import { MediaContract } from "@paperbits/common/media";
2+
import { MediaPermalinkResolver } from "@paperbits/common/media/mediaPermalinkResolver.publish";
3+
import { PictureViewModelBinder } from "../src/picture/ko/pictureViewModelBinder";
4+
import { PictureModelBinder } from "../src/picture/pictureModelBinder";
5+
import { expect } from "chai";
6+
7+
8+
describe("Media permalink resolver", async () => {
9+
const styleCompiler: any = {
10+
getStyleModelAsync: (styles: any) => {
11+
return Promise.resolve({});
12+
}
13+
}
14+
15+
const pictureContract: any = {
16+
type: "picture",
17+
caption: "Logo",
18+
width: "150px",
19+
height: "40px",
20+
sourceKey: "uploads/7aa4ab1f-cf14-ef3c-c4c5-420434f4de9e"
21+
}
22+
23+
it("Correctly resolves permalink for uploaded media.", async () => {
24+
const mediaService: any = {
25+
getMediaByKey: (mediaKey: string): Promise<MediaContract> => {
26+
const mediaContract: MediaContract = {
27+
key: "uploads/7aa4ab1f-cf14-ef3c-c4c5-420434f4de9e",
28+
fileName: "your-specs-logo.svg",
29+
blobKey: "7aa4ab1f-cf14-ef3c-c4c5-420434f4de9e",
30+
description: "Company logo",
31+
keywords: "logo",
32+
permalink: "/content/your-specs-logo.svg",
33+
mimeType: "image/svg+xml",
34+
variants: []
35+
}
36+
37+
return Promise.resolve(mediaContract);
38+
}
39+
};
40+
41+
const permalinkResolver = new MediaPermalinkResolver(mediaService);
42+
const pictureViewModelBinder = new PictureViewModelBinder(styleCompiler, permalinkResolver, mediaService);
43+
const modelBinder = new PictureModelBinder(permalinkResolver);
44+
45+
const pictureModel = await modelBinder.contractToModel(pictureContract, null);
46+
const pictureWidgetState: any = {};
47+
await pictureViewModelBinder.modelToState(pictureModel, pictureWidgetState);
48+
49+
expect(pictureWidgetState.sourceUrl).to.be.equal("/content/your-specs-logo.svg");
50+
});
51+
52+
it("Correctly resolves permalink for referenced media.", async () => {
53+
const mediaService: any = {
54+
getMediaByKey: (mediaKey: string): Promise<MediaContract> => {
55+
const mediaContract: MediaContract = {
56+
key: "uploads/7aa4ab1f-cf14-ef3c-c4c5-420434f4de9e",
57+
fileName: "your-specs-logo.svg",
58+
downloadUrl: "https://content/your-specs-logo.svg",
59+
description: "Company logo",
60+
keywords: "logo",
61+
permalink: "/content/your-specs-logo.svg",
62+
mimeType: "image/svg+xml",
63+
variants: []
64+
}
65+
66+
return Promise.resolve(mediaContract);
67+
}
68+
};
69+
70+
const permalinkResolver = new MediaPermalinkResolver(mediaService);
71+
const pictureViewModelBinder = new PictureViewModelBinder(styleCompiler, permalinkResolver, mediaService);
72+
const modelBinder = new PictureModelBinder(permalinkResolver);
73+
74+
const pictureModel = await modelBinder.contractToModel(pictureContract, null);
75+
const pictureWidgetState: any = {};
76+
await pictureViewModelBinder.modelToState(pictureModel, pictureWidgetState);
77+
78+
expect(pictureWidgetState.sourceUrl).to.be.equal("/content/your-specs-logo.svg");
79+
});
80+
});

src/picture/ko/pictureViewModelBinder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export class PictureViewModelBinder implements ViewModelBinder<PictureModel, Pic
3131
if (model.sourceKey) {
3232
const media = await this.mediaService.getMediaByKey(model.sourceKey);
3333

34-
if (media?.variants) {
34+
if (media?.variants?.length > 0) {
3535
const variants = media.variants.map(variantContract => {
3636
const variantModel = new MediaVariantModel();
3737
variantModel.width = variantContract.width;

src/publishing/mediaPublisher.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export class MediaPublisher implements IPublisher {
6464
private async publishFromStorageStream(permalink: string, mediaFile: MediaVariantContract): Promise<void> {
6565
try {
6666
const contentStream = await this.blobStorage.getBlobAsStream(mediaFile.blobKey);
67-
67+
6868
if (!contentStream) {
6969
this.logger.trackEvent("Publishing", { message: `Blob with key ${mediaFile.blobKey} not found in source storage.` });
7070
return null;
@@ -89,7 +89,7 @@ export class MediaPublisher implements IPublisher {
8989

9090
private async renderMediaFile(permalink: string, mediaFile: MediaVariantContract): Promise<void> {
9191
if (mediaFile.blobKey) {
92-
if(this.blobStorage.getBlobAsStream) {
92+
if (this.blobStorage.getBlobAsStream) {
9393
await this.publishFromStorageStream(permalink, mediaFile);
9494
} else {
9595
await this.publishFromStorage(permalink, mediaFile);

src/workshops/media/ko/mediaItem.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ export class MediaItem {
3636
this.setThumbnail(mediaContract);
3737

3838
this.isReferenced = ko.computed(() => {
39-
return !!mediaContract.downloadUrl
40-
&& (mediaContract.downloadUrl.startsWith("https://") || mediaContract.downloadUrl.startsWith("https//"));
39+
return !mediaContract.blobKey
40+
&& (mediaContract.downloadUrl?.startsWith("http://") || mediaContract.downloadUrl?.startsWith("https//"));
4141
});
4242
}
4343

0 commit comments

Comments
 (0)