Skip to content

Commit 71ce8a3

Browse files
committed
Binarieservice implementasjon
1 parent f31b21d commit 71ce8a3

4 files changed

+70
-68
lines changed
+42-2
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,57 @@
11
import { CmsArchiveSiteConfig } from './CmsArchiveSite';
22
import { CmsArchiveOpenSearchClient } from '../opensearch/CmsArchiveOpenSearchClient';
3+
import { CmsBinaryDocument } from '../../../common/cms-documents/binary';
4+
import { AssetDocument } from '../opensearch/types';
35

46
type ConstructorProps = {
57
config: CmsArchiveSiteConfig;
68
client: CmsArchiveOpenSearchClient;
79
};
810

911
export class CmsArchiveBinariesService {
10-
private readonly config: CmsArchiveSiteConfig;
1112
private readonly client: CmsArchiveOpenSearchClient;
1213

14+
private readonly binariesIndex: string;
15+
private readonly assetsIndex: string;
16+
1317
constructor({ config, client }: ConstructorProps) {
14-
this.config = config;
18+
const { indexPrefix } = config;
19+
1520
this.client = client;
21+
22+
this.binariesIndex = `${indexPrefix}_binaries`;
23+
this.assetsIndex = `${indexPrefix}_assets`;
24+
}
25+
26+
public async getBinary(binaryKey: string): Promise<CmsBinaryDocument | null> {
27+
return this.client.getDocument<CmsBinaryDocument>({
28+
index: this.binariesIndex,
29+
id: binaryKey,
30+
});
31+
}
32+
33+
public async getStaticAsset(filePath: string): Promise<AssetDocument | null> {
34+
const withoutLeadingSlash = filePath.replace(/^\//, '');
35+
36+
const result = await this.client.search<AssetDocument>({
37+
index: this.assetsIndex,
38+
body: {
39+
query: {
40+
term: {
41+
path: withoutLeadingSlash,
42+
},
43+
},
44+
},
45+
});
46+
47+
if (!result || result.hits.length === 0) {
48+
return null;
49+
}
50+
51+
if (result.hits.length > 1) {
52+
console.error(`Found multiple files with path ${filePath}!`);
53+
}
54+
55+
return result.hits[0];
1656
}
1757
}

server/src/cms/CmsArchiveCategoriesService.ts

+11-11
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,18 @@ export class CmsArchiveCategoriesService {
1313
private readonly client: CmsArchiveOpenSearchClient;
1414
private readonly config: CmsArchiveSiteConfig;
1515

16-
private readonly index: string;
16+
private readonly categoriesIndex: string;
1717

1818
private readonly categoriesMap: Map<string, CmsCategoryListItem> = new Map();
1919
private readonly rootCategories: CmsCategoryListItem[] = [];
2020

2121
constructor({ client, config }: ConstructorProps) {
2222
this.client = client;
2323
this.config = config;
24-
this.index = `${config.indexPrefix}_categories`;
24+
this.categoriesIndex = `${config.indexPrefix}_categories`;
2525
}
2626

27-
async init() {
27+
public async init() {
2828
const allCategories = await this.getAllCategories();
2929
if (!allCategories) {
3030
console.error(`Could not retrieve categories for ${this.config.name}`);
@@ -40,22 +40,22 @@ export class CmsArchiveCategoriesService {
4040
);
4141
}
4242

43-
getRootCategories(): CmsCategoryListItem[] {
43+
public getRootCategories(): CmsCategoryListItem[] {
4444
return this.rootCategories;
4545
}
4646

47-
getCategory(categoryKey: string): CmsCategoryListItem | undefined {
47+
public getCategory(categoryKey: string): CmsCategoryListItem | undefined {
4848
return this.categoriesMap.get(categoryKey);
4949
}
5050

51-
async getCategoryFull(categoryKey: string): Promise<CmsCategoryDocument | null> {
51+
public async getCategoryFull(categoryKey: string): Promise<CmsCategoryDocument | null> {
5252
return this.client.getDocument<CmsCategoryDocument>({
53-
index: this.index,
53+
index: this.categoriesIndex,
5454
id: categoryKey,
5555
});
5656
}
5757

58-
getCategories(categoryKeys: string[]): CmsCategoryListItem[] {
58+
public getCategories(categoryKeys: string[]): CmsCategoryListItem[] {
5959
return categoryKeys.reduce<CmsCategoryListItem[]>((acc, key) => {
6060
const category = this.categoriesMap.get(key);
6161
if (category) {
@@ -87,7 +87,7 @@ export class CmsArchiveCategoriesService {
8787
this.rootCategories.sort((a, b) => a.title.localeCompare(b.title));
8888
}
8989

90-
resolveCategoryPath(categoryKey: string, path: CmsCategoryPath = []): CmsCategoryPath {
90+
public resolveCategoryPath(categoryKey: string, path: CmsCategoryPath = []): CmsCategoryPath {
9191
const category = this.categoriesMap.get(categoryKey);
9292
if (!category) {
9393
console.error(`No category found for key ${categoryKey}`);
@@ -111,7 +111,7 @@ export class CmsArchiveCategoriesService {
111111
private async getAllCategories(): Promise<CmsCategoryListItem[] | null> {
112112
return this.client
113113
.search<CmsCategoryDocument>({
114-
index: this.index,
114+
index: this.categoriesIndex,
115115
_source_excludes: ['xmlAsString'],
116116
size: 10000,
117117
body: {
@@ -123,7 +123,7 @@ export class CmsArchiveCategoriesService {
123123
.then((result) => {
124124
if (result && result.total > 10000) {
125125
console.error(
126-
`Found more than 10000 categories in ${this.index} - expected 3127 (sbs) or 3866 (fss)`
126+
`Found more than 10000 categories in ${this.categoriesIndex} - expected 3127 (sbs) or 3866 (fss)`
127127
);
128128
}
129129

server/src/cms/CmsArchiveContentService.ts

+3-41
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import { CmsContent, CmsContentDocument } from '../../../common/cms-documents/content';
22
import { CmsArchiveOpenSearchClient } from '../opensearch/CmsArchiveOpenSearchClient';
3-
import { CmsBinaryDocument } from '../../../common/cms-documents/binary';
43
import { CmsArchiveSiteConfig } from './CmsArchiveSite';
54
import { sortVersions } from '../utils/sort';
6-
import { AssetDocument } from '../opensearch/types';
75
import { ContentSearchParams, ContentSearchResult } from '../../../common/contentSearch';
86
import { buildContentSearchParams } from '../opensearch/queries/contentSearch';
97
import { CmsArchiveCategoriesService } from './CmsArchiveCategoriesService';
@@ -16,23 +14,19 @@ type ConstructorProps = {
1614

1715
export class CmsArchiveContentService {
1816
private readonly client: CmsArchiveOpenSearchClient;
19-
private readonly siteConfig: CmsArchiveSiteConfig;
17+
private readonly config: CmsArchiveSiteConfig;
2018
private readonly categoriesService: CmsArchiveCategoriesService;
2119

2220
private readonly contentsIndex: string;
23-
private readonly binariesIndex: string;
24-
private readonly staticAssetsIndex: string;
2521

2622
constructor({ client, config, categoriesService }: ConstructorProps) {
2723
const { indexPrefix } = config;
2824

2925
this.client = client;
30-
this.siteConfig = config;
26+
this.config = config;
3127
this.categoriesService = categoriesService;
3228

3329
this.contentsIndex = `${indexPrefix}_content`;
34-
this.binariesIndex = `${indexPrefix}_binaries`;
35-
this.staticAssetsIndex = `${indexPrefix}_assets`;
3630
}
3731

3832
public async contentSearch(params: ContentSearchParams): Promise<ContentSearchResult> {
@@ -114,38 +108,6 @@ export class CmsArchiveContentService {
114108
return this.fixContent(result);
115109
}
116110

117-
public async getBinary(binaryKey: string): Promise<CmsBinaryDocument | null> {
118-
return this.client.getDocument<CmsBinaryDocument>({
119-
index: this.binariesIndex,
120-
id: binaryKey,
121-
});
122-
}
123-
124-
public async getStaticAsset(filePath: string): Promise<AssetDocument | null> {
125-
const withoutLeadingSlash = filePath.replace(/^\//, '');
126-
127-
const result = await this.client.search<AssetDocument>({
128-
index: this.staticAssetsIndex,
129-
body: {
130-
query: {
131-
term: {
132-
path: withoutLeadingSlash,
133-
},
134-
},
135-
},
136-
});
137-
138-
if (!result || result.hits.length === 0) {
139-
return null;
140-
}
141-
142-
if (result.hits.length > 1) {
143-
console.error(`Found multiple files with path ${filePath}!`);
144-
}
145-
146-
return result.hits[0];
147-
}
148-
149111
private fixContent(contentDocument: CmsContentDocument | null): CmsContent | null {
150112
if (!contentDocument) {
151113
return null;
@@ -166,6 +128,6 @@ export class CmsArchiveContentService {
166128

167129
return html
168130
.replace(/(\r\n|\r|\n)/, '')
169-
.replace(/="\/(\d)+\//g, `="${this.siteConfig.basePath}/`);
131+
.replace(/="\/(\d)+\//g, `="${this.config.basePath}/`);
170132
}
171133
}

server/src/cms/CmsArchiveSite.ts

+14-14
Original file line numberDiff line numberDiff line change
@@ -65,34 +65,34 @@ export class CmsArchiveSite {
6565
return res.send(rootCategories);
6666
});
6767

68-
router.get('/categories/:keys', async (req, res) => {
68+
router.get('/categories/:keys', (req, res) => {
6969
const keys = parseQueryParamsList(req.params.keys);
7070
if (!keys) {
7171
return res.status(400).send('Required parameter "keys" is not valid');
7272
}
7373

74-
const category = await this.cmsArchiveCategoriesService.getCategories(keys);
74+
const category = this.cmsArchiveCategoriesService.getCategories(keys);
7575
return res.send(category);
7676
});
7777

78-
router.get('/content/:contentKey', async (req, res) => {
78+
router.get('/content/:contentKey', async (req, res, next) => {
7979
const { contentKey } = req.params;
8080

8181
const content = await this.cmsArchiveContentService.getContent(contentKey);
8282
if (!content) {
83-
return res.status(404).send(`Content with key ${contentKey} not found`);
83+
return next();
8484
}
8585

8686
return res.send(content);
8787
});
8888

89-
router.get('/version/:versionKey', async (req, res) => {
89+
router.get('/version/:versionKey', async (req, res, next) => {
9090
const { versionKey } = req.params;
9191

9292
const contentVersion =
9393
await this.cmsArchiveContentService.getContentVersion(versionKey);
9494
if (!contentVersion) {
95-
return res.status(404).send(`Content version with key ${versionKey} not found`);
95+
return next();
9696
}
9797

9898
return res.send(contentVersion);
@@ -126,12 +126,12 @@ export class CmsArchiveSite {
126126
return res.send(html);
127127
});
128128

129-
router.get('/html/:versionKey', cspMiddleware, async (req, res) => {
129+
router.get('/html/:versionKey', cspMiddleware, async (req, res, next) => {
130130
const { versionKey } = req.params;
131131

132132
const version = await this.cmsArchiveContentService.getContentVersion(versionKey);
133133
if (!version) {
134-
return res.status(404).send(`Content with version key ${versionKey} not found`);
134+
return next();
135135
}
136136

137137
if (!version.html) {
@@ -145,12 +145,12 @@ export class CmsArchiveSite {
145145
}
146146

147147
private setupFileRoutes(router: Router) {
148-
router.get('/binary/file/:binaryKey', async (req, res) => {
148+
router.get('/binary/file/:binaryKey', async (req, res, next) => {
149149
const { binaryKey } = req.params;
150150

151-
const binary = await this.cmsArchiveContentService.getBinary(binaryKey);
151+
const binary = await this.cmsArchiveBinariesService.getBinary(binaryKey);
152152
if (!binary) {
153-
return res.status(404).send(`Binary with key ${binaryKey} not found`);
153+
return next();
154154
}
155155

156156
return this.fileResponse(
@@ -162,7 +162,7 @@ export class CmsArchiveSite {
162162
});
163163

164164
router.use('/_public', async (req, res, next) => {
165-
const file = await this.cmsArchiveContentService.getStaticAsset(req.path);
165+
const file = await this.cmsArchiveBinariesService.getStaticAsset(req.path);
166166
if (!file) {
167167
return next();
168168
}
@@ -181,7 +181,7 @@ export class CmsArchiveSite {
181181
return next();
182182
}
183183

184-
const binary = await this.cmsArchiveContentService.getBinary(binaryKey);
184+
const binary = await this.cmsArchiveBinariesService.getBinary(binaryKey);
185185
if (!binary) {
186186
return next();
187187
}
@@ -202,7 +202,7 @@ export class CmsArchiveSite {
202202
return next();
203203
}
204204

205-
const binary = await this.cmsArchiveContentService.getBinary(binaryKey);
205+
const binary = await this.cmsArchiveBinariesService.getBinary(binaryKey);
206206
if (!binary) {
207207
return next();
208208
}

0 commit comments

Comments
 (0)