Skip to content

Commit 4e41cb6

Browse files
authored
Merge pull request veliovgroup#254 from rafaelcorreiapoli/master
Custom DDP connection support
2 parents 7afa0b6 + 4016c9e commit 4e41cb6

File tree

2 files changed

+32
-26
lines changed

2 files changed

+32
-26
lines changed

files.coffee

+31-25
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ class FilesCollection
467467
events.EventEmitter.call @
468468
else
469469
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
471471

472472
self = @
473473
cookie = new Cookies()
@@ -489,9 +489,11 @@ class FilesCollection
489489
@namingFunction ?= false
490490
@onBeforeUpload ?= false
491491
@allowClientCode ?= true
492+
@ddp ?= Meteor
492493
@onInitiateUpload ?= false
493494
@interceptDownload ?= false
494495

496+
495497
if Meteor.isClient
496498
@onbeforeunloadMessage ?= 'Upload in a progress... Do you want to abort?'
497499
delete @strict
@@ -585,7 +587,7 @@ class FilesCollection
585587
sp = storagePath
586588
else if _.isFunction storagePath
587589
sp = storagePath.call self, "assets#{nodePath.sep}app#{nodePath.sep}uploads#{nodePath.sep}#{self.collectionName}"
588-
590+
589591
unless _.isString sp
590592
throw new Meteor.Error 400, "[FilesCollection.#{self.collectionName}] \"storagePath\" function must return a String!"
591593

@@ -673,7 +675,7 @@ class FilesCollection
673675
userId:
674676
type: String
675677
optional: true
676-
updatedAt:
678+
updatedAt:
677679
type: Date
678680
optional: true
679681
versions:
@@ -690,6 +692,7 @@ class FilesCollection
690692
check @onBeforeUpload, Match.OneOf false, Function
691693
check @onInitiateUpload, Match.OneOf false, Function
692694
check @allowClientCode, Boolean
695+
check @ddp, Match.Any
693696

694697
if @public and @protected
695698
throw new Meteor.Error 500, "[FilesCollection.#{@collectionName}]: Files can not be public and protected at the same time!"
@@ -813,7 +816,7 @@ class FilesCollection
813816

814817
uris = uri.split '/'
815818
if uris.length is 3
816-
params =
819+
params =
817820
query: if request._parsedUrl.query then JSON.parse('{"' + decodeURI(request._parsedUrl.query).replace(/"/g, '\\"').replace(/&/g, '","').replace(/=/g,'":"') + '"}') else {}
818821
_id: uris[0]
819822
version: uris[1]
@@ -840,7 +843,7 @@ class FilesCollection
840843
version = 'original'
841844
_file = _file.split('?')[0]
842845

843-
params =
846+
params =
844847
query: if request._parsedUrl.query then JSON.parse('{"' + decodeURI(request._parsedUrl.query).replace(/"/g, '\\"').replace(/&/g, '","').replace(/=/g,'":"') + '"}') else {}
845848
file: _file
846849
_id: _file.split('.')[0]
@@ -862,7 +865,7 @@ class FilesCollection
862865
_methods[self._methodNames._Remove] = (selector) ->
863866
check selector, Match.OneOf String, Object
864867
console.info "[FilesCollection] [Unlink Method] [.remove(#{selector})]" if self.debug
865-
868+
866869
if self.allowClientCode
867870
if self.onBeforeRemove and _.isFunction self.onBeforeRemove
868871
user = false
@@ -882,7 +885,7 @@ class FilesCollection
882885

883886

884887
# Method used to receive "first byte" of upload
885-
# and all file's meta-data, so
888+
# and all file's meta-data, so
886889
# it won't be transferred with every chunk
887890
# Basically it prepares everything
888891
# So user can pause/disconnect and
@@ -1087,7 +1090,7 @@ class FilesCollection
10871090
{mime, ext} = fileType buf
10881091
catch error
10891092
if not mime or not _.isString mime
1090-
mime = 'application/octet-stream'
1093+
mime = 'application/octet-stream'
10911094
return mime
10921095

10931096
###
@@ -1113,16 +1116,16 @@ class FilesCollection
11131116
@returns {Object}
11141117
###
11151118
_getUser: (http) ->
1116-
result =
1119+
result =
11171120
user: -> return null
11181121
userId: null
1119-
1122+
11201123
if Meteor.isServer
11211124
if http
11221125
mtok = null
11231126
if http.request.headers['x-mtok']
11241127
mtok = http.request.headers['x-mtok']
1125-
else
1128+
else
11261129
cookie = http.request.Cookies
11271130
if cookie.has 'x_mtok'
11281131
mtok = cookie.get 'x_mtok'
@@ -1298,7 +1301,7 @@ class FilesCollection
12981301
FSName = if @namingFunction then @namingFunction() else fileId
12991302
pathParts = url.split('/')
13001303
fileName = if (opts.name or opts.fileName) then (opts.name or opts.fileName) else pathParts[pathParts.length - 1] or FSName
1301-
1304+
13021305
{extension, extensionWithDot} = @_getExt fileName
13031306
opts.meta ?= {}
13041307
opts.path = "#{@storagePath}#{nodePath.sep}#{FSName}#{extensionWithDot}"
@@ -1476,6 +1479,7 @@ class FilesCollection
14761479
{Number|dynamic} streams - Quantity of parallel upload streams, default: 2
14771480
{Number|dynamic} chunkSize - Chunk size for upload
14781481
{String} transport - Upload transport `http` or `ddp`
1482+
{DDP} ddp - Custom DDP connection
14791483
{Function} onUploaded - Callback triggered when upload is finished, with two arguments `error` and `fileRef`
14801484
{Function} onStart - Callback triggered when upload is started after all successful validations, with two arguments `error` (always null) and `fileRef`
14811485
{Function} onError - Callback triggered on error in upload and/or FileReader, with two arguments `error` and `fileData`
@@ -1512,6 +1516,7 @@ class FilesCollection
15121516
EventEmitter.call @
15131517
console.info '[FilesCollection] [insert()]' if @collection.debug
15141518
self = @
1519+
@config.ddp = @collection.ddp
15151520
@config.meta ?= {}
15161521
@config.streams ?= 2
15171522
@config.streams = 2 if @config.streams < 1
@@ -1535,7 +1540,8 @@ class FilesCollection
15351540
onUploaded: Match.Optional Function
15361541
onProgress: Match.Optional Function
15371542
onBeforeUpload: Match.Optional Function
1538-
allowWebWorkers: Boolean
1543+
allowWebWorkers: Boolean,
1544+
ddp: Match.Any,
15391545
}
15401546

15411547
if not @config.fileName and not @config.file.name
@@ -1681,7 +1687,7 @@ class FilesCollection
16811687

16821688
if opts.binData
16831689
if @config.transport is 'ddp'
1684-
Meteor.call @collection._methodNames._Write, opts, (error) ->
1690+
@config.ddp.call @collection._methodNames._Write, opts, (error) ->
16851691
self.transferTime += (+new Date) - self.startTime[opts.chunkId]
16861692
if error
16871693
if self.result.state.get() isnt 'aborted'
@@ -1729,7 +1735,7 @@ class FilesCollection
17291735
fileId: @fileId
17301736

17311737
if @config.transport is 'ddp'
1732-
Meteor.call @collection._methodNames._Write, opts, ->
1738+
@config.ddp.call @collection._methodNames._Write, opts, ->
17331739
self.emitEvent 'end', arguments
17341740
return
17351741
else
@@ -1779,7 +1785,7 @@ class FilesCollection
17791785

17801786
else if FileReaderSync
17811787
fileReader = new FileReaderSync
1782-
1788+
17831789
self.emitEvent 'sendChunk', [{
17841790
data: {
17851791
bin: fileReader.readAsDataURL(chunk).split(',')[1]
@@ -1790,7 +1796,7 @@ class FilesCollection
17901796
self.emitEvent 'end', ['File API is not supported in this Browser!']
17911797
return
17921798

1793-
upload: ->
1799+
upload: ->
17941800
if @result.onPause.get()
17951801
return
17961802

@@ -1868,18 +1874,18 @@ class FilesCollection
18681874
return
18691875

18701876
if @config.transport is 'ddp'
1871-
Meteor.call @collection._methodNames._Start, opts, handleStart
1877+
@config.ddp.call @collection._methodNames._Start, opts, handleStart
18721878
else
18731879
opts.file.meta = fixJSONStringify opts.file.meta if opts.file?.meta
18741880
HTTP.call 'POST', "#{@collection.downloadRoute}/#{@collection.collectionName}/__upload", {
18751881
data: opts
1876-
headers:
1882+
headers:
18771883
'x-start': '1'
18781884
'x-mtok': Meteor.connection?._lastSessionId or null
18791885
}, handleStart
18801886
return
18811887

1882-
pipe: (func) ->
1888+
pipe: (func) ->
18831889
@pipes.push func
18841890
return @
18851891

@@ -2000,7 +2006,7 @@ class FilesCollection
20002006
@config._onEnd()
20012007
@state.set 'aborted'
20022008
console.timeEnd('insert ' + @config.fileData.name) if @config.debug
2003-
Meteor.call @config._Abort, @config.fileId
2009+
@config.ddp.call @config._Abort, @config.fileId
20042010
return
20052011
else undefined
20062012

@@ -2020,7 +2026,7 @@ class FilesCollection
20202026

20212027
if Meteor.isClient
20222028
if @allowClientCode
2023-
Meteor.call @_methodNames._Remove, selector, (callback or NOOP)
2029+
@config.ddp.call @_methodNames._Remove, selector, (callback or NOOP)
20242030
else
20252031
callback and callback new Meteor.Error 401, '[FilesCollection] [remove] Run code from client is not allowed!'
20262032
console.warn '[FilesCollection] [remove] Run code from client is not allowed!' if @debug
@@ -2031,7 +2037,7 @@ class FilesCollection
20312037
files.forEach (file) ->
20322038
self.unlink file
20332039
return
2034-
2040+
20352041
if @onAfterRemove
20362042
self = @
20372043
docs = files.fetch()
@@ -2313,7 +2319,7 @@ class FilesCollection
23132319
http.response.setHeader 'Content-Range', "bytes #{reqRange.start}-#{reqRange.end}/#{vRef.size}"
23142320
stream = readableStream or fs.createReadStream vRef.path, {start: reqRange.start, end: reqRange.end}
23152321
http.response.writeHead 206 if readableStream
2316-
stream.on('open', ->
2322+
stream.on('open', ->
23172323
http.response.writeHead 206
23182324
return
23192325
).on('error', streamErrorHandler
@@ -2390,4 +2396,4 @@ if Meteor.isClient
23902396
Export the FilesCollection class
23912397
###
23922398
Meteor.Files = FilesCollection
2393-
`export { FilesCollection }`
2399+
`export { FilesCollection }`

package.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@ Npm.depends({
2222
'request': '2.74.0',
2323
'throttle': '1.0.3',
2424
'file-type': '3.8.0'
25-
});
25+
});

0 commit comments

Comments
 (0)