-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
36 additions
and
87 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,93 +1,42 @@ | ||
const fs = require('fs') | ||
const http = require('http') | ||
const path = require('path') | ||
const multer = require('multer') | ||
|
||
const { prettifySVG, savePrettifiedSVG } = require('../src/utils/svg-prettifier/prettifier.cjs') // Ensure correct path | ||
|
||
const port = 8000 | ||
|
||
// Use memory storage for uploaded files | ||
const storage = multer.memoryStorage() | ||
const upload = multer({ storage: storage }) | ||
|
||
// Custom filename sanitizer | ||
function sanitizeFileName(filename) { | ||
return filename.replace(/[^a-z0-9\.\-_]/gi, '_') // Replace any non-alphanumeric characters (except . - _) with an underscore | ||
const { JSDOM } = require('jsdom'); | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
|
||
function prettifySVG(content) { | ||
const { window } = new JSDOM(content); | ||
const svgElement = window.document.querySelector('svg'); | ||
|
||
if (svgElement !== null) { | ||
svgElement.removeAttribute('fill'); | ||
svgElement.querySelectorAll('path').forEach(pathElement => { | ||
pathElement.removeAttribute('fill'); | ||
}); | ||
|
||
svgElement.setAttribute('fill', 'currentColor'); | ||
svgElement.removeAttribute('height'); | ||
svgElement.removeAttribute('width'); | ||
|
||
const viewBoxValue = svgElement.getAttribute('viewBox'); | ||
if (viewBoxValue !== '0 0 20 20') { | ||
throw new Error('Error: The SVG must have a viewBox of 0 0 20 20 for the prettifier to process it correctly.'); | ||
} | ||
|
||
return svgElement.outerHTML; | ||
} else { | ||
throw new Error('Invalid SVG content'); | ||
} | ||
} | ||
|
||
const server = http.createServer((req, res) => { | ||
// Add CORS headers | ||
res.setHeader('Access-Control-Allow-Origin', '*') | ||
res.setHeader('Access-Control-Allow-Methods', 'POST') | ||
res.setHeader('Access-Control-Allow-Headers', 'Content-Type') | ||
function savePrettifiedSVG(filePath, content) { | ||
const fileName = path.basename(filePath, path.extname(filePath)); | ||
const targetDirectory = path.join(__dirname, '../../assets/svg'); | ||
|
||
if (req.method === 'OPTIONS') { | ||
// Handle preflight requests | ||
res.writeHead(204) | ||
res.end() | ||
return | ||
if (!fs.existsSync(targetDirectory)) { | ||
fs.mkdirSync(targetDirectory, { recursive: true }); | ||
} | ||
|
||
if (req.method === 'POST' && req.url === '/api') { | ||
upload.array('files')(req, res, err => { | ||
if (err) { | ||
console.error('Error uploading file:', err) | ||
res.writeHead(500, { 'Content-Type': 'text/plain' }) | ||
res.end('Error uploading file') | ||
return | ||
} | ||
|
||
// Accumulate success or error messages | ||
const results = [] | ||
let hasErrors = false // Flag to check if any file failed | ||
|
||
// Process each uploaded file | ||
req.files.forEach(file => { | ||
try { | ||
const safeFileName = sanitizeFileName(file.originalname) // Use custom sanitizer | ||
const content = file.buffer.toString('utf8') | ||
const prettifiedSVG = prettifySVG(content) | ||
|
||
// Destination path for the prettified SVG | ||
const filePath = path.join(__dirname, '../src/assets/svg/', safeFileName) | ||
|
||
// Check if the file already exists | ||
if (fs.existsSync(filePath)) { | ||
results.push({ file: file.originalname, status: 'error', message: 'File already exists' }) | ||
hasErrors = true // Mark that there is an error | ||
return | ||
} | ||
|
||
// Save the prettified SVG | ||
savePrettifiedSVG(filePath, prettifiedSVG) | ||
|
||
// Add success message | ||
results.push({ file: file.originalname, status: 'success' }) | ||
} catch (error) { | ||
console.error(`Error prettifying file ${file.originalname}:`, error) | ||
hasErrors = true // Mark that there is an error | ||
|
||
// Add error message | ||
results.push({ file: file.originalname, status: 'error', message: error.message }) | ||
} | ||
}) | ||
|
||
// If there are errors, return a 400 Bad Request status | ||
if (hasErrors) { | ||
res.writeHead(400, { 'Content-Type': 'application/json' }) | ||
} else { | ||
res.writeHead(200, { 'Content-Type': 'application/json' }) | ||
} | ||
|
||
res.end(JSON.stringify(results)) // Send results as JSON | ||
}) | ||
} else { | ||
res.writeHead(404, { 'Content-Type': 'text/plain' }) | ||
res.end('Not found') | ||
} | ||
}) | ||
// Save the prettified SVG without sanitization | ||
fs.writeFileSync(path.join(targetDirectory, `${fileName}.svg`), content); | ||
} | ||
|
||
server.listen(port, () => { | ||
console.log(`Server is listening on port ${port}`) | ||
}) | ||
module.exports = { prettifySVG, savePrettifiedSVG }; |