Skip to content

Commit

Permalink
Merge pull request #985 from pxgo/master
Browse files Browse the repository at this point in the history
优化,调整,修复
  • Loading branch information
pxgo authored Sep 27, 2024
2 parents 57b5a54 + c3fdc48 commit ab33ea1
Show file tree
Hide file tree
Showing 1,687 changed files with 13,129 additions and 5,107 deletions.
3 changes: 2 additions & 1 deletion dataModels/MessageModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -2146,8 +2146,9 @@ messageSchema.statics.extendMessages = async (messages = []) => {
message.content = message.content.replace(
/\[f\/(.*?)]/g,
function (r, v1) {
const emojiUrl = getUrl('emoji', v1);
return (
'<img class="message-emoji" src="/twemoji/2/svg/' + v1 + '.svg"/>'
'<img class="message-emoji" src="' + emojiUrl + '"/>'
);
},
);
Expand Down
9 changes: 5 additions & 4 deletions dataModels/MomentModel.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const mongoose = require('../settings/database');
const { twemoji } = require('../settings/editor');
const { fromNow } = require('../nkcModules/tools');
const { fluentuiEmojiUnicode } = require('../nkcModules/fluentuiEmoji');

// 包含所有document的状态
// 并且额外包含 deleted, cancelled
Expand Down Expand Up @@ -1511,6 +1510,7 @@ schema.statics.getQuoteDefaultContent = async (quoteType) => {
* */
schema.statics.renderContent = async (content) => {
const nkcRender = require('../nkcModules/nkcRender');
const { getUrl } = require('../nkcModules/tools');
const { filterMessageContent } = require('../nkcModules/xssFilters');
// 替换空格
// content = content.replace(/ /g, '&nbsp;');
Expand All @@ -1522,10 +1522,11 @@ schema.statics.renderContent = async (content) => {
// 替换换行符
content = content.replace(/\n/g, '<br/>');
content = content.replace(/\[(.*?)]/g, function (r, v1) {
if (!twemoji.includes(v1)) {
if (!fluentuiEmojiUnicode.includes(v1)) {
return r;
}
return '<img class="message-emoji" src="/twemoji/2/svg/' + v1 + '.svg"/>';
const emojiUrl = getUrl('emoji', v1);
return '<img class="message-emoji" src="' + emojiUrl + '"/>';
});
return content;
};
Expand Down
79 changes: 47 additions & 32 deletions middlewares/body/remoteFile.js
Original file line number Diff line number Diff line change
@@ -1,55 +1,70 @@
const http = require('http');
const FILE = require("../../nkcModules/file");
const FILE = require('../../nkcModules/file');
const ContentDisposition = require('content-disposition');
const {pictureExtensions, breakpointExtensions} = FILE;
const { pictureExtensions, breakpointExtensions } = FILE;
const fileBody = require('./file');
module.exports = async (ctx) => {
// 关闭 gzip 压缩
ctx.compress = false;
const {remoteFile, isAttachment, settings} = ctx;
const {
url: fileUrl,
filename = '',
} = remoteFile;
const { remoteFile, isAttachment, settings } = ctx;
const { url: fileUrl, filename = '' } = remoteFile;
let ext = filename.split('.');
ext = ext.pop() || '';
ext = ext.toLowerCase();
if(!ext) ctx.throw(500, `远程文件格式不能为空`);
if (!ext) {
ctx.throw(500, `远程文件格式不能为空`);
}
let fileRes;
try{
try {
fileRes = await getRemoteFileRes(fileUrl.toString(), ctx);
} catch(err) {
} catch (err) {
// store service 报错时返回一张默认图片
const {defaultAvatarPath} = settings.statics;
ctx.filePath = defaultAvatarPath;
const { lostImage } = settings.statics;
ctx.filePath = lostImage;
return await fileBody(ctx);
}

let contentDispositionType;
if (isAttachment || (!pictureExtensions.includes(ext) && (!breakpointExtensions.includes(ext)))) {
if (
isAttachment ||
(!pictureExtensions.includes(ext) && !breakpointExtensions.includes(ext))
) {
contentDispositionType = 'attachment';
} else {
contentDispositionType = 'inline';
}
ctx.type = ext;
ctx.body = fileRes;
ctx.set('content-disposition', ContentDisposition(filename, {type: contentDispositionType}));
ctx.set(
'content-disposition',
ContentDisposition(filename, { type: contentDispositionType }),
);
const fileResContentLength = fileRes.headers[`content-length`];
const fileResAcceptRanges = fileRes.headers[`accept-ranges`];
const fileResCacheControl = fileRes.headers[`cache-control`];
const fileResRange = fileRes.headers[`content-range`];
const fileResConnection = fileRes.headers[`connection`];
const fileResEtag = fileRes.headers[`etag`];
if(fileResAcceptRanges) ctx.set('accept-ranges', fileResAcceptRanges);
if(fileResCacheControl) ctx.set('cache-control', fileResCacheControl);
if(fileResConnection) ctx.set('connection', fileResConnection);
if(fileResContentLength) ctx.set('content-length', fileResContentLength);
if(fileResEtag) ctx.set('etag', fileResEtag);
if(ctx.fresh) {
if (fileResAcceptRanges) {
ctx.set('accept-ranges', fileResAcceptRanges);
}
if (fileResCacheControl) {
ctx.set('cache-control', fileResCacheControl);
}
if (fileResConnection) {
ctx.set('connection', fileResConnection);
}
if (fileResContentLength) {
ctx.set('content-length', fileResContentLength);
}
if (fileResEtag) {
ctx.set('etag', fileResEtag);
}
if (ctx.fresh) {
ctx.status = 304;
return;
}
if(fileResRange) {
if (fileResRange) {
ctx.set(`content-range`, fileResRange);
ctx.status = 206;
}
Expand All @@ -60,31 +75,31 @@ function getRemoteFileRes(url, ctx) {
const headerRange = ctx.request.headers['range'];
const options = {
agent: new http.Agent({
"keepAlive": true,
"timeout": 30000
keepAlive: true,
timeout: 30000,
}),
method: 'GET',
};
if (headerRange) {
options.headers = {
range: headerRange
}
range: headerRange,
};
}
return new Promise((resolve, reject) => {
const req = http.request(url, options, res => {
const req = http.request(url, options, (res) => {
res.on('error', reject);
if(res.statusCode >= 400) {
if (res.statusCode >= 400) {
res.setEncoding('utf8');
let resData = '';
res.on('data', d => {
res.on('data', (d) => {
resData += d;
});
res.on('end', () => {
try{
try {
const parsedData = JSON.parse(resData);
reject(parsedData);
} catch(err) {
reject(err)
} catch (err) {
reject(err);
}
});
} else {
Expand All @@ -94,4 +109,4 @@ function getRemoteFileRes(url, ctx) {
req.on('error', reject);
req.end();
});
}
}
41 changes: 41 additions & 0 deletions nkcModules/fluentuiEmoji.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const fluentuiEmoji = require('./fluentuiEmoji.json');
const emojiRegex = require('emoji-regex');
const regex = (emojiRegex.default || emojiRegex)();
const fluentuiEmojiUnicode = [];
const fluentuiEmoji_unicode_emoji = {};
const fluentuiEmoji_char_emoji = {};

for (const group of fluentuiEmoji) {
for (const emoji of group.emoji) {
fluentuiEmojiUnicode.push(emoji.unicode);
fluentuiEmoji_unicode_emoji[emoji.unicode] = emoji;
fluentuiEmoji_char_emoji[emoji.glyph] = emoji;
}
}

function replaceEmojiWithImgTags(htmlString = '') {
const { getUrl } = require('./tools');
return htmlString.replace(regex, (char) => {
const emoji = fluentuiEmoji_char_emoji[char];
if (!emoji) {
return char;
}
const emojiUrl = getUrl('emoji', emoji.unicode);
return `<img src="${emojiUrl}" class="emoji" data-tag="nkcsource" data-type="twemoji" alt="${char}">`;
});
}

function getEmojiCharByUnicode(unicode) {
const emoji = fluentuiEmoji_unicode_emoji[unicode];
if (!emoji) {
return '?';
}
return emoji.glyph;
}

module.exports = {
fluentuiEmojiUnicode,
fluentuiEmoji,
replaceEmojiWithImgTags,
getEmojiCharByUnicode,
};
Loading

0 comments on commit ab33ea1

Please sign in to comment.