[resumes][feat] upload pdf file into file storage (#321)
* [resumes][feat] upload pdf file into file storage * [resumes][fix] fix file upload failure * [resumes][chore] update .env.local.example * [resumes][fix] process file transfer over next.js * [resumes][feat] file upload * [resumes][chore] cleanup * [resumes][feat] add GET method for file-storage API * [portal[chore] Update env.example file * [resumes][chore] cleanup * [portal][chore] update yarn lock filepull/333/head
parent
fbf1517901
commit
384981716d
@ -0,0 +1 @@
|
||||
export const RESUME_STORAGE_KEY = 'resumes';
|
@ -0,0 +1,65 @@
|
||||
import formidable from 'formidable';
|
||||
import * as fs from 'fs';
|
||||
import type { NextApiRequest, NextApiResponse } from 'next';
|
||||
|
||||
import { supabase } from '~/utils/supabase';
|
||||
|
||||
export const config = {
|
||||
api: {
|
||||
bodyParser: false,
|
||||
},
|
||||
};
|
||||
|
||||
export default async function handler(
|
||||
req: NextApiRequest,
|
||||
res: NextApiResponse,
|
||||
) {
|
||||
if (req.method === 'POST') {
|
||||
try {
|
||||
const form = formidable({ keepExtensions: true });
|
||||
form.parse(req, async (err, fields, files) => {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
|
||||
const { key } = fields;
|
||||
const { file } = files;
|
||||
|
||||
const parsedFile: formidable.File =
|
||||
file instanceof Array ? file[0] : file;
|
||||
const filePath = `${Date.now()}-${parsedFile.originalFilename}`;
|
||||
const convertedFile = fs.readFileSync(parsedFile.filepath);
|
||||
|
||||
const { error } = await supabase.storage
|
||||
.from(key as string)
|
||||
.upload(filePath, convertedFile);
|
||||
|
||||
if (error) {
|
||||
throw error;
|
||||
}
|
||||
|
||||
return res.status(200).json({
|
||||
url: 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);
|
||||
}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
import { createClient } from '@supabase/supabase-js';
|
||||
|
||||
import { env } from '~/env/server.mjs';
|
||||
|
||||
const { SUPABASE_URL, SUPABASE_ANON_KEY } = env;
|
||||
|
||||
// Create a single supabase client for interacting with the file storage
|
||||
export const supabase = createClient(SUPABASE_URL, SUPABASE_ANON_KEY);
|
Loading…
Reference in new issue