mirror of https://github.com/MaxLeiter/Drift
client: add downloading and viewing raw files (#21)
parent
606e38e192
commit
f9e9c6fe06
@ -0,0 +1,24 @@
|
|||||||
|
import { NextApiRequest, NextApiResponse } from "next"
|
||||||
|
|
||||||
|
const getRawFile = async (req: NextApiRequest, res: NextApiResponse) => {
|
||||||
|
const { id, download } = req.query
|
||||||
|
const file = await fetch(`${process.env.API_URL}/files/raw/${id}`)
|
||||||
|
if (file.ok) {
|
||||||
|
const data = await file.json()
|
||||||
|
const { title, content } = data
|
||||||
|
// serve the file raw as plain text
|
||||||
|
res.setHeader("Content-Type", "text/plain")
|
||||||
|
res.setHeader('Cache-Control', 's-maxage=86400');
|
||||||
|
if (download) {
|
||||||
|
res.setHeader("Content-Disposition", `attachment; filename="${title}"`)
|
||||||
|
} else {
|
||||||
|
res.setHeader("Content-Disposition", `inline; filename="${title}"`)
|
||||||
|
}
|
||||||
|
|
||||||
|
res.status(200).send(content)
|
||||||
|
} else {
|
||||||
|
res.status(404).send("File not found")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default getRawFile
|
@ -0,0 +1,29 @@
|
|||||||
|
import { Router } from 'express'
|
||||||
|
// import { Movie } from '../models/Post'
|
||||||
|
import { File } from '../../lib/models/File'
|
||||||
|
|
||||||
|
export const files = Router()
|
||||||
|
|
||||||
|
files.get("/raw/:id", async (req, res, next) => {
|
||||||
|
try {
|
||||||
|
const file = await File.findOne({
|
||||||
|
where: {
|
||||||
|
id: req.params.id
|
||||||
|
},
|
||||||
|
attributes: ["title", "content"],
|
||||||
|
})
|
||||||
|
// TODO: fix post inclusion
|
||||||
|
// if (file?.post.visibility === 'public' || file?.post.visibility === 'unlisted') {
|
||||||
|
res.setHeader("Cache-Control", "public, max-age=86400");
|
||||||
|
res.json(file);
|
||||||
|
// } else {
|
||||||
|
// TODO: should this be `private, `?
|
||||||
|
// res.setHeader("Cache-Control", "max-age=86400");
|
||||||
|
// res.json(file);
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
catch (e) {
|
||||||
|
next(e);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
@ -1,3 +1,4 @@
|
|||||||
export { auth } from './auth';
|
export { auth } from './auth';
|
||||||
export { posts } from './posts';
|
export { posts } from './posts';
|
||||||
export { users } from './users';
|
export { users } from './users';
|
||||||
|
export { files } from './files';
|
||||||
|
Loading…
Reference in New Issue