From 81c2bd3dd163d2e1210f771f77d1eae0d08b97d8 Mon Sep 17 00:00:00 2001 From: lesion Date: Mon, 5 Sep 2022 13:03:35 +0200 Subject: [PATCH] use res.download to download flyer --- pages/event/_slug.vue | 4 ++-- plugins/filters.js | 5 +++-- server/helpers.js | 3 +++ 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/pages/event/_slug.vue b/pages/event/_slug.vue index d1490283..08292aa7 100644 --- a/pages/event/_slug.vue +++ b/pages/event/_slug.vue @@ -49,7 +49,7 @@ v-container#event.pa-0.pa-sm-2 :href='`/api/event/${event.slug || event.id}.ics`') v-icon(v-text='mdiCalendarExport') v-btn.ml-2(v-if='hasMedia' large icon :title="$t('event.download_flyer')" color='primary' :aria-label="$t('event.download_flyer')" - :href='event | mediaURL') + :href='event | mediaURL("download")') v-icon(v-text='mdiFileDownloadOutline') .p-description.text-body-1.pa-3.rounded(v-if='hasMedia && event.description' itemprop='description' v-html='event.description') @@ -331,4 +331,4 @@ export default { } } } - \ No newline at end of file + diff --git a/plugins/filters.js b/plugins/filters.js index b0c0a3cb..9a5329ba 100644 --- a/plugins/filters.js +++ b/plugins/filters.js @@ -42,14 +42,15 @@ export default ({ app, store }) => { // shown in mobile homepage Vue.filter('day', value => dayjs.unix(value).tz().locale(store.state.locale).format('dddd, D MMM')) Vue.filter('mediaURL', (event, type, format = '.jpg') => { + const mediaPath = type === 'download' ? '/download/' : '/media/' if (event.media && event.media.length) { if (type === 'alt') { return event.media[0].name } else { - return store.state.settings.baseurl + '/media/' + (type === 'thumb' ? 'thumb/' : '') + event.media[0].url.replace(/.jpg$/, format) + return store.state.settings.baseurl + mediaPath + (type === 'thumb' ? 'thumb/' : '') + event.media[0].url.replace(/.jpg$/, format) } } else if (type !== 'alt') { - return store.state.settings.baseurl + '/media/' + (type === 'thumb' ? 'thumb/' : '') + 'logo.svg' + return store.state.settings.baseurl + mediaPath + (type === 'thumb' ? 'thumb/' : '') + 'logo.svg' } return '' }) diff --git a/server/helpers.js b/server/helpers.js index 1af57593..c0f42498 100644 --- a/server/helpers.js +++ b/server/helpers.js @@ -91,6 +91,9 @@ module.exports = { const router = express.Router() // serve images/thumb router.use('/media/', express.static(config.upload_path, { immutable: true, maxAge: '1y' }), (_req, res) => res.sendStatus(404)) + router.use('/download/:filename', (req, res, next) => { + return res.download(req.params.filename, undefined, { root: config.upload_path }, err => res.status(404).send('Not found (but nice try 😊)')) + }) router.use('/noimg.svg', express.static('./static/noimg.svg')) router.use('/logo.png', (req, res, next) => {