Skip to content

Commit

Permalink
v1.3.11
Browse files Browse the repository at this point in the history
 - Fix issue mentioned in #41
 - Add throttle #40
  • Loading branch information
dr-dimitru committed Oct 30, 2015
1 parent 5c8d069 commit 53ede40
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 49 deletions.
28 changes: 14 additions & 14 deletions .versions
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ boilerplate-generator@1.0.4
caching-compiler@1.0.0
caching-html-compiler@1.0.2
callback-hook@1.0.4
check@1.0.6
coffeescript@1.0.10
check@1.1.0
coffeescript@1.0.11
ddp@1.2.2
ddp-client@1.2.1
ddp-common@1.2.1
ddp-server@1.2.1
ddp-common@1.2.2
ddp-server@1.2.2
deps@1.0.9
diff-sequence@1.0.1
ecmascript@0.1.5
ecmascript-collections@0.1.6
ecmascript@0.1.6
ecmascript-runtime@0.2.6
ejson@1.0.7
geojson-utils@1.0.4
html-tools@1.0.5
Expand All @@ -36,30 +36,30 @@ iron:router@1.0.12
iron:url@1.0.11
jquery@1.11.4
logging@1.0.8
meteor@1.1.9
meteor@1.1.10
minifiers@1.1.7
minimongo@1.0.10
mongo@1.1.2
mongo@1.1.3
mongo-id@1.0.1
npm-mongo@1.4.39_1
observe-sequence@1.0.7
ordered-dict@1.0.4
ostrio:cookies@2.0.1
ostrio:files@1.3.10
promise@0.5.0
random@1.0.4
reactive-dict@1.1.2
ostrio:files@1.3.11
promise@0.5.1
random@1.0.5
reactive-dict@1.1.3
reactive-var@1.0.6
retry@1.0.4
routepolicy@1.0.6
sha@1.0.4
spacebars@1.0.7
spacebars-compiler@1.0.7
templating@1.1.4
templating@1.1.5
templating-tools@1.0.0
tracker@1.0.9
ui@1.0.8
underscore@1.0.4
url@1.0.5
webapp@1.2.2
webapp@1.2.3
webapp-hashing@1.0.5
71 changes: 37 additions & 34 deletions files.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ if Meteor.isServer
###
@description Require "fs-extra" npm package
###
fs = Npm.require "fs-extra"
request = Npm.require "request"
fs = Npm.require "fs-extra"
request = Npm.require "request"
Throttle = Npm.require "throttle"
###
@var {object} bound - Meteor.bindEnvironment aka Fiber wrapper
###
bound = Meteor.bindEnvironment (callback) ->
return callback()
bound = Meteor.bindEnvironment (callback) -> return callback()

###
@object
Expand Down Expand Up @@ -158,9 +157,9 @@ class Meteor.Files
check @schema, Object
check @public, Boolean
check @strict, Boolean
check @throttle, Match.OneOf false, Number
check @protected, Match.OneOf Boolean, Function
check @chunkSize, Number
check @throttle, Number
check @permissions, Number
check @storagePath, String
check @downloadRoute, String
Expand Down Expand Up @@ -253,6 +252,7 @@ class Meteor.Files
throw new Meteor.Error 401, '[Meteor.Files] [remove()] Run code from client is not allowed!'

_methods[self.methodNames.MeteorFileWrite] = (unitArray, fileData, meta = {}, first, chunksQty, currentChunk, totalSentChunks, randFileName, part, partsQty, fileSize) ->
@unblock()
check part, Number
check meta, Match.Optional Object
check first, Boolean
Expand All @@ -273,8 +273,6 @@ class Meteor.Files
if isUploadAllowed isnt true
throw new Meteor.Error(403, if _.isString(isUploadAllowed) then isUploadAllowed else "@onBeforeUpload() returned false")

@unblock()

i = 0
binary = ''
while i < unitArray.byteLength
Expand All @@ -301,10 +299,15 @@ class Meteor.Files
result.chunk = currentChunk
result.last = last

if first
fs.outputFileSync pathPart, binary, 'binary'
else
fs.appendFileSync pathPart, binary, 'binary'
try
if first
fs.outputFileSync pathPart, binary, 'binary'
else
fs.appendFileSync pathPart, binary, 'binary'
catch e
error = new Meteor.Error 500, "Unfinished upload (probably caused by server reboot)", e
console.error error
return error

console.info "Meteor.Files Debugger: The part ##{part} of file #{fileName} (binary) was saved to #{pathPart}" if (chunksQty is currentChunk) and self.debug

Expand Down Expand Up @@ -556,20 +559,20 @@ class Meteor.Files

request.get(url).on('error', (error)->
throw new Meteor.Error 500, "Error on [load(#{url}, #{opts})]; Error:" + JSON.stringify error
).on('response', (response) ->
bound ->
result = self.dataToSchema
name: fileName
path: path
meta: opts.meta
type: response.headers['content-type']
size: response.headers['content-length']
extension: extension

console.info "Meteor.Files Debugger: The file #{fileName} (binary) was loaded to #{@collectionName}" if @debug
).on('response', (response) -> bound ->

result._id = self.collection.insert _.clone result
callback and callback null, result
result = self.dataToSchema
name: fileName
path: path
meta: opts.meta
type: response.headers['content-type']
size: response.headers['content-length']
extension: extension

console.info "Meteor.Files Debugger: The file #{fileName} (binary) was loaded to #{@collectionName}" if @debug

result._id = self.collection.insert _.clone result
callback and callback null, result

).pipe fs.createOutputStream path

Expand Down Expand Up @@ -999,10 +1002,10 @@ class Meteor.Files

if partiral or (http.params.query.play and http.params.query.play == 'true')
reqRange = {start, end}
if isNaN(start) and not isNaN(end)
if isNaN(start) and not isNaN end
reqRange.start = end - take
reqRange.end = end
if not isNaN(start) and isNaN(end)
if not isNaN(start) and isNaN end
reqRange.start = start
reqRange.end = start + take

Expand Down Expand Up @@ -1050,27 +1053,27 @@ class Meteor.Files
when '200'
console.info "Meteor.Files Debugger: [download(#{http}, #{version})] [200]: #{fileRef.path}" if @debug
stream = fs.createReadStream fileRef.path
self = @
stream.on('open', ->
stream.on('open', =>
http.response.writeHead 200
if(self.throttle)
stream.pipe(new Throttle({bps:self.throttle,chunksize:self.chunkSize})).pipe http.response
if @throttle
stream.pipe( new Throttle {bps: @throttle, chunksize: @chunkSize}
).pipe http.response
else
stream.pipe http.response
).on 'error', streamErrorHandler
break
when '206'
self = @
console.info "Meteor.Files Debugger: [download(#{http}, #{version})] [206]: #{fileRef.path}" if @debug
http.response.setHeader 'Content-Range', "bytes #{reqRange.start}-#{reqRange.end}/#{fileRef.size}"
http.response.setHeader 'Content-Length', take
http.response.setHeader 'Transfer-Encoding', 'chunked'
if(@throttle)
if @throttle
stream = fs.createReadStream fileRef.path, {start: reqRange.start, end: reqRange.end}
stream.on('open', -> http.response.writeHead 206
).on('error', streamErrorHandler
).on('end', -> http.response.end())
.pipe(new Throttle({bps:self.throttle,chunksize:self.chunkSize})).pipe http.response
).on('end', -> http.response.end()
).pipe( new Throttle {bps: @throttle, chunksize: @chunkSize}
).pipe http.response
else
stream = fs.createReadStream fileRef.path, {start: reqRange.start, end: reqRange.end}
stream.on('open', -> http.response.writeHead 206
Expand Down
2 changes: 1 addition & 1 deletion package.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package.describe({
name: 'ostrio:files',
version: '1.3.10',
version: '1.3.11',
summary: 'Upload, Store and Stream (Video & Audio streaming) files to/from file system (FS) via DDP and HTTP',
git: 'https://github.com/VeliovGroup/Meteor-Files',
documentation: 'README.md'
Expand Down

0 comments on commit 53ede40

Please sign in to comment.