Commit f5d61070 authored by nanahira's avatar nanahira

improve url handling

parent bd23db52
This diff is collapsed.
...@@ -28,12 +28,14 @@ ...@@ -28,12 +28,14 @@
"homepage": "https://github.com/koishijs/koishi-plugin-pics", "homepage": "https://github.com/koishijs/koishi-plugin-pics",
"dependencies": { "dependencies": {
"@koishijs/assets": "^1.0.1", "@koishijs/assets": "^1.0.1",
"koishi-thirdeye": "^11.1.17", "ext2mime": "^1.0.1",
"file-type": "16.5.3",
"koishi-thirdeye": "^11.1.20",
"lodash": "^4.17.21" "lodash": "^4.17.21"
}, },
"devDependencies": { "devDependencies": {
"@koishijs/plugin-console": "^5.0.0", "@koishijs/plugin-console": "^5.0.2",
"@koishijs/plugin-database-memory": "^2.0.1", "@koishijs/plugin-database-memory": "^2.2.0",
"@koishijs/plugin-sandbox": "^2.5.0", "@koishijs/plugin-sandbox": "^2.5.0",
"@types/jest": "^29.2.0", "@types/jest": "^29.2.0",
"@types/lodash": "^4.14.176", "@types/lodash": "^4.14.176",
...@@ -51,13 +53,13 @@ ...@@ -51,13 +53,13 @@
"ts-jest": "^29.0.3", "ts-jest": "^29.0.3",
"ts-loader": "^9.3.1", "ts-loader": "^9.3.1",
"ts-node": "^10.9.1", "ts-node": "^10.9.1",
"typescript": "^4.8.4", "typescript": "^4.9.4",
"webpack": "^5.74.0", "webpack": "^5.74.0",
"webpack-cli": "^4.10.0", "webpack-cli": "^4.10.0",
"ws": "^8.3.0" "ws": "^8.3.0"
}, },
"peerDependencies": { "peerDependencies": {
"koishi": "^4.10.10" "koishi": "^4.11.0"
}, },
"jest": { "jest": {
"moduleFileExtensions": [ "moduleFileExtensions": [
......
// import 'source-map-support/register'; // import 'source-map-support/register';
import { Context, Random, Logger, Bot, remove, Session, Dict } from 'koishi'; import { Context, Random, Logger, remove, Session, Dict } from 'koishi';
import { PicsPluginConfig } from './config'; import { PicsPluginConfig } from './config';
import _ from 'lodash'; import _ from 'lodash';
import { segment, Quester, Element } from 'koishi'; import { segment, Quester, Element } from 'koishi';
...@@ -20,11 +20,14 @@ import { ...@@ -20,11 +20,14 @@ import {
UseCommand, UseCommand,
UseComponent, UseComponent,
} from 'koishi-thirdeye'; } from 'koishi-thirdeye';
import { AxiosRequestConfig } from 'axios';
import { PicAssetsTransformMiddleware } from './middlewares/assets'; import { PicAssetsTransformMiddleware } from './middlewares/assets';
import { PicDownloaderMiddleware } from './middlewares/download'; import { PicDownloaderMiddleware } from './middlewares/download';
import { PicMiddleware, PicNext, PicResult } from './def'; import { PicMiddleware, PicNext, PicResult } from './def';
import { PicSource } from './picsource'; import { PicSource } from './picsource';
import FileType from 'file-type';
import path from 'path';
import ext2mime from 'ext2mime';
import * as fs from 'fs';
export * from './config'; export * from './config';
export * from './middleware'; export * from './middleware';
export * from './picsource'; export * from './picsource';
...@@ -174,38 +177,45 @@ export default class PicsContainer ...@@ -174,38 +177,45 @@ export default class PicsContainer
return this.fetchPicsWithSources(sources, picTags); return this.fetchPicsWithSources(sources, picTags);
} }
isOneBotBot(bot?: Bot) { async urlToBuffer(url: string): Promise<{ buffer: Buffer; mime: string }> {
return (
bot &&
(bot.platform === 'onebot' ||
(bot.platform === 'qqguild' && bot['parentBot']?.platform === 'onebot'))
);
}
async urlToBuffer(
url: string,
extraConfig: AxiosRequestConfig = {},
): Promise<Buffer> {
if (url.startsWith('base64://')) { if (url.startsWith('base64://')) {
return Buffer.from(url.slice(9), 'base64'); const buf = Buffer.from(url.slice(9), 'base64');
const type = await FileType.fromBuffer(buf);
return { buffer: buf, mime: type?.mime || 'application/octet-stream' };
} }
const data = await this._http.get<Buffer>(url, { if (url.startsWith('file://')) {
responseType: 'arraybuffer', const filePath = url.slice(7);
...extraConfig, const buf = await fs.promises.readFile(filePath);
}); const mime =
return data as Buffer; ext2mime(path.extname(filePath)) ||
(await FileType.fromBuffer(buf)).mime;
return { buffer: buf, mime };
}
const data = await this._http.file(url);
return {
buffer: data.data as Buffer,
mime: data.mime,
};
} }
bufferToUrl(buffer: Buffer) { async bufferToUrl(buffer: Buffer, mime?: string) {
return `base64://${buffer.toString('base64')}`; if (!mime) {
const result = await FileType.fromBuffer(buffer);
if (result) {
mime = result.mime;
} else {
mime = 'application/octet-stream';
}
}
return `data:${mime};base64,${buffer.toString('base64')}`;
} }
async download(url: string, extraConfig: AxiosRequestConfig = {}) { async download(url: string) {
if (url.startsWith('base64://')) { if (url.startsWith('base64://')) {
return url; return this.bufferToUrl(Buffer.from(url.slice(9), 'base64'));
} }
const buffer = await this.urlToBuffer(url, extraConfig); const data = await this.urlToBuffer(url);
return this.bufferToUrl(buffer); return this.bufferToUrl(data.buffer, data.mime);
} }
async resolveUrl(url: string, middlewares = this.picMiddlewares) { async resolveUrl(url: string, middlewares = this.picMiddlewares) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment