@@ -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} "
@@ -1476,6 +1479,7 @@ class FilesCollection
1476
1479
{Number|dynamic} streams - Quantity of parallel upload streams, default: 2
1477
1480
{Number|dynamic} chunkSize - Chunk size for upload
1478
1481
{String} transport - Upload transport `http` or `ddp`
1482
+ {DDP} ddp - Custom DDP connection
1479
1483
{Function} onUploaded - Callback triggered when upload is finished, with two arguments `error` and `fileRef`
1480
1484
{Function} onStart - Callback triggered when upload is started after all successful validations, with two arguments `error` (always null) and `fileRef`
1481
1485
{Function} onError - Callback triggered on error in upload and/or FileReader, with two arguments `error` and `fileData`
@@ -1512,6 +1516,7 @@ class FilesCollection
1512
1516
EventEmitter .call @
1513
1517
console .info ' [FilesCollection] [insert()]' if @collection .debug
1514
1518
self = @
1519
+ @config .ddp = @collection .ddp
1515
1520
@config .meta ?= {}
1516
1521
@config .streams ?= 2
1517
1522
@config .streams = 2 if @config .streams < 1
@@ -1535,7 +1540,8 @@ class FilesCollection
1535
1540
onUploaded : Match .Optional Function
1536
1541
onProgress : Match .Optional Function
1537
1542
onBeforeUpload : Match .Optional Function
1538
- allowWebWorkers : Boolean
1543
+ allowWebWorkers : Boolean ,
1544
+ ddp : Match .Any ,
1539
1545
}
1540
1546
1541
1547
if not @config .fileName and not @config .file .name
@@ -1681,7 +1687,7 @@ class FilesCollection
1681
1687
1682
1688
if opts .binData
1683
1689
if @config .transport is ' ddp'
1684
- Meteor .call @collection ._methodNames ._Write , opts, (error ) ->
1690
+ @config . ddp .call @collection ._methodNames ._Write , opts, (error ) ->
1685
1691
self .transferTime += (+ new Date ) - self .startTime [opts .chunkId ]
1686
1692
if error
1687
1693
if self .result .state .get () isnt ' aborted'
@@ -1729,7 +1735,7 @@ class FilesCollection
1729
1735
fileId : @fileId
1730
1736
1731
1737
if @config .transport is ' ddp'
1732
- Meteor .call @collection ._methodNames ._Write , opts, ->
1738
+ @config . ddp .call @collection ._methodNames ._Write , opts, ->
1733
1739
self .emitEvent ' end' , arguments
1734
1740
return
1735
1741
else
@@ -1779,7 +1785,7 @@ class FilesCollection
1779
1785
1780
1786
else if FileReaderSync
1781
1787
fileReader = new FileReaderSync
1782
-
1788
+
1783
1789
self .emitEvent ' sendChunk' , [{
1784
1790
data : {
1785
1791
bin : fileReader .readAsDataURL (chunk).split (' ,' )[1 ]
@@ -1790,7 +1796,7 @@ class FilesCollection
1790
1796
self .emitEvent ' end' , [' File API is not supported in this Browser!' ]
1791
1797
return
1792
1798
1793
- upload : ->
1799
+ upload : ->
1794
1800
if @result .onPause .get ()
1795
1801
return
1796
1802
@@ -1868,18 +1874,18 @@ class FilesCollection
1868
1874
return
1869
1875
1870
1876
if @config .transport is ' ddp'
1871
- Meteor .call @collection ._methodNames ._Start , opts, handleStart
1877
+ @config . ddp .call @collection ._methodNames ._Start , opts, handleStart
1872
1878
else
1873
1879
opts .file .meta = fixJSONStringify opts .file .meta if opts .file ? .meta
1874
1880
HTTP .call ' POST' , " #{ @collection .downloadRoute } /#{ @collection .collectionName } /__upload" , {
1875
1881
data : opts
1876
- headers :
1882
+ headers :
1877
1883
' x-start' : ' 1'
1878
1884
' x-mtok' : Meteor .connection ? ._lastSessionId or null
1879
1885
}, handleStart
1880
1886
return
1881
1887
1882
- pipe : (func ) ->
1888
+ pipe : (func ) ->
1883
1889
@pipes .push func
1884
1890
return @
1885
1891
@@ -2000,7 +2006,7 @@ class FilesCollection
2000
2006
@config ._onEnd ()
2001
2007
@state .set ' aborted'
2002
2008
console .timeEnd (' insert ' + @config .fileData .name ) if @config .debug
2003
- Meteor .call @config ._Abort , @config .fileId
2009
+ @config . ddp .call @config ._Abort , @config .fileId
2004
2010
return
2005
2011
else undefined
2006
2012
@@ -2020,7 +2026,7 @@ class FilesCollection
2020
2026
2021
2027
if Meteor .isClient
2022
2028
if @allowClientCode
2023
- Meteor .call @_methodNames ._Remove , selector, (callback or NOOP)
2029
+ @config . ddp .call @_methodNames ._Remove , selector, (callback or NOOP)
2024
2030
else
2025
2031
callback and callback new Meteor.Error 401 , ' [FilesCollection] [remove] Run code from client is not allowed!'
2026
2032
console .warn ' [FilesCollection] [remove] Run code from client is not allowed!' if @debug
@@ -2031,7 +2037,7 @@ class FilesCollection
2031
2037
files .forEach (file) ->
2032
2038
self .unlink file
2033
2039
return
2034
-
2040
+
2035
2041
if @onAfterRemove
2036
2042
self = @
2037
2043
docs = files .fetch ()
@@ -2313,7 +2319,7 @@ class FilesCollection
2313
2319
http .response .setHeader ' Content-Range' , " bytes #{ reqRange .start } -#{ reqRange .end } /#{ vRef .size } "
2314
2320
stream = readableStream or fs .createReadStream vRef .path , {start : reqRange .start , end : reqRange .end }
2315
2321
http .response .writeHead 206 if readableStream
2316
- stream .on (' open' , ->
2322
+ stream .on (' open' , ->
2317
2323
http .response .writeHead 206
2318
2324
return
2319
2325
).on (' error' , streamErrorHandler
@@ -2390,4 +2396,4 @@ if Meteor.isClient
2390
2396
Export the FilesCollection class
2391
2397
###
2392
2398
Meteor .Files = FilesCollection
2393
- ` export { FilesCollection }`
2399
+ ` export { FilesCollection }`
0 commit comments