w-converhp 2.0.8 → 2.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.
- package/README.md +66 -1
- package/dist/w-converhp-client.umd.js +2 -2
- package/dist/w-converhp-client.umd.js.map +1 -1
- package/dist/w-converhp-server.umd.js +2 -2
- package/dist/w-converhp-server.umd.js.map +1 -1
- package/docs/WConverhpClient.html +3 -3
- package/docs/WConverhpClient.mjs.html +164 -6
- package/docs/WConverhpServer.html +3 -3
- package/docs/WConverhpServer.mjs.html +258 -71
- package/docs/index.html +1 -1
- package/package.json +3 -3
- package/sclc.mjs +4 -2
- package/scld.mjs +54 -0
- package/src/WConverhpClient.mjs +162 -4
- package/src/WConverhpServer.mjs +257 -70
- package/src/mergeFiles.mjs +16 -105
- package/src/mergeFiles.wk.umd.js +1 -1
- package/srv.mjs +39 -1
- package/test/1mb.7z +0 -0
- package/test/downloadLargeFile.test.mjs +166 -0
- package/test/executeWithFile.test.mjs +1 -1
- package/test/uploadLargeFile.test.mjs +5 -5
- package/web.html +28 -1
package/src/WConverhpClient.mjs
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import path from 'path'
|
|
2
|
+
import fs from 'fs'
|
|
1
3
|
import axios from 'axios'
|
|
2
4
|
import get from 'lodash-es/get.js'
|
|
3
5
|
import isWindow from 'wsemi/src/isWindow.mjs'
|
|
@@ -19,6 +21,8 @@ import obj2u8arr from 'wsemi/src/obj2u8arr.mjs'
|
|
|
19
21
|
import u8arr2obj from 'wsemi/src/u8arr2obj.mjs'
|
|
20
22
|
import pmConvertResolve from 'wsemi/src/pmConvertResolve.mjs'
|
|
21
23
|
import now2strp from 'wsemi/src/now2strp.mjs'
|
|
24
|
+
import fsIsFolder from 'wsemi/src/fsIsFolder.mjs'
|
|
25
|
+
import fsCreateFolder from 'wsemi/src/fsCreateFolder.mjs'
|
|
22
26
|
|
|
23
27
|
|
|
24
28
|
/**
|
|
@@ -130,6 +134,35 @@ import now2strp from 'wsemi/src/now2strp.mjs'
|
|
|
130
134
|
* }
|
|
131
135
|
* uploadLargeFile()
|
|
132
136
|
*
|
|
137
|
+
* function downloadLargeFile() {
|
|
138
|
+
* let core = async() => {
|
|
139
|
+
*
|
|
140
|
+
* await wo.download('id-for-file',
|
|
141
|
+
* function ({ prog, p, m }) {
|
|
142
|
+
* // console.log('client web: download: prog', prog, p, m)
|
|
143
|
+
* if (m === 'download') {
|
|
144
|
+
* console.log('client web: download: prog', prog)
|
|
145
|
+
* }
|
|
146
|
+
* },
|
|
147
|
+
* {
|
|
148
|
+
* fdDownload: './',
|
|
149
|
+
* })
|
|
150
|
+
* .then(function(res) {
|
|
151
|
+
* console.log('client web: download: then', res)
|
|
152
|
+
* ms.push({ 'download output': res })
|
|
153
|
+
* })
|
|
154
|
+
* .catch(function (err) {
|
|
155
|
+
* console.log('client web: download: catch', err)
|
|
156
|
+
* })
|
|
157
|
+
*
|
|
158
|
+
* console.log('ms', ms)
|
|
159
|
+
*
|
|
160
|
+
* }
|
|
161
|
+
* core()
|
|
162
|
+
* }
|
|
163
|
+
*
|
|
164
|
+
* downloadLargeFile()
|
|
165
|
+
*
|
|
133
166
|
*/
|
|
134
167
|
function WConverhpClient(opt) {
|
|
135
168
|
|
|
@@ -254,6 +287,9 @@ function WConverhpClient(opt) {
|
|
|
254
287
|
else if (type === 'slicemerge') {
|
|
255
288
|
urlUse = `${url}slcm`
|
|
256
289
|
}
|
|
290
|
+
else if (type === 'download') {
|
|
291
|
+
urlUse = `${url}dw`
|
|
292
|
+
}
|
|
257
293
|
else {
|
|
258
294
|
throw new Error(`invalid type[${type}]`)
|
|
259
295
|
}
|
|
@@ -328,6 +364,9 @@ function WConverhpClient(opt) {
|
|
|
328
364
|
let rt = 'blob'
|
|
329
365
|
if (env === 'nodejs') {
|
|
330
366
|
rt = 'arraybuffer' //nodejs下沒有blob, 只能設定'json', 'arraybuffer', 'document', 'json', 'text', 'stream'
|
|
367
|
+
if (type === 'download') {
|
|
368
|
+
rt = 'stream' //nodejs download模式採用stream接收
|
|
369
|
+
}
|
|
331
370
|
}
|
|
332
371
|
// console.log('rt', rt)
|
|
333
372
|
|
|
@@ -385,11 +424,115 @@ function WConverhpClient(opt) {
|
|
|
385
424
|
}
|
|
386
425
|
// console.log('s', s)
|
|
387
426
|
|
|
427
|
+
//getFilenameByHeader
|
|
428
|
+
let getFilenameByHeader = (contentDisposition) => {
|
|
429
|
+
let fn = 'unknow'
|
|
430
|
+
try {
|
|
431
|
+
let reg = /filename="(.+?)"/
|
|
432
|
+
let matches = reg.exec(contentDisposition)
|
|
433
|
+
fn = matches ? matches[1] : 'unknown'
|
|
434
|
+
}
|
|
435
|
+
catch (err) {}
|
|
436
|
+
return fn
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
//downloadStream
|
|
440
|
+
let downloadStream = (res) => {
|
|
441
|
+
// console.log('res.headers', res.headers)
|
|
442
|
+
|
|
443
|
+
//pm
|
|
444
|
+
let pm = genPm()
|
|
445
|
+
|
|
446
|
+
//returnType
|
|
447
|
+
let returnType = get(res, `headers['return-type']`, '')
|
|
448
|
+
// console.log('returnType', returnType)
|
|
449
|
+
|
|
450
|
+
//returnMsg
|
|
451
|
+
let returnMsg = get(res, `headers['return-msg']`, '')
|
|
452
|
+
// console.log('returnMsg', returnMsg)
|
|
453
|
+
|
|
454
|
+
//check
|
|
455
|
+
if (returnType === 'error') {
|
|
456
|
+
pm.reject(returnMsg)
|
|
457
|
+
return pm
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
//contentDisposition
|
|
461
|
+
let contentDisposition = get(res, `headers['content-disposition']`, '')
|
|
462
|
+
// console.log('contentDisposition', contentDisposition)
|
|
463
|
+
|
|
464
|
+
//filename
|
|
465
|
+
let filename = getFilenameByHeader(contentDisposition)
|
|
466
|
+
// console.log('filename', filename)
|
|
467
|
+
|
|
468
|
+
//streamRecv
|
|
469
|
+
let streamRecv = get(res, 'data')
|
|
470
|
+
|
|
471
|
+
if (env === 'browser') {
|
|
472
|
+
|
|
473
|
+
//通過createObjectURL與a元素下載
|
|
474
|
+
let url = URL.createObjectURL(streamRecv)
|
|
475
|
+
let a = document.createElement('a')
|
|
476
|
+
a.href = url
|
|
477
|
+
a.download = filename // 動態設置檔名
|
|
478
|
+
document.body.appendChild(a)
|
|
479
|
+
a.click()
|
|
480
|
+
a.remove()
|
|
481
|
+
URL.revokeObjectURL(url)
|
|
482
|
+
|
|
483
|
+
pm.resolve(filename)
|
|
484
|
+
}
|
|
485
|
+
else {
|
|
486
|
+
|
|
487
|
+
//fdDownload, 只有後端下載才使用fdDownload
|
|
488
|
+
let fdDownload = get(opt, 'fdDownload', '')
|
|
489
|
+
if (!fsIsFolder(fdDownload)) {
|
|
490
|
+
fsCreateFolder(fdDownload)
|
|
491
|
+
}
|
|
492
|
+
// console.log('fdDownload', fdDownload)
|
|
493
|
+
|
|
494
|
+
//fp
|
|
495
|
+
let fp = path.resolve(fdDownload, filename)
|
|
496
|
+
// console.log('fp', fp)
|
|
497
|
+
|
|
498
|
+
//streamWriter
|
|
499
|
+
let streamWriter = fs.createWriteStream(fp)
|
|
500
|
+
|
|
501
|
+
//pipe
|
|
502
|
+
streamRecv.pipe(streamWriter)
|
|
503
|
+
|
|
504
|
+
//finish
|
|
505
|
+
streamWriter.on('finish', () => {
|
|
506
|
+
pm.resolve(fp)
|
|
507
|
+
})
|
|
508
|
+
|
|
509
|
+
//error
|
|
510
|
+
streamWriter.on('error', (err) => {
|
|
511
|
+
pm.reject(err)
|
|
512
|
+
})
|
|
513
|
+
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
return pm
|
|
517
|
+
}
|
|
518
|
+
|
|
388
519
|
//axios
|
|
389
520
|
axios(s)
|
|
390
521
|
.then(async (res) => {
|
|
391
522
|
// console.log('axios then', res)
|
|
392
523
|
|
|
524
|
+
//check download
|
|
525
|
+
if (type === 'download') {
|
|
526
|
+
await downloadStream(res)
|
|
527
|
+
.then((_res) => {
|
|
528
|
+
pm.resolve(_res)
|
|
529
|
+
})
|
|
530
|
+
.catch((_err) => {
|
|
531
|
+
pm.reject(_err)
|
|
532
|
+
})
|
|
533
|
+
return
|
|
534
|
+
}
|
|
535
|
+
|
|
393
536
|
//bb
|
|
394
537
|
let bb = get(res, 'data')
|
|
395
538
|
// console.log('bb', bb)
|
|
@@ -426,7 +569,7 @@ function WConverhpClient(opt) {
|
|
|
426
569
|
|
|
427
570
|
})
|
|
428
571
|
.catch(async (res) => {
|
|
429
|
-
//console.log('axios catch', res.toJSON())
|
|
572
|
+
// console.log('axios catch', res.toJSON())
|
|
430
573
|
//Network Error除可能是網路斷線之外, 可能被瀏覽器外掛封鎖阻擋, 亦可能因硬碟空間不足(<4g)無法下載被瀏覽器拒絕
|
|
431
574
|
|
|
432
575
|
//data
|
|
@@ -460,6 +603,7 @@ function WConverhpClient(opt) {
|
|
|
460
603
|
if (data === 'Network Error') {
|
|
461
604
|
data = `Network Error. Make sure your space of hard drive is large enough or blocking by browser plugins.`
|
|
462
605
|
}
|
|
606
|
+
// console.log('data', data)
|
|
463
607
|
|
|
464
608
|
pm.reject(data)
|
|
465
609
|
})
|
|
@@ -536,7 +680,7 @@ function WConverhpClient(opt) {
|
|
|
536
680
|
}
|
|
537
681
|
if (n === 0) {
|
|
538
682
|
try {
|
|
539
|
-
n = bb.length //for Uint8Array
|
|
683
|
+
n = bb.length //for Uint8Array //後端用fs讀有檔案大小上限, 除非改傳入檔名用stream讀, 否則無法支援超大檔
|
|
540
684
|
n = cint(n)
|
|
541
685
|
}
|
|
542
686
|
catch (err) {}
|
|
@@ -544,9 +688,9 @@ function WConverhpClient(opt) {
|
|
|
544
688
|
if (n === 0) {
|
|
545
689
|
// eeEmit('error', `can not get size of bb`)
|
|
546
690
|
// return Promise.reject(`can not get size of bb`)
|
|
547
|
-
n = 1
|
|
691
|
+
n = 1 //最小給1, 使能支援無大小檔案上傳
|
|
548
692
|
}
|
|
549
|
-
console.log('n', n)
|
|
693
|
+
// console.log('n', n)
|
|
550
694
|
|
|
551
695
|
//chunkTotal
|
|
552
696
|
let chunkTotal = Math.ceil(n / sizeSlice)
|
|
@@ -697,9 +841,23 @@ function WConverhpClient(opt) {
|
|
|
697
841
|
return sendDataSlice(tempId, bb, cbProgress)
|
|
698
842
|
}
|
|
699
843
|
|
|
844
|
+
//download
|
|
845
|
+
async function download(fileId, cbProgress, opt = {}) {
|
|
846
|
+
|
|
847
|
+
//msg
|
|
848
|
+
let msg = { fileId }
|
|
849
|
+
|
|
850
|
+
//send download
|
|
851
|
+
let resMg = await send('download', msg, { ...opt, dataType: 'json', cbProgress })
|
|
852
|
+
// console.log('resMg', resMg)
|
|
853
|
+
|
|
854
|
+
return resMg
|
|
855
|
+
}
|
|
856
|
+
|
|
700
857
|
//save
|
|
701
858
|
ee.execute = execute
|
|
702
859
|
ee.upload = upload
|
|
860
|
+
ee.download = download
|
|
703
861
|
|
|
704
862
|
return ee
|
|
705
863
|
}
|