@@ -467,7 +467,7 @@ class FilesCollection
467
467
events .EventEmitter .call @
468
468
else
469
469
EventEmitter .call @
470
- {storagePath , @collection , @collectionName , @downloadRoute , @schema , @chunkSize , @namingFunction , @debug , @onbeforeunloadMessage , @permissions , @parentDirPermissions , @allowClientCode , @onBeforeUpload , @onInitiateUpload , @integrityCheck , @protected , @public , @strict , @downloadCallback , @cacheControl , @responseHeaders , @throttle , @onAfterUpload , @onAfterRemove , @interceptDownload , @onBeforeRemove , @continueUploadTTL } = config if config
470
+ {storagePath , @ddp , @ collection , @collectionName , @downloadRoute , @schema , @chunkSize , @namingFunction , @debug , @onbeforeunloadMessage , @permissions , @parentDirPermissions , @allowClientCode , @onBeforeUpload , @onInitiateUpload , @integrityCheck , @protected , @public , @strict , @downloadCallback , @cacheControl , @responseHeaders , @throttle , @onAfterUpload , @onAfterRemove , @interceptDownload , @onBeforeRemove , @continueUploadTTL } = config if config
471
471
472
472
self = @
473
473
cookie = new Cookies ()
@@ -489,9 +489,11 @@ class FilesCollection
489
489
@namingFunction ?= false
490
490
@onBeforeUpload ?= false
491
491
@allowClientCode ?= true
492
+ @ddp ?= Meteor
492
493
@onInitiateUpload ?= false
493
494
@interceptDownload ?= false
494
495
496
+
495
497
if Meteor .isClient
496
498
@onbeforeunloadMessage ?= ' Upload in a progress... Do you want to abort?'
497
499
delete @strict
@@ -585,7 +587,7 @@ class FilesCollection
585
587
sp = storagePath
586
588
else if _ .isFunction storagePath
587
589
sp = storagePath .call self, " assets#{ nodePath .sep } app#{ nodePath .sep } uploads#{ nodePath .sep }#{ self .collectionName } "
588
-
590
+
589
591
unless _ .isString sp
590
592
throw new Meteor.Error 400 , " [FilesCollection.#{ self .collectionName } ] \" storagePath\" function must return a String!"
591
593
@@ -673,7 +675,7 @@ class FilesCollection
673
675
userId :
674
676
type : String
675
677
optional : true
676
- updatedAt :
678
+ updatedAt :
677
679
type : Date
678
680
optional : true
679
681
versions :
@@ -690,6 +692,7 @@ class FilesCollection
690
692
check @onBeforeUpload , Match .OneOf false , Function
691
693
check @onInitiateUpload , Match .OneOf false , Function
692
694
check @allowClientCode , Boolean
695
+ check @ddp , Match .Any
693
696
694
697
if @public and @protected
695
698
throw new Meteor.Error 500 , " [FilesCollection.#{ @collectionName } ]: Files can not be public and protected at the same time!"
@@ -813,7 +816,7 @@ class FilesCollection
813
816
814
817
uris = uri .split ' /'
815
818
if uris .length is 3
816
- params =
819
+ params =
817
820
query : if request ._parsedUrl .query then JSON .parse (' {"' + decodeURI (request ._parsedUrl .query ).replace (/ "/ g , ' \\ "' ).replace (/ &/ g , ' ","' ).replace (/ =/ g ,' ":"' ) + ' "}' ) else {}
818
821
_id : uris[0 ]
819
822
version : uris[1 ]
@@ -840,7 +843,7 @@ class FilesCollection
840
843
version = ' original'
841
844
_file = _file .split (' ?' )[0 ]
842
845
843
- params =
846
+ params =
844
847
query : if request ._parsedUrl .query then JSON .parse (' {"' + decodeURI (request ._parsedUrl .query ).replace (/ "/ g , ' \\ "' ).replace (/ &/ g , ' ","' ).replace (/ =/ g ,' ":"' ) + ' "}' ) else {}
845
848
file : _file
846
849
_id : _file .split (' .' )[0 ]
@@ -862,7 +865,7 @@ class FilesCollection
862
865
_methods[self ._methodNames ._Remove ] = (selector ) ->
863
866
check selector, Match .OneOf String , Object
864
867
console .info " [FilesCollection] [Unlink Method] [.remove(#{ selector} )]" if self .debug
865
-
868
+
866
869
if self .allowClientCode
867
870
if self .onBeforeRemove and _ .isFunction self .onBeforeRemove
868
871
user = false
@@ -882,7 +885,7 @@ class FilesCollection
882
885
883
886
884
887
# Method used to receive "first byte" of upload
885
- # and all file's meta-data, so
888
+ # and all file's meta-data, so
886
889
# it won't be transferred with every chunk
887
890
# Basically it prepares everything
888
891
# So user can pause/disconnect and
@@ -1087,7 +1090,7 @@ class FilesCollection
1087
1090
{mime , ext } = fileType buf
1088
1091
catch error
1089
1092
if not mime or not _ .isString mime
1090
- mime = ' application/octet-stream'
1093
+ mime = ' application/octet-stream'
1091
1094
return mime
1092
1095
1093
1096
###
@@ -1113,16 +1116,16 @@ class FilesCollection
1113
1116
@returns {Object}
1114
1117
###
1115
1118
_getUser : (http ) ->
1116
- result =
1119
+ result =
1117
1120
user : -> return null
1118
1121
userId : null
1119
-
1122
+
1120
1123
if Meteor .isServer
1121
1124
if http
1122
1125
mtok = null
1123
1126
if http .request .headers [' x-mtok' ]
1124
1127
mtok = http .request .headers [' x-mtok' ]
1125
- else
1128
+ else
1126
1129
cookie = http .request .Cookies
1127
1130
if cookie .has ' x_mtok'
1128
1131
mtok = cookie .get ' x_mtok'
@@ -1298,7 +1301,7 @@ class FilesCollection
1298
1301
FSName = if @namingFunction then @ namingFunction () else fileId
1299
1302
pathParts = url .split (' /' )
1300
1303
fileName = if (opts .name or opts .fileName ) then (opts .name or opts .fileName ) else pathParts[pathParts .length - 1 ] or FSName
1301
-
1304
+
1302
1305
{extension , extensionWithDot } = @ _getExt fileName
1303
1306
opts .meta ?= {}
1304
1307
opts .path = " #{ @storagePath }#{ nodePath .sep }#{ FSName}#{ extensionWithDot} "
@@ -1473,6 +1476,7 @@ class FilesCollection
1473
1476
{Number|dynamic} streams - Quantity of parallel upload streams, default: 2
1474
1477
{Number|dynamic} chunkSize - Chunk size for upload
1475
1478
{String} transport - Upload transport `http` or `ddp`
1479
+ {DDP} ddp - Custom DDP connection
1476
1480
{Function} onUploaded - Callback triggered when upload is finished, with two arguments `error` and `fileRef`
1477
1481
{Function} onStart - Callback triggered when upload is started after all successful validations, with two arguments `error` (always null) and `fileRef`
1478
1482
{Function} onError - Callback triggered on error in upload and/or FileReader, with two arguments `error` and `fileData`
@@ -1509,6 +1513,7 @@ class FilesCollection
1509
1513
EventEmitter .call @
1510
1514
console .info ' [FilesCollection] [insert()]' if @collection .debug
1511
1515
self = @
1516
+ @config .ddp = @collection .ddp
1512
1517
@config .meta ?= {}
1513
1518
@config .streams ?= 2
1514
1519
@config .streams = 2 if @config .streams < 1
@@ -1532,7 +1537,8 @@ class FilesCollection
1532
1537
onUploaded : Match .Optional Function
1533
1538
onProgress : Match .Optional Function
1534
1539
onBeforeUpload : Match .Optional Function
1535
- allowWebWorkers : Boolean
1540
+ allowWebWorkers : Boolean ,
1541
+ ddp : Match .Any ,
1536
1542
}
1537
1543
1538
1544
if not @config .fileName and not @config .file .name
@@ -1678,7 +1684,7 @@ class FilesCollection
1678
1684
1679
1685
if opts .binData
1680
1686
if @config .transport is ' ddp'
1681
- Meteor .call @collection ._methodNames ._Write , opts, (error ) ->
1687
+ @config . ddp .call @collection ._methodNames ._Write , opts, (error ) ->
1682
1688
self .transferTime += (+ new Date ) - self .startTime [opts .chunkId ]
1683
1689
if error
1684
1690
if self .result .state .get () isnt ' aborted'
@@ -1726,7 +1732,7 @@ class FilesCollection
1726
1732
fileId : @fileId
1727
1733
1728
1734
if @config .transport is ' ddp'
1729
- Meteor .call @collection ._methodNames ._Write , opts, ->
1735
+ @config . ddp .call @collection ._methodNames ._Write , opts, ->
1730
1736
self .emitEvent ' end' , arguments
1731
1737
return
1732
1738
else
@@ -1776,7 +1782,7 @@ class FilesCollection
1776
1782
1777
1783
else if FileReaderSync
1778
1784
fileReader = new FileReaderSync
1779
-
1785
+
1780
1786
self .emitEvent ' sendChunk' , [{
1781
1787
data : {
1782
1788
bin : fileReader .readAsDataURL (chunk).split (' ,' )[1 ]
@@ -1787,7 +1793,7 @@ class FilesCollection
1787
1793
self .emitEvent ' end' , [' File API is not supported in this Browser!' ]
1788
1794
return
1789
1795
1790
- upload : ->
1796
+ upload : ->
1791
1797
if @result .onPause .get ()
1792
1798
return
1793
1799
@@ -1865,18 +1871,18 @@ class FilesCollection
1865
1871
return
1866
1872
1867
1873
if @config .transport is ' ddp'
1868
- Meteor .call @collection ._methodNames ._Start , opts, handleStart
1874
+ @config . ddp .call @collection ._methodNames ._Start , opts, handleStart
1869
1875
else
1870
1876
opts .file .meta = fixJSONStringify opts .file .meta if opts .file ? .meta
1871
1877
HTTP .call ' POST' , " #{ @collection .downloadRoute } /#{ @collection .collectionName } /__upload" , {
1872
1878
data : opts
1873
- headers :
1879
+ headers :
1874
1880
' x-start' : ' 1'
1875
1881
' x-mtok' : Meteor .connection ? ._lastSessionId or null
1876
1882
}, handleStart
1877
1883
return
1878
1884
1879
- pipe : (func ) ->
1885
+ pipe : (func ) ->
1880
1886
@pipes .push func
1881
1887
return @
1882
1888
@@ -1997,7 +2003,7 @@ class FilesCollection
1997
2003
@config ._onEnd ()
1998
2004
@state .set ' aborted'
1999
2005
console .timeEnd (' insert ' + @config .fileData .name ) if @config .debug
2000
- Meteor .call @config ._Abort , @config .fileId
2006
+ @config . ddp .call @config ._Abort , @config .fileId
2001
2007
return
2002
2008
else undefined
2003
2009
@@ -2017,7 +2023,7 @@ class FilesCollection
2017
2023
2018
2024
if Meteor .isClient
2019
2025
if @allowClientCode
2020
- Meteor .call @_methodNames ._Remove , selector, (callback or NOOP)
2026
+ @config . ddp .call @_methodNames ._Remove , selector, (callback or NOOP)
2021
2027
else
2022
2028
callback and callback new Meteor.Error 401 , ' [FilesCollection] [remove] Run code from client is not allowed!'
2023
2029
console .warn ' [FilesCollection] [remove] Run code from client is not allowed!' if @debug
@@ -2028,7 +2034,7 @@ class FilesCollection
2028
2034
files .forEach (file) ->
2029
2035
self .unlink file
2030
2036
return
2031
-
2037
+
2032
2038
if @onAfterRemove
2033
2039
self = @
2034
2040
docs = files .fetch ()
@@ -2310,7 +2316,7 @@ class FilesCollection
2310
2316
http .response .setHeader ' Content-Range' , " bytes #{ reqRange .start } -#{ reqRange .end } /#{ vRef .size } "
2311
2317
stream = readableStream or fs .createReadStream vRef .path , {start : reqRange .start , end : reqRange .end }
2312
2318
http .response .writeHead 206 if readableStream
2313
- stream .on (' open' , ->
2319
+ stream .on (' open' , ->
2314
2320
http .response .writeHead 206
2315
2321
return
2316
2322
).on (' error' , streamErrorHandler
@@ -2387,4 +2393,4 @@ if Meteor.isClient
2387
2393
Export the FilesCollection class
2388
2394
###
2389
2395
Meteor .Files = FilesCollection
2390
- ` export { FilesCollection }`
2396
+ ` export { FilesCollection }`
0 commit comments