mirror of
https://github.com/yangshun/tech-interview-handbook.git
synced 2025-07-30 05:34:33 +08:00
[resumes][feat] revert to storing fileURL instead
This commit is contained in:
@ -1,12 +1,9 @@
|
|||||||
import axios from 'axios';
|
import { useState } from 'react';
|
||||||
import { useEffect, useState } from 'react';
|
|
||||||
import { Document, Page, pdfjs } from 'react-pdf';
|
import { Document, Page, pdfjs } from 'react-pdf';
|
||||||
import type { PDFDocumentProxy } from 'react-pdf/node_modules/pdfjs-dist';
|
import type { PDFDocumentProxy } from 'react-pdf/node_modules/pdfjs-dist';
|
||||||
import { ArrowLeftIcon, ArrowRightIcon } from '@heroicons/react/20/solid';
|
import { ArrowLeftIcon, ArrowRightIcon } from '@heroicons/react/20/solid';
|
||||||
import { Button, Spinner } from '@tih/ui';
|
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`;
|
pdfjs.GlobalWorkerOptions.workerSrc = `//unpkg.com/pdfjs-dist@${pdfjs.version}/build/pdf.worker.min.js`;
|
||||||
|
|
||||||
type Props = Readonly<{
|
type Props = Readonly<{
|
||||||
@ -16,30 +13,16 @@ type Props = Readonly<{
|
|||||||
export default function ResumePdf({ url }: Props) {
|
export default function ResumePdf({ url }: Props) {
|
||||||
const [numPages, setNumPages] = useState(0);
|
const [numPages, setNumPages] = useState(0);
|
||||||
const [pageNumber, setPageNumber] = useState(1);
|
const [pageNumber, setPageNumber] = useState(1);
|
||||||
const [file, setFile] = useState<File>();
|
|
||||||
|
|
||||||
const onPdfLoadSuccess = (pdf: PDFDocumentProxy) => {
|
const onPdfLoadSuccess = (pdf: PDFDocumentProxy) => {
|
||||||
setNumPages(pdf.numPages);
|
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 (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<Document
|
<Document
|
||||||
className="flex h-[calc(100vh-17rem)] flex-row justify-center overflow-scroll"
|
className="flex h-[calc(100vh-17rem)] flex-row justify-center overflow-scroll"
|
||||||
file={file}
|
file={url}
|
||||||
loading={<Spinner display="block" label="" size="lg" />}
|
loading={<Spinner display="block" label="" size="lg" />}
|
||||||
noData=""
|
noData=""
|
||||||
onLoadSuccess={onPdfLoadSuccess}>
|
onLoadSuccess={onPdfLoadSuccess}>
|
||||||
|
@ -2,6 +2,7 @@ import formidable from 'formidable';
|
|||||||
import * as fs from 'fs';
|
import * as fs from 'fs';
|
||||||
import type { NextApiRequest, NextApiResponse } from 'next';
|
import type { NextApiRequest, NextApiResponse } from 'next';
|
||||||
|
|
||||||
|
import { env } from '~/env/server.mjs';
|
||||||
import { supabase } from '~/utils/supabase';
|
import { supabase } from '~/utils/supabase';
|
||||||
|
|
||||||
export const config = {
|
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(
|
export default async function handler(
|
||||||
req: NextApiRequest,
|
req: NextApiRequest,
|
||||||
res: NextApiResponse,
|
res: NextApiResponse,
|
||||||
@ -38,28 +41,12 @@ export default async function handler(
|
|||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
|
|
||||||
return res.status(200).json({
|
return res.status(201).json({
|
||||||
url: filePath,
|
url: `${BASE_FILE_URL}/${key}/${filePath}`,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
} catch (error: unknown) {
|
} catch (error: unknown) {
|
||||||
return Promise.reject(error);
|
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user