@nger/fk-upload 1.0.125 → 1.0.128

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.
@@ -125,73 +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((e) => {
165
- console.log(`upload error`, e.message);
166
- return this.startUploadFile(task);
167
- });
168
- if (uploadResult.code !== 200) {
169
- if (uploadResult.data) {
170
- const data = uploadResult.data;
171
- if (data.limit) {
172
- // to big
173
- 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
+ }
174
183
  }
184
+ throw new Error(`${task.filename}--${uploadResult.msg}`);
175
185
  }
176
- throw new Error(`${task.filename}--${uploadResult.msg}`);
177
- }
178
- console.log(`upload ----- `, { index, max, start, end });
179
- if (uploadResult) {
180
- const data = uploadResult.data;
181
- const downUrl = data.path;
182
- if (downUrl) {
183
- if (downUrl.startsWith('https://site-upload')) {
184
- console.log(`upload fail`);
185
- 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;
186
197
  }
187
- const fullUrl = downUrl.startsWith('http') ? downUrl : `https:` + downUrl;
188
- task.uploadUrl = fullUrl;
189
198
  }
190
- }
191
- index = index + 1;
192
- } while (!complete);
193
- console.log(`uploading ${task.filename} ${task.uploadUrl}`);
194
- index = 0;
199
+ index = index + 1;
200
+ } while (!complete);
201
+ return uploadResult;
202
+ };
203
+ const result = await upload();
204
+ console.log(`upload result: -----> `, result);
195
205
  // upload complete
196
206
  axios_1.default.post(`https://smr00.vip.webportal.top/ajax/advanceUpload.jsp?cmd=_report&_TOKEN=${task.token}`, this.encodeURIComponent({
197
207
  type: 1,
@@ -62,14 +62,15 @@ class UploadTask extends rabbitmq_1.Task {
62
62
  await manager.send(effectTask);
63
63
  await complete();
64
64
  }).catch(async (e) => {
65
+ console.log(`upload error ${this.errorCount}`, e.message);
65
66
  if (e.message === 'to big') {
67
+ await db.manager.update(entities_1.FkDownloadTaskEntity, task.filename, { isBigFile: true });
66
68
  return complete();
67
69
  }
68
70
  if (this.errorCount > 10) {
69
71
  this.errorCount = 0;
70
72
  }
71
73
  this.errorCount += 1;
72
- console.log(`upload error ${this.errorCount}`, e.message);
73
74
  throw e;
74
75
  });
75
76
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nger/fk-upload",
3
- "version": "1.0.125",
3
+ "version": "1.0.128",
4
4
  "description": "",
5
5
  "main": "dist/core.js",
6
6
  "types": "dist/core.d.ts",