Browse Source

Merge pull request #5151 from nocodb/feat/set-cache-header

feat(nocodb): set cache header for attachment urls
refactor/icons-update
աɨռɢӄաօռɢ 2 years ago committed by GitHub
parent
commit
519ed72761
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 11
      packages/nocodb/src/lib/meta/api/attachmentApis.ts
  2. 13
      packages/nocodb/src/lib/meta/api/helpers/cacheHelpers.ts
  3. 1
      packages/nocodb/src/lib/meta/api/helpers/index.ts

11
packages/nocodb/src/lib/meta/api/attachmentApis.ts

@ -14,6 +14,7 @@ import catchError, { NcError } from '../helpers/catchError';
import NcPluginMgrv2 from '../helpers/NcPluginMgrv2'; import NcPluginMgrv2 from '../helpers/NcPluginMgrv2';
import Local from '../../v1-legacy/plugins/adapters/storage/Local'; import Local from '../../v1-legacy/plugins/adapters/storage/Local';
import { NC_ATTACHMENT_FIELD_SIZE } from '../../constants'; import { NC_ATTACHMENT_FIELD_SIZE } from '../../constants';
import { getCacheMiddleware } from './helpers';
const isUploadAllowed = async (req: Request, _res: Response, next: any) => { const isUploadAllowed = async (req: Request, _res: Response, next: any) => {
if (!req['user']?.id) { if (!req['user']?.id) {
@ -161,7 +162,10 @@ export async function fileRead(req, res) {
const router = Router({ mergeParams: true }); const router = Router({ mergeParams: true });
router.get(/^\/dl\/([^/]+)\/([^/]+)\/(.+)$/, async (req, res) => { router.get(
/^\/dl\/([^/]+)\/([^/]+)\/(.+)$/,
getCacheMiddleware(),
async (req, res) => {
try { try {
// const type = mimetypes[path.extname(req.params.fileName).slice(1)] || 'text/plain'; // const type = mimetypes[path.extname(req.params.fileName).slice(1)] || 'text/plain';
const type = const type =
@ -186,7 +190,8 @@ router.get(/^\/dl\/([^/]+)\/([^/]+)\/(.+)$/, async (req, res) => {
} catch (e) { } catch (e) {
res.status(404).send('Not found'); res.status(404).send('Not found');
} }
}); }
);
export function sanitizeUrlPath(paths) { export function sanitizeUrlPath(paths) {
return paths.map((url) => url.replace(/[/.?#]+/g, '_')); return paths.map((url) => url.replace(/[/.?#]+/g, '_'));
@ -217,6 +222,6 @@ router.post(
] ]
); );
router.get(/^\/download\/(.+)$/, catchError(fileRead)); router.get(/^\/download\/(.+)$/, getCacheMiddleware(), catchError(fileRead));
export default router; export default router;

13
packages/nocodb/src/lib/meta/api/helpers/cacheHelpers.ts

@ -0,0 +1,13 @@
// return a middleware to set cache-control header
// default period is 30 days
export const getCacheMiddleware = (period: string | number = 2592000) => {
return async (req, res, next) => {
const { method } = req;
// only cache GET requests
if (method === 'GET') {
// set cache-control header
res.set('Cache-Control', `public, max-age=${period}`);
}
next();
};
};

1
packages/nocodb/src/lib/meta/api/helpers/index.ts

@ -1,5 +1,6 @@
import { populateMeta } from './populateMeta'; import { populateMeta } from './populateMeta';
export * from './columnHelpers'; export * from './columnHelpers';
export * from './apiHelpers'; export * from './apiHelpers';
export * from './cacheHelpers';
export { populateMeta }; export { populateMeta };

Loading…
Cancel
Save