56 lines
1.4 KiB
JavaScript
56 lines
1.4 KiB
JavaScript
import {Router} from 'express'
|
|
import path from 'path'
|
|
import cors from 'cors'
|
|
import Fontmin from 'fontmin'
|
|
import validPerm from '../../utils/valid-perm'
|
|
const router = new Router()
|
|
|
|
const whitelist = ['https://servicewechat.com']
|
|
const corsOptions = {
|
|
origin: function(origin, callback) {
|
|
if (whitelist.indexOf(origin) !== -1) {
|
|
callback(null, true)
|
|
} else {
|
|
callback(new Error('Not allowed by CORS'))
|
|
}
|
|
},
|
|
}
|
|
|
|
router.get('/get_font', cors(), async (req, res, next) => {
|
|
const query = req.query
|
|
const font_name = query.font_name
|
|
const ext = query.ext
|
|
const fontPath = path.join(__dirname, `../../fonts/${font_name}.${ext}`)
|
|
|
|
res.download(fontPath)
|
|
})
|
|
|
|
router.get('/fontmin', cors(),async (req, res, next) => {
|
|
const query = req.query
|
|
const {text, font_name, ext} = query
|
|
console.log(text)
|
|
const fontPath = path.join(__dirname, `../../fonts/${font_name}.${ext}`)
|
|
const fontmin = new Fontmin().src(fontPath).use(
|
|
Fontmin.glyph({
|
|
text: text,
|
|
hinting: false,
|
|
})
|
|
)
|
|
fontmin.run(function(err, files) {
|
|
if (err) {
|
|
console.log(err)
|
|
next(err)
|
|
return
|
|
}
|
|
const data = files[0].contents
|
|
res.writeHead(200, {
|
|
'Content-Type': 'application/vnd.ms-fontobject',
|
|
'Content-disposition': `attachment;filename=${font_name}.${ext}`,
|
|
'Content-Length': data.length,
|
|
})
|
|
res.end(data)
|
|
})
|
|
})
|
|
|
|
export default router
|