w-converhp 2.0.4 → 2.0.5

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.
@@ -80,7 +80,7 @@
80
80
 
81
81
  <dt class="tag-source">Source:</dt>
82
82
  <dd class="tag-source"><ul class="dummy"><li>
83
- <a href="WConverhpServer.mjs.html">WConverhpServer.mjs</a>, <a href="WConverhpServer.mjs.html#line119">line 119</a>
83
+ <a href="WConverhpServer.mjs.html">WConverhpServer.mjs</a>, <a href="WConverhpServer.mjs.html#line117">line 117</a>
84
84
  </li></ul></dd>
85
85
 
86
86
 
@@ -127,7 +127,7 @@
127
127
 
128
128
  <h5 class="h5-examples">Example</h5>
129
129
 
130
- <pre class="prettyprint"><code>import _ from 'lodash-es'
131
130
  port: 8080,
132
131
  apiName: 'api',
133
132
  pathStaticFiles: '.', //要存取專案資料夾下web.html, 故不能給dist
134
133
  funCheck: () => {
135
134
  return true
136
135
  },
137
136
  // console.log(`Server[port:${opt.port}]: execute`, func, input)
138
137
  console.log(`Server[port:${opt.port}]: execute`, func)
139
138
  try {
140
139
  if (func === 'add') {
141
140
  if (_.get(input, 'p.d.u8a', null)) {
142
141
  console.log('input.p.d.u8a', input.p.d.u8a)
143
142
  }
144
143
  let r = {
145
144
  _add: input.p.a + input.p.b,
146
145
  _data: [11, 22.22, 'abc', { x: '21', y: 65.43, z: 'test中文' }],
147
146
  _bin: {
148
147
  name: 'zdata.b2',
149
148
  u8a: new Uint8Array([66, 97, 115]),
150
149
  // name: '100mb.7z',
151
150
  // u8a: new Uint8Array(fs.readFileSync('D:\\開源-JS-006-2-w-converhp\\_temp\\100mb.7z')),
152
151
  // name: '500mb.7z',
153
152
  // u8a: new Uint8Array(fs.readFileSync('D:\\開源-JS-006-2-w-converhp\\_temp\\500mb.7z')),
154
153
  // name: '1000mb.7z',
155
154
  // u8a: new Uint8Array(fs.readFileSync('D:\\開源-JS-006-2-w-converhp\\_temp\\1000mb.7z')),
156
155
  },
157
156
  }
158
157
  pm.resolve(r)
159
158
  }
160
159
  else {
161
160
  console.log('invalid func')
162
161
  pm.reject('invalid func')
163
162
  }
164
163
  }
165
164
  catch (err) {
166
165
  console.log('execute error', err)
167
166
  pm.reject('execute error')
168
167
  }
169
168
  console.log(`Server[port:${opt.port}]: upload`, input)
170
169
  try {
171
170
  let output = input
172
171
  pm.resolve(output)
173
172
  }
174
173
  catch (err) {
175
174
  console.log('execute error', err)
176
175
  pm.reject('execute error')
177
176
  }
178
177
  // console.log(`Server[port:${opt.port}]: handler`, data)
178
+ <pre class="prettyprint"><code>import _ from 'lodash-es'
179
179
  port: 8080,
180
180
  apiName: 'api',
181
181
  pathStaticFiles: '.', //要存取專案資料夾下web.html, 故不能給dist
182
182
  funCheck: () => {
183
183
  return true
184
184
  },
185
185
  // console.log(`Server[port:${opt.port}]: execute`, func, input)
186
186
  console.log(`Server[port:${opt.port}]: execute`, func)
187
187
  try {
188
188
  if (func === 'add') {
189
189
  if (_.get(input, 'p.d.u8a', null)) {
190
190
  console.log('input.p.d.u8a', input.p.d.u8a)
191
191
  }
192
192
  let r = {
193
193
  _add: input.p.a + input.p.b,
194
194
  _data: [11, 22.22, 'abc', { x: '21', y: 65.43, z: 'test中文' }],
195
195
  _bin: {
196
196
  name: 'zdata.b2',
197
197
  u8a: new Uint8Array([66, 97, 115]),
198
198
  // name: '100mb.7z',
199
199
  // u8a: new Uint8Array(fs.readFileSync('D:\\開源-JS-006-2-w-converhp\\_temp\\100mb.7z')),
200
200
  // name: '500mb.7z',
201
201
  // u8a: new Uint8Array(fs.readFileSync('D:\\開源-JS-006-2-w-converhp\\_temp\\500mb.7z')),
202
202
  // name: '1000mb.7z',
203
203
  // u8a: new Uint8Array(fs.readFileSync('D:\\開源-JS-006-2-w-converhp\\_temp\\1000mb.7z')),
204
204
  },
205
205
  }
206
206
  pm.resolve(r)
207
207
  }
208
208
  else {
209
209
  console.log('invalid func')
210
210
  pm.reject('invalid func')
211
211
  }
212
212
  }
213
213
  catch (err) {
214
214
  console.log('execute error', err)
215
215
  pm.reject('execute error')
216
216
  }
217
217
  console.log(`Server[port:${opt.port}]: upload`, input)
218
218
  try {
219
219
  let output = input
220
220
  pm.resolve(output)
221
221
  }
222
222
  catch (err) {
223
223
  console.log('upload error', err)
224
224
  pm.reject('upload error')
225
225
  }
226
226
  // console.log(`Server[port:${opt.port}]: handler`, data)
227
227
 
228
228
 
229
229
 
@@ -709,7 +709,7 @@
709
709
  <br class="clear">
710
710
 
711
711
  <footer>
712
- Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.2</a> on Sun Mar 30 2025 21:48:05 GMT+0800 (台北標準時間) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
712
+ Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.2</a> on Mon Mar 31 2025 23:59:37 GMT+0800 (台北標準時間) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
713
713
  </footer>
714
714
 
715
715
  <script>prettyPrint();</script>
@@ -45,11 +45,10 @@
45
45
 
46
46
  <section>
47
47
  <article>
48
- <pre class="prettyprint source linenums"><code>import path from 'path'
49
- import fs from 'fs'
48
+ <pre class="prettyprint source linenums"><code>import fs from 'fs'
49
+ import stream from 'stream'
50
50
  import Hapi from '@hapi/hapi'
51
51
  import Inert from '@hapi/inert' //提供靜態檔案
52
- import stream from 'stream'
53
52
  import get from 'lodash-es/get.js'
54
53
  import genPm from 'wsemi/src/genPm.mjs'
55
54
  import iseobj from 'wsemi/src/iseobj.mjs'
@@ -66,8 +65,7 @@ import obj2u8arr from 'wsemi/src/obj2u8arr.mjs'
66
65
  import u8arr2obj from 'wsemi/src/u8arr2obj.mjs'
67
66
  import fsIsFolder from 'wsemi/src/fsIsFolder.mjs'
68
67
  import fsCreateFolder from 'wsemi/src/fsCreateFolder.mjs'
69
- import fsIsFile from 'wsemi/src/fsIsFile.mjs'
70
- import fsDeleteFile from 'wsemi/src/fsDeleteFile.mjs'
68
+ import mergeFiles from './mergeFiles.wk.umd.js'
71
69
 
72
70
 
73
71
  /**
@@ -103,7 +101,7 @@ import fsDeleteFile from 'wsemi/src/fsDeleteFile.mjs'
103
101
  * //new
104
102
  * let wo = new WConverhpServer(opt)
105
103
  *
106
- * wo.on('execute', function(func, input, pm) {
104
+ * wo.on('execute', (func, input, pm) => {
107
105
  * // console.log(`Server[port:${opt.port}]: execute`, func, input)
108
106
  * console.log(`Server[port:${opt.port}]: execute`, func)
109
107
  *
@@ -145,7 +143,7 @@ import fsDeleteFile from 'wsemi/src/fsDeleteFile.mjs'
145
143
  * }
146
144
  *
147
145
  * })
148
- * wo.on('upload', function(input, pm) {
146
+ * wo.on('upload', (input, pm) => {
149
147
  * console.log(`Server[port:${opt.port}]: upload`, input)
150
148
  *
151
149
  * try {
@@ -153,12 +151,12 @@ import fsDeleteFile from 'wsemi/src/fsDeleteFile.mjs'
153
151
  * pm.resolve(output)
154
152
  * }
155
153
  * catch (err) {
156
- * console.log('execute error', err)
157
- * pm.reject('execute error')
154
+ * console.log('upload error', err)
155
+ * pm.reject('upload error')
158
156
  * }
159
157
  *
160
158
  * })
161
- * wo.on('handler', function(data) {
159
+ * wo.on('handler', (data) => {
162
160
  * // console.log(`Server[port:${opt.port}]: handler`, data)
163
161
  * })
164
162
  *
@@ -244,6 +242,10 @@ function WConverhpServer(opt = {}) {
244
242
  //host: 'localhost',
245
243
  port,
246
244
  routes: {
245
+ timeout: {
246
+ server: false, //關閉伺服器超時
247
+ socket: false, //關閉socket超時
248
+ },
247
249
  cors: {
248
250
  origin: corsOrigins, //Access-Control-Allow-Origin
249
251
  credentials: false, //Access-Control-Allow-Credentials
@@ -360,8 +362,8 @@ function WConverhpServer(opt = {}) {
360
362
  parse: false,
361
363
  },
362
364
  timeout: {
363
- socket: false, //避免socket自動關閉
364
- server: false,
365
+ server: false, //關閉伺服器超時
366
+ socket: false, //關閉socket超時
365
367
  },
366
368
  },
367
369
  handler: async function (req, res) {
@@ -518,8 +520,8 @@ function WConverhpServer(opt = {}) {
518
520
  parse: false,
519
521
  },
520
522
  timeout: {
521
- socket: false, //避免socket自動關閉
522
- server: false,
523
+ server: false, //關閉伺服器超時
524
+ socket: false, //關閉socket超時
523
525
  },
524
526
  },
525
527
  handler: async function (req, res) {
@@ -664,8 +666,8 @@ function WConverhpServer(opt = {}) {
664
666
  parse: true, //前端送obj過來
665
667
  },
666
668
  timeout: {
667
- socket: false, //避免socket自動關閉
668
- server: false,
669
+ server: false, //關閉伺服器超時
670
+ socket: false, //關閉socket超時
669
671
  },
670
672
  },
671
673
  handler: async function (req, res) {
@@ -739,78 +741,13 @@ function WConverhpServer(opt = {}) {
739
741
  return res.response({ error: 'invalid packageId in payload' }).code(400)
740
742
  }
741
743
 
742
- //merge
743
- let merge = async () => {
744
- let errTemp = ''
745
-
746
- //pathFileMerge
747
- let pathFileMerge = path.join(pathUploadTemp, `${packageId}`)
748
- // console.log('pathFileMerge', pathFileMerge)
749
-
750
- //fileStream
751
- let fileStream = fs.createWriteStream(pathFileMerge)
752
- // console.log(`merge filename[${filename}] start`)
753
-
754
- //使用try catch攔截,使stream能完整創造與結束
755
- try {
756
- for (let i = 0; i &lt; chunkTotal; i++) {
757
- // console.log(`merge ${i + 1}/${chunkTotal}`)
758
-
759
- //chunkIndex
760
- let chunkIndex = i
761
-
762
- //pathFileChunk
763
- let pathFileChunk = path.join(pathUploadTemp, `${packageId}_${chunkIndex}`)
764
-
765
- //check
766
- if (!fsIsFile(pathFileChunk)) {
767
- // console.log(`pathFileChunk[${pathFileChunk}] is not a file`)
768
- throw new Error(`Missing chunk ${i} of filename[${filename}]`)
769
- }
770
-
771
- // console.log(`merging chunk[${chunkIndex + 1}/${chunkTotal}]...`)
772
-
773
- //chunkData
774
- let chunkData = fs.readFileSync(pathFileChunk)
775
-
776
- //write
777
- fileStream.write(chunkData)
778
-
779
- //fsDeleteFile
780
- fsDeleteFile(pathFileChunk)
781
-
782
- // console.log(`merge chunk[${chunkIndex + 1}/${chunkTotal}] done`)
783
- }
784
- }
785
- catch (err) {
786
- errTemp = err.message
787
- }
788
-
789
- //end
790
- fileStream.end()
791
- // console.log(`merge filename[${filename}] done`)
792
-
793
- //check
794
- if (isestr(errTemp)) {
795
- return Promise.reject(errTemp)
796
- }
797
-
798
- //r
799
- let r = {
800
- // message: `Merged filename[${filename}] successfully`,
801
- filename,
802
- path: pathFileMerge,
803
- }
804
-
805
- return r
806
- }
807
-
808
744
  //core
809
745
  let core = async() => {
810
746
 
811
747
  //merge
812
748
  // console.log('merge start')
813
- let r = await merge()
749
+ // let r = await merge()
750
+ let r = await mergeFiles(pathUploadTemp, packageId, chunkTotal, filename)
814
751
  // console.log('merge done', r)
815
752
 
816
753
  //procUpload
@@ -886,7 +823,8 @@ function WConverhpServer(opt = {}) {
886
823
 
887
824
  //start
888
825
  if (get(opt, 'serverHapi')) {
889
- opt.serverHapi.route([apiMain, apiSlice, apiSliceMerge])
826
+ // opt.serverHapi.route([apiMain, apiSlice, apiSliceMerge])
827
+ server.route([apiMain, apiSlice, apiSliceMerge])
890
828
  }
891
829
  else {
892
830
  startServer()
@@ -911,7 +849,7 @@ export default WConverhpServer
911
849
  <br class="clear">
912
850
 
913
851
  <footer>
914
- Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.2</a> on Sun Mar 30 2025 21:48:05 GMT+0800 (台北標準時間) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
852
+ Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.2</a> on Mon Mar 31 2025 23:59:37 GMT+0800 (台北標準時間) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
915
853
  </footer>
916
854
 
917
855
  <script>prettyPrint();</script>
package/docs/index.html CHANGED
@@ -71,7 +71,7 @@
71
71
  <br class="clear">
72
72
 
73
73
  <footer>
74
- Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.2</a> on Sun Mar 30 2025 21:48:05 GMT+0800 (台北標準時間) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
74
+ Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.2</a> on Mon Mar 31 2025 23:59:37 GMT+0800 (台北標準時間) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
75
75
  </footer>
76
76
 
77
77
  <script>prettyPrint();</script>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "w-converhp",
3
- "version": "2.0.4",
3
+ "version": "2.0.5",
4
4
  "main": "dist/w-converhp-server.umd.js",
5
5
  "dependencies": {
6
6
  "@hapi/hapi": "^21.4.0",
@@ -11,7 +11,7 @@
11
11
  },
12
12
  "devDependencies": {
13
13
  "form-data": "^4.0.2",
14
- "w-package-tools": "^1.0.82"
14
+ "w-package-tools": "^1.0.85"
15
15
  },
16
16
  "scripts": {
17
17
  "@hapi/hapi": "^20.2.1",
package/scla.mjs CHANGED
@@ -16,7 +16,7 @@ let opt = {
16
16
  //new
17
17
  let wo = new WConverhpClient(opt)
18
18
 
19
- wo.on('error', function(err) {
19
+ wo.on('error', (err) => {
20
20
  console.log(`error`, err)
21
21
  })
22
22
 
package/sclb.mjs CHANGED
@@ -16,7 +16,7 @@ let opt = {
16
16
  //new
17
17
  let wo = new WConverhpClient(opt)
18
18
 
19
- wo.on('error', function(err) {
19
+ wo.on('error', (err) => {
20
20
  console.log(`error`, err)
21
21
  })
22
22
 
package/sclc.mjs CHANGED
@@ -16,7 +16,7 @@ let opt = {
16
16
  //new
17
17
  let wo = new WConverhpClient(opt)
18
18
 
19
- wo.on('error', function(err) {
19
+ wo.on('error', (err) => {
20
20
  console.log(`error`, err)
21
21
  })
22
22
 
@@ -542,8 +542,8 @@ function WConverhpClient(opt) {
542
542
  catch (err) {}
543
543
  }
544
544
  if (n === 0) {
545
- eeEmit('error', `Can not get size of bb`)
546
- return Promise.reject(`Can not get size of bb`)
545
+ eeEmit('error', `can not get size of bb`)
546
+ return Promise.reject(`can not get size of bb`)
547
547
  }
548
548
  // console.log('n', n)
549
549
 
@@ -609,7 +609,7 @@ function WConverhpClient(opt) {
609
609
  }
610
610
 
611
611
  //send merge
612
- console.log(`merging tempId[${tempId}]...`)
612
+ // console.log(`merging tempId[${tempId}]...`)
613
613
  let msg = {
614
614
  'filename': tempId,
615
615
  'chunk-total': chunkTotal,
@@ -1,8 +1,7 @@
1
- import path from 'path'
2
1
  import fs from 'fs'
2
+ import stream from 'stream'
3
3
  import Hapi from '@hapi/hapi'
4
4
  import Inert from '@hapi/inert' //提供靜態檔案
5
- import stream from 'stream'
6
5
  import get from 'lodash-es/get.js'
7
6
  import genPm from 'wsemi/src/genPm.mjs'
8
7
  import iseobj from 'wsemi/src/iseobj.mjs'
@@ -19,8 +18,7 @@ import obj2u8arr from 'wsemi/src/obj2u8arr.mjs'
19
18
  import u8arr2obj from 'wsemi/src/u8arr2obj.mjs'
20
19
  import fsIsFolder from 'wsemi/src/fsIsFolder.mjs'
21
20
  import fsCreateFolder from 'wsemi/src/fsCreateFolder.mjs'
22
- import fsIsFile from 'wsemi/src/fsIsFile.mjs'
23
- import fsDeleteFile from 'wsemi/src/fsDeleteFile.mjs'
21
+ import mergeFiles from './mergeFiles.wk.umd.js'
24
22
 
25
23
 
26
24
  /**
@@ -56,7 +54,7 @@ import fsDeleteFile from 'wsemi/src/fsDeleteFile.mjs'
56
54
  * //new
57
55
  * let wo = new WConverhpServer(opt)
58
56
  *
59
- * wo.on('execute', function(func, input, pm) {
57
+ * wo.on('execute', (func, input, pm) => {
60
58
  * // console.log(`Server[port:${opt.port}]: execute`, func, input)
61
59
  * console.log(`Server[port:${opt.port}]: execute`, func)
62
60
  *
@@ -98,7 +96,7 @@ import fsDeleteFile from 'wsemi/src/fsDeleteFile.mjs'
98
96
  * }
99
97
  *
100
98
  * })
101
- * wo.on('upload', function(input, pm) {
99
+ * wo.on('upload', (input, pm) => {
102
100
  * console.log(`Server[port:${opt.port}]: upload`, input)
103
101
  *
104
102
  * try {
@@ -106,12 +104,12 @@ import fsDeleteFile from 'wsemi/src/fsDeleteFile.mjs'
106
104
  * pm.resolve(output)
107
105
  * }
108
106
  * catch (err) {
109
- * console.log('execute error', err)
110
- * pm.reject('execute error')
107
+ * console.log('upload error', err)
108
+ * pm.reject('upload error')
111
109
  * }
112
110
  *
113
111
  * })
114
- * wo.on('handler', function(data) {
112
+ * wo.on('handler', (data) => {
115
113
  * // console.log(`Server[port:${opt.port}]: handler`, data)
116
114
  * })
117
115
  *
@@ -197,6 +195,10 @@ function WConverhpServer(opt = {}) {
197
195
  //host: 'localhost',
198
196
  port,
199
197
  routes: {
198
+ timeout: {
199
+ server: false, //關閉伺服器超時
200
+ socket: false, //關閉socket超時
201
+ },
200
202
  cors: {
201
203
  origin: corsOrigins, //Access-Control-Allow-Origin
202
204
  credentials: false, //Access-Control-Allow-Credentials
@@ -313,8 +315,8 @@ function WConverhpServer(opt = {}) {
313
315
  parse: false,
314
316
  },
315
317
  timeout: {
316
- socket: false, //避免socket自動關閉
317
- server: false,
318
+ server: false, //關閉伺服器超時
319
+ socket: false, //關閉socket超時
318
320
  },
319
321
  },
320
322
  handler: async function (req, res) {
@@ -471,8 +473,8 @@ function WConverhpServer(opt = {}) {
471
473
  parse: false,
472
474
  },
473
475
  timeout: {
474
- socket: false, //避免socket自動關閉
475
- server: false,
476
+ server: false, //關閉伺服器超時
477
+ socket: false, //關閉socket超時
476
478
  },
477
479
  },
478
480
  handler: async function (req, res) {
@@ -617,8 +619,8 @@ function WConverhpServer(opt = {}) {
617
619
  parse: true, //前端送obj過來
618
620
  },
619
621
  timeout: {
620
- socket: false, //避免socket自動關閉
621
- server: false,
622
+ server: false, //關閉伺服器超時
623
+ socket: false, //關閉socket超時
622
624
  },
623
625
  },
624
626
  handler: async function (req, res) {
@@ -692,78 +694,13 @@ function WConverhpServer(opt = {}) {
692
694
  return res.response({ error: 'invalid packageId in payload' }).code(400)
693
695
  }
694
696
 
695
- //merge
696
- let merge = async () => {
697
- let errTemp = ''
698
-
699
- //pathFileMerge
700
- let pathFileMerge = path.join(pathUploadTemp, `${packageId}`)
701
- // console.log('pathFileMerge', pathFileMerge)
702
-
703
- //fileStream
704
- let fileStream = fs.createWriteStream(pathFileMerge)
705
- // console.log(`merge filename[${filename}] start`)
706
-
707
- //使用try catch攔截,使stream能完整創造與結束
708
- try {
709
- for (let i = 0; i < chunkTotal; i++) {
710
- // console.log(`merge ${i + 1}/${chunkTotal}`)
711
-
712
- //chunkIndex
713
- let chunkIndex = i
714
-
715
- //pathFileChunk
716
- let pathFileChunk = path.join(pathUploadTemp, `${packageId}_${chunkIndex}`)
717
-
718
- //check
719
- if (!fsIsFile(pathFileChunk)) {
720
- // console.log(`pathFileChunk[${pathFileChunk}] is not a file`)
721
- throw new Error(`Missing chunk ${i} of filename[${filename}]`)
722
- }
723
-
724
- // console.log(`merging chunk[${chunkIndex + 1}/${chunkTotal}]...`)
725
-
726
- //chunkData
727
- let chunkData = fs.readFileSync(pathFileChunk)
728
-
729
- //write
730
- fileStream.write(chunkData)
731
-
732
- //fsDeleteFile
733
- fsDeleteFile(pathFileChunk)
734
-
735
- // console.log(`merge chunk[${chunkIndex + 1}/${chunkTotal}] done`)
736
- }
737
- }
738
- catch (err) {
739
- errTemp = err.message
740
- }
741
-
742
- //end
743
- fileStream.end()
744
- // console.log(`merge filename[${filename}] done`)
745
-
746
- //check
747
- if (isestr(errTemp)) {
748
- return Promise.reject(errTemp)
749
- }
750
-
751
- //r
752
- let r = {
753
- // message: `Merged filename[${filename}] successfully`,
754
- filename,
755
- path: pathFileMerge,
756
- }
757
-
758
- return r
759
- }
760
-
761
697
  //core
762
698
  let core = async() => {
763
699
 
764
700
  //merge
765
701
  // console.log('merge start')
766
- let r = await merge()
702
+ // let r = await merge()
703
+ let r = await mergeFiles(pathUploadTemp, packageId, chunkTotal, filename)
767
704
  // console.log('merge done', r)
768
705
 
769
706
  //procUpload
@@ -839,7 +776,8 @@ function WConverhpServer(opt = {}) {
839
776
 
840
777
  //start
841
778
  if (get(opt, 'serverHapi')) {
842
- opt.serverHapi.route([apiMain, apiSlice, apiSliceMerge])
779
+ // opt.serverHapi.route([apiMain, apiSlice, apiSliceMerge])
780
+ server.route([apiMain, apiSlice, apiSliceMerge])
843
781
  }
844
782
  else {
845
783
  startServer()
@@ -0,0 +1,73 @@
1
+ import path from 'path' //native lib要用動態import加載
2
+ import fs from 'fs' //native lib要用動態import加載
3
+ import isestr from 'wsemi/src/isestr.mjs'
4
+ import fsIsFile from 'wsemi/src/fsIsFile.mjs'
5
+ import fsDeleteFile from 'wsemi/src/fsDeleteFile.mjs'
6
+
7
+
8
+ let mergeFiles = async (pathUploadTemp, packageId, chunkTotal, filename) => {
9
+ let errTemp = ''
10
+
11
+ //pathFileMerge
12
+ let pathFileMerge = path.join(pathUploadTemp, packageId)
13
+ // console.log('pathFileMerge', pathFileMerge)
14
+
15
+ //fileStream
16
+ let fileStream = fs.createWriteStream(pathFileMerge)
17
+ // console.log(`merge filename[${filename}] start`)
18
+
19
+ //使用try catch攔截,使stream能完整創造與結束
20
+ try {
21
+ for (let i = 0; i < chunkTotal; i++) {
22
+ // console.log(`merge ${i + 1}/${chunkTotal}`)
23
+
24
+ //chunkIndex
25
+ let chunkIndex = i
26
+
27
+ //pathFileChunk
28
+ let pathFileChunk = path.join(pathUploadTemp, `${packageId}_${chunkIndex}`)
29
+
30
+ //check
31
+ if (!fsIsFile(pathFileChunk)) {
32
+ // console.log(`pathFileChunk[${pathFileChunk}] is not a file`)
33
+ throw new Error(`missing chunk ${i} of filename[${filename}]`)
34
+ }
35
+
36
+ // console.log(`merging chunk[${chunkIndex + 1}/${chunkTotal}]...`)
37
+
38
+ //chunkData
39
+ let chunkData = fs.readFileSync(pathFileChunk)
40
+
41
+ //write
42
+ fileStream.write(chunkData)
43
+
44
+ //fsDeleteFile
45
+ fsDeleteFile(pathFileChunk)
46
+
47
+ // console.log(`merge chunk[${chunkIndex + 1}/${chunkTotal}] done`)
48
+ }
49
+ }
50
+ catch (err) {
51
+ errTemp = err.message
52
+ }
53
+
54
+ //end
55
+ fileStream.end()
56
+ // console.log(`merge filename[${filename}] done`)
57
+
58
+ //check
59
+ if (isestr(errTemp)) {
60
+ return Promise.reject(errTemp)
61
+ }
62
+
63
+ //r
64
+ let r = {
65
+ // message: `Merged filename[${filename}] successfully`,
66
+ filename,
67
+ path: pathFileMerge,
68
+ }
69
+
70
+ return r
71
+ }
72
+
73
+ export default mergeFiles