Skip to content

Commit eef9ae2

Browse files
committed
Merge pull request #479 from RocketChat/upload-preview
Add dialog to preview and confirm file upload
2 parents f4909ad + 92ceb37 commit eef9ae2

File tree

5 files changed

+73
-17
lines changed

5 files changed

+73
-17
lines changed

client/lib/fileUpload.coffee

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
@fileUpload = (files) ->
2+
files = [].concat files
3+
4+
consume = ->
5+
file = files.pop()
6+
if not file?
7+
swal.close()
8+
return
9+
10+
reader = new FileReader()
11+
reader.onload = (event) ->
12+
image = event.target.result
13+
swal
14+
title: t('Upload_file_question')
15+
text: """
16+
<div class='upload-preview'>
17+
<div class='upload-preview-file' style='background-image: url(#{image})'></div>
18+
</div>
19+
<div class='upload-preview-title'>#{file.name}</div>
20+
"""
21+
showCancelButton: true
22+
closeOnConfirm: false
23+
closeOnCancel: false
24+
html: true
25+
, (isConfirm) ->
26+
consume()
27+
28+
if isConfirm isnt true
29+
return
30+
31+
newFile = new (FS.File)(file.file)
32+
if file.name?
33+
newFile.name(file.name)
34+
newFile.rid = Session.get('openedRoom')
35+
newFile.recId = Random.id()
36+
newFile.userId = Meteor.userId()
37+
Files.insert newFile, (error, fileObj) ->
38+
unless error
39+
toastr.success 'Upload succeeded!'
40+
41+
reader.readAsDataURL(file.file)
42+
43+
consume()

client/stylesheets/base.less

+16
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,22 @@ blockquote {
7575
}
7676
}
7777

78+
.upload-preview {
79+
background-color: #f5f5f5;
80+
.upload-preview-file {
81+
height: 200px;
82+
background-size: contain;
83+
background-repeat: no-repeat;
84+
background-position: center center;
85+
}
86+
}
87+
88+
.upload-preview-title {
89+
background-color: #eee;
90+
padding: 3px;
91+
border-radius: 0 0 5px 5px;
92+
}
93+
7894
.flex-center {
7995
display: -webkit-flex;
8096
display: flex;

client/views/app/room.coffee

+11-17
Original file line numberDiff line numberDiff line change
@@ -349,20 +349,15 @@ Template.room.events
349349
return
350350

351351
items = e.originalEvent.clipboardData.items
352+
files = []
352353
for item in items
353354
if item.kind is 'file' and item.type.indexOf('image/') isnt -1
354355
e.preventDefault()
356+
files.push
357+
file: item.getAsFile()
358+
name: 'Clipboard'
355359

356-
blob = item.getAsFile()
357-
358-
newFile = new (FS.File)(blob)
359-
newFile.name('Clipboard')
360-
newFile.rid = Session.get('openedRoom')
361-
newFile.recId = Random.id()
362-
newFile.userId = Meteor.userId()
363-
Files.insert newFile, (error, fileObj) ->
364-
unless error
365-
toastr.success 'Upload from clipboard succeeded!'
360+
fileUpload files
366361

367362
'keydown .input-message': (event) ->
368363
Template.instance().chatMessages.keydown(@_id, event, Template.instance())
@@ -547,14 +542,13 @@ Template.room.events
547542
'dropped .dropzone-overlay': (e) ->
548543
e.currentTarget.parentNode.classList.remove 'over'
549544

545+
files = []
550546
FS?.Utility?.eachFile e, (file) ->
551-
newFile = new (FS.File)(file)
552-
newFile.rid = Session.get('openedRoom')
553-
newFile.recId = Random.id()
554-
newFile.userId = Meteor.userId()
555-
Files.insert newFile, (error, fileObj) ->
556-
unless error
557-
toastr.success 'Upload succeeded!'
547+
files.push
548+
file: file
549+
name: file.name
550+
551+
fileUpload files
558552

559553
'click .deactivate': ->
560554
username = Session.get('showUserInfo')

i18n/en.i18n.json

+1
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@
184184
"The_field_is_required" : "The field %s is required.",
185185
"True" : "True",
186186
"Unnamed" : "Unnamed",
187+
"Upload_file_question": "Upload file?",
187188
"Use_initials_avatar" : "Use your username initials",
188189
"use_menu" : "Use the side menu to access your rooms and chats",
189190
"Use_service_avatar" : "Use %s avatar",

i18n/pt.i18n.json

+2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"Created_at" : "Data criação",
4141
"Direct_Messages" : "Mensagens Diretas",
4242
"Deleted" : "Deletado!",
43+
"Drop_to_upload_file": "Largue para enviar arquivos",
4344
"Duplicate_private_group_name" : "Já existe um Grupo Privado com nome '%s'",
4445
"Duplicate_channel_name" : "Já existe um Canal com nome '%s'",
4546
"edited" : "editado",
@@ -145,6 +146,7 @@
145146
"strike" : "tachado",
146147
"Submit" : "Enviar",
147148
"The_field_is_required" : "O campo %s é obrigatório.",
149+
"Upload_file_question": "Enviar arquivo?",
148150
"Use_initials_avatar" : "Usar as iniciais do seu nome de usuário",
149151
"use_menu" : "Utilize o menu à esquerda para acessar suas salas",
150152
"Use_service_avatar" : "Use o avatar de %s",

0 commit comments

Comments
 (0)