@ginstone/nga-api
Version:
61 lines (60 loc) • 1.97 kB
JavaScript
;
// 附件工具类
Object.defineProperty(exports, "__esModule", { value: true });
exports.AttachmentUtils = exports.THUMB_DIC = void 0;
const StrUtils_1 = require("./StrUtils");
/**
* 缩略图bit数据, 位置与后缀的对应关系
*/
exports.THUMB_DIC = [
{ index: 3, suffix: ".thumb_ss.jpg", label: "缩略图(极小)" },
{ index: 4, suffix: ".thumb_s.jpg", label: "缩略图(小)" },
{ index: 5, suffix: ".thumb.jpg", label: "缩略图(中)" },
{ index: 6, suffix: ".medium.jpg", label: "缩略图(大)" },
];
class AttachmentUtils {
}
/**
* 生成缩略图+原图论坛代码
* @param url 原图地址
* @param thumb 缩略图bit数据
*/
AttachmentUtils.getImageUrls = (url, thumb) => {
const res = new Array;
if (thumb) {
const s = (0, StrUtils_1.parseBit)(thumb);
exports.THUMB_DIC
.filter(({ index }) => s.length > index && s[index] === '1')
.map(({ suffix, label }) => ({
label, url: url + suffix
})).forEach(i => res.push(i));
}
res.push({ label: "原图", url });
return res;
};
/**
* 生成预设论坛代码
* @param url 文件url
* @param thumb 缩略图bit数据
* @param filename 文件名
*/
AttachmentUtils.getBbsCodes = (url, thumb, filename) => {
const ext = (0, StrUtils_1.getExtension)(url);
switch (ext) {
case 'zip': {
const code = `[url=https://img.nga.178.com/attachments/${url}]下载文件${filename ? (": " + filename) : undefined}[/url]`;
return [{ label: "附件", code }];
}
case "mp3":
case "mp4": {
const code = `[flash]./${url}[/flash]`;
return [{ label: "媒体", code }];
}
default:
return AttachmentUtils.getImageUrls(url, thumb).map(({ label, url }) => ({
label,
code: `[img]./${url}[/img]`,
}));
}
};
exports.AttachmentUtils = AttachmentUtils;