[resumes][feat] revert to storing fileURL instead

pull/350/head
Keane Chan 2 years ago
parent 5d0c95e085
commit 057f915b29
No known key found for this signature in database
GPG Key ID: 32718398E1E9F87C

@ -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<File>();
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 (
<div>
<Document
className="flex h-[calc(100vh-17rem)] flex-row justify-center overflow-scroll"
file={file}
file={url}
loading={<Spinner display="block" label="" size="lg" />}
noData=""
onLoadSuccess={onPdfLoadSuccess}>

@ -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);
}
}

Loading…
Cancel
Save