diff --git a/api/src/controllers/activity.ts b/api/src/controllers/activity.ts index 53041c7b6b..f3ec5f06d2 100644 --- a/api/src/controllers/activity.ts +++ b/api/src/controllers/activity.ts @@ -4,9 +4,12 @@ import ActivityService from '../services/activity'; import MetaService from '../services/meta'; import { Action } from '../types'; import { ForbiddenException } from '../exceptions'; +import useCollection from '../middleware/use-collection'; const router = express.Router(); +router.use(useCollection('directus_activity')); + router.get( '/', asyncHandler(async (req, res, next) => { diff --git a/api/src/controllers/assets.ts b/api/src/controllers/assets.ts index 542c5d005f..f10fbde8c7 100644 --- a/api/src/controllers/assets.ts +++ b/api/src/controllers/assets.ts @@ -9,9 +9,12 @@ import { pick } from 'lodash'; import { Transformation } from '../types/assets'; import storage from '../storage'; import PayloadService from '../services/payload'; +import useCollection from '../middleware/use-collection'; const router = Router(); +router.use(useCollection('directus_Files')); + router.get( '/:pk', diff --git a/api/src/controllers/auth.ts b/api/src/controllers/auth.ts index d7844795f8..3825962ef7 100644 --- a/api/src/controllers/auth.ts +++ b/api/src/controllers/auth.ts @@ -11,7 +11,6 @@ import ms from 'ms'; import cookieParser from 'cookie-parser'; import env from '../env'; import UsersService from '../services/users'; -import { respond } from '../middleware/respond'; const router = Router(); diff --git a/api/src/controllers/fields.ts b/api/src/controllers/fields.ts index 5ffee6319f..4d36982244 100644 --- a/api/src/controllers/fields.ts +++ b/api/src/controllers/fields.ts @@ -7,9 +7,12 @@ import { InvalidPayloadException, ForbiddenException } from '../exceptions'; import Joi from 'joi'; import { Field } from '../types/field'; import { types } from '../types'; +import useCollection from '../middleware/use-collection'; const router = Router(); +router.use(useCollection('directus_fields')); + router.get( '/', asyncHandler(async (req, res, next) => { diff --git a/api/src/controllers/files.ts b/api/src/controllers/files.ts index b662550739..7d29e4ce61 100644 --- a/api/src/controllers/files.ts +++ b/api/src/controllers/files.ts @@ -11,9 +11,12 @@ import Joi from 'joi'; import { InvalidPayloadException, ForbiddenException } from '../exceptions'; import url from 'url'; import path from 'path'; +import useCollection from '../middleware/use-collection'; const router = express.Router(); +router.use(useCollection('directus_files')); + const multipartHandler = asyncHandler(async (req, res, next) => { if (req.is('multipart/form-data') === false) return next(); diff --git a/api/src/controllers/folders.ts b/api/src/controllers/folders.ts index 7668c4c7e1..ba630c6132 100644 --- a/api/src/controllers/folders.ts +++ b/api/src/controllers/folders.ts @@ -3,9 +3,12 @@ import asyncHandler from 'express-async-handler'; import FoldersService from '../services/folders'; import MetaService from '../services/meta'; import { ForbiddenException } from '../exceptions'; +import useCollection from '../middleware/use-collection'; const router = express.Router(); +router.use(useCollection('directus_folders')); + router.post( '/', asyncHandler(async (req, res, next) => { diff --git a/api/src/controllers/permissions.ts b/api/src/controllers/permissions.ts index fe4ce36f8f..6a7c5dcb37 100644 --- a/api/src/controllers/permissions.ts +++ b/api/src/controllers/permissions.ts @@ -4,9 +4,12 @@ import PermissionsService from '../services/permissions'; import MetaService from '../services/meta'; import { clone } from 'lodash'; import { InvalidCredentialsException, ForbiddenException } from '../exceptions'; +import useCollection from '../middleware/use-collection'; const router = express.Router(); +router.use(useCollection('directus_permissions')); + router.post( '/', asyncHandler(async (req, res, next) => { diff --git a/api/src/controllers/presets.ts b/api/src/controllers/presets.ts index 5b71b68e14..333751a367 100644 --- a/api/src/controllers/presets.ts +++ b/api/src/controllers/presets.ts @@ -3,9 +3,12 @@ import asyncHandler from 'express-async-handler'; import PresetsService from '../services/presets'; import MetaService from '../services/meta'; import { ForbiddenException } from '../exceptions'; +import useCollection from '../middleware/use-collection'; const router = express.Router(); +router.use(useCollection('directus_presets')); + router.post( '/', asyncHandler(async (req, res, next) => { diff --git a/api/src/controllers/relations.ts b/api/src/controllers/relations.ts index fc49d6935a..2d5bf71eb0 100644 --- a/api/src/controllers/relations.ts +++ b/api/src/controllers/relations.ts @@ -3,9 +3,12 @@ import asyncHandler from 'express-async-handler'; import RelationsService from '../services/relations'; import MetaService from '../services/meta'; import { ForbiddenException } from '../exceptions'; +import useCollection from '../middleware/use-collection'; const router = express.Router(); +router.use(useCollection('directus_relations')); + router.post( '/', asyncHandler(async (req, res, next) => { diff --git a/api/src/controllers/revisions.ts b/api/src/controllers/revisions.ts index eccb50a3ea..2c69fe2e4b 100644 --- a/api/src/controllers/revisions.ts +++ b/api/src/controllers/revisions.ts @@ -2,9 +2,12 @@ import express from 'express'; import asyncHandler from 'express-async-handler'; import RevisionsService from '../services/revisions'; import MetaService from '../services/meta'; +import useCollection from '../middleware/use-collection'; const router = express.Router(); +router.use(useCollection('directus_revisions')); + router.get( '/', asyncHandler(async (req, res, next) => { diff --git a/api/src/controllers/roles.ts b/api/src/controllers/roles.ts index 4b95917ea9..9af7583ff3 100644 --- a/api/src/controllers/roles.ts +++ b/api/src/controllers/roles.ts @@ -3,9 +3,12 @@ import asyncHandler from 'express-async-handler'; import RolesService from '../services/roles'; import MetaService from '../services/meta'; import { ForbiddenException } from '../exceptions'; +import useCollection from '../middleware/use-collection'; const router = express.Router(); +router.use(useCollection('directus_roles')); + router.post( '/', asyncHandler(async (req, res, next) => { diff --git a/api/src/controllers/settings.ts b/api/src/controllers/settings.ts index cf38789706..cefd137392 100644 --- a/api/src/controllers/settings.ts +++ b/api/src/controllers/settings.ts @@ -2,9 +2,12 @@ import express from 'express'; import asyncHandler from 'express-async-handler'; import SettingsService from '../services/settings'; import { ForbiddenException } from '../exceptions'; +import useCollection from '../middleware/use-collection'; const router = express.Router(); +router.use(useCollection('directus_settings')); + router.get( '/', asyncHandler(async (req, res, next) => { diff --git a/api/src/controllers/users.ts b/api/src/controllers/users.ts index 2881542d7e..dca81815fd 100644 --- a/api/src/controllers/users.ts +++ b/api/src/controllers/users.ts @@ -5,9 +5,12 @@ import { InvalidPayloadException, InvalidCredentialsException, ForbiddenExceptio import UsersService from '../services/users'; import MetaService from '../services/meta'; import AuthService from '../services/authentication'; +import useCollection from '../middleware/use-collection'; const router = express.Router(); +router.use(useCollection('directus_users')); + router.post( '/', asyncHandler(async (req, res, next) => { diff --git a/api/src/controllers/webhooks.ts b/api/src/controllers/webhooks.ts index 7157629157..dbd05c7b00 100644 --- a/api/src/controllers/webhooks.ts +++ b/api/src/controllers/webhooks.ts @@ -3,9 +3,12 @@ import asyncHandler from 'express-async-handler'; import WebhooksService from '../services/webhooks'; import MetaService from '../services/meta'; import { ForbiddenException } from '../exceptions'; +import useCollection from '../middleware/use-collection'; const router = express.Router(); +router.use(useCollection('directus_webhooks')); + router.post( '/', asyncHandler(async (req, res, next) => { diff --git a/api/src/middleware/respond.ts b/api/src/middleware/respond.ts index 8a7f0c893b..a49c25ad95 100644 --- a/api/src/middleware/respond.ts +++ b/api/src/middleware/respond.ts @@ -4,7 +4,7 @@ import env from "../env"; import { getCacheKey } from "../utils/get-cache-key"; import cache from '../cache'; import { Transform, transforms } from 'json2csv'; -import { PassThrough } from "stream"; +import { PassThrough } from 'stream'; export const respond: RequestHandler = asyncHandler(async (req, res) => { if (req.method.toLowerCase() === 'get' && env.CACHE_ENABLED === true && cache && !req.sanitizedQuery.export) { @@ -12,20 +12,44 @@ export const respond: RequestHandler = asyncHandler(async (req, res) => { await cache.set(key, res.locals.payload); } - if (req.sanitizedQuery.export === 'json') { - res.attachment('export.json'); - res.set('Content-Type', 'application/json'); - return res.status(200).send(JSON.stringify(res.locals.payload, null, '\t')); - } + if (req.sanitizedQuery.export) { + let filename = ''; - if (req.sanitizedQuery.export === 'csv') { - res.attachment('export.csv'); - res.set('Content-Type', 'text/csv'); - const stream = new PassThrough(); - stream.end(Buffer.from(JSON.stringify(res.locals.payload.data), 'utf-8')); - const json2csv = new Transform({ transforms: [transforms.flatten({ separator: '.' })] }); - return stream.pipe(json2csv).pipe(res); + if (req.collection) { + filename += req.collection; + } else { + filename += 'Export'; + } + + filename += ' ' + getDateFormatted(); + + if (req.sanitizedQuery.export === 'json') { + res.attachment(`${filename}.json`); + res.set('Content-Type', 'application/json'); + return res.status(200).send(JSON.stringify(res.locals.payload, null, '\t')); + } + + if (req.sanitizedQuery.export === 'csv') { + res.attachment(`${filename}.csv`); + res.set('Content-Type', 'text/csv'); + const stream = new PassThrough(); + stream.end(Buffer.from(JSON.stringify(res.locals.payload.data), 'utf-8')); + const json2csv = new Transform({ transforms: [transforms.flatten({ separator: '.' })] }); + return stream.pipe(json2csv).pipe(res); + } } return res.json(res.locals.payload); }); + +function getDateFormatted() { + const date = new Date(); + + let month = String(date.getMonth() + 1); + if (month.length === 1) month = '0' + month; + + let day = String(date.getDate()); + if (day.length === 1) day = '0' + day; + + return `${date.getFullYear()}-${month}-${day} at ${date.getHours()}.${date.getMinutes()}.${date.getSeconds()}`; +}