From 5c596cc7c25a6536d873d79ca35be6643c0d651f Mon Sep 17 00:00:00 2001 From: Dobromir Popov Date: Tue, 7 May 2024 21:37:13 +0300 Subject: [PATCH] fix uploads encoding (cyrilic) --- pages/api/content/[subfolder].ts | 13 +++++--- pages/permits.tsx | 55 ++++++++++++++++++++------------ src/axiosSecure.js | 7 ++-- 3 files changed, 48 insertions(+), 27 deletions(-) diff --git a/pages/api/content/[subfolder].ts b/pages/api/content/[subfolder].ts index 780be31..f15ac0b 100644 --- a/pages/api/content/[subfolder].ts +++ b/pages/api/content/[subfolder].ts @@ -9,12 +9,17 @@ import { createRouter } from 'next-connect'; // Generalized Multer configuration const storage = multer.diskStorage({ destination: (req, file, cb) => { - const uploadPath = path.join(process.cwd(), 'public/content', req.query.subfolder as string); - fs.mkdir(uploadPath, { recursive: true }).then(() => cb(null, uploadPath)).catch(cb); + const subfolder = req.query.subfolder ? decodeURIComponent(req.query.subfolder as string) : 'default'; + const uploadPath = path.join(process.cwd(), 'public/content', subfolder); + + fs.mkdir(uploadPath, { recursive: true }) + .then(() => cb(null, uploadPath)) + .catch(cb); }, filename: (req, file, cb) => { - const prefix = req.body.prefix || path.parse(file.originalname).name; - cb(null, `${prefix}${path.extname(file.originalname)}`); + const filename = decodeURIComponent(file.originalname); // Ensure the filename is correctly decoded + const prefix = req.body.prefix ? decodeURIComponent(req.body.prefix) : path.parse(filename).name; + cb(null, `${prefix}${path.extname(filename)}`); } }); diff --git a/pages/permits.tsx b/pages/permits.tsx index 5f28dc3..07bdf33 100644 --- a/pages/permits.tsx +++ b/pages/permits.tsx @@ -23,17 +23,23 @@ const PDFViewerPage = ({ pdfFiles }) => { const handleFileUpload = async (event) => { const file = event.target.files[0]; + //utf-8 encoding + // const formData = new FormData(); const formData = new FormData(); - formData.append('file', file); + // formData.append('file', file); + const newFile = new File([file], encodeURI(file.name), { type: file.type }); + formData.append('file', newFile); const subfolder = 'permits'; // Change this as needed based on your subfolder structure try { const response = await axiosInstance.post(`/api/content/${subfolder}`, formData, { headers: { - 'Content-Type': 'multipart/form-data' + 'Content-Type': 'multipart/form-data', + // 'Content-Encoding': 'utf-8' } }); - setFiles([...files, response.data]); + const newFiles = response.data.files.map(file => ({ name: decodeURIComponent(file.originalname), url: file.path })); + setFiles([...files, ...newFiles]); } catch (error) { console.error('Error uploading file:', error); } @@ -44,25 +50,35 @@ const PDFViewerPage = ({ pdfFiles }) => {

Разрешителни

-
-
- За да качите файл кликнете на бутона по-долу и изберете файл от вашия компютър. +
+
+

За да качите файл кликнете на бутона по-долу и изберете файл от вашия компютър.

+
- -
- Съществуващи файлове: +
+

Съществуващи файлове:

+ {files.length > 0 ? ( + files.map((file, index) => ( +
+ + {file.name} + + +
+ )) + ) : ( +

Няма качени файлове.

+ )}
- {files.map((file, index) => ( -
- - {file.name} - - -
- ))}
+
{/* Adjust the 100px based on your header/footer size */} @@ -111,5 +127,4 @@ export const getServerSideProps = async (context) => { } }; } -// export const getServerSideProps = async (context) => { diff --git a/src/axiosSecure.js b/src/axiosSecure.js index e79adb0..75bcabc 100644 --- a/src/axiosSecure.js +++ b/src/axiosSecure.js @@ -5,9 +5,10 @@ import { applyAuthTokenInterceptor } from 'axios-jwt'; const axiosInstance = axios.create({ baseURL: common.getBaseUrl(), withCredentials: true, - // headers: { - // "Content-Type": "application/json", - // }, + headers: { + // "Content-Type": "application/json", + "Content-Encoding": "utf-8" + }, }); // 2. Define token refresh function.