@nger/fk-upload 1.0.8 → 1.0.11

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.
@@ -8,266 +8,39 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
8
8
  var __metadata = (this && this.__metadata) || function (k, v) {
9
9
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
10
  };
11
- var __importDefault = (this && this.__importDefault) || function (mod) {
12
- return (mod && mod.__esModule) ? mod : { "default": mod };
13
- };
14
11
  Object.defineProperty(exports, "__esModule", { value: true });
15
12
  exports.UploadService = void 0;
16
- const path_1 = require("path");
17
13
  const entities_1 = require("../entities");
18
- const cids_1 = __importDefault(require("cids"));
19
- const multihashing_async_1 = __importDefault(require("multihashing-async"));
20
14
  const typeorm_1 = require("@nger/typeorm");
21
- const rxjs_1 = require("rxjs");
22
- const request_1 = require("request");
23
- const fs_1 = require("fs");
24
- const md5_file_1 = __importDefault(require("md5-file"));
25
15
  const core_1 = require("@nger/core");
26
- const axios_1 = __importDefault(require("axios"));
27
- const form_data_1 = __importDefault(require("form-data"));
28
- const fs_extra_1 = require("fs-extra");
16
+ const ws_1 = require("@nger/ws");
17
+ const task_service_1 = require("./task.service");
29
18
  let UploadService = class UploadService {
30
19
  db;
31
20
  injector;
21
+ get ws() {
22
+ return this.injector.get(ws_1.WsService);
23
+ }
24
+ get task() {
25
+ return this.injector.get(task_service_1.TaskService);
26
+ }
32
27
  constructor(db, injector) {
33
28
  this.db = db;
34
29
  this.injector = injector;
35
30
  }
36
- async createDownLoadTask(url, fkLoginId) {
37
- const observable = await this.createObservable(url);
38
- return observable.listen.pipe((0, rxjs_1.throttleTime)(1000), (0, rxjs_1.switchMap)(async (res) => {
39
- const exist = await this.db.manager.findOne(entities_1.FkDownloadTaskEntity, { where: { filename: observable.filename } });
40
- if (exist) {
41
- const size = res.size;
42
- const total = res.totalSize;
43
- const num = Math.floor((size / total) * 100);
44
- await this.db.manager.update(entities_1.FkDownloadTaskEntity, exist.filename, { size: num });
45
- }
46
- else {
47
- await this.db.manager.save(entities_1.FkDownloadTaskEntity, res);
48
- }
49
- return res;
50
- })).subscribe({
51
- next: () => { },
52
- complete: async () => {
53
- const exist = await this.db.manager.findOne(entities_1.FkDownloadTaskEntity, { where: { filename: observable.filename } });
54
- const md5 = await (0, md5_file_1.default)(exist.path);
55
- await this.db.manager.update(entities_1.FkDownloadTaskEntity, observable.filename, { status: 1, size: 100, fileMd5: md5, loginId: fkLoginId });
56
- // upload
57
- return await this.createUplaodTask(exist.filename);
58
- }
59
- });
60
- }
61
- getUploadUrl(token, forceDirect = true) {
62
- if (forceDirect) {
63
- return `https://${token.url}/${token.visitType}/${token.app}/upload?cmd=${token.cmd}&token=${token.token}`;
64
- }
65
- else {
66
- return `https://${token.url}/${token.visitType}/${token.app}/advance?cmd=${token.cmd}&token=${token.token}`;
67
- }
68
- }
69
- getUploadInfourl(token) {
70
- return `https://${token.url}/${token.visitType}/${token.app}/advance/info?cmd=${token.cmd}&token=${token.token}`;
71
- }
72
- encodeURIComponent(data) {
73
- return Object.keys(data).map(key => {
74
- const value = Reflect.get(data, key);
75
- return `${key}=${encodeURIComponent(value)}`;
76
- }).join('&');
77
- }
78
- async createUplaodTask(filename) {
79
- //
31
+ async continueTask(filename) {
80
32
  const task = await this.db.manager.findOne(entities_1.FkDownloadTaskEntity, { where: { filename } });
81
33
  if (task) {
82
34
  if (task.status === 0) {
83
- return {
84
- msg: 'downloading'
85
- };
86
- }
87
- if (task.status === 2) {
88
- return {
89
- msg: 'uploaded'
90
- };
35
+ return this.task.createDownLoadTask(task.url, task.loginId, 0);
91
36
  }
92
- const loginEntity = await this.db.manager.findOne(entities_1.FkLoginEntity, { where: { fkLoginId: task.loginId } });
93
- const cookies = await this.db.manager.find(entities_1.FkLoginCookieEntity, { where: { fkLoginId: task.loginId } });
94
- const cookie = cookies.map(cookie => `${cookie.key}=${cookie.value}`).join(';');
95
- // 00 take token
96
- const url = `https://i.vip.webportal.top/`;
97
- const iVipWebPortal = await axios_1.default.get(url, {
98
- headers: {
99
- Cookie: cookie
100
- }
101
- }).then(res => res.data);
102
- const items = iVipWebPortal.match(/<meta id='_TOKEN' value='(.*?)'\/>/);
103
- let token = ``;
104
- if (items && items.length === 2) {
105
- token = items[1];
37
+ else if (task.status === 1) {
38
+ return this.task.startUploadTask(task.filename);
106
39
  }
107
- if (token.length > 0) {
108
- // 01
109
- const url = `https://smr00.vip.webportal.top/cn/api/manage/advanceUpload/genAccessKey?_v=1651232099799&_TOKEN=${token}`;
110
- const accessKey = await axios_1.default.get(url).then(res => res.data);
111
- if (accessKey.success) {
112
- const accessTokenString = decode(accessKey.accessKey);
113
- if (accessTokenString) {
114
- const accessToken = JSON.parse(accessTokenString);
115
- const uploadInfoUrl = this.getUploadInfourl(accessToken);
116
- const bssInfo = {
117
- fromSite: true, siteId: 0, groupId: 0, fileSizeLimit: 1024
118
- };
119
- const info = await axios_1.default.post(uploadInfoUrl, this.encodeURIComponent({
120
- fileMd5: task.fileMd5,
121
- fileSize: task.totalSize,
122
- isFreeVer: false,
123
- aid: loginEntity.aid,
124
- folderId: loginEntity.uploadGroupId,
125
- bssInfo: JSON.stringify(bssInfo),
126
- fileName: task.filename
127
- }), { headers: { cookie: cookie } }).then(res => res.data);
128
- if (info.code === 200) {
129
- const data = info.data;
130
- const { delayTime, splitSize, uploadedSize } = data;
131
- const uploadUrl = this.getUploadUrl(accessToken, false);
132
- const totalChunks = Math.ceil((task.totalSize - uploadedSize) / splitSize);
133
- let start = uploadedSize;
134
- let index = Math.ceil(uploadedSize / splitSize);
135
- let complete = false;
136
- do {
137
- if (index >= totalChunks - 1) {
138
- complete = true;
139
- }
140
- const fileStream = (0, fs_1.createReadStream)(task.path, { start: start, end: start + splitSize });
141
- const formdata = new form_data_1.default();
142
- formdata.append('ctrl', fileStream);
143
- formdata.append('isFreeVer', 'false');
144
- formdata.append('aid', `${loginEntity.aid || 0}`);
145
- formdata.append('folderId', `${loginEntity.uploadGroupId || 0}`);
146
- formdata.append('fileName', task.filename);
147
- formdata.append(`totalSize`, `${task.totalSize}`);
148
- formdata.append(`fileMd5`, task.fileMd5);
149
- formdata.append('index', `${index}`);
150
- formdata.append('chunkSize', `${splitSize}`);
151
- formdata.append('totalChunks', `${totalChunks}`);
152
- formdata.append(`complete`, `${complete}`);
153
- formdata.append('bssInfo', JSON.stringify(bssInfo));
154
- const headers = formdata.getHeaders();
155
- const uploadResult = await axios_1.default.post(uploadUrl, formdata, {
156
- headers: {
157
- ...headers,
158
- Cookie: cookie
159
- }
160
- }).then(res => {
161
- const data = res.data;
162
- return data;
163
- });
164
- if (uploadResult.code === 200) {
165
- const data = uploadResult.data;
166
- if (data) {
167
- const downUrl = data.downUrl;
168
- if (downUrl) {
169
- await this.db.manager.update(entities_1.FkDownloadTaskEntity, task.filename, {
170
- status: 2,
171
- uploadUrl: `https://` + downUrl,
172
- uploadSize: 100
173
- });
174
- }
175
- else {
176
- await this.db.manager.update(entities_1.FkDownloadTaskEntity, task.filename, {
177
- uploadSize: Math.floor(((index + 1) / totalChunks) * 100)
178
- });
179
- }
180
- }
181
- index = index + 1;
182
- start = start + splitSize;
183
- }
184
- } while (!complete);
185
- }
186
- }
187
- }
188
- }
189
- }
190
- }
191
- async createObservable(url) {
192
- const hash = await (0, multihashing_async_1.default)(Buffer.from(url), 'sha1', 16);
193
- const cid = new cids_1.default(1, 'raw', hash, 'base64');
194
- // const _url = new URL(url);
195
- // const pathname = _url.pathname;
196
- // const ext = extname(pathname)
197
- const filename = cid.toString('base64');
198
- let item = await this.db.manager.findOne(entities_1.FkDownloadTaskEntity, { where: { filename } });
199
- if (item) {
200
- let task = item;
201
- if (task.status) {
202
- return {
203
- listen: (0, rxjs_1.of)(task),
204
- filename
205
- };
40
+ else {
41
+ console.log(task);
206
42
  }
207
- const observable = new rxjs_1.Observable((sub) => {
208
- // go on
209
- const writeStream = (0, fs_1.createWriteStream)(task.path, { start: task.size });
210
- const req = (0, request_1.get)(url, {
211
- headers: {
212
- 'Content-Type': 'application/octet-stream'
213
- }
214
- });
215
- let length = 0;
216
- req.on('data', (buf) => {
217
- length += buf.length;
218
- if (length >= task.size) {
219
- task.size = length;
220
- writeStream.write(buf, (err) => {
221
- if (err)
222
- sub.error(err);
223
- });
224
- sub.next(task);
225
- }
226
- });
227
- req.on('end', () => {
228
- sub.complete();
229
- writeStream.end();
230
- });
231
- });
232
- return { listen: observable, filename };
233
43
  }
234
- ;
235
- const root = this.injector.get(core_1.APP_ROOT);
236
- const observable = new rxjs_1.Observable((sub) => {
237
- let task = new entities_1.FkDownloadTaskEntity();
238
- task.url = url;
239
- const req = (0, request_1.get)(url);
240
- (0, fs_extra_1.ensureDirSync)((0, path_1.join)(root, 'attachments/fk-upload'));
241
- const path = (0, path_1.join)(root, 'attachments/fk-upload', encodeURIComponent(filename));
242
- const writeStream = (0, fs_1.createWriteStream)(path);
243
- task.path = path;
244
- let total = 0;
245
- let length = 0;
246
- task.filename = filename;
247
- task.uploadSize = 0;
248
- req.on('data', (buf) => {
249
- length += buf.length;
250
- task.size = length;
251
- writeStream.write(buf, (err) => {
252
- if (err)
253
- sub.error(err);
254
- });
255
- sub.next(task);
256
- });
257
- req.on('end', () => {
258
- sub.complete();
259
- writeStream.end();
260
- });
261
- req.on('response', (resp) => {
262
- const headers = resp.headers;
263
- total = Number(Reflect.get(headers, 'content-length'));
264
- task.totalSize = total;
265
- task.size = 0;
266
- task.status = 0;
267
- sub.next(task);
268
- });
269
- });
270
- return { listen: observable, filename };
271
44
  }
272
45
  };
273
46
  UploadService = __decorate([
@@ -275,24 +48,3 @@ UploadService = __decorate([
275
48
  __metadata("design:paramtypes", [typeorm_1.Db, core_1.Injector])
276
49
  ], UploadService);
277
50
  exports.UploadService = UploadService;
278
- function decode(base64Str) {
279
- if (!base64Str)
280
- return;
281
- const t = base64Str.replace(/_/g, "/").replace(/-/g, "+");
282
- const f = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
283
- let l = 0;
284
- let p = 0;
285
- let h = [];
286
- do {
287
- const o = f.indexOf(t.charAt(l++));
288
- const u = f.indexOf(t.charAt(l++));
289
- const a = f.indexOf(t.charAt(l++));
290
- const c = f.indexOf(t.charAt(l++));
291
- const s = o << 18 | u << 12 | a << 6 | c;
292
- const e = s >> 16 & 255;
293
- const r = s >> 8 & 255;
294
- const n = 255 & s;
295
- h[p++] = 64 === a ? String.fromCharCode(e) : 64 === c ? String.fromCharCode(e, r) : String.fromCharCode(e, r, n);
296
- } while (l < t.length);
297
- return h.join('');
298
- }
package/docs.md ADDED
@@ -0,0 +1,34 @@
1
+ ## host
2
+
3
+ ### ssh
4
+
5
+ ```ts
6
+ ssh root@111.229.255.176
7
+ Yang1989.
8
+ ```
9
+
10
+ ### manage url
11
+
12
+ ```
13
+ http://111.229.255.176:8081/@nger/fk-upload/help
14
+ ```
15
+
16
+ ### redis
17
+
18
+ ```
19
+ REDIS_HOST=111.229.255.176
20
+ REDIS_PORT=6379
21
+ REDIS_PASS=123qwe
22
+ ```
23
+
24
+ ### rabbit mq
25
+
26
+ http://111.229.255.176:15672/#/
27
+
28
+ ```
29
+ RABBITMQ_HOST=111.229.255.176
30
+ RABBITMQ_PORT=5672
31
+ RABBITMQ_USERNAME=imeepos
32
+ RABBITMQ_PASSWORD=123qwe
33
+ ```
34
+
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nger/fk-upload",
3
- "version": "1.0.8",
3
+ "version": "1.0.11",
4
4
  "description": "",
5
5
  "main": "dist/core.js",
6
6
  "types": "dist/core.d.ts",
@@ -14,7 +14,11 @@
14
14
  "@nger/api": "^1.0.0",
15
15
  "@nger/http": "^4.0.0",
16
16
  "@nger/typeorm": "^1.0.0",
17
+ "@nger/redis": "^1.0.0",
18
+ "@nger/rabbitmq": "^1.0.0",
17
19
  "@nger/ws": "^1.0.0",
20
+ "@nger/schedule": "^1.0.0",
21
+ "@nger/w7": "^1.0.0",
18
22
  "@types/download": "^8.0.1",
19
23
  "@types/request": "^2.48.8",
20
24
  "axios": "^0.26.1",
package/pnpm-lock.yaml CHANGED
@@ -4,7 +4,11 @@ specifiers:
4
4
  '@nger/api': workspace:^1.0.0
5
5
  '@nger/core': workspace:^1.0.0
6
6
  '@nger/http': workspace:^4.0.0
7
+ '@nger/rabbitmq': workspace:^1.0.0
8
+ '@nger/redis': workspace:^1.0.0
9
+ '@nger/schedule': workspace:^1.0.0
7
10
  '@nger/typeorm': workspace:^1.0.0
11
+ '@nger/w7': workspace:^1.0.0
8
12
  '@nger/ws': workspace:^1.0.0
9
13
  '@types/download': ^8.0.1
10
14
  '@types/request': ^2.48.8
@@ -21,7 +25,11 @@ dependencies:
21
25
  '@nger/api': link:../api
22
26
  '@nger/core': link:../core
23
27
  '@nger/http': link:../http
28
+ '@nger/rabbitmq': link:../rabbitmq
29
+ '@nger/redis': link:../redis
30
+ '@nger/schedule': link:../schedule
24
31
  '@nger/typeorm': link:../typeorm
32
+ '@nger/w7': link:../w7
25
33
  '@nger/ws': link:../ws
26
34
  '@types/download': 8.0.1
27
35
  '@types/request': 2.48.8