Skip to content

Commit

Permalink
fix: non-english characters encoding (#471)
Browse files Browse the repository at this point in the history
  • Loading branch information
diced committed Oct 8, 2023
1 parent d112c3a commit 40fb112
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/components/pages/Upload/File.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ export default function File({ chunks: chunks_config }) {
if (chunks_config.enabled && file.size >= chunks_config.max_size) {
toChunkFiles.push(file);
} else {
body.append('file', files[i]);
body.append('file', files[i], encodeURIComponent(files[i].name));
}
}

Expand Down
11 changes: 7 additions & 4 deletions src/pages/api/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,14 +199,17 @@ async function handler(req: NextApiReq, res: NextApiRes) {

for (let i = 0; i !== req.files.length; ++i) {
const file = req.files[i];

if (file.size > zconfig.uploader[user.administrator ? 'admin_limit' : 'user_limit'])
return res.badRequest(`file[${i}]: size too big`);
if (!file.originalname) return res.badRequest(`file[${i}]: no filename`);

const ext = file.originalname.split('.').length === 1 ? '' : file.originalname.split('.').pop();
const decodedName = decodeURI(file.originalname);

const ext = decodedName.split('.').length === 1 ? '' : decodedName.split('.').pop();
if (zconfig.uploader.disabled_extensions.includes(ext))
return res.badRequest(`file[${i}]: disabled extension recieved: ${ext}`);
let fileName = await formatFileName(format, file.originalname);
let fileName = await formatFileName(format, decodedName);

if (req.headers['x-zipline-filename']) {
fileName = req.headers['x-zipline-filename'] as string;
Expand All @@ -226,7 +229,7 @@ async function handler(req: NextApiReq, res: NextApiRes) {
let mimetype = file.mimetype;

if (file.mimetype === 'application/octet-stream' && zconfig.uploader.assume_mimetypes) {
const ext = parse(file.originalname).ext.replace('.', '');
const ext = parse(decodedName).ext.replace('.', '');
const mime = await guess(ext);

if (!mime) response.assumed_mimetype = false;
Expand All @@ -247,7 +250,7 @@ async function handler(req: NextApiReq, res: NextApiRes) {
password,
expiresAt: expiry,
maxViews: fileMaxViews,
originalName: req.headers['original-name'] ? file.originalname ?? null : null,
originalName: req.headers['original-name'] ? decodedName ?? null : null,
size: file.size,
},
});
Expand Down
2 changes: 1 addition & 1 deletion src/server/decorators/dbFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function dbFileDecorator(fastify: FastifyInstance, _, done) {

this.header('Content-Length', size);
this.header('Content-Type', download ? 'application/octet-stream' : file.mimetype);
this.header('Content-Disposition', `inline; filename="${file.originalName || file.name}"`);
this.header('Content-Disposition', `inline; filename="${encodeURI(file.originalName || file.name)}"`);

return this.send(data);
}
Expand Down
2 changes: 1 addition & 1 deletion src/server/routes/uploads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default async function uploadsRoute(this: FastifyInstance, req: FastifyRe

const image = await this.prisma.file.findFirst({
where: {
OR: [{ name: id }, { invisible: { invis: decodeURI(encodeURI(id)) } }],
OR: [{ name: id }, { name: decodeURI(id) }, { invisible: { invis: decodeURI(encodeURI(id)) } }],
},
});
if (!image) return reply.rawFile(id);
Expand Down

0 comments on commit 40fb112

Please sign in to comment.