Skip to content

Commit 4016c9e

Browse files
Custom DDP connection support
1 parent dd59d88 commit 4016c9e

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}"
@@ -1473,6 +1476,7 @@ class FilesCollection
14731476
{Number|dynamic} streams - Quantity of parallel upload streams, default: 2
14741477
{Number|dynamic} chunkSize - Chunk size for upload
14751478
{String} transport - Upload transport `http` or `ddp`
1479+
{DDP} ddp - Custom DDP connection
14761480
{Function} onUploaded - Callback triggered when upload is finished, with two arguments `error` and `fileRef`
14771481
{Function} onStart - Callback triggered when upload is started after all successful validations, with two arguments `error` (always null) and `fileRef`
14781482
{Function} onError - Callback triggered on error in upload and/or FileReader, with two arguments `error` and `fileData`
@@ -1509,6 +1513,7 @@ class FilesCollection
15091513
EventEmitter.call @
15101514
console.info '[FilesCollection] [insert()]' if @collection.debug
15111515
self = @
1516+
@config.ddp = @collection.ddp
15121517
@config.meta ?= {}
15131518
@config.streams ?= 2
15141519
@config.streams = 2 if @config.streams < 1
@@ -1532,7 +1537,8 @@ class FilesCollection
15321537
onUploaded: Match.Optional Function
15331538
onProgress: Match.Optional Function
15341539
onBeforeUpload: Match.Optional Function
1535-
allowWebWorkers: Boolean
1540+
allowWebWorkers: Boolean,
1541+
ddp: Match.Any,
15361542
}
15371543

15381544
if not @config.fileName and not @config.file.name
@@ -1678,7 +1684,7 @@ class FilesCollection
16781684

16791685
if opts.binData
16801686
if @config.transport is 'ddp'
1681-
Meteor.call @collection._methodNames._Write, opts, (error) ->
1687+
@config.ddp.call @collection._methodNames._Write, opts, (error) ->
16821688
self.transferTime += (+new Date) - self.startTime[opts.chunkId]
16831689
if error
16841690
if self.result.state.get() isnt 'aborted'
@@ -1726,7 +1732,7 @@ class FilesCollection
17261732
fileId: @fileId
17271733

17281734
if @config.transport is 'ddp'
1729-
Meteor.call @collection._methodNames._Write, opts, ->
1735+
@config.ddp.call @collection._methodNames._Write, opts, ->
17301736
self.emitEvent 'end', arguments
17311737
return
17321738
else
@@ -1776,7 +1782,7 @@ class FilesCollection
17761782

17771783
else if FileReaderSync
17781784
fileReader = new FileReaderSync
1779-
1785+
17801786
self.emitEvent 'sendChunk', [{
17811787
data: {
17821788
bin: fileReader.readAsDataURL(chunk).split(',')[1]
@@ -1787,7 +1793,7 @@ class FilesCollection
17871793
self.emitEvent 'end', ['File API is not supported in this Browser!']
17881794
return
17891795

1790-
upload: ->
1796+
upload: ->
17911797
if @result.onPause.get()
17921798
return
17931799

@@ -1865,18 +1871,18 @@ class FilesCollection
18651871
return
18661872

18671873
if @config.transport is 'ddp'
1868-
Meteor.call @collection._methodNames._Start, opts, handleStart
1874+
@config.ddp.call @collection._methodNames._Start, opts, handleStart
18691875
else
18701876
opts.file.meta = fixJSONStringify opts.file.meta if opts.file?.meta
18711877
HTTP.call 'POST', "#{@collection.downloadRoute}/#{@collection.collectionName}/__upload", {
18721878
data: opts
1873-
headers:
1879+
headers:
18741880
'x-start': '1'
18751881
'x-mtok': Meteor.connection?._lastSessionId or null
18761882
}, handleStart
18771883
return
18781884

1879-
pipe: (func) ->
1885+
pipe: (func) ->
18801886
@pipes.push func
18811887
return @
18821888

@@ -1997,7 +2003,7 @@ class FilesCollection
19972003
@config._onEnd()
19982004
@state.set 'aborted'
19992005
console.timeEnd('insert ' + @config.fileData.name) if @config.debug
2000-
Meteor.call @config._Abort, @config.fileId
2006+
@config.ddp.call @config._Abort, @config.fileId
20012007
return
20022008
else undefined
20032009

@@ -2017,7 +2023,7 @@ class FilesCollection
20172023

20182024
if Meteor.isClient
20192025
if @allowClientCode
2020-
Meteor.call @_methodNames._Remove, selector, (callback or NOOP)
2026+
@config.ddp.call @_methodNames._Remove, selector, (callback or NOOP)
20212027
else
20222028
callback and callback new Meteor.Error 401, '[FilesCollection] [remove] Run code from client is not allowed!'
20232029
console.warn '[FilesCollection] [remove] Run code from client is not allowed!' if @debug
@@ -2028,7 +2034,7 @@ class FilesCollection
20282034
files.forEach (file) ->
20292035
self.unlink file
20302036
return
2031-
2037+
20322038
if @onAfterRemove
20332039
self = @
20342040
docs = files.fetch()
@@ -2310,7 +2316,7 @@ class FilesCollection
23102316
http.response.setHeader 'Content-Range', "bytes #{reqRange.start}-#{reqRange.end}/#{vRef.size}"
23112317
stream = readableStream or fs.createReadStream vRef.path, {start: reqRange.start, end: reqRange.end}
23122318
http.response.writeHead 206 if readableStream
2313-
stream.on('open', ->
2319+
stream.on('open', ->
23142320
http.response.writeHead 206
23152321
return
23162322
).on('error', streamErrorHandler
@@ -2387,4 +2393,4 @@ if Meteor.isClient
23872393
Export the FilesCollection class
23882394
###
23892395
Meteor.Files = FilesCollection
2390-
`export { FilesCollection }`
2396+
`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)