Skip to content

Commit

Permalink
Merge pull request #518 from RocketChat/jalik-ufs
Browse files Browse the repository at this point in the history
Jalik ufs
  • Loading branch information
rodrigok committed Aug 21, 2015
2 parents 532ed7a + a7f1511 commit 43ab6c6
Show file tree
Hide file tree
Showing 8 changed files with 265 additions and 85 deletions.
8 changes: 2 additions & 6 deletions .meteor/packages
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,6 @@ konecty:multiple-instances-status
konecty:nrr
konecty:user-presence

cfs:standard-packages
cfs:gridfs
#cfs:filesystem
#cfs:s3
#cfs:dropbox

chrismbeckett:toastr
dispatch:run-as-user
francocatena:status
Expand Down Expand Up @@ -81,4 +75,6 @@ yasaricli:slugify
yasinuslu:blaze-meta
rocketchat:colors
raix:push@2.6.13-rc.1
jalik:ufs
jalik:ufs-gridfs
monbro:mongodb-mapreduce-aggregation
18 changes: 2 additions & 16 deletions .meteor/versions
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,7 @@ blaze@2.1.2
blaze-tools@1.0.3
boilerplate-generator@1.0.3
callback-hook@1.0.3
cfs:access-point@0.1.49
cfs:base-package@0.0.30
cfs:collection@0.5.5
cfs:collection-filters@0.2.4
cfs:data-man@0.0.6
cfs:file@0.1.17
cfs:gridfs@0.0.33
cfs:http-methods@0.0.29
cfs:http-publish@0.0.13
cfs:power-queue@0.9.11
cfs:reactive-list@0.0.9
cfs:reactive-property@0.0.4
cfs:standard-packages@0.5.9
cfs:storage-adapter@0.2.2
cfs:tempstore@0.1.5
cfs:upload-http@0.0.20
cfs:worker@0.1.4
check@1.0.5
chrismbeckett:toastr@2.1.2_1
coffeescript@1.0.6
Expand All @@ -52,6 +36,8 @@ html-tools@1.0.4
htmljs@1.0.4
http@1.1.0
id-map@1.0.3
jalik:ufs@0.2.7
jalik:ufs-gridfs@0.1.0
jparker:crypto-core@0.1.0
jparker:crypto-md5@0.1.1
jparker:gravatar@0.3.1
Expand Down
115 changes: 100 additions & 15 deletions client/lib/fileUpload.coffee
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
readAsDataURL = (file, callback) ->
reader = new FileReader()
reader.onload = (ev) ->
callback ev.target.result, file

reader.readAsDataURL file

readAsArrayBuffer = (file, callback) ->
reader = new FileReader()
reader.onload = (ev) ->
callback ev.target.result, file

reader.readAsArrayBuffer file


@fileUpload = (files) ->
files = [].concat files

Expand All @@ -7,10 +22,7 @@
swal.close()
return

reader = new FileReader()
reader.onload = (event) ->
fileContent = event.target.result

readAsDataURL file.file, (fileContent) ->
text = ''

if file.type is 'audio'
Expand Down Expand Up @@ -44,16 +56,89 @@
if isConfirm isnt true
return

newFile = new (FS.File)(file.file)
if file.name?
newFile.name(file.name)
newFile.rid = Session.get('openedRoom')
newFile.recId = Random.id()
newFile.userId = Meteor.userId()
Files.insert newFile, (error, fileObj) ->
unless error
toastr.success 'Upload succeeded!'

reader.readAsDataURL(file.file)
readAsArrayBuffer file.file, (data) ->
record =
name: file.name or file.file.name
size: file.file.size
type: file.file.type

upload = new UploadFS.Uploader
store: Meteor.fileStore
data: data
file: record
onError: (err) ->
console.error(err)

onComplete: (file) ->
self = this
Meteor.call 'sendMessage', {
_id: Random.id()
rid: Session.get('openedRoom')
msg: """
File Uploaded: *#{file.name}*
#{file.url}
"""
file:
_id: file._id
}, ->
Meteor.setTimeout ->
uploading = Session.get 'uploading'
if uploading?
item = _.findWhere(uploading, {id: self.id})
Session.set 'uploading', _.without(uploading, item)
, 2000

upload.id = Random.id()

# // Reactive method to get upload progress
Tracker.autorun (c) ->
uploading = undefined
cancel = undefined

Tracker.nonreactive ->
cancel = Session.get "uploading-cancel-#{upload.id}"
uploading = Session.get 'uploading'

if cancel
return c.stop()

uploading ?= []

item = _.findWhere(uploading, {id: upload.id})

if not item?
item =
id: upload.id
name: upload.getFile().name

uploading.push item

item.percentage = Math.round(upload.getProgress() * 100)
Session.set 'uploading', uploading

upload.start();

# upload.stop();

Tracker.autorun (c) ->
cancel = Session.get "uploading-cancel-#{upload.id}"
if cancel
upload.stop()
upload.abort()
c.stop()

uploading = Session.get 'uploading'
if uploading?
item = _.findWhere(uploading, {id: upload.id})
if item?
item.percentage = 0
Session.set 'uploading', uploading

Meteor.setTimeout ->
uploading = Session.get 'uploading'
if uploading?
item = _.findWhere(uploading, {id: upload.id})
Session.set 'uploading', _.without(uploading, item)
, 1000

consume()
42 changes: 42 additions & 0 deletions client/stylesheets/base.less
Original file line number Diff line number Diff line change
Expand Up @@ -1988,6 +1988,48 @@ a.github-fork {
}
}

.upload-progress {
position: absolute;
top: 60px;
background-color: #b2d5c9;
width: 100%;
z-index: 11;
color: white;
font-weight: bold;
text-shadow: 1px 1px 0px rgba(0,0,0,.2);

.upload-progress-item {
position: relative;
line-height: 24px;
padding: 0 10px;
border-bottom: 1px solid #6a8179;

&:first-child {
border-top: 1px solid #6a8179;
}

.upload-progress-progress {
position: absolute;
left: 0px;
height: 100%;
width: 0%;
background-color: #4c9789;
z-index: -1;
.transition(width, 1s, ease-out);
}

> a {
float: right;
text-transform: uppercase;

&:hover {
color: #eee;
cursor: pointer;
}
}
}
}

// change to page-messages
.messages-container {
position: absolute;
Expand Down
14 changes: 13 additions & 1 deletion client/views/app/room.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ Template.room.helpers
windowId: ->
return "chat-window-#{this._id}"

uploading: ->
return Session.get 'uploading'

usersTyping: ->
users = MsgTyping.get @_id
if users.length is 0
Expand Down Expand Up @@ -316,6 +319,9 @@ Template.room.events
"touchcancel .message": (e, t) ->
Meteor.clearTimeout t.touchtime

"click .upload-progress-item > a": ->
Session.set "uploading-cancel-#{this.id}", true

"click .flex-tab .more": (event) ->
if (Session.get('flexOpened'))
Session.set('rtcLayoutmode', 0)
Expand Down Expand Up @@ -570,7 +576,13 @@ Template.room.events
e.currentTarget.parentNode.classList.remove 'over'

files = []
FS?.Utility?.eachFile e, (file) ->

evt = e.originalEvent or e

if not evt.target.files or evt.target.files.length is 0
evt.target.files = if evt.dataTransfer then evt.dataTransfer.files else []

for file in evt.target.files
files.push
file: file
name: file.name
Expand Down
11 changes: 11 additions & 0 deletions client/views/app/room.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ <h2>
{{/if}}
</h2>
</header>
<div class="upload-progress">
{{#each uploading}}
<div class="upload-progress-item">
<div class="upload-progress-progress" style="width: {{percentage}}%;"></div>
{{name}}... {{percentage}}%
<a>
cancel
</a>
</div>
{{/each}}
</div>
<div class="messages-box">
<div class="wrapper">
<ul>
Expand Down
65 changes: 18 additions & 47 deletions lib/fileUpload.coffee
Original file line number Diff line number Diff line change
@@ -1,51 +1,22 @@
if FS?
if UploadFS?
@fileCollection = new Mongo.Collection 'rocketchat_uploads'

@fileStore = new FS.Store.GridFS 'files'
fileCollection.allow
insert: (userId, doc) ->
return userId

fileStore.on 'stored', Meteor.bindEnvironment (storeName, fileObj) ->
Meteor.runAsUser fileObj.userId, ->
if not ChatMessage.findOne(fileObj.recId)?
Meteor.call 'sendMessage',
_id: fileObj.recId
rid: fileObj.rid
msg: """
File Uploaded: *#{fileObj.original.name}*
#{Meteor.absoluteUrl()}cfs/files/Files/#{fileObj._id}
"""
file:
_id: fileObj._id
, {upsert: true}
update: (userId, doc) ->
return userId is doc.userId

@Files = new FS.Collection 'Files',
stores: [fileStore],
filter:
maxSize: 2097152,
allow:
contentTypes: ['image/*', 'audio/*']
onInvalid: (message) ->
if Meteor.isClient
toastr.error message
else
console.log message
return
remove: (userId, doc) ->
return userId is doc.userId

# Allow rules
Files.allow
insert: ->
true
update: ->
false
download: ->
true
remove: ->
false

Files.deny
insert: ->
false
update: ->
true
remove: ->
true
download: ->
false
Meteor.fileStore = new UploadFS.store.GridFS
collection: fileCollection
name: 'rocketchat_uploads'
collectionName: 'rocketchat_uploads'
filter: new UploadFS.Filter
maxSize: 2097152
contentTypes: ['image/*', 'audio/*']
onFinishUpload: ->
console.log arguments
Loading

0 comments on commit 43ab6c6

Please sign in to comment.