generated from mantinedev/next-pages-template
-
-
Notifications
You must be signed in to change notification settings - Fork 292
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add download functionality for board configuration files (#2111)
- Loading branch information
1 parent
cc240f4
commit a879358
Showing
4 changed files
with
100 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import AdmZip from 'adm-zip'; | ||
import fs from 'fs'; | ||
import { NextApiRequest, NextApiResponse } from 'next'; | ||
import { getServerAuthSession } from '~/server/auth'; | ||
import { getFrontendConfig } from '~/tools/config/getFrontendConfig'; | ||
|
||
const handler = async (req: NextApiRequest, res: NextApiResponse) => { | ||
const session = await getServerAuthSession({ req, res }); | ||
if (!session) { | ||
return res.status(401).end(); | ||
} | ||
|
||
if (!session.user.isAdmin) { | ||
return res.status(403).end(); | ||
} | ||
|
||
const files = fs.readdirSync('./data/configs').filter((file) => file.endsWith('.json')); | ||
|
||
const zip = new AdmZip(); | ||
|
||
for (const file of files) { | ||
const data = await getFrontendConfig(file.replace('.json', '')); | ||
const content = JSON.stringify(data, null, 2); | ||
zip.addFile(file, Buffer.from(content, 'utf-8')); | ||
} | ||
|
||
const zipBuffer = zip.toBuffer(); | ||
res.setHeader('Content-Type', 'application/zip'); | ||
res.setHeader('Content-Disposition', 'attachment; filename=board-configs.zip'); | ||
res.setHeader('Content-Length', zipBuffer.length.toString()); | ||
res.status(200).end(zipBuffer); | ||
}; | ||
|
||
export default handler; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters