Skip to content

Commit

Permalink
Allow downloading files with non-standard names
Browse files Browse the repository at this point in the history
  • Loading branch information
backjonas committed Sep 8, 2023
1 parent 6e254c6 commit b58d6f9
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 10 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"@fullcalendar/react": "^6.1.8",
"@types/gapi": "^0.0.44",
"classnames": "^2.3.1",
"content-disposition": "^0.5.4",
"framer-motion": "^10.13.0",
"googleapis": "105",
"marked": "^8.0.0",
Expand Down
6 changes: 2 additions & 4 deletions pages/api/drive/private/download.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { NextApiRequest, NextApiResponse } from 'next'
import { getDriveInstance, getDriveFile } from '@lib/api/driveFiles'
import requireAuthMiddleware from '@middleware/checkAuth'
import contentDisposition from 'content-disposition'

const drive = getDriveInstance(process.env.GOOGLE_PRIVATE_CREDS)

Expand All @@ -9,10 +10,7 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const { fileId, fileName } = req.query
const stream = await getDriveFile(fileId, drive)

res.setHeader(
'content-disposition',
`attachment; filename="${fileName || fileId}"`
)
res.setHeader('content-disposition', contentDisposition(fileName || fileId))
stream.pipe(res)
} catch (error) {
console.error('Error downloading file', error)
Expand Down
8 changes: 3 additions & 5 deletions pages/api/drive/public/download.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import { NextApiRequest, NextApiResponse } from 'next'
import { getDriveInstance, getDriveFile } from '@lib/api/driveFiles'
import contentDisposition from 'content-disposition'

const drive = getDriveInstance()

const handler = async (req: NextApiRequest, res: NextApiResponse) => {
try {
const { fileId, fileName } = req.query
const stream = await getDriveFile(fileId, drive)

res.setHeader(
'content-disposition',
`attachment; filename="${fileName || fileId}"`
)
console.log(contentDisposition(fileName))
res.setHeader('content-disposition', contentDisposition(fileName || fileId))
stream.pipe(res)
} catch (error) {
console.error('Error downloading file', error)
Expand Down
9 changes: 8 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,13 @@ [email protected]:
resolved "https://registry.yarnpkg.com/concat-map/-/concat-map-0.0.1.tgz#d8a96bd77fd68df7793a73036a3ba0d5405d477b"
integrity sha512-/Srv4dswyQNBfohGpz9o6Yb3Gz3SrUDqBH5rTuhGR7ahtlbYKnVxw2bCFMRljaA7EXHaXZ8wsHdodFvbkhKmqg==

content-disposition@^0.5.4:
version "0.5.4"
resolved "https://registry.yarnpkg.com/content-disposition/-/content-disposition-0.5.4.tgz#8b82b4efac82512a02bb0b1dcec9d2c5e8eb5bfe"
integrity sha512-FveZTNuGw04cxlAiWbzi6zTAL/lhehaWbTtgluJh4/E95DqMwTmha3KZN1aAWA8cFIhHzMZUvLevkw5Rqk+tSQ==
dependencies:
safe-buffer "5.2.1"

cookie@^0.5.0:
version "0.5.0"
resolved "https://registry.yarnpkg.com/cookie/-/cookie-0.5.0.tgz#d1f5d71adec6558c58f389987c366aa47e994f8b"
Expand Down Expand Up @@ -2772,7 +2779,7 @@ run-parallel@^1.1.9:
dependencies:
queue-microtask "^1.2.2"

safe-buffer@^5.0.1, safe-buffer@~5.2.0:
safe-buffer@5.2.1, safe-buffer@^5.0.1, safe-buffer@~5.2.0:
version "5.2.1"
resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.2.1.tgz#1eaf9fa9bdb1fdd4ec75f58f9cdb4e6b7827eec6"
integrity sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==
Expand Down

0 comments on commit b58d6f9

Please sign in to comment.