Skip to content

Commit

Permalink
Merge pull request #747 from desci-labs/thumbnail-share-fix
Browse files Browse the repository at this point in the history
Cover thumbnails edgecase
  • Loading branch information
kadamidev authored Jan 8, 2025
2 parents 50372ee + 25dec78 commit 91f4e7f
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions desci-server/src/controllers/nodes/thumbnails.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@ import { NodeUuid } from '../../services/manifestRepo.js';
import { type ThumbnailMap, thumbnailsService } from '../../services/Thumbnails.js';
import { ensureUuidEndsWithDot } from '../../utils.js';

type ThumbnailsReqBodyParams = {
type ThumbnailsReqParams = {
uuid: string;
manifestCid?: string;
};

type ThumbnailsQueryParams = {
shareCode?: string;
};

type ThumbnailsResponse = {
ok: true;
thumbnailMap: ThumbnailMap;
Expand All @@ -29,11 +33,12 @@ type ThumbnailsErrorResponse = {
* @return {ThumbnailMap} ThumbnailMap = Record<ComponentCidString, Record<HeightPx, ThumbnailCidString>>
*/
export const thumbnails = async (
req: Request<any, any, ThumbnailsReqBodyParams>,
req: Request<any, any, ThumbnailsReqParams, ThumbnailsQueryParams>,
res: Response<ThumbnailsResponse | ThumbnailsErrorResponse>,
) => {
const user = (req as any).user;
const { uuid, manifestCid } = req.params;
const { shareCode } = req.query;
// debugger;
const logger = parentLogger.child({
module: 'NODES::Thumbnails',
Expand All @@ -45,13 +50,23 @@ export const thumbnails = async (

if (!uuid) return res.status(400).json({ ok: false, error: 'UUID is required.' });

if (!user && !manifestCid) {
// If there's no manifestCid passed in, we're looking at a draft node, and it requires auth.
let validShareCode = null;
if (shareCode) {
// Validate sharecode belongs to node and is valid
const validShare = await prisma.privateShare.findFirst({
where: { shareId: shareCode, nodeUUID: ensureUuidEndsWithDot(uuid) },
});
if (validShare) validShareCode = true;
}

if (!user && !manifestCid && !validShareCode) {
// If there's no manifestCid passed in, we're looking at a draft node, and it requires auth or a valid share code
return res.status(401).json({ ok: false, error: 'Unauthorized' });
}

if (user && !manifestCid) {
if (user && !manifestCid && !validShareCode) {
// Check if user owns node, if requesting draft thumbnails
// validShareCode already does the check if the shareCode belongs to that node
const node = await prisma.node.findFirst({
where: {
ownerId: user.id,
Expand Down

0 comments on commit 91f4e7f

Please sign in to comment.