-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow your own backgrounds to be uploaded
- Loading branch information
Showing
11 changed files
with
2,961 additions
and
1,743 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import { IncomingForm } from 'formidable'; | ||
import { readFile } from 'node:fs/promises'; | ||
import path from 'node:path'; | ||
import { ncWithSession } from '@/lib/routing'; | ||
import HTTP_CODE from '@/lib/constants/httpStatusCodes'; | ||
import { saveFile } from '@/lib/utils/fileUtils'; | ||
import { CACHE } from '@/lib/constants/filePaths'; | ||
import prisma from '@/lib/db'; | ||
import { makeBanner } from '@/lib/riitag/banner'; | ||
import logger from '@/lib/logger'; | ||
|
||
async function uploadBackground(request, response) { | ||
if (request.socket.bytesRead > 2_107_638) { | ||
return response | ||
.status(HTTP_CODE.REQUEST_ENTITY_TOO_LARGE) | ||
.send({ error: 'Request entity too large.' }); | ||
} | ||
const username = request.session?.username; | ||
|
||
if (!username) { | ||
return response | ||
.status(HTTP_CODE.UNAUTHORIZED) | ||
.json({ error: 'Unauthorized' }); | ||
} | ||
|
||
const data = await new Promise((resolve, reject) => { | ||
const form = new IncomingForm(); | ||
|
||
form.parse(request, (error, fields, files) => { | ||
if (error) { | ||
return reject(error); | ||
} | ||
return resolve({ fields, files }); | ||
}); | ||
}).catch((error) => { | ||
logger.error(error); | ||
return response | ||
.status(HTTP_CODE.BAD_REQUEST) | ||
.send({ error: 'Invalid data' }); | ||
}); | ||
|
||
const { file } = data.files; | ||
|
||
if (file.mimetype !== 'image/png') { | ||
return response | ||
.status(HTTP_CODE.BAD_REQUEST) | ||
.send({ error: 'Invalid data' }); | ||
} | ||
|
||
// Hard cap of 2MBs for custom backgrounds | ||
if (file.size > 2_000_000) { | ||
return response | ||
.status(HTTP_CODE.REQUEST_ENTITY_TOO_LARGE) | ||
.send({ error: 'Request entity too large.' }); | ||
} | ||
|
||
let user = await prisma.user.findFirst({ | ||
where: { | ||
username, | ||
}, | ||
select: { | ||
username: true, | ||
}, | ||
}); | ||
|
||
const filepath = path.resolve(CACHE.BACKGROUNDS, `${user.username}.png`); | ||
|
||
try { | ||
await saveFile(filepath, await readFile(file.filepath)); | ||
|
||
user = await prisma.user.update({ | ||
where: { | ||
username, | ||
}, | ||
data: { | ||
background: `${user.username}.png`, | ||
}, | ||
}); | ||
} catch (error) { | ||
logger.error(error); | ||
return response | ||
.status(HTTP_CODE.BAD_REQUEST) | ||
.send({ error: 'Invalid data' }); | ||
} | ||
|
||
makeBanner(user); | ||
|
||
return response.status(HTTP_CODE.OK).send(); | ||
} | ||
|
||
const handler = ncWithSession().post(uploadBackground); | ||
|
||
export const config = { | ||
api: { | ||
bodyParser: false, | ||
}, | ||
}; | ||
|
||
export default handler; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import fs from 'node:fs'; | ||
import path from 'node:path'; | ||
import { ncWithSession } from '@/lib/routing'; | ||
import HTTP_CODE from '@/lib/constants/httpStatusCodes'; | ||
import { setFileHeaders } from '@/lib/utils/utils'; | ||
import { CACHE } from '@/lib/constants/filePaths'; | ||
|
||
async function getMyUploadedBackground(request, response) { | ||
const username = request.session?.username; | ||
|
||
if (!username) { | ||
return response | ||
.status(HTTP_CODE.UNAUTHORIZED) | ||
.json({ error: 'Unauthorized' }); | ||
} | ||
|
||
response.setHeader('Content-Type', 'image/png'); | ||
setFileHeaders(response, `${username}.png`); | ||
return response | ||
.status(HTTP_CODE.OK) | ||
.send(await fs.promises.readFile(path.resolve(CACHE.BACKGROUNDS, username + ".png"))); | ||
} | ||
|
||
const handler = ncWithSession().get(getMyUploadedBackground); | ||
|
||
export default handler; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters