diff --git a/src/middleware/etag.ts b/src/middleware/etag.ts index 786586f..3f1f30d 100644 --- a/src/middleware/etag.ts +++ b/src/middleware/etag.ts @@ -1,9 +1,9 @@ import { NextFunction, Request, Response } from "express"; import { VideoID, VideoIDHash, Service } from "../types/segments.model"; import { QueryCacher } from "../utils/queryCacher"; -import { skipSegmentsHashKey, skipSegmentsKey, videoLabelsHashKey, videoLabelsKey } from "../utils/redisKeys"; +import { brandingHashKey, brandingKey, skipSegmentsHashKey, skipSegmentsKey, videoLabelsHashKey, videoLabelsKey } from "../utils/redisKeys"; -type hashType = "skipSegments" | "skipSegmentsHash" | "videoLabel" | "videoLabelHash"; +type hashType = "skipSegments" | "skipSegmentsHash" | "videoLabel" | "videoLabelHash" | "branding" | "brandingHash"; type ETag = `"${hashType};${VideoIDHash};${Service};${number}"`; type hashKey = string | VideoID | VideoIDHash; @@ -32,6 +32,8 @@ function getLastModified(hashType: hashType, hashKey: hashKey, service: Service) else if (hashType === "skipSegmentsHash") redisKey = skipSegmentsHashKey(hashKey as VideoIDHash, service); else if (hashType === "videoLabel") redisKey = videoLabelsKey(hashKey as VideoID, service); else if (hashType === "videoLabelHash") redisKey = videoLabelsHashKey(hashKey as VideoIDHash, service); + else if (hashType === "branding") redisKey = brandingKey(hashKey as VideoID, service); + else if (hashType === "brandingHash") redisKey = brandingHashKey(hashKey as VideoIDHash, service); else return Promise.reject(); return QueryCacher.getKeyLastModified(redisKey); } diff --git a/src/routes/getBranding.ts b/src/routes/getBranding.ts index b8e135d..ac897d0 100644 --- a/src/routes/getBranding.ts +++ b/src/routes/getBranding.ts @@ -15,6 +15,7 @@ import { promiseOrTimeout } from "../utils/promise"; import { QueryCacher } from "../utils/queryCacher"; import { brandingHashKey, brandingIPKey, brandingKey } from "../utils/redisKeys"; import * as SeedRandom from "seedrandom"; +import { getEtag } from "../middleware/etag"; enum BrandingSubmissionType { Title = "title", @@ -294,6 +295,10 @@ export async function getBranding(req: Request, res: Response) { try { const result = await getVideoBranding(res, videoID, service, ip, returnUserID, fetchAll); + await getEtag("branding", (videoID as string), service) + .then(etag => res.set("ETag", etag)) + .catch(() => null); + const status = result.titles.length > 0 || result.thumbnails.length > 0 ? 200 : 404; return res.status(status).json(result); } catch (e) { @@ -317,6 +322,10 @@ export async function getBrandingByHashEndpoint(req: Request, res: Response) { try { const result = await getVideoBrandingByHash(hashPrefix, service, ip, returnUserID, fetchAll); + await getEtag("brandingHash", (hashPrefix as string), service) + .then(etag => res.set("ETag", etag)) + .catch(() => null); + const status = !isEmpty(result) ? 200 : 404; return res.status(status).json(result); } catch (e) {