Saving, technically done
This commit is contained in:
@@ -2,12 +2,15 @@ import React from 'react';
|
||||
import { AppProps } from 'next/app';
|
||||
import './globals.scss';
|
||||
import { LanguageProvider } from '@/providers/LanguageProvider';
|
||||
import { APIProvider } from '@/providers/APIProvider';
|
||||
|
||||
const RootLayout:React.FC<AppProps> = ({ Component, pageProps }) => {
|
||||
return (
|
||||
<>
|
||||
<LanguageProvider>
|
||||
<Component {...pageProps} />
|
||||
<APIProvider>
|
||||
<Component {...pageProps} />
|
||||
</APIProvider>
|
||||
</LanguageProvider>
|
||||
</>
|
||||
);
|
||||
|
||||
@@ -4,13 +4,41 @@ import * as path from 'path';
|
||||
|
||||
const PATH_SAVES = path.resolve('.', 'data', 'saves');
|
||||
|
||||
const handler:NextApiHandler = async (req, res) => {
|
||||
if (req.method !== 'GET') {
|
||||
res.setHeader('Allow', ['GET']);
|
||||
res.status(405).end(`Method Not Allowed`);
|
||||
const handlerPut:NextApiHandler = async (req, res) => {
|
||||
if(!req.body || typeof req.body !== 'object') {
|
||||
res.status(400).end(`Bad Request`);
|
||||
return;
|
||||
}
|
||||
|
||||
if(!req.body.gameId || typeof req.body.gameId !== 'string') {
|
||||
res.status(400).end(`Bad Request`);
|
||||
}
|
||||
|
||||
if(!req.body.data || typeof req.body.data !== 'string') {
|
||||
res.status(400).end(`Bad Request`);
|
||||
}
|
||||
|
||||
// Check the length of the data.
|
||||
if(req.body.data.length > 1024 * 1024) {// 1MB
|
||||
res.status(413).end(`Payload Too Large`);
|
||||
return;
|
||||
}
|
||||
|
||||
// Data is a base64 string, convert it to a binary zip file.
|
||||
const pathSave = path.resolve(PATH_SAVES, `${req.body.gameId}.zip`);
|
||||
try {
|
||||
const data = Buffer.from(req.body.data, 'base64');
|
||||
fs.promises.writeFile(pathSave, data);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
res.status(500).end(`Internal Server Error`);
|
||||
return;
|
||||
}
|
||||
|
||||
res.status(200).end(`true`);
|
||||
}
|
||||
|
||||
const handlerGet:NextApiHandler = async (req, res) => {
|
||||
// ID query param
|
||||
if(!req.query.id || typeof req.query.id !== 'string') {
|
||||
res.status(404).end(`Not Found`);
|
||||
@@ -39,6 +67,20 @@ const handler:NextApiHandler = async (req, res) => {
|
||||
} catch (error) {
|
||||
res.status(500).end(`Internal Server Error`);
|
||||
}
|
||||
}
|
||||
|
||||
const handler:NextApiHandler = async (req, res) => {
|
||||
switch(req.method) {
|
||||
case 'POST':
|
||||
case 'PUT':
|
||||
return handlerPut(req, res);
|
||||
|
||||
case 'GET':
|
||||
return handlerGet(req, res);
|
||||
|
||||
default:
|
||||
res.status(405).end(`Method Not Allowed`);
|
||||
}
|
||||
};
|
||||
|
||||
export default handler;
|
||||
Reference in New Issue
Block a user