mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
完善设置
This commit is contained in:
1 parent
fc0d9873a2
commit
7edb29d7b8
43 files changed
+219
-184
No files matched your search
@@ -1,10 +1,6 @@
|
||||
const crypto = require('crypto');
|
||||
|
||||
module.exports = {
|
||||
_b64encode: (str) => Buffer.from(str).toString('base64'),
|
||||
_b64decode: (str) => Buffer.from(str, 'base64').toString(),
|
||||
_md5: (str) => crypto.createHash('md5').update(str).digest('hex'),
|
||||
_sha1: (str) => crypto.createHash('sha1').update(str).digest('hex'),
|
||||
global.Hydro.lib['hash.hydro'] = module.exports = {
|
||||
/**
|
||||
* @param {string} password
|
||||
* @param {string} salt
|
||||
@@ -21,7 +17,4 @@ module.exports = {
|
||||
const res = crypto.pbkdf2Sync(password, salt, 100000, 64, 'sha256');
|
||||
return res.toString('Hex');
|
||||
},
|
||||
salt() {
|
||||
return String.random();
|
||||
},
|
||||
};
|
||||
+40
-38
@@ -1,43 +1,45 @@
|
||||
const nodemailer = require('nodemailer');
|
||||
const { SendMailError } = require('../error');
|
||||
|
||||
module.exports = {
|
||||
/**
|
||||
* @param {string} to
|
||||
* @param {string} subject
|
||||
* @param {string} text
|
||||
* @param {string} html
|
||||
*/
|
||||
async sendMail(to, subject, text, html) {
|
||||
let t;
|
||||
try {
|
||||
const system = require('../model/system');
|
||||
const [host, port, secure, user, pass, from] = await Promise.all([
|
||||
system.get('smtp.host'),
|
||||
system.get('smtp.port'),
|
||||
system.get('smtp.secure'),
|
||||
system.get('smtp.user'),
|
||||
system.get('smtp.pass'),
|
||||
system.get('smtp.from'),
|
||||
]);
|
||||
const transporter = nodemailer.createTransport({
|
||||
host, port, secure, auth: { user, pass },
|
||||
/**
|
||||
* @param {string} to
|
||||
* @param {string} subject
|
||||
* @param {string} text
|
||||
* @param {string} html
|
||||
*/
|
||||
async function sendMail(to, subject, text, html) {
|
||||
let t;
|
||||
try {
|
||||
const { system } = global.Hydro.model;
|
||||
const [host, port, secure, user, pass, from] = await Promise.all([
|
||||
system.get('smtp.host'),
|
||||
system.get('smtp.port'),
|
||||
system.get('smtp.secure'),
|
||||
system.get('smtp.user'),
|
||||
system.get('smtp.pass'),
|
||||
system.get('smtp.from'),
|
||||
]);
|
||||
const transporter = nodemailer.createTransport({
|
||||
host, port, secure, auth: { user, pass },
|
||||
});
|
||||
t = await new Promise((resolve, reject) => {
|
||||
transporter.sendMail({
|
||||
from,
|
||||
to,
|
||||
subject,
|
||||
text,
|
||||
html,
|
||||
}, (err, info) => {
|
||||
if (err) reject(err);
|
||||
else resolve(info);
|
||||
});
|
||||
t = await new Promise((resolve, reject) => {
|
||||
transporter.sendMail({
|
||||
from,
|
||||
to,
|
||||
subject,
|
||||
text,
|
||||
html,
|
||||
}, (err, info) => {
|
||||
if (err) reject(err);
|
||||
else resolve(info);
|
||||
});
|
||||
});
|
||||
} catch (e) {
|
||||
throw new SendMailError(to);
|
||||
}
|
||||
return t;
|
||||
},
|
||||
});
|
||||
} catch (e) {
|
||||
throw new SendMailError(to);
|
||||
}
|
||||
return t;
|
||||
}
|
||||
|
||||
global.Hydro.lib.mail = module.exports = {
|
||||
sendMail,
|
||||
};
|
||||
@@ -21,7 +21,8 @@ class Markdown extends MarkdownIt {
|
||||
this.use(katex);
|
||||
}
|
||||
}
|
||||
module.exports = {
|
||||
|
||||
global.Hydro.lib.markdown = module.exports = {
|
||||
unsafe: new Markdown(false),
|
||||
safe: new Markdown(true),
|
||||
};
|
||||
+2
-1
@@ -126,4 +126,5 @@ function rstr2hex(input) {
|
||||
}
|
||||
return output;
|
||||
}
|
||||
module.exports = (s) => rstr2hex(rstrMD5(unescape(encodeURIComponent(s))));
|
||||
|
||||
global.Hydro.lib.md5 = module.exports = (s) => rstr2hex(rstrMD5(unescape(encodeURIComponent(s))));
|
||||
+19
-13
@@ -1,10 +1,10 @@
|
||||
const md5 = require('./md5');
|
||||
|
||||
exports.gravatar = function gravatar(email, size = 32) {
|
||||
return `//gravatar.loli.net/avatar/${md5((email || '').toString().trim().toLowerCase())}?d=mm&s=${size}`;
|
||||
};
|
||||
function gravatar(email, s = 32) {
|
||||
return `//gravatar.loli.net/avatar/${md5((email || '').toString().trim().toLowerCase())}?d=mm&s=${s}`;
|
||||
}
|
||||
|
||||
exports.datetime_span = function datetimeSpan(dt, relative = true, format = '%Y-%m-%d %H:%M:%S') {
|
||||
function datetimeSpan(dt, relative = true, format = '%Y-%m-%d %H:%M:%S') {
|
||||
if (!dt) return 'DATETIME_SPAN_ERROR';
|
||||
if (dt.generationTime) dt = new Date(dt.generationTime * 1000);
|
||||
else if (typeof dt === 'number' || typeof dt === 'string') dt = new Date(dt);
|
||||
@@ -13,9 +13,9 @@ exports.datetime_span = function datetimeSpan(dt, relative = true, format = '%Y-
|
||||
dt.getTime() / 1000,
|
||||
dt.format(format),
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
exports.paginate = function* paginate(page, numPages) {
|
||||
function* paginate(page, numPages) {
|
||||
const radius = 2; let first; let
|
||||
last;
|
||||
if (page > 1) {
|
||||
@@ -25,7 +25,9 @@ exports.paginate = function* paginate(page, numPages) {
|
||||
if (page <= radius) [first, last] = [1, Math.min(1 + radius * 2, numPages)];
|
||||
else if (page >= numPages - radius) {
|
||||
[first, last] = [Math.max(1, numPages - radius * 2), numPages];
|
||||
} else [first, last] = [page - radius, page + radius];
|
||||
} else {
|
||||
[first, last] = [page - radius, page + radius];
|
||||
}
|
||||
if (first > 1) yield ['ellipsis', 0];
|
||||
for (let page0 = first; page0 < last + 1; page0++) {
|
||||
if (page0 !== page) yield ['page', page0];
|
||||
@@ -34,15 +36,19 @@ exports.paginate = function* paginate(page, numPages) {
|
||||
if (last < numPages) yield ['ellipsis', 0];
|
||||
if (page < numPages) yield ['next', page + 1];
|
||||
yield ['last', numPages];
|
||||
};
|
||||
}
|
||||
|
||||
exports.size = function formatSize(size, base = 1) {
|
||||
size *= base;
|
||||
function size(s, base = 1) {
|
||||
s *= base;
|
||||
const unit = 1024;
|
||||
const unitNames = ['Bytes', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'];
|
||||
for (const unitName of unitNames) {
|
||||
if (size < unit) return '{0} {1}'.format(Math.round(size), unitName);
|
||||
size /= unit;
|
||||
if (s < unit) return '{0} {1}'.format(Math.round(s), unitName);
|
||||
s /= unit;
|
||||
}
|
||||
return '{0} {1}'.format(Math.round(size * unit), unitNames[unitNames.length - 1]);
|
||||
return '{0} {1}'.format(Math.round(s * unit), unitNames[unitNames.length - 1]);
|
||||
}
|
||||
|
||||
global.Hydro.lib.misc = module.exports = {
|
||||
gravatar, datetimeSpan, paginate, size,
|
||||
};
|
||||
@@ -1,6 +1,6 @@
|
||||
const { ValidationError } = require('../error');
|
||||
|
||||
module.exports = async function paginate(cursor, page, pageSize) {
|
||||
async function paginate(cursor, page, pageSize) {
|
||||
if (page <= 0) throw new ValidationError('page');
|
||||
const [count, pageDocs] = await Promise.all([
|
||||
cursor.count(),
|
||||
@@ -8,4 +8,6 @@ module.exports = async function paginate(cursor, page, pageSize) {
|
||||
]);
|
||||
const numPages = Math.floor((count + pageSize - 1) / pageSize);
|
||||
return [pageDocs, numPages, count];
|
||||
};
|
||||
}
|
||||
|
||||
global.Hydro.lib.paginate = module.exports = paginate;
|
||||
+22
-22
@@ -28,26 +28,26 @@ class Nunjucks extends nunjucks.Environment {
|
||||
}
|
||||
const env = new Nunjucks();
|
||||
|
||||
module.exports = {
|
||||
render(name, state) {
|
||||
Object.assign(state, {
|
||||
typeof: (o) => typeof o,
|
||||
static_url: (str) => `/${str}`,
|
||||
datetime_span: misc.datetime_span,
|
||||
paginate: misc.paginate,
|
||||
perm,
|
||||
status: builtin.status,
|
||||
size: misc.size,
|
||||
gravatar: misc.gravatar,
|
||||
model,
|
||||
Context: global.Hydro.ui,
|
||||
isIE: (str) => str.includes('MSIE') || str.includes('rv:11.0'),
|
||||
function render(name, state) {
|
||||
Object.assign(state, {
|
||||
typeof: (o) => typeof o,
|
||||
static_url: (str) => `/${str}`,
|
||||
datetimeSpan: misc.datetimeSpan,
|
||||
paginate: misc.paginate,
|
||||
perm,
|
||||
status: builtin.status,
|
||||
size: misc.size,
|
||||
gravatar: misc.gravatar,
|
||||
model,
|
||||
Context: global.Hydro.ui,
|
||||
isIE: (str) => str.includes('MSIE') || str.includes('rv:11.0'),
|
||||
});
|
||||
return new Promise((resolve, reject) => {
|
||||
env.render(name, state, (err, res) => {
|
||||
if (err) reject(err);
|
||||
else resolve(res);
|
||||
});
|
||||
return new Promise((resolve, reject) => {
|
||||
env.render(name, state, (err, res) => {
|
||||
if (err) reject(err);
|
||||
else resolve(res);
|
||||
});
|
||||
});
|
||||
},
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
global.Hydro.lib.template = module.exports = { render };
|
||||
@@ -29,7 +29,7 @@ const checkIntro = (s) => { if (!isIntro(s)) throw new ValidationError('intro');
|
||||
const isDescription = () => true;
|
||||
const checkDescription = (s) => { if (!isDescription(s)) throw new ValidationError('description'); else return s; };
|
||||
|
||||
module.exports = {
|
||||
global.Hydro.lib.validator = module.exports = {
|
||||
isTitle,
|
||||
checkTitle,
|
||||
isUid,
|
||||
|
||||
Reference in new issue
Block a user