feat: Export collection as direct download instead of emailing (#1001)

* feat: Export collection as zip instead of emailing

* Flow typing download.js
This commit is contained in:
Tom Moor
2019-07-29 22:35:34 -07:00
committed by GitHub
parent c9b86ec2e7
commit 92a18159b5
8 changed files with 192 additions and 17 deletions

View File

@ -1,11 +1,13 @@
// @flow
import fs from 'fs';
import Router from 'koa-router';
import auth from '../middlewares/authentication';
import pagination from './middlewares/pagination';
import { presentCollection, presentUser } from '../presenters';
import { Collection, CollectionUser, Team, User } from '../models';
import { ValidationError, InvalidRequestError } from '../errors';
import { exportCollection, exportCollections } from '../logistics';
import { exportCollections } from '../logistics';
import { archiveCollection } from '../utils/zip';
import policy from '../policies';
import events from '../events';
@ -144,12 +146,11 @@ router.post('collections.export', auth(), async ctx => {
const collection = await Collection.findByPk(id);
authorize(user, 'export', collection);
// async operation to create zip archive and email user
exportCollection(id, user.email);
const filePath = await archiveCollection(collection);
ctx.body = {
success: true,
};
ctx.attachment(`${collection.name}.zip`);
ctx.set('Content-Type', 'application/force-download');
ctx.body = fs.createReadStream(filePath);
});
router.post('collections.exportAll', auth(), async ctx => {

View File

@ -45,7 +45,7 @@ router.use('/', shares.routes());
router.use('/', team.routes());
router.use('/', integrations.routes());
router.use('/', notificationSettings.routes());
router.post('*', async (ctx, next) => {
router.post('*', ctx => {
ctx.throw(new NotFoundError('Endpoint not found'));
});

View File

@ -1,4 +1,5 @@
// @flow
import stream from 'stream';
import { type Context } from 'koa';
export default function apiWrapper() {
@ -10,7 +11,10 @@ export default function apiWrapper() {
const ok = ctx.status < 400;
if (typeof ctx.body !== 'string') {
if (
typeof ctx.body !== 'string' &&
!(ctx.body instanceof stream.Readable)
) {
// $FlowFixMe
ctx.body = {
...ctx.body,