|
1 | 1 | import { NextApiRequest, NextApiResponse } from 'next' |
2 | 2 |
|
3 | | -// import { slackClient } from '@/utils/slackClient' |
| 3 | +import { slackClient } from '@/utils/slackClient' |
4 | 4 |
|
5 | | -export default async function handler(req: NextApiRequest, res: NextApiResponse) { |
6 | | - // const { fileId } = req.query |
7 | | - |
8 | | - return res.status(403).json({ error: 'Unauthorized' }) |
9 | | - |
10 | | - // if (!fileId || typeof fileId !== 'string') { |
11 | | - // return res.status(400).json({ error: 'Invalid fileId' }) |
12 | | - // } |
13 | | - |
14 | | - // try { |
15 | | - // const result = await slackClient.files.info({ file: fileId }) |
| 5 | +import { isAuthorized } from './auth' |
16 | 6 |
|
17 | | - // if (!result.file || !result.file.url_private) { |
18 | | - // return res.status(404).json({ error: 'File not found' }) |
19 | | - // } |
20 | | - |
21 | | - // const fileContent = await fetch(result.file.url_private, { |
22 | | - // headers: { Authorization: `Bearer ${process.env.SLACK_BOT_TOKEN}` }, |
23 | | - // }) |
24 | | - |
25 | | - // const jsonData = await fileContent.json() |
26 | | - // res.status(200).json(jsonData) |
27 | | - // } catch (error) { |
28 | | - // console.error('Error fetching Slack file:', error) |
29 | | - // res.status(500).json({ error: 'Error fetching Slack file' }) |
30 | | - // } |
| 7 | +export default async function handler(req: NextApiRequest, res: NextApiResponse) { |
| 8 | + isAuthorized(req, res) |
| 9 | + |
| 10 | + const { fileId } = req.query |
| 11 | + if (!fileId || typeof fileId !== 'string') { |
| 12 | + return res.status(400).json({ error: 'Invalid fileId' }) |
| 13 | + } |
| 14 | + |
| 15 | + try { |
| 16 | + const result = await slackClient.files.info({ file: fileId }) |
| 17 | + |
| 18 | + if (!result.file || !result.file.url_private) { |
| 19 | + return res.status(404).json({ error: 'File not found' }) |
| 20 | + } |
| 21 | + |
| 22 | + const fileContent = await fetch(result.file.url_private, { |
| 23 | + headers: { Authorization: `Bearer ${process.env.SLACK_BOT_TOKEN}` }, |
| 24 | + }) |
| 25 | + |
| 26 | + const jsonData = await fileContent.json() |
| 27 | + res.status(200).json(jsonData) |
| 28 | + } catch (error) { |
| 29 | + console.error('Error fetching Slack file:', error) |
| 30 | + res.status(500).json({ error: 'Error fetching Slack file' }) |
| 31 | + } |
31 | 32 | } |
0 commit comments