Skip to content

Commit 063607f

Browse files
committed
Add etags for branding as well
1 parent 4b795da commit 063607f

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

src/middleware/etag.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { NextFunction, Request, Response } from "express";
22
import { VideoID, VideoIDHash, Service } from "../types/segments.model";
33
import { QueryCacher } from "../utils/queryCacher";
4-
import { skipSegmentsHashKey, skipSegmentsKey, videoLabelsHashKey, videoLabelsKey } from "../utils/redisKeys";
4+
import { brandingHashKey, brandingKey, skipSegmentsHashKey, skipSegmentsKey, videoLabelsHashKey, videoLabelsKey } from "../utils/redisKeys";
55

6-
type hashType = "skipSegments" | "skipSegmentsHash" | "videoLabel" | "videoLabelHash";
6+
type hashType = "skipSegments" | "skipSegmentsHash" | "videoLabel" | "videoLabelHash" | "branding" | "brandingHash";
77
type ETag = `"${hashType};${VideoIDHash};${Service};${number}"`;
88
type hashKey = string | VideoID | VideoIDHash;
99

@@ -32,6 +32,8 @@ function getLastModified(hashType: hashType, hashKey: hashKey, service: Service)
3232
else if (hashType === "skipSegmentsHash") redisKey = skipSegmentsHashKey(hashKey as VideoIDHash, service);
3333
else if (hashType === "videoLabel") redisKey = videoLabelsKey(hashKey as VideoID, service);
3434
else if (hashType === "videoLabelHash") redisKey = videoLabelsHashKey(hashKey as VideoIDHash, service);
35+
else if (hashType === "branding") redisKey = brandingKey(hashKey as VideoID, service);
36+
else if (hashType === "brandingHash") redisKey = brandingHashKey(hashKey as VideoIDHash, service);
3537
else return Promise.reject();
3638
return QueryCacher.getKeyLastModified(redisKey);
3739
}

src/routes/getBranding.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { promiseOrTimeout } from "../utils/promise";
1515
import { QueryCacher } from "../utils/queryCacher";
1616
import { brandingHashKey, brandingIPKey, brandingKey } from "../utils/redisKeys";
1717
import * as SeedRandom from "seedrandom";
18+
import { getEtag } from "../middleware/etag";
1819

1920
enum BrandingSubmissionType {
2021
Title = "title",
@@ -294,6 +295,10 @@ export async function getBranding(req: Request, res: Response) {
294295
try {
295296
const result = await getVideoBranding(res, videoID, service, ip, returnUserID, fetchAll);
296297

298+
await getEtag("branding", (videoID as string), service)
299+
.then(etag => res.set("ETag", etag))
300+
.catch(() => null);
301+
297302
const status = result.titles.length > 0 || result.thumbnails.length > 0 ? 200 : 404;
298303
return res.status(status).json(result);
299304
} catch (e) {
@@ -317,6 +322,10 @@ export async function getBrandingByHashEndpoint(req: Request, res: Response) {
317322
try {
318323
const result = await getVideoBrandingByHash(hashPrefix, service, ip, returnUserID, fetchAll);
319324

325+
await getEtag("brandingHash", (hashPrefix as string), service)
326+
.then(etag => res.set("ETag", etag))
327+
.catch(() => null);
328+
320329
const status = !isEmpty(result) ? 200 : 404;
321330
return res.status(status).json(result);
322331
} catch (e) {

0 commit comments

Comments
 (0)