diff options
| author | Jad <[email protected]> | 2020-08-12 10:03:35 +0000 |
|---|---|---|
| committer | Jad <[email protected]> | 2020-08-12 10:03:35 +0000 |
| commit | 3bd1ae58e4e9019cc0bab7c9221da73f02d914c7 (patch) | |
| tree | d824e32c08eec7ede53aec0340d2f2dfe74949db /utils | |
| parent | feat: add message board (diff) | |
| download | counter-3bd1ae58e4e9019cc0bab7c9221da73f02d914c7.tar.xz counter-3bd1ae58e4e9019cc0bab7c9221da73f02d914c7.zip | |
feat: support mongoDB
feat: support dynamic image size
docs: add Readme.md
Diffstat (limited to 'utils')
| -rw-r--r-- | utils/db.js | 60 | ||||
| -rw-r--r-- | utils/themify.js | 43 |
2 files changed, 38 insertions, 65 deletions
diff --git a/utils/db.js b/utils/db.js deleted file mode 100644 index 0912101..0000000 --- a/utils/db.js +++ /dev/null @@ -1,60 +0,0 @@ -'use strict' - -const path = require('path') -const sqlite3 = require('sqlite3') - -const db = new sqlite3.Database(path.resolve(__dirname, '../count.db')) - -db.run(`CREATE TABLE IF NOT EXISTS tb_count ( - id INTEGER PRIMARY KEY AUTOINCREMENT - NOT NULL - UNIQUE, - name VARCHAR (32) NOT NULL - UNIQUE, - num BIGINT NOT NULL - DEFAULT (0) -);`) - -function getNum(name) { - return new Promise((resolve, reject) => { - db.get('SELECT `name`, `num` from tb_count WHERE `name` = ?', name, (err, row) => { - if (err) reject(err) - - resolve(row || { name, num: 0 }) - }) - }) -} - -function getAll(name) { - return new Promise((resolve, reject) => { - db.get('SELECT * from tb_count', (err, row) => { - if (err) reject(err) - - resolve(row) - }) - }) -} - -function setNum(name, num) { - return new Promise((resolve, reject) => { - db.run(`INSERT INTO tb_count(\`name\`, \`num\`) - VALUES($name, $num) - ON CONFLICT(name) DO - UPDATE SET \`num\` = $num;` - , { - $name: name, - $num: num - } - , (err, row) => { - if (err) reject(err) - - resolve(row) - }) - }) -} - -module.exports = { - getNum, - getAll, - setNum -}
\ No newline at end of file diff --git a/utils/themify.js b/utils/themify.js index c440cd8..2db9886 100644 --- a/utils/themify.js +++ b/utils/themify.js @@ -3,6 +3,7 @@ const fs = require('fs') const path = require('path') const mimeType = require('mime-types') +const sizeOf = require('image-size') const themePath = path.resolve(__dirname, '../assets/theme') @@ -12,8 +13,15 @@ fs.readdirSync(themePath).forEach(theme => { if(!(theme in themeList)) themeList[theme] = {} const imgList = fs.readdirSync(path.resolve(themePath, theme)) imgList.forEach(img => { + const imgPath = path.resolve(themePath, theme, img) const name = path.parse(img).name - themeList[theme][name] = convertToDatauri(path.resolve(themePath, theme, img)) + const { width, height } = sizeOf(imgPath) + + themeList[theme][name] = { + width, + height, + data: convertToDatauri(imgPath) + } }) }) @@ -24,11 +32,36 @@ function convertToDatauri(path){ return `data:${mime};base64,${base64}` } -function wrap(num, theme='konachan'){ - if(!(theme in themeList)) theme = 'konachan' - return themeList[theme][num] +function getCountImage({ count, theme='moebooru', length=7 }) { + if(!(theme in themeList)) theme = 'moebooru' + + // This is not the greatest way for generating an SVG but it'll do for now + const countArray = count.toString().padStart(length, '0').split('') + + let x = 0, y = 0 + const parts = countArray.reduce((acc, next, index) => { + const { width, height, data } = themeList[theme][next] + + const image = `${acc} + <image x="${x}" y="0" width="${width}" height="${height}" xlink:href="${data}" />` + + x += width + + if(height > y) y = height + + return image + }, '') + + return `<?xml version="1.0" encoding="UTF-8"?> +<svg width="${x}" height="${y}" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"> + <title>Moe Count</title> + <g> + ${parts} + </g> +</svg> +` } module.exports = { - wrap + getCountImage }
\ No newline at end of file |