You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
131 lines
3.5 KiB
131 lines
3.5 KiB
2 years ago
|
|
||
|
var express = require('express');
|
||
|
var router = express.Router();
|
||
|
const fontDb = require('../models/iconFontmodels');
|
||
|
const font = fontDb.font
|
||
|
const iconFont = fontDb.iconfont
|
||
|
const svgtofont = require('svgtofont');
|
||
|
const fs = require('fs');
|
||
|
const path = require('path');
|
||
|
const { where } = require('sequelize');
|
||
|
const { resolve } = require('path/posix');
|
||
|
router.post('/createIconFont', async (req, res) => {
|
||
|
|
||
|
svgFiles = []
|
||
|
let fontId = await font.findOne( {
|
||
|
|
||
|
where : {
|
||
|
name : req.body.fontName,
|
||
|
}
|
||
|
})
|
||
|
let svgList = await font.findAll({
|
||
|
include : [{model: iconFont , where : {
|
||
|
varient : "Bulks",
|
||
|
iconfontID :fontId.dataValues.id,
|
||
|
|
||
|
}}, ],
|
||
|
|
||
|
})
|
||
|
svgList = svgList[0].dataValues.iconfonts
|
||
|
res.send(svgList)
|
||
|
|
||
|
const cssFileGenerator = `
|
||
|
@font-face {
|
||
|
font-family: ${req.body.fontName};
|
||
|
src: url('${req.body.fontName}.eot'); /* IE9*/
|
||
|
src: url('${req.body.fontName}.eot') format('embedded-opentype'), /* IE6-IE8 */
|
||
|
url("${req.body.fontName}.woff2") format("woff2"),
|
||
|
url("${req.body.fontName}.woff") format("woff"),
|
||
|
url('${req.body.fontName}.ttf') format('truetype'), /* chrome, firefox, opera, Safari, Android, iOS 4.2+*/
|
||
|
url('${req.body.fontName}.svg') format('svg'); /* iOS 4.1- */
|
||
|
}
|
||
|
|
||
|
.characters {
|
||
|
font-family: '${req.body.fontName}' !important;
|
||
|
font-size:16px;
|
||
|
font-style:normal;
|
||
|
-webkit-font-smoothing: antialiased;
|
||
|
-moz-osx-font-smoothing: grayscale;
|
||
|
}
|
||
|
|
||
|
`
|
||
|
fs.unlink('iconFont/iconFontsOf'+req.body.fontName ,function(err){
|
||
|
if(err) return console.log(err);
|
||
|
console.log('file deleted successfully');
|
||
|
});
|
||
|
if (!fs.existsSync('iconFont/iconFontsOf'+req.body.fontName )){
|
||
|
fs.mkdirSync('iconFont/iconFontsOf'+req.body.fontName );
|
||
|
}
|
||
|
fs.appendFile('iconFont/iconFontsOf'+req.body.fontName + "/"+req.body.fontName+".css", cssFileGenerator, err => {
|
||
|
if (err) {
|
||
|
console.error(err);
|
||
|
}
|
||
|
// file written successfully
|
||
|
});
|
||
|
|
||
|
svgtofont({
|
||
|
src: path.resolve('svgs/'+req.body.fontName),
|
||
|
dist: path.resolve('iconFont/iconFontsOf'+req.body.fontName),
|
||
|
fontName: req.body.fontName,
|
||
|
|
||
|
// emptyDist: true,
|
||
|
useNameAsUnicode : true,
|
||
|
|
||
|
});
|
||
|
|
||
|
});
|
||
|
|
||
|
router.post('/addIcon', async (req, res) => {
|
||
|
let fontId = await font.findOne({
|
||
|
where : {
|
||
|
name : req.body.fontName,
|
||
|
}
|
||
|
})
|
||
|
|
||
|
const svgsFolder = "svgs/"+ req.body.fontName
|
||
|
async function nameGenerator(){
|
||
|
nameGeneratorCounter = 500
|
||
|
fs.readdirSync(svgsFolder).forEach(file => {
|
||
|
|
||
|
fs.rename("svgs/iconsax2/"+file ,"svgs/iconsax2/" +String.fromCharCode(parseInt(nameGeneratorCounter))+ ".svg",function(err) {
|
||
|
if ( err ) console.log('ERROR: ' + err);
|
||
|
|
||
|
} )
|
||
|
console.log(file)
|
||
|
nameGeneratorCounter+=1
|
||
|
});
|
||
|
}
|
||
|
|
||
|
await nameGenerator();
|
||
|
await iconFont.destroy({
|
||
|
|
||
|
where : { id : fontId.id }
|
||
|
})
|
||
|
|
||
|
|
||
|
fs.readdirSync(svgsFolder).forEach(file => {
|
||
|
const iconCreate = iconFont.create(
|
||
|
{
|
||
|
name : req.body.fontName,
|
||
|
iconfontID : fontId.id,
|
||
|
char : file.substring(0,1) ,
|
||
|
svgFile : svgsFolder + '/' + file,
|
||
|
varient : req.body.varient,
|
||
|
description : file,
|
||
|
}
|
||
|
)
|
||
|
}
|
||
|
)
|
||
|
res.send("svgs added successfully.")
|
||
|
});
|
||
|
router.post('/createfont', async (req, res) => {
|
||
|
|
||
|
font.create(
|
||
|
{
|
||
|
name : req.body.fontName,
|
||
|
})
|
||
|
const list = font.findAll()
|
||
|
res.send(list);
|
||
|
});
|
||
|
|
||
|
module.exports = router
|