koishi-plugin-assets-cloudreve 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index.d.ts +22 -0
- package/lib/index.js +145 -0
- package/package.json +47 -0
- package/readme.md +5 -0
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Context, Schema } from 'koishi';
|
|
2
|
+
import Assets from '@koishijs/assets';
|
|
3
|
+
export declare const name = "assets-cloudreve";
|
|
4
|
+
export interface Config extends AssetsCloudreve.Config {
|
|
5
|
+
}
|
|
6
|
+
declare class AssetsCloudreve extends Assets<AssetsCloudreve.Config> {
|
|
7
|
+
accesstoken: string;
|
|
8
|
+
constructor(ctx: Context, config: AssetsCloudreve.Config);
|
|
9
|
+
stats(): Promise<Assets.Stats>;
|
|
10
|
+
upload(url: string, file: string): Promise<string>;
|
|
11
|
+
}
|
|
12
|
+
declare namespace AssetsCloudreve {
|
|
13
|
+
interface Config extends Assets.Config {
|
|
14
|
+
baseurl: string;
|
|
15
|
+
email: string;
|
|
16
|
+
password: string;
|
|
17
|
+
basepath: string;
|
|
18
|
+
}
|
|
19
|
+
const Config: Schema<Config>;
|
|
20
|
+
const usage = "\n ***\n\n \u6B64\u63D2\u4EF6\u5229\u7528Cloudreve\u5B9E\u73B0\u8D44\u6E90\u8F6C\u5B58\uFF0C\u76EE\u524D\u652F\u6301\u56FE\u7247\u7B49\u5C0F\u6587\u4EF6\uFF0C\u4F1A\u5148\u5C06\u8D44\u6E90\u8F6C\u5B58\u5230Cloudreve\u7136\u540E\u8FD4\u56DE\u76F4\u94FE\u3002\u4F7F\u7528\u6B64\u63D2\u4EF6\u65F6\u9700\u5173\u95ED\u5176\u4ED6\u63D0\u4F9BAssets\u670D\u52A1\u7684\u63D2\u4EF6\n\n \u672C\u63D2\u4EF6\u901A\u8FC7Cloudreve\u7684\u90AE\u7BB1\u548C\u5BC6\u7801\u767B\u5F55\uFF0C\u6240\u4EE5\u8BF7\u4E0D\u8981\u5728Cloudreve\u7684\u8BBE\u7F6E\u4E2D\u5F00\u542F\u767B\u5F55\u9A8C\u8BC1\u7801\u3002\n\n \u5982\u51FA\u73B0\u62A5\u9519\uFF0C\u53EF\u53C2\u8003\u5B98\u65B9\u9519\u8BEF\u4EE3\u7801: https://docs.cloudreve.org/zh/api/overview ";
|
|
21
|
+
}
|
|
22
|
+
export default AssetsCloudreve;
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name2 in all)
|
|
10
|
+
__defProp(target, name2, { get: all[name2], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var src_exports = {};
|
|
32
|
+
__export(src_exports, {
|
|
33
|
+
default: () => src_default,
|
|
34
|
+
name: () => name
|
|
35
|
+
});
|
|
36
|
+
module.exports = __toCommonJS(src_exports);
|
|
37
|
+
var import_koishi = require("koishi");
|
|
38
|
+
var import_assets = __toESM(require("@koishijs/assets"));
|
|
39
|
+
var import_chalk = __toESM(require("chalk"));
|
|
40
|
+
var name = "assets-cloudreve";
|
|
41
|
+
async function getaccess(config) {
|
|
42
|
+
let headers = new Headers();
|
|
43
|
+
let raw = JSON.stringify({
|
|
44
|
+
"email": config.email,
|
|
45
|
+
"password": config.password
|
|
46
|
+
});
|
|
47
|
+
headers.append("Content-Type", "application/json");
|
|
48
|
+
let accessurl = "/api/v4/session/token";
|
|
49
|
+
try {
|
|
50
|
+
let response = await fetch(`${config.baseurl}${accessurl}`, {
|
|
51
|
+
headers,
|
|
52
|
+
method: "POST",
|
|
53
|
+
body: raw,
|
|
54
|
+
redirect: "follow"
|
|
55
|
+
});
|
|
56
|
+
return response.json();
|
|
57
|
+
} catch (error) {
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
__name(getaccess, "getaccess");
|
|
61
|
+
var AssetsCloudreve = class extends import_assets.default {
|
|
62
|
+
static {
|
|
63
|
+
__name(this, "AssetsCloudreve");
|
|
64
|
+
}
|
|
65
|
+
accesstoken;
|
|
66
|
+
constructor(ctx, config) {
|
|
67
|
+
super(ctx, config);
|
|
68
|
+
ctx.logger.info(`您使用的Cloudreve地址为: ${this.config.baseurl}`);
|
|
69
|
+
getaccess(this.config).then((result) => {
|
|
70
|
+
this.ctx.logger.info("响应代码为", result.code);
|
|
71
|
+
if (result.code === 0) {
|
|
72
|
+
this.accesstoken = result.data.token.access_token;
|
|
73
|
+
ctx.logger.info(import_chalk.default.greenBright.bold("获取access_token成功,access_token="), this.accesstoken);
|
|
74
|
+
} else {
|
|
75
|
+
ctx.logger.warn("获取access_token失败,响应信息为:", result);
|
|
76
|
+
}
|
|
77
|
+
}, (err) => {
|
|
78
|
+
this.ctx.logger.warn("初始化失败"), err;
|
|
79
|
+
}).catch((err) => {
|
|
80
|
+
this.ctx.logger.error(import_chalk.default.hex("#FFA500").bold("获取access_token出现错误,请检查输入的api地址是否错误。错误信息为") + err);
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
stats() {
|
|
84
|
+
return Promise.resolve(void 0);
|
|
85
|
+
}
|
|
86
|
+
async upload(url, file) {
|
|
87
|
+
const { buffer, filename, type } = await this.analyze(url, file);
|
|
88
|
+
this.ctx.logger.info(`文件buffer大小为: ${buffer.length} 文件名为: ${filename}`);
|
|
89
|
+
let myheaders = new Headers();
|
|
90
|
+
myheaders.append("Authorization", this.accesstoken);
|
|
91
|
+
myheaders.append("Content-Type", "application/octet-stream");
|
|
92
|
+
myheaders.append("Content-Length", buffer.length.toString());
|
|
93
|
+
let response = await fetch(this.config.baseurl + "/api/v4/file/content?uri=cloudreve://my/" + this.config.basepath + filename, {
|
|
94
|
+
method: "PUT",
|
|
95
|
+
headers: myheaders,
|
|
96
|
+
body: new Uint8Array(buffer),
|
|
97
|
+
redirect: "follow"
|
|
98
|
+
});
|
|
99
|
+
let responsetext = await response.json();
|
|
100
|
+
let path = responsetext.data.path;
|
|
101
|
+
let directlinkheaders = new Headers();
|
|
102
|
+
directlinkheaders.append("Authorization", "Bearer " + this.accesstoken);
|
|
103
|
+
directlinkheaders.append("Content-Type", "application/json");
|
|
104
|
+
let directlinkbody = JSON.stringify({
|
|
105
|
+
"uris": [
|
|
106
|
+
path
|
|
107
|
+
]
|
|
108
|
+
});
|
|
109
|
+
let directlinkresponse = await fetch(this.config.baseurl + "/api/v4/file/source", {
|
|
110
|
+
method: "PUT",
|
|
111
|
+
headers: directlinkheaders,
|
|
112
|
+
body: directlinkbody,
|
|
113
|
+
redirect: "follow"
|
|
114
|
+
});
|
|
115
|
+
let link = await directlinkresponse.json();
|
|
116
|
+
if (link.code === 0) {
|
|
117
|
+
let directLink = link.data[0].link;
|
|
118
|
+
this.ctx.logger.info(import_chalk.default.greenBright(import_chalk.default.bold.blueBright("[assets-cloudreve]"), "获取直链成功,直链为:"), directLink);
|
|
119
|
+
return directLink;
|
|
120
|
+
} else {
|
|
121
|
+
this.ctx.logger.warn("直链创建失败", link);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
((AssetsCloudreve2) => {
|
|
126
|
+
AssetsCloudreve2.Config = import_koishi.Schema.intersect([import_koishi.Schema.object({
|
|
127
|
+
baseurl: import_koishi.Schema.string().required().description(`请输入**Cloudreve**的网址,示例:https://example.com`),
|
|
128
|
+
email: import_koishi.Schema.string().required().description("请输入Cloudreve的email"),
|
|
129
|
+
password: import_koishi.Schema.string().required().description("请输入Cloudreve的密码"),
|
|
130
|
+
basepath: import_koishi.Schema.string().default("base/").description("文件路径,用于指定文件在文件系统中的位置。需要以/结尾<br>详细信息可看: https://docs.cloudreve.org/zh/api/file-uri ")
|
|
131
|
+
}), import_assets.default.Config]);
|
|
132
|
+
AssetsCloudreve2.usage = `
|
|
133
|
+
***
|
|
134
|
+
|
|
135
|
+
此插件利用Cloudreve实现资源转存,目前支持图片等小文件,会先将资源转存到Cloudreve然后返回直链。使用此插件时需关闭其他提供Assets服务的插件
|
|
136
|
+
|
|
137
|
+
本插件通过Cloudreve的邮箱和密码登录,所以请不要在Cloudreve的设置中开启登录验证码。
|
|
138
|
+
|
|
139
|
+
如出现报错,可参考官方错误代码: https://docs.cloudreve.org/zh/api/overview `;
|
|
140
|
+
})(AssetsCloudreve || (AssetsCloudreve = {}));
|
|
141
|
+
var src_default = AssetsCloudreve;
|
|
142
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
143
|
+
0 && (module.exports = {
|
|
144
|
+
name
|
|
145
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "koishi-plugin-assets-cloudreve",
|
|
3
|
+
"description": "通过Cloudreve转存资源,你可以通过自己部署Cloudreve配合此插件实现资源转存",
|
|
4
|
+
"version": "1.0.0",
|
|
5
|
+
"main": "lib/index.js",
|
|
6
|
+
"typings": "lib/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"lib",
|
|
9
|
+
"dist"
|
|
10
|
+
],
|
|
11
|
+
"author": {
|
|
12
|
+
"name": "material666",
|
|
13
|
+
"email": "fcc97733@gmail"
|
|
14
|
+
},
|
|
15
|
+
"license": "Apache-2.0",
|
|
16
|
+
"scripts": {},
|
|
17
|
+
"keywords": [
|
|
18
|
+
"chatbot",
|
|
19
|
+
"koishi",
|
|
20
|
+
"plugin",
|
|
21
|
+
"asstes",
|
|
22
|
+
"cloudreve",
|
|
23
|
+
"storage"
|
|
24
|
+
],
|
|
25
|
+
"koishi": {
|
|
26
|
+
"category": "storage",
|
|
27
|
+
"description": {
|
|
28
|
+
"en": "By using Cloudreve for file transfer, you can deploy Cloudreve yourself and integrate it with this plugin to achieve resource transfer",
|
|
29
|
+
"zh": "使用Cloudreve转存文件,你可以自己部署Cloudreve并配合本插件实现转存资源"
|
|
30
|
+
},
|
|
31
|
+
"service": {
|
|
32
|
+
"implements": [
|
|
33
|
+
"assets"
|
|
34
|
+
]
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"@koishijs/plugin-server": "^3.2.3"
|
|
39
|
+
},
|
|
40
|
+
"peerDependencies": {
|
|
41
|
+
"koishi": "^4.18.7"
|
|
42
|
+
},
|
|
43
|
+
"dependencies": {
|
|
44
|
+
"@koishijs/assets": "^1.1.2",
|
|
45
|
+
"chalk": "^4.1.2"
|
|
46
|
+
}
|
|
47
|
+
}
|