w-converhp 2.0.10 → 2.0.12
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 +177 -4
- 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 +176 -3
- package/src/WConverhpServer.mjs +257 -70
- package/src/mergeFiles.mjs +16 -105
- package/src/mergeFiles.wk.umd.js +1 -1
- package/srv.mjs +38 -0
- 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
|
@@ -19,6 +19,8 @@ import obj2u8arr from 'wsemi/src/obj2u8arr.mjs'
|
|
|
19
19
|
import u8arr2obj from 'wsemi/src/u8arr2obj.mjs'
|
|
20
20
|
import pmConvertResolve from 'wsemi/src/pmConvertResolve.mjs'
|
|
21
21
|
import now2strp from 'wsemi/src/now2strp.mjs'
|
|
22
|
+
import fsIsFolder from 'wsemi/src/fsIsFolder.mjs'
|
|
23
|
+
import fsCreateFolder from 'wsemi/src/fsCreateFolder.mjs'
|
|
22
24
|
|
|
23
25
|
|
|
24
26
|
/**
|
|
@@ -130,6 +132,35 @@ import now2strp from 'wsemi/src/now2strp.mjs'
|
|
|
130
132
|
* }
|
|
131
133
|
* uploadLargeFile()
|
|
132
134
|
*
|
|
135
|
+
* function downloadLargeFile() {
|
|
136
|
+
* let core = async() => {
|
|
137
|
+
*
|
|
138
|
+
* await wo.download('id-for-file',
|
|
139
|
+
* function ({ prog, p, m }) {
|
|
140
|
+
* // console.log('client web: download: prog', prog, p, m)
|
|
141
|
+
* if (m === 'download') {
|
|
142
|
+
* console.log('client web: download: prog', prog)
|
|
143
|
+
* }
|
|
144
|
+
* },
|
|
145
|
+
* {
|
|
146
|
+
* fdDownload: './',
|
|
147
|
+
* })
|
|
148
|
+
* .then(function(res) {
|
|
149
|
+
* console.log('client web: download: then', res)
|
|
150
|
+
* ms.push({ 'download output': res })
|
|
151
|
+
* })
|
|
152
|
+
* .catch(function (err) {
|
|
153
|
+
* console.log('client web: download: catch', err)
|
|
154
|
+
* })
|
|
155
|
+
*
|
|
156
|
+
* console.log('ms', ms)
|
|
157
|
+
*
|
|
158
|
+
* }
|
|
159
|
+
* core()
|
|
160
|
+
* }
|
|
161
|
+
*
|
|
162
|
+
* downloadLargeFile()
|
|
163
|
+
*
|
|
133
164
|
*/
|
|
134
165
|
function WConverhpClient(opt) {
|
|
135
166
|
|
|
@@ -254,6 +285,9 @@ function WConverhpClient(opt) {
|
|
|
254
285
|
else if (type === 'slicemerge') {
|
|
255
286
|
urlUse = `${url}slcm`
|
|
256
287
|
}
|
|
288
|
+
else if (type === 'download') {
|
|
289
|
+
urlUse = `${url}dw`
|
|
290
|
+
}
|
|
257
291
|
else {
|
|
258
292
|
throw new Error(`invalid type[${type}]`)
|
|
259
293
|
}
|
|
@@ -328,6 +362,9 @@ function WConverhpClient(opt) {
|
|
|
328
362
|
let rt = 'blob'
|
|
329
363
|
if (env === 'nodejs') {
|
|
330
364
|
rt = 'arraybuffer' //nodejs下沒有blob, 只能設定'json', 'arraybuffer', 'document', 'json', 'text', 'stream'
|
|
365
|
+
if (type === 'download') {
|
|
366
|
+
rt = 'stream' //nodejs download模式採用stream接收
|
|
367
|
+
}
|
|
331
368
|
}
|
|
332
369
|
// console.log('rt', rt)
|
|
333
370
|
|
|
@@ -385,11 +422,132 @@ function WConverhpClient(opt) {
|
|
|
385
422
|
}
|
|
386
423
|
// console.log('s', s)
|
|
387
424
|
|
|
425
|
+
//getFilenameByHeader
|
|
426
|
+
let getFilenameByHeader = (contentDisposition) => {
|
|
427
|
+
let fn = 'unknow'
|
|
428
|
+
try {
|
|
429
|
+
let reg = /filename="(.+?)"/
|
|
430
|
+
let matches = reg.exec(contentDisposition)
|
|
431
|
+
fn = matches ? matches[1] : 'unknown'
|
|
432
|
+
}
|
|
433
|
+
catch (err) {}
|
|
434
|
+
return fn
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
//downloadStream
|
|
438
|
+
let downloadStream = async (res) => {
|
|
439
|
+
// console.log('res.headers', res.headers)
|
|
440
|
+
|
|
441
|
+
//pm
|
|
442
|
+
let pm = genPm()
|
|
443
|
+
|
|
444
|
+
//returnType
|
|
445
|
+
let returnType = get(res, `headers['return-type']`, '')
|
|
446
|
+
// console.log('returnType', returnType)
|
|
447
|
+
|
|
448
|
+
//returnMsg
|
|
449
|
+
let returnMsg = get(res, `headers['return-msg']`, '')
|
|
450
|
+
// console.log('returnMsg', returnMsg)
|
|
451
|
+
|
|
452
|
+
//check
|
|
453
|
+
if (returnType === 'error') {
|
|
454
|
+
pm.reject(returnMsg)
|
|
455
|
+
return pm
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
//contentDisposition
|
|
459
|
+
let contentDisposition = get(res, `headers['content-disposition']`, '')
|
|
460
|
+
// console.log('contentDisposition', contentDisposition)
|
|
461
|
+
|
|
462
|
+
//filename
|
|
463
|
+
let filename = getFilenameByHeader(contentDisposition)
|
|
464
|
+
// console.log('filename', filename)
|
|
465
|
+
|
|
466
|
+
//streamRecv
|
|
467
|
+
let streamRecv = get(res, 'data')
|
|
468
|
+
|
|
469
|
+
if (env === 'browser') {
|
|
470
|
+
|
|
471
|
+
//通過createObjectURL與a元素下載
|
|
472
|
+
try {
|
|
473
|
+
let url = URL.createObjectURL(streamRecv)
|
|
474
|
+
let a = document.createElement('a')
|
|
475
|
+
a.href = url
|
|
476
|
+
a.download = filename // 動態設置檔名
|
|
477
|
+
document.body.appendChild(a)
|
|
478
|
+
a.click()
|
|
479
|
+
a.remove()
|
|
480
|
+
URL.revokeObjectURL(url)
|
|
481
|
+
pm.resolve(filename)
|
|
482
|
+
}
|
|
483
|
+
catch (err) {
|
|
484
|
+
console.log(err)
|
|
485
|
+
pm.reject(err)
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
}
|
|
489
|
+
else {
|
|
490
|
+
|
|
491
|
+
try {
|
|
492
|
+
|
|
493
|
+
//path, fs
|
|
494
|
+
let path = await import('path')
|
|
495
|
+
let fs = await import('fs')
|
|
496
|
+
|
|
497
|
+
//fdDownload, 只有後端下載才使用fdDownload
|
|
498
|
+
let fdDownload = get(opt, 'fdDownload', '')
|
|
499
|
+
if (!fsIsFolder(fdDownload)) {
|
|
500
|
+
fsCreateFolder(fdDownload)
|
|
501
|
+
}
|
|
502
|
+
// console.log('fdDownload', fdDownload)
|
|
503
|
+
|
|
504
|
+
//fp
|
|
505
|
+
let fp = path.resolve(fdDownload, filename)
|
|
506
|
+
// console.log('fp', fp)
|
|
507
|
+
|
|
508
|
+
//streamWriter
|
|
509
|
+
let streamWriter = fs.createWriteStream(fp)
|
|
510
|
+
|
|
511
|
+
//pipe
|
|
512
|
+
streamRecv.pipe(streamWriter)
|
|
513
|
+
|
|
514
|
+
//finish
|
|
515
|
+
streamWriter.on('finish', () => {
|
|
516
|
+
pm.resolve(fp)
|
|
517
|
+
})
|
|
518
|
+
|
|
519
|
+
//error
|
|
520
|
+
streamWriter.on('error', (err) => {
|
|
521
|
+
pm.reject(err)
|
|
522
|
+
})
|
|
523
|
+
|
|
524
|
+
}
|
|
525
|
+
catch (err) {
|
|
526
|
+
pm.reject(err)
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
return pm
|
|
532
|
+
}
|
|
533
|
+
|
|
388
534
|
//axios
|
|
389
535
|
axios(s)
|
|
390
536
|
.then(async (res) => {
|
|
391
537
|
// console.log('axios then', res)
|
|
392
538
|
|
|
539
|
+
//check download
|
|
540
|
+
if (type === 'download') {
|
|
541
|
+
await downloadStream(res)
|
|
542
|
+
.then((_res) => {
|
|
543
|
+
pm.resolve(_res)
|
|
544
|
+
})
|
|
545
|
+
.catch((_err) => {
|
|
546
|
+
pm.reject(_err)
|
|
547
|
+
})
|
|
548
|
+
return
|
|
549
|
+
}
|
|
550
|
+
|
|
393
551
|
//bb
|
|
394
552
|
let bb = get(res, 'data')
|
|
395
553
|
// console.log('bb', bb)
|
|
@@ -426,7 +584,7 @@ function WConverhpClient(opt) {
|
|
|
426
584
|
|
|
427
585
|
})
|
|
428
586
|
.catch(async (res) => {
|
|
429
|
-
//console.log('axios catch', res.toJSON())
|
|
587
|
+
// console.log('axios catch', res.toJSON())
|
|
430
588
|
//Network Error除可能是網路斷線之外, 可能被瀏覽器外掛封鎖阻擋, 亦可能因硬碟空間不足(<4g)無法下載被瀏覽器拒絕
|
|
431
589
|
|
|
432
590
|
//data
|
|
@@ -460,6 +618,7 @@ function WConverhpClient(opt) {
|
|
|
460
618
|
if (data === 'Network Error') {
|
|
461
619
|
data = `Network Error. Make sure your space of hard drive is large enough or blocking by browser plugins.`
|
|
462
620
|
}
|
|
621
|
+
// console.log('data', data)
|
|
463
622
|
|
|
464
623
|
pm.reject(data)
|
|
465
624
|
})
|
|
@@ -536,7 +695,7 @@ function WConverhpClient(opt) {
|
|
|
536
695
|
}
|
|
537
696
|
if (n === 0) {
|
|
538
697
|
try {
|
|
539
|
-
n = bb.length //for Uint8Array
|
|
698
|
+
n = bb.length //for Uint8Array //後端用fs讀有檔案大小上限, 除非改傳入檔名用stream讀, 否則無法支援超大檔
|
|
540
699
|
n = cint(n)
|
|
541
700
|
}
|
|
542
701
|
catch (err) {}
|
|
@@ -544,7 +703,7 @@ function WConverhpClient(opt) {
|
|
|
544
703
|
if (n === 0) {
|
|
545
704
|
// eeEmit('error', `can not get size of bb`)
|
|
546
705
|
// return Promise.reject(`can not get size of bb`)
|
|
547
|
-
n = 1
|
|
706
|
+
n = 1 //最小給1, 使能支援無大小檔案上傳
|
|
548
707
|
}
|
|
549
708
|
// console.log('n', n)
|
|
550
709
|
|
|
@@ -697,9 +856,23 @@ function WConverhpClient(opt) {
|
|
|
697
856
|
return sendDataSlice(tempId, bb, cbProgress)
|
|
698
857
|
}
|
|
699
858
|
|
|
859
|
+
//download
|
|
860
|
+
async function download(fileId, cbProgress, opt = {}) {
|
|
861
|
+
|
|
862
|
+
//msg
|
|
863
|
+
let msg = { fileId }
|
|
864
|
+
|
|
865
|
+
//send download
|
|
866
|
+
let resMg = await send('download', msg, { ...opt, dataType: 'json', cbProgress })
|
|
867
|
+
// console.log('resMg', resMg)
|
|
868
|
+
|
|
869
|
+
return resMg
|
|
870
|
+
}
|
|
871
|
+
|
|
700
872
|
//save
|
|
701
873
|
ee.execute = execute
|
|
702
874
|
ee.upload = upload
|
|
875
|
+
ee.download = download
|
|
703
876
|
|
|
704
877
|
return ee
|
|
705
878
|
}
|