Add dynamic export file name

Closes #387
This commit is contained in:
rijkvanzanten
2020-09-21 17:56:35 -04:00
parent 15043c7a95
commit ba85e4e6a2
15 changed files with 76 additions and 14 deletions

View File

@@ -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) => {

View File

@@ -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',

View File

@@ -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();

View File

@@ -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) => {

View File

@@ -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();

View File

@@ -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) => {

View File

@@ -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) => {

View File

@@ -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) => {

View File

@@ -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) => {

View File

@@ -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) => {

View File

@@ -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) => {

View File

@@ -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) => {

View File

@@ -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) => {

View File

@@ -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) => {

View File

@@ -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()}`;
}