@nger/fk-upload 1.0.123 → 1.0.126

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.
@@ -7,7 +7,6 @@ const typeorm_1 = require("@nger/typeorm");
7
7
  const w7_1 = require("@nger/w7");
8
8
  const entities_1 = require("../../entities");
9
9
  const fs_extra_1 = require("fs-extra");
10
- const task_service_1 = require("./task.service");
11
10
  class EffectTask extends rabbitmq_1.Task {
12
11
  constructor() {
13
12
  super(actions_1.EFFECT_ACTION);
@@ -17,31 +16,44 @@ class EffectTask extends rabbitmq_1.Task {
17
16
  const task = injector.get(rabbitmq_1.DATA);
18
17
  const complete = injector.get(rabbitmq_1.COMPLETE);
19
18
  const manager = injector.get(rabbitmq_1.TaskManager);
20
- const taskService = injector.get(task_service_1.TaskService);
21
19
  console.log(`effect`, task.filename);
22
- if (task.uploadUrl.startsWith('https://site')) {
23
- // redownload
24
- const item = await db.manager.findOne(entities_1.FkDownloadTaskEntity, { where: { filename: task.filename } });
25
- if (item) {
26
- const downloadTask = await taskService.createDownLoadTask(item.url, item.loginId, item.topicId);
27
- await manager.send(downloadTask);
20
+ try {
21
+ if (task.uploadUrl.startsWith('https://site')) {
22
+ // redownload
23
+ const item = await db.manager.findOne(entities_1.FkDownloadTaskEntity, { where: { filename: task.filename } });
24
+ if (item) {
25
+ const download = {
26
+ filename: item.filename,
27
+ path: item.path,
28
+ url: item.url,
29
+ topicId: item.topicId,
30
+ loginId: item.loginId
31
+ };
32
+ const downloadTask = new rabbitmq_1.TaskEntity();
33
+ downloadTask.topic = actions_1.DOWNLOAD_ACTION;
34
+ downloadTask.data = download;
35
+ await manager.send(downloadTask);
36
+ }
37
+ await complete();
38
+ return next && next();
28
39
  }
29
- await complete();
30
- return next && next();
31
- }
32
- const fail = injector.get(rabbitmq_1.FAIL);
33
- const w7 = injector.get(w7_1.W7DataSource);
34
- await db.manager.update(entities_1.FkDownloadTaskEntity, task.filename, { status: 3 });
35
- if (task.topicId) {
36
- try {
37
- await (0, fs_extra_1.unlink)(task.path);
40
+ const fail = injector.get(rabbitmq_1.FAIL);
41
+ const w7 = injector.get(w7_1.W7DataSource);
42
+ await db.manager.update(entities_1.FkDownloadTaskEntity, task.filename, { status: 3 });
43
+ if (task.topicId) {
44
+ try {
45
+ await (0, fs_extra_1.unlink)(task.path);
46
+ }
47
+ catch (e) { }
48
+ await w7.manager.update(w7_1.W7ChatTopicEntity, task.topicId, { foreverUrl: task.uploadUrl }).then(() => complete()).catch(() => fail());
49
+ await complete();
50
+ }
51
+ else {
52
+ await complete();
38
53
  }
39
- catch (e) { }
40
- await w7.manager.update(w7_1.W7ChatTopicEntity, task.topicId, { foreverUrl: task.uploadUrl }).then(() => complete()).catch(() => fail());
41
- await complete();
42
54
  }
43
- else {
44
- await complete();
55
+ catch (e) {
56
+ console.log(`effect error`, e.message);
45
57
  }
46
58
  if (next)
47
59
  await next();
@@ -125,72 +125,83 @@ let FkService = class FkService {
125
125
  return size;
126
126
  }
127
127
  async uploadFile(task) {
128
- let complete = false;
129
- const max = task.total - 1;
130
- let index = task.index || 0;
131
- do {
132
- let start = index * task.splitSize;
133
- let _end = start + task.splitSize - 1;
134
- let end = _end > max ? max : _end;
135
- if (end >= max) {
136
- complete = true;
137
- }
138
- const fileStream = (0, fs_extra_1.createReadStream)(task.path, { start: start, end: end });
139
- const formdata = new form_data_1.default();
140
- formdata.append('ctrl', fileStream);
141
- formdata.append('isFreeVer', this.encodeURIComponent(false));
142
- formdata.append('aid', this.encodeURIComponent(task.aid));
143
- formdata.append('folderId', this.encodeURIComponent(task.folderId));
144
- formdata.append('fileName', task.filename);
145
- formdata.append(`totalSize`, this.encodeURIComponent(task.total));
146
- formdata.append(`fileMd5`, task.fileMd5);
147
- formdata.append('index', this.encodeURIComponent(index));
148
- formdata.append('chunkSize', this.encodeURIComponent(task.splitSize));
149
- formdata.append('totalChunks', this.encodeURIComponent(task.totalChunks));
150
- formdata.append(`complete`, this.encodeURIComponent(complete));
151
- const bssInfo = {
152
- fromSite: true, siteId: 0, groupId: 0, fileSizeLimit: 1024
153
- };
154
- formdata.append('bssInfo', JSON.stringify(bssInfo));
155
- const headers = formdata.getHeaders();
156
- const uploadResult = await axios_1.default.post(task.uploadUrl, formdata, {
157
- headers: {
158
- ...headers,
159
- Cookie: task.cookies
160
- },
161
- timeout: 8000
162
- }).then(res => {
163
- return res.data;
164
- }).catch(async (e) => {
165
- throw e;
166
- });
167
- if (uploadResult.code !== 200) {
168
- if (uploadResult.data) {
169
- const data = uploadResult.data;
170
- if (data.limit) {
171
- // to big
172
- throw new Error(`to big`);
128
+ const upload = async () => {
129
+ const info = await this.getUploadInfo(task.loginId, task.filename, task.fileMd5, task.total);
130
+ task.cookies = info.cookies;
131
+ task.splitSize = info.splitSize;
132
+ task.start = info.uploadedSize;
133
+ task.uploadUrl = info.uploadUrl;
134
+ task.index = Math.ceil(info.uploadedSize / info.splitSize);
135
+ let complete = false;
136
+ const max = task.total - 1;
137
+ let index = task.index || 0;
138
+ let uploadResult;
139
+ do {
140
+ let start = index * task.splitSize;
141
+ let _end = start + task.splitSize - 1;
142
+ let end = _end > max ? max : _end;
143
+ if (end >= max) {
144
+ complete = true;
145
+ }
146
+ const fileStream = (0, fs_extra_1.createReadStream)(task.path, { start: start, end: end });
147
+ const formdata = new form_data_1.default();
148
+ formdata.append('ctrl', fileStream);
149
+ formdata.append('isFreeVer', this.encodeURIComponent(false));
150
+ formdata.append('aid', this.encodeURIComponent(task.aid));
151
+ formdata.append('folderId', this.encodeURIComponent(task.folderId));
152
+ formdata.append('fileName', task.filename);
153
+ formdata.append(`totalSize`, this.encodeURIComponent(task.total));
154
+ formdata.append(`fileMd5`, task.fileMd5);
155
+ formdata.append('index', this.encodeURIComponent(index));
156
+ formdata.append('chunkSize', this.encodeURIComponent(task.splitSize));
157
+ formdata.append('totalChunks', this.encodeURIComponent(task.totalChunks));
158
+ formdata.append(`complete`, this.encodeURIComponent(complete));
159
+ const bssInfo = {
160
+ fromSite: true, siteId: 0, groupId: 0, fileSizeLimit: 1024
161
+ };
162
+ formdata.append('bssInfo', JSON.stringify(bssInfo));
163
+ const headers = formdata.getHeaders();
164
+ uploadResult = await axios_1.default.post(task.uploadUrl, formdata, {
165
+ headers: {
166
+ ...headers,
167
+ Cookie: task.cookies
168
+ },
169
+ timeout: 8000
170
+ }).then(res => {
171
+ return res.data;
172
+ }).catch((e) => {
173
+ console.log(`upload error`, e.message);
174
+ return upload();
175
+ });
176
+ if (uploadResult.code !== 200) {
177
+ if (uploadResult.data) {
178
+ const data = uploadResult.data;
179
+ if (data.limit) {
180
+ // to big
181
+ throw new Error(`to big`);
182
+ }
173
183
  }
184
+ throw new Error(`${task.filename}--${uploadResult.msg}`);
174
185
  }
175
- throw new Error(`${task.filename}--${uploadResult.msg}`);
176
- }
177
- console.log(`upload ----- `, { index, max, start, end });
178
- if (uploadResult) {
179
- const data = uploadResult.data;
180
- const downUrl = data.path;
181
- if (downUrl) {
182
- if (downUrl.startsWith('https://site-upload')) {
183
- console.log(`upload fail`);
184
- throw new Error(`need re bind`);
186
+ console.log(`upload ----- `, { index, max, start, end });
187
+ if (uploadResult) {
188
+ const data = uploadResult.data;
189
+ const downUrl = data.path;
190
+ if (downUrl) {
191
+ if (downUrl.startsWith('https://site-upload')) {
192
+ console.log(`upload fail`);
193
+ throw new Error(`need re bind`);
194
+ }
195
+ const fullUrl = downUrl.startsWith('http') ? downUrl : `https:` + downUrl;
196
+ task.uploadUrl = fullUrl;
185
197
  }
186
- const fullUrl = downUrl.startsWith('http') ? downUrl : `https:` + downUrl;
187
- task.uploadUrl = fullUrl;
188
198
  }
189
- }
190
- index = index + 1;
191
- } while (!complete);
192
- console.log(`uploading ${task.filename} ${task.uploadUrl}`);
193
- index = 0;
199
+ index = index + 1;
200
+ } while (!complete);
201
+ return uploadResult;
202
+ };
203
+ const result = await upload();
204
+ console.log(`upload result: -----> `, result);
194
205
  // upload complete
195
206
  axios_1.default.post(`https://smr00.vip.webportal.top/ajax/advanceUpload.jsp?cmd=_report&_TOKEN=${task.token}`, this.encodeURIComponent({
196
207
  type: 1,
@@ -67,7 +67,6 @@ class UploadTask extends rabbitmq_1.Task {
67
67
  }
68
68
  if (this.errorCount > 10) {
69
69
  this.errorCount = 0;
70
- return fail();
71
70
  }
72
71
  this.errorCount += 1;
73
72
  console.log(`upload error ${this.errorCount}`, e.message);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nger/fk-upload",
3
- "version": "1.0.123",
3
+ "version": "1.0.126",
4
4
  "description": "",
5
5
  "main": "dist/core.js",
6
6
  "types": "dist/core.d.ts",