From b2b528fc074e558e2e198b703a8c6a25b2002a1c Mon Sep 17 00:00:00 2001 From: yoosangwook Date: Wed, 4 Sep 2024 08:58:36 +0900 Subject: [PATCH] feat: Add CAD file conversion functionality to Playground component --- src/components/Playground.jsx | 20 +++++++++++++++++++- src/lib/cadAction.js | 13 +++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 src/lib/cadAction.js diff --git a/src/components/Playground.jsx b/src/components/Playground.jsx index d7b06e58..4d8007b9 100644 --- a/src/components/Playground.jsx +++ b/src/components/Playground.jsx @@ -6,6 +6,7 @@ import ColorPicker from './common/color-picker/ColorPicker' import { useAxios } from '@/hooks/useAxios' import { useMessage } from '@/hooks/useMessage' +import { convertDwgToPng } from '@/lib/cadAction' // import { get } from '@/lib/Axios' import QSelect from '@/components/ui/QSelect' @@ -13,7 +14,8 @@ import QSelect from '@/components/ui/QSelect' import styles from './playground.module.css' export default function Playground() { - const { get } = useAxios() + const fileRef = useRef(null) + const { get, post } = useAxios() const testVar = process.env.NEXT_PUBLIC_TEST const { getMessage } = useMessage() @@ -28,6 +30,16 @@ export default function Playground() { console.log('users', users) } + const handleConvert = async () => { + console.log('file', fileRef.current.files[0]) + + const formData = new FormData() + formData.append('file', fileRef.current.files[0]) + + const result = await post({ url: process.env.CONVERTER_API_URL, data: formData }) + await convertDwgToPng(result.Files[0].FileName, result.Files[0].FileData) + } + const data = [ { id: 1, @@ -70,6 +82,12 @@ export default function Playground() {
{color}
+
+ +
+ +
+
) diff --git a/src/lib/cadAction.js b/src/lib/cadAction.js new file mode 100644 index 00000000..be69fecb --- /dev/null +++ b/src/lib/cadAction.js @@ -0,0 +1,13 @@ +const convertDwgToPng = async (fileName, data) => { + console.log('fileName', fileName) + const imagePath = 'public/cad-images' + try { + await fs.readdir(imagePath) + } catch { + await fs.mkdir(imagePath) + } + + return fs.writeFile(`${imagePath}/${fileName}`, data, 'base64') +} + +export { convertDwgToPng }