fixed book in chinese title failed downloading in kobo issue
This commit is contained in:
29
index.js
29
index.js
@@ -10,6 +10,7 @@ const fs = require('fs')
|
||||
const { spawn } = require('child_process')
|
||||
const { extname, basename, dirname } = require('path')
|
||||
const FileType = require('file-type')
|
||||
const ToPinyin = require("chinese-to-pinyin");
|
||||
|
||||
const port = 3001
|
||||
const expireDelay = 30 // 30 seconds
|
||||
@@ -25,6 +26,29 @@ const allowedExtensions = ['epub', 'mobi', 'pdf', 'cbz', 'cbr', 'html', 'txt']
|
||||
const keyChars = "3469ACEGHLMNPRTY"
|
||||
const keyLength = 4
|
||||
|
||||
function capitalizeFirstLetter(string) {
|
||||
return string.charAt(0).toUpperCase() + string.slice(1);
|
||||
}
|
||||
|
||||
function romanizeChinese(filename) {
|
||||
const splitedFilename = filename.split(".");
|
||||
const name = splitedFilename[0];
|
||||
const ext = splitedFilename.pop();
|
||||
|
||||
let pinyinNames = ToPinyin(name, {
|
||||
KeepRest: true,
|
||||
removeTone: true,
|
||||
}).split(' ');
|
||||
|
||||
pinyinNames = pinyinNames.map(n => capitalizeFirstLetter(n));
|
||||
let pinyinName = pinyinNames.join("").replace(/\W/g, ""); //remove punctuation
|
||||
if (pinyinName.length > 40) {
|
||||
pinyinName = pinyinName.substring(0, 40);
|
||||
}
|
||||
|
||||
return pinyinName+'.'+ext;
|
||||
}
|
||||
|
||||
function randomKey () {
|
||||
const choices = Math.pow(keyChars.length, keyLength)
|
||||
const rnd = Math.floor(Math.random() * choices)
|
||||
@@ -160,6 +184,9 @@ router.post('/upload', upload.single('file'), async ctx => {
|
||||
const key = ctx.request.body.key.toUpperCase()
|
||||
|
||||
if (ctx.request.file) {
|
||||
ctx.request.file.originalname = romanizeChinese(
|
||||
ctx.request.file.originalname
|
||||
);
|
||||
console.log('Uploaded file:', ctx.request.file)
|
||||
}
|
||||
|
||||
@@ -355,4 +382,4 @@ fs.rm('uploads', {recursive: true}, (err) => {
|
||||
app.listen(port)
|
||||
console.log('server is listening on port ' + port)
|
||||
})
|
||||
})
|
||||
})
|
||||
11
package-lock.json
generated
11
package-lock.json
generated
@@ -11,6 +11,7 @@
|
||||
"dependencies": {
|
||||
"@koa/multer": "^3.0.0",
|
||||
"@koa/router": "^10.1.0",
|
||||
"chinese-to-pinyin": "^1.3.1",
|
||||
"file-type": "^16.5.3",
|
||||
"koa": "^2.13.1",
|
||||
"koa-logger": "^3.2.1",
|
||||
@@ -133,6 +134,11 @@
|
||||
"node": ">=4"
|
||||
}
|
||||
},
|
||||
"node_modules/chinese-to-pinyin": {
|
||||
"version": "1.3.1",
|
||||
"resolved": "https://registry.npmjs.org/chinese-to-pinyin/-/chinese-to-pinyin-1.3.1.tgz",
|
||||
"integrity": "sha512-QGB9HmbMLh726Aj3QKB5NcjKDgKn5NCLMz8rmPfKuU5FgwmF4NT2ULh5/2G6Z0pWBcagtKBS08ymaJcYSvbbQg=="
|
||||
},
|
||||
"node_modules/co": {
|
||||
"version": "4.6.0",
|
||||
"resolved": "https://registry.npmjs.org/co/-/co-4.6.0.tgz",
|
||||
@@ -1037,6 +1043,11 @@
|
||||
"supports-color": "^5.3.0"
|
||||
}
|
||||
},
|
||||
"chinese-to-pinyin": {
|
||||
"version": "1.3.1",
|
||||
"resolved": "https://registry.npmjs.org/chinese-to-pinyin/-/chinese-to-pinyin-1.3.1.tgz",
|
||||
"integrity": "sha512-QGB9HmbMLh726Aj3QKB5NcjKDgKn5NCLMz8rmPfKuU5FgwmF4NT2ULh5/2G6Z0pWBcagtKBS08ymaJcYSvbbQg=="
|
||||
},
|
||||
"co": {
|
||||
"version": "4.6.0",
|
||||
"resolved": "https://registry.npmjs.org/co/-/co-4.6.0.tgz",
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
"dependencies": {
|
||||
"@koa/multer": "^3.0.0",
|
||||
"@koa/router": "^10.1.0",
|
||||
"chinese-to-pinyin": "^1.3.1",
|
||||
"file-type": "^16.5.3",
|
||||
"koa": "^2.13.1",
|
||||
"koa-logger": "^3.2.1",
|
||||
|
||||
Reference in New Issue
Block a user