Skip to content

Commit

Permalink
Move privateFileSharingMaxAge access into file adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikearaya committed Dec 16, 2024
1 parent 7cd5bfc commit d8ce9a7
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
7 changes: 2 additions & 5 deletions packages/api/src/resolvers/type/media-types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { filesSettings, File as FileType, getFileAdapter } from '@unchainedshop/core-files';
import { File as FileType, getFileAdapter } from '@unchainedshop/core-files';
import { Context } from '../../context.js';
import { checkAction } from '../../acl.js';
import { actions } from '../../roles/index.js';
Expand All @@ -14,10 +14,7 @@ export const Media: MediaHelperTypes = {
const fileUploadAdapter = getFileAdapter();
const mediaUrl = modules.files.getUrl(file, params);
if (file.isPrivate) {
const expiryTimestamp = new Date(
new Date().getTime() + (filesSettings?.privateFileSharingMaxAge || 0),
).getTime();
return fileUploadAdapter.signUrl(mediaUrl, file._id, expiryTimestamp);
return fileUploadAdapter.signUrl(mediaUrl, file._id);
} else {
return mediaUrl;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/file-upload/src/director/FileAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { IBaseAdapter } from '@unchainedshop/utils';
import { UploadedFile, UploadFileData } from '../types.js';

export interface IFileAdapter<Context = unknown> extends IBaseAdapter {
signUrl: (fileUrl: string, mediaId: string, expiry: number) => Promise<string | null>;
signUrl: (fileUrl: string, mediaId: string, expiry?: number) => Promise<string | null>;
createSignedURL: (
directoryName: string,
fileName: string,
Expand Down
13 changes: 10 additions & 3 deletions packages/plugins/src/files/gridfs/gridfs-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
import { UploadFileData } from '@unchainedshop/file-upload';
import sign from './sign.js';
import crypto from 'crypto';
import { filesSettings } from '@unchainedshop/core-files';

const { ROOT_URL } = process.env;

Expand All @@ -29,15 +30,21 @@ export const GridFSAdapter: IFileAdapter = {
version: '1.0.0',

...FileAdapter,
async signUrl(fileUrl: string, mediaId: string, expiry: number) {
async signUrl(fileUrl: string, mediaId: string, expiry?: number) {
const secretKey = process.env.UNCHAINED_SECRET;
if (!secretKey) {
throw new Error('UNCHAINED_SECRET is not set in environment variables');
}

const data = `${mediaId}:${expiry}`;
const expiryTimestamp = new Date(
new Date().getTime() + (filesSettings?.privateFileSharingMaxAge || 0),
).getTime();

const normalizedTimestamp = expiry || expiryTimestamp;
const data = `${mediaId}:${normalizedTimestamp}`;

const signature = crypto.createHmac('sha256', secretKey).update(data).digest('hex');
return `${fileUrl}?s=${signature}&e=${expiry}`;
return `${fileUrl}?s=${signature}&e=${normalizedTimestamp}`;
},
async createSignedURL(directoryName, fileName) {
const expiryDate = resolveExpirationDate();
Expand Down

0 comments on commit d8ce9a7

Please sign in to comment.