Skip to content

Commit

Permalink
Merge pull request #7 from VeliovGroup/dev_1
Browse files Browse the repository at this point in the history
 - Avoid any action on `insert()` method if `file` is empty
  • Loading branch information
dr-dimitru committed Apr 23, 2015
2 parents edd8547 + f25f6c9 commit 17a2ab3
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 119 deletions.
2 changes: 1 addition & 1 deletion .versions
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ [email protected]
[email protected]
[email protected]
[email protected]
ostrio:[email protected].2
ostrio:[email protected].3
ostrio:[email protected]
[email protected]
[email protected]
Expand Down
234 changes: 117 additions & 117 deletions files.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,6 @@ class Meteor.Files
fs.appendFileSync pathName + '_1.' + ext, fs.readFileSync(pathName + '_' + i + '.' + ext), 'binary'
fs.unlink pathName + '_' + i + '.' + ext
i++

fs.renameSync pathName + '_1.' + ext, path

fs.chmod path, self.permissions
Expand Down Expand Up @@ -436,113 +435,104 @@ class Meteor.Files
check onBeforeUpload, Match.Optional Function
check streams, Match.Optional Number

self = @
result =
onPause: new ReactiveVar false
continueFrom: []
pause: () ->
@onPause.set true
continue: () ->
@onPause.set false
for func in @continueFrom
func.call null
@continueFrom = []
toggle: () ->
if @onPause.get() then @continue() else @pause()
progress: new ReactiveVar 0

Tracker.autorun ->
if Meteor.status().connected
result.continue()
console.info "Meteor.Files Debugger: Connection established continue() upload" if self.debug
else
result.pause()
console.info "Meteor.Files Debugger: Connection error set upload on pause()" if self.debug

streams = 1 if not streams
totalSentChunks = 0

fileData =
size: file.size
type: file.type
name: file.name
ext: file.name.split('.').pop()
extension: file.name.split('.').pop()

file = _.extend file, fileData

console.time('insert') if @debug

randFileName = @namingFunction.call null, true
partSize = Math.ceil file.size / streams
parts = []
uploaded = 0
last = false

window.onbeforeunload = (e) ->
message = if _.isFunction(self.onbeforeunloadMessage) then self.onbeforeunloadMessage.call(null) else self.onbeforeunloadMessage

if e
e.returnValue = message
return message

i = 1
while i <= streams
parts.push
from: partSize * (i-1)
to: partSize * i
size: partSize
part: i
chunksQty: if @chunkSize < partSize then Math.ceil(partSize / @chunkSize) else 1
i++


end = (error, data) ->
console.timeEnd('insert') if self.debug
window.onbeforeunload = null
result.progress.set 0
onUploaded and onUploaded.call self, error, data

if onBeforeUpload
chres = onBeforeUpload.call file
if chres isnt true
end new Meteor.Error(500, if _.isString(chres) then chres else "onBeforeUpload() returned false"), null
return false

upload = (filePart, part, chunksQtyInPart, fileReader) ->
currentChunk = 1
first = true
console.time("insertPart#{part}") if @debug

fileReader.onload = (chunk) ->
++totalSentChunks
progress = (uploaded / file.size) * 100
result.progress.set progress
onProgress and onProgress(progress)

uploaded += self.chunkSize
arrayBuffer = chunk.srcElement or chunk.target
unitArray = new Uint8Array arrayBuffer.result
last = (part is streams and currentChunk >= chunksQtyInPart)


if chunksQtyInPart is 1
Meteor.call self.methodNames.MeteorFileWrite, unitArray, fileData, meta, first, chunksQtyInPart, currentChunk, totalSentChunks, randFileName, part, streams, file.size, (error, data) ->
if data.last
end error, data
if file
self = @
result =
onPause: new ReactiveVar false
continueFrom: []
pause: () ->
@onPause.set true
continue: () ->
@onPause.set false
for func in @continueFrom
func.call null
@continueFrom = []
toggle: () ->
if @onPause.get() then @continue() else @pause()
progress: new ReactiveVar 0

Tracker.autorun ->
if Meteor.status().connected
result.continue()
console.info "Meteor.Files Debugger: Connection established continue() upload" if self.debug
else
Meteor.call self.methodNames.MeteorFileWrite, unitArray, fileData, meta, first, chunksQtyInPart, currentChunk, totalSentChunks, randFileName, part, streams, file.size, (error, data)->
if not result.onPause.get()
if data.chunk + 1 <= chunksQtyInPart
from = currentChunk * self.chunkSize
to = from + self.chunkSize

fileReader.readAsArrayBuffer filePart.slice from, to
currentChunk = ++data.chunk
else if data.last
result.pause()
console.info "Meteor.Files Debugger: Connection error set upload on pause()" if self.debug

streams = 1 if not streams
totalSentChunks = 0

fileData =
size: file.size
type: file.type
name: file.name
ext: file.name.split('.').pop()
extension: file.name.split('.').pop()

file = _.extend file, fileData

console.time('insert') if @debug

randFileName = @namingFunction.call null, true
partSize = Math.ceil file.size / streams
parts = []
uploaded = 0
last = false

window.onbeforeunload = (e) ->
message = if _.isFunction(self.onbeforeunloadMessage) then self.onbeforeunloadMessage.call(null) else self.onbeforeunloadMessage

if e
e.returnValue = message
return message

i = 1
while i <= streams
parts.push
from: partSize * (i-1)
to: partSize * i
size: partSize
part: i
chunksQty: if @chunkSize < partSize then Math.ceil(partSize / @chunkSize) else 1
i++


end = (error, data) ->
console.timeEnd('insert') if self.debug
window.onbeforeunload = null
result.progress.set 0
onUploaded and onUploaded.call self, error, data

if onBeforeUpload
chres = onBeforeUpload.call file
if chres isnt true
end new Meteor.Error(500, if _.isString(chres) then chres else "onBeforeUpload() returned false"), null
return false

upload = (filePart, part, chunksQtyInPart, fileReader) ->
currentChunk = 1
first = true
console.time("insertPart#{part}") if @debug

fileReader.onload = (chunk) ->
++totalSentChunks
progress = (uploaded / file.size) * 100
result.progress.set progress
onProgress and onProgress(progress)

uploaded += self.chunkSize
arrayBuffer = chunk.srcElement or chunk.target
unitArray = new Uint8Array arrayBuffer.result
last = (part is streams and currentChunk >= chunksQtyInPart)


if chunksQtyInPart is 1
Meteor.call self.methodNames.MeteorFileWrite, unitArray, fileData, meta, first, chunksQtyInPart, currentChunk, totalSentChunks, randFileName, part, streams, file.size, (error, data) ->
if data.last
end error, data
else
result.continueFrom.push () ->
else
Meteor.call self.methodNames.MeteorFileWrite, unitArray, fileData, meta, first, chunksQtyInPart, currentChunk, totalSentChunks, randFileName, part, streams, file.size, (error, data)->
if not result.onPause.get()
if data.chunk + 1 <= chunksQtyInPart
from = currentChunk * self.chunkSize
to = from + self.chunkSize
Expand All @@ -551,17 +541,27 @@ class Meteor.Files
currentChunk = ++data.chunk
else if data.last
end error, data
first = false

fileReader.readAsArrayBuffer filePart.slice 0, self.chunkSize

for part, i in parts
part = parts[i]
fileReader = new FileReader
upload.call null, file.slice(part.from, part.to), i + 1, part.chunksQty, fileReader
--i

return result
else
result.continueFrom.push () ->
if data.chunk + 1 <= chunksQtyInPart
from = currentChunk * self.chunkSize
to = from + self.chunkSize

fileReader.readAsArrayBuffer filePart.slice from, to
currentChunk = ++data.chunk
else if data.last
end error, data
first = false

fileReader.readAsArrayBuffer filePart.slice 0, self.chunkSize

for part, i in parts
part = parts[i]
fileReader = new FileReader
upload.call null, file.slice(part.from, part.to), i + 1, part.chunksQty, fileReader
--i

return result
else
undefined

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.1.2',
version: '1.1.3',
summary: 'Upload, Store and Download small and huge 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 17a2ab3

Please sign in to comment.