From 057f915b29a6fd68068d02528ee18adb967fe098 Mon Sep 17 00:00:00 2001 From: Keane Chan Date: Mon, 10 Oct 2022 09:44:57 +0800 Subject: [PATCH] [resumes][feat] revert to storing fileURL instead --- .../src/components/resumes/ResumePdf.tsx | 21 ++--------------- apps/portal/src/pages/api/file-storage.ts | 23 ++++--------------- 2 files changed, 7 insertions(+), 37 deletions(-) diff --git a/apps/portal/src/components/resumes/ResumePdf.tsx b/apps/portal/src/components/resumes/ResumePdf.tsx index 5281a52b..a190606e 100644 --- a/apps/portal/src/components/resumes/ResumePdf.tsx +++ b/apps/portal/src/components/resumes/ResumePdf.tsx @@ -1,12 +1,9 @@ -import axios from 'axios'; -import { useEffect, useState } from 'react'; +import { useState } from 'react'; import { Document, Page, pdfjs } from 'react-pdf'; import type { PDFDocumentProxy } from 'react-pdf/node_modules/pdfjs-dist'; import { ArrowLeftIcon, ArrowRightIcon } from '@heroicons/react/20/solid'; import { Button, Spinner } from '@tih/ui'; -import { RESUME_STORAGE_KEY } from '~/constants/file-storage-keys'; - pdfjs.GlobalWorkerOptions.workerSrc = `//unpkg.com/pdfjs-dist@${pdfjs.version}/build/pdf.worker.min.js`; type Props = Readonly<{ @@ -16,30 +13,16 @@ type Props = Readonly<{ export default function ResumePdf({ url }: Props) { const [numPages, setNumPages] = useState(0); const [pageNumber, setPageNumber] = useState(1); - const [file, setFile] = useState(); const onPdfLoadSuccess = (pdf: PDFDocumentProxy) => { setNumPages(pdf.numPages); }; - useEffect(() => { - async function fetchData() { - await axios - .get(`/api/file-storage?key=${RESUME_STORAGE_KEY}&url=${url}`, { - responseType: 'blob', - }) - .then((res) => { - setFile(res.data); - }); - } - fetchData(); - }, [url]); - return (
} noData="" onLoadSuccess={onPdfLoadSuccess}> diff --git a/apps/portal/src/pages/api/file-storage.ts b/apps/portal/src/pages/api/file-storage.ts index d57f2453..cc48bab1 100644 --- a/apps/portal/src/pages/api/file-storage.ts +++ b/apps/portal/src/pages/api/file-storage.ts @@ -2,6 +2,7 @@ import formidable from 'formidable'; import * as fs from 'fs'; import type { NextApiRequest, NextApiResponse } from 'next'; +import { env } from '~/env/server.mjs'; import { supabase } from '~/utils/supabase'; export const config = { @@ -10,6 +11,8 @@ export const config = { }, }; +const BASE_FILE_URL = `${env.SUPABASE_URL}/storage/v1/object/public`; + export default async function handler( req: NextApiRequest, res: NextApiResponse, @@ -38,28 +41,12 @@ export default async function handler( throw error; } - return res.status(200).json({ - url: filePath, + return res.status(201).json({ + url: `${BASE_FILE_URL}/${key}/${filePath}`, }); }); } catch (error: unknown) { return Promise.reject(error); } } - - if (req.method === 'GET') { - const { key, url } = req.query; - - const { data, error } = await supabase.storage - .from(`public/${key as string}`) - .download(url as string); - - if (error || data == null) { - throw error; - } - - const arrayBuffer = await data.arrayBuffer(); - const buffer = Buffer.from(arrayBuffer); - res.status(200).send(buffer); - } }