Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

download with filename #82

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/client/lib/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class Files {
const requestType = 'stream'
const params = {}
if (metadata) params.metadata = JSON.stringify(metadata)

return await this.collection.fetch('/file', {
method: 'POST',
body: stream,
Expand Down Expand Up @@ -81,7 +82,8 @@ class Files {
if (opts.range) {
opts.headers.Range = `bytes=${opts.range.from || 0}-${opts.range.to || ''}`
}
return this.collection.fetch('/file/' + id, opts)
const query = opts.query || ''
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the fetch function being used here (client/lib/fetch.js) already supports a params option with an object to set query string in the URL. So lets not add anothet way here. Instead add a doc comment that passing { params: { dl: 1 }} option will set the Content-Disposition header to attachement with the filename provided at upload, making the browser treat the file as a download (and not display in the browser).

return this.collection.fetch('/file/' + id + query, opts)
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/core/lib/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ module.exports = class Files {

if (method === 'HEAD') return new EmptyStream()
const readStream = hyperdrive.createReadStream(path, range)
return { stream: readStream, headers: responseHeaders, statusCode }
return { stream: readStream, headers: responseHeaders, statusCode, record }
}

async resolveFile (id) {
Expand Down
17 changes: 13 additions & 4 deletions packages/server/handlers/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,9 +342,9 @@ module.exports = function createCollectionRoutes () {
'/:collection/file',
AH(async function (req, res, next) {
let metadata = {}
if (req.params.metadata) {
if (req.query.metadata) {
try {
metadata = JSON.parse(req.params.metadata)
metadata = JSON.parse(req.query.metadata)
if (typeof metadata !== 'object' || Array.isArray(metadata)) {
throw new Error('Metadata has to be a JSON object')
}
Expand All @@ -360,18 +360,27 @@ module.exports = function createCollectionRoutes () {
router.get(
'/:collection/file/:id',
AH(async function (req, res, next) {
if (req.query.meta) {
const query = req.query
if (query.meta) {
const record = await req.collection.files.getRecord(req.params.id)
return res.json(record)
}
const {
headers,
stream,
statusCode
statusCode,
record
} = await req.collection.files.readFileWithHeaders(req.params.id, req)
for (const [name, value] of Object.entries(headers)) {
res.setHeader(name, value)
}

if (query.dl) {
const filename = record.value.filename || record.id
res.setHeader('Content-Disposition', `attachment; filename="${filename}"`)
} else {
res.setHeader('Content-Disposition', 'inline')
}
res.status(statusCode)

pipeline(stream, res, err => {
Expand Down