diff --git a/packages/component-aws-download/index.js b/packages/component-aws-download/index.js new file mode 100644 index 0000000000000000000000000000000000000000..140b65ac0183438e989b7e7f552b3498baa32820 --- /dev/null +++ b/packages/component-aws-download/index.js @@ -0,0 +1,5 @@ +require('dotenv').config() + +module.exports = { + backend: () => app => require('./src/FileBackend')(app), +} diff --git a/packages/component-aws-download/package.json b/packages/component-aws-download/package.json new file mode 100644 index 0000000000000000000000000000000000000000..4d54d2c1d945d705640586d78e3b8cace92fd724 --- /dev/null +++ b/packages/component-aws-download/package.json @@ -0,0 +1,18 @@ +{ + "name": "component-aws-download", + "version": "0.0.1", + "main": "index.js", + "license": "MIT", + "dependencies": { + "archiver": "^2.1.1", + "aws-sdk": "^2.185.0", + "body-parser": "^1.17.2", + "multer": "^1.3.0", + "multer-s3": "^2.7.0", + "node-mocks-http": "^1.6.6" + }, + "peerDependencies": { + "@pubsweet/logger": "^0.0.1", + "pubsweet-server": "^1.0.1" + } +} diff --git a/packages/component-aws-download/src/FileBackend.js b/packages/component-aws-download/src/FileBackend.js new file mode 100644 index 0000000000000000000000000000000000000000..bedb9966cb9ed670c76188321b26b229e19822be --- /dev/null +++ b/packages/component-aws-download/src/FileBackend.js @@ -0,0 +1,62 @@ +const AWS = require('aws-sdk') +// const logger = require('@pubsweet/logger') +const fs = require('fs') +const _ = require('lodash') +const util = require('util') +const config = require('config') +const archiver = require('archiver') + +const s3Config = _.get(config, 'pubsweet-component-aws-s3') + +const FileBackend = app => { + const authBearer = app.locals.passport.authenticate('bearer', { + session: false, + }) + AWS.config.update({ + secretAccessKey: s3Config.secretAccessKey, + accessKeyId: s3Config.accessKeyId, + region: s3Config.region, + }) + const s3 = new AWS.S3() + + app.get('/api/fileZip/:fragmentId', authBearer, async (req, res) => { + const output = fs.createWriteStream(`${__dirname}/files-output.zip`) + const params = { + Bucket: s3Config.bucket, + Prefix: `${req.params.fragmentId}`, + } + + const listObjects = util.promisify(s3.listObjects.bind(s3)) + const getObject = util.promisify(s3.getObject.bind(s3)) + + const archive = archiver('zip') + archive.pipe(output) + + return listObjects(params).then(data => { + Promise.all( + data.Contents.map(content => + getObject({ + Bucket: s3Config.bucket, + Key: content.Key, + }), + ), + ).then(values => { + values.forEach((v, index) => { + archive.append(v.Body, { name: v.ETag }) + }) + + archive.finalize().then(() => { + res.writeHead(200, { + 'Content-Type': 'application/octet-stream', + 'Content-Disposition': `attachment; filename=files-output.zip`, + }) + const readStream = fs.createReadStream(output.path) + readStream.pipe(res) + // res.download(output.path) + }) + }) + }) + }) +} + +module.exports = FileBackend diff --git a/packages/component-aws-download/src/files-output.zip b/packages/component-aws-download/src/files-output.zip new file mode 100644 index 0000000000000000000000000000000000000000..73ec43c1f18c5b3f8d234759bfe4115842d7ff01 Binary files /dev/null and b/packages/component-aws-download/src/files-output.zip differ diff --git a/packages/components-faraday/src/components/Dashboard/DashboardCard.js b/packages/components-faraday/src/components/Dashboard/DashboardCard.js index 06dba138b991aa68954de0401e12df0286b9babc..2be82fc80d899373b6e7112256922934148979f9 100644 --- a/packages/components-faraday/src/components/Dashboard/DashboardCard.js +++ b/packages/components-faraday/src/components/Dashboard/DashboardCard.js @@ -3,9 +3,10 @@ import PropTypes from 'prop-types' import { get, isEmpty } from 'lodash' import styled, { css } from 'styled-components' import { Button, Icon } from '@pubsweet/ui' -import { compose, getContext } from 'recompose' +import { compose, getContext, withHandlers } from 'recompose' +import * as api from 'pubsweet-client/src/helpers/api' -import { parseVersion, getFilesURL, downloadAll } from './utils' +import { parseVersion, getFilesURL } from './utils' const DashboardCard = ({ deleteProject, @@ -14,6 +15,7 @@ const DashboardCard = ({ version, showAbstractModal, journal, + getItems, ...rest }) => { const { submitted, title, type, version: vers } = parseVersion(version) @@ -45,9 +47,14 @@ const DashboardCard = ({ </ManuscriptInfo> </Left> <Right> + {/* <form onSubmit={getItems}> + <Icon>download</Icon> + <button type="submit">DOWNLOAD</button> + </form> */} <ClickableIcon disabled={!hasFiles} - onClick={() => (hasFiles ? downloadAll(files) : null)} + // onClick={() => (hasFiles ? downloadAll(files) : null)} + onClick={getItems} > <Icon>download</Icon> </ClickableIcon> @@ -115,7 +122,25 @@ const DashboardCard = ({ ) : null } -export default compose(getContext({ journal: PropTypes.object }))(DashboardCard) +export default compose( + getContext({ journal: PropTypes.object }), + withHandlers({ + getItems: ({ version }) => () => { + api.get(`/fileZip/a466f9d0-ecf2-41b4-88ff-92159d848cff`).then(r => { + const blob = new File([r], 'myzip.zip', { type: 'application/zip' }) + const url = URL.createObjectURL(blob) + window.open(url) + const a = document.createElement('a') + a.href = url + a.download = 'myfile.zip' + document.body.appendChild(a) + a.click() + // const zip = new File(r, 'myzip.zip') + // console.log('the zip', zip) + }) + }, + }), +)(DashboardCard) // #region styled-components const defaultText = css` diff --git a/packages/xpub-faraday/config/components.json b/packages/xpub-faraday/config/components.json index a6b66287113c39f421842f3d071e277c953150cb..77c96db312b2a90d5c7927a153cf11c48d40af95 100644 --- a/packages/xpub-faraday/config/components.json +++ b/packages/xpub-faraday/config/components.json @@ -7,5 +7,6 @@ "pubsweet-component-modal", "pubsweet-components-faraday", "@pubsweet/component-aws-s3", - "pubsweet-component-invite" + "pubsweet-component-invite", + "component-aws-download" ] diff --git a/packages/xpub-faraday/package.json b/packages/xpub-faraday/package.json index b5f745123b8d861d432231839d87cf365a6a1657..1b4c5f08d7ad21b048d1985b43cd68fea450ad35 100644 --- a/packages/xpub-faraday/package.json +++ b/packages/xpub-faraday/package.json @@ -8,6 +8,7 @@ "url": "https://gitlab.coko.foundation/xpub/xpub" }, "dependencies": { + "component-aws-download": "0.0.1", "@pubsweet/component-aws-s3": "^0.1.1", "@pubsweet/ui": "^2.0.0", "aws-sdk": "^2.197.0", @@ -74,7 +75,8 @@ "setupdb": "pubsweet setupdb ./", "reset": "pubsweet setupdb --clobber ./", "start": "pubsweet start", - "start-now": "echo $secret > config/local-development.json && npm run start", + "start-now": + "echo $secret > config/local-development.json && npm run start", "build": "NODE_ENV=production pubsweet build" } } diff --git a/yarn.lock b/yarn.lock index 0de9ae2cde82b8f8e7a379139f09d91bd020b0e6..78d69805b69afb1b85c43e2ef717764edcdc8dbf 100644 --- a/yarn.lock +++ b/yarn.lock @@ -433,6 +433,30 @@ aproba@^1.0.3, aproba@^1.1.1: version "1.2.0" resolved "https://registry.yarnpkg.com/aproba/-/aproba-1.2.0.tgz#6802e6264efd18c790a1b0d517f0f2627bf2c94a" +archiver-utils@^1.3.0: + version "1.3.0" + resolved "https://registry.yarnpkg.com/archiver-utils/-/archiver-utils-1.3.0.tgz#e50b4c09c70bf3d680e32ff1b7994e9f9d895174" + dependencies: + glob "^7.0.0" + graceful-fs "^4.1.0" + lazystream "^1.0.0" + lodash "^4.8.0" + normalize-path "^2.0.0" + readable-stream "^2.0.0" + +archiver@^2.1.1: + version "2.1.1" + resolved "https://registry.yarnpkg.com/archiver/-/archiver-2.1.1.tgz#ff662b4a78201494a3ee544d3a33fe7496509ebc" + dependencies: + archiver-utils "^1.3.0" + async "^2.0.0" + buffer-crc32 "^0.2.1" + glob "^7.0.0" + lodash "^4.8.0" + readable-stream "^2.0.0" + tar-stream "^1.5.0" + zip-stream "^1.2.0" + are-we-there-yet@~1.1.2: version "1.1.4" resolved "https://registry.yarnpkg.com/are-we-there-yet/-/are-we-there-yet-1.1.4.tgz#bb5dca382bb94f05e15194373d16fd3ba1ca110d" @@ -630,7 +654,7 @@ async@^1.4.0, async@^1.5.0, async@^1.5.2: version "1.5.2" resolved "https://registry.yarnpkg.com/async/-/async-1.5.2.tgz#ec6a61ae56480c0c3cb241c95618e20892f9672a" -async@^2.1.2, async@^2.1.4, async@^2.1.5, async@^2.4.1: +async@^2.0.0, async@^2.1.2, async@^2.1.4, async@^2.1.5, async@^2.4.1: version "2.6.0" resolved "https://registry.yarnpkg.com/async/-/async-2.6.0.tgz#61a29abb6fcc026fea77e56d1c6ec53a795951f4" dependencies: @@ -1749,6 +1773,10 @@ buble@^0.19.2: os-homedir "^1.0.1" vlq "^1.0.0" +buffer-crc32@^0.2.1: + version "0.2.13" + resolved "https://registry.yarnpkg.com/buffer-crc32/-/buffer-crc32-0.2.13.tgz#0d333e3f00eac50aa1454abd30ef8c2a5d9a7242" + buffer-equal-constant-time@1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/buffer-equal-constant-time/-/buffer-equal-constant-time-1.0.1.tgz#f8e71132f7ffe6e01a5c9697a4c6f3e48d5cc819" @@ -2334,6 +2362,15 @@ component-emitter@^1.2.0, component-emitter@^1.2.1: version "1.2.1" resolved "https://registry.yarnpkg.com/component-emitter/-/component-emitter-1.2.1.tgz#137918d6d78283f7df7a6b7c5a63e140e69425e6" +compress-commons@^1.2.0: + version "1.2.2" + resolved "https://registry.yarnpkg.com/compress-commons/-/compress-commons-1.2.2.tgz#524a9f10903f3a813389b0225d27c48bb751890f" + dependencies: + buffer-crc32 "^0.2.1" + crc32-stream "^2.0.0" + normalize-path "^2.0.0" + readable-stream "^2.0.0" + compressible@~2.0.13: version "2.0.13" resolved "https://registry.yarnpkg.com/compressible/-/compressible-2.0.13.tgz#0d1020ab924b2fdb4d6279875c7d6daba6baa7a9" @@ -2657,6 +2694,17 @@ cosmiconfig@^4.0.0: parse-json "^4.0.0" require-from-string "^2.0.1" +crc32-stream@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/crc32-stream/-/crc32-stream-2.0.0.tgz#e3cdd3b4df3168dd74e3de3fbbcb7b297fe908f4" + dependencies: + crc "^3.4.4" + readable-stream "^2.0.0" + +crc@^3.4.4: + version "3.5.0" + resolved "https://registry.yarnpkg.com/crc/-/crc-3.5.0.tgz#98b8ba7d489665ba3979f59b21381374101a1964" + create-ecdh@^4.0.0: version "4.0.0" resolved "https://registry.yarnpkg.com/create-ecdh/-/create-ecdh-4.0.0.tgz#888c723596cdf7612f6498233eebd7a35301737d" @@ -4699,7 +4747,7 @@ got@^6.7.1: unzip-response "^2.0.1" url-parse-lax "^1.0.0" -graceful-fs@^4.1.11, graceful-fs@^4.1.2, graceful-fs@^4.1.4, graceful-fs@^4.1.6: +graceful-fs@^4.1.0, graceful-fs@^4.1.11, graceful-fs@^4.1.2, graceful-fs@^4.1.4, graceful-fs@^4.1.6: version "4.1.11" resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.1.11.tgz#0e8bdfe4d1ddb8854d64e04ea7c00e2a026e5658" @@ -6437,6 +6485,12 @@ lazy-cache@^2.0.2: dependencies: set-getter "^0.1.0" +lazystream@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/lazystream/-/lazystream-1.0.0.tgz#f6995fe0f820392f61396be89462407bb77168e4" + dependencies: + readable-stream "^2.0.5" + lcid@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/lcid/-/lcid-1.0.0.tgz#308accafa0bc483a3867b4b6f2b9506251d1b835" @@ -6885,7 +6939,7 @@ lodash.uniq@^4.5.0: version "4.5.0" resolved "https://registry.yarnpkg.com/lodash.uniq/-/lodash.uniq-4.5.0.tgz#d0225373aeb652adc1bc82e4945339a842754773" -lodash@^4, lodash@^4.0.0, lodash@^4.1.0, lodash@^4.13.1, lodash@^4.14.0, lodash@^4.15.0, lodash@^4.17.2, lodash@^4.17.3, lodash@^4.17.4, lodash@^4.17.5, lodash@^4.2.0, lodash@^4.2.1, lodash@^4.3.0, lodash@^4.6.1, lodash@~4.17.4: +lodash@^4, lodash@^4.0.0, lodash@^4.1.0, lodash@^4.13.1, lodash@^4.14.0, lodash@^4.15.0, lodash@^4.17.2, lodash@^4.17.3, lodash@^4.17.4, lodash@^4.17.5, lodash@^4.2.0, lodash@^4.2.1, lodash@^4.3.0, lodash@^4.6.1, lodash@^4.8.0, lodash@~4.17.4: version "4.17.5" resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.5.tgz#99a92d65c0272debe8c96b6057bc8fbfa3bed511" @@ -11359,7 +11413,7 @@ tar-pack@^3.4.0: tar "^2.2.1" uid-number "^0.0.6" -tar-stream@^1.1.2: +tar-stream@^1.1.2, tar-stream@^1.5.0: version "1.5.5" resolved "https://registry.yarnpkg.com/tar-stream/-/tar-stream-1.5.5.tgz#5cad84779f45c83b1f2508d96b09d88c7218af55" dependencies: @@ -12668,3 +12722,12 @@ yargs@~3.10.0: zen-observable@^0.7.0: version "0.7.1" resolved "https://registry.yarnpkg.com/zen-observable/-/zen-observable-0.7.1.tgz#f84075c0ee085594d3566e1d6454207f126411b3" + +zip-stream@^1.2.0: + version "1.2.0" + resolved "https://registry.yarnpkg.com/zip-stream/-/zip-stream-1.2.0.tgz#a8bc45f4c1b49699c6b90198baacaacdbcd4ba04" + dependencies: + archiver-utils "^1.3.0" + compress-commons "^1.2.0" + lodash "^4.8.0" + readable-stream "^2.0.0"