multi-file sending

master
f0x 5 years ago
parent f62cf21ab0
commit d4d1de4e95

@ -12,6 +12,9 @@ let Event = create({
getInitialState: function() { getInitialState: function() {
let hs = this.props.client.baseUrl let hs = this.props.client.baseUrl
let event = this.props.event let event = this.props.event
if (event.content.url == undefined) {
return {url: {media: null, thumb: null}}
}
let media_mxc = event.content.url.slice(6) let media_mxc = event.content.url.slice(6)
let thumb_mxc = media_mxc let thumb_mxc = media_mxc
if (event.content.info != undefined && event.content.info.thumbnail_info != undefined) { if (event.content.info != undefined && event.content.info.thumbnail_info != undefined) {

@ -20,11 +20,11 @@ let fileUpload = create({
if (file.type.startsWith("image/")) { if (file.type.startsWith("image/")) {
let reader = new FileReader() let reader = new FileReader()
reader.onloadend = () => { reader.onloadend = () => {
let file = { let fileObject = {
file: file, file: file,
preview: reader.result preview: reader.result
} }
this.props.addUpload(file) this.props.addUpload(fileObject)
} }
reader.readAsDataURL(file) reader.readAsDataURL(file)
} }

@ -6,6 +6,7 @@ const Promise = require('bluebird')
const colorConvert = require('color-convert') const colorConvert = require('color-convert')
const sanitize = require('sanitize-html') const sanitize = require('sanitize-html')
const riot = require('../lib/riot-utils.js')
const FileUpload = require('./fileUpload.js') const FileUpload = require('./fileUpload.js')
let input = create({ let input = create({
@ -40,7 +41,6 @@ let input = create({
addUpload: function(upload) { addUpload: function(upload) {
let uploads = this.state.uploads let uploads = this.state.uploads
uploads.push(upload) uploads.push(upload)
console.log(uploads)
this.setState({uploads: uploads}) this.setState({uploads: uploads})
}, },
@ -67,26 +67,97 @@ let input = create({
send: function(e) { send: function(e) {
let msg = e.target.value let msg = e.target.value
if (msg.startsWith('/')) { if (msg.trim().length != 0) {
// Handle other commands //TODO: parse markdown (commonmark?)
let parts = msg.split(' ') if (msg.startsWith('/')) {
let command = parts[0] // Handle other commands
let result = handleCommands(command, parts) let parts = msg.split(' ')
if (result != null) { let command = parts[0]
if (result.type == "html") { let result = handleCommands(command, parts)
this.sendHTML(result.content) if (result != null) {
} else { if (result.type == "html") {
this.sendPlain(result.content) this.sendHTML(result.content)
} else {
this.sendPlain(result.content)
}
} }
} else {
this.sendPlain(msg)
} }
} else { }
this.sendPlain(msg)
if (this.state.uploads.length > 0) {
this.uploadFiles(this.state.uploads)
this.setState({uploads: []})
} }
e.target.value = "" e.target.value = ""
e.preventDefault() e.preventDefault()
this.resize_textarea_delayed(e) this.resize_textarea_delayed(e)
}, },
uploadFiles: function(uploads) {
let client = this.props.client
Promise.map(uploads, (upload) => {
let fileUploadPromise = client.uploadContent(upload.file,
{onlyContentUri: false}).then((response) => {
return response.content_uri
})
let mimeType = upload.file.type
let eventType = "m.file"
let additionalPromise
if (mimeType.startsWith("image/")) {
eventType = "m.image"
// create and upload thumbnail
let thumbnailType = "image/png"
if (mimeType == "image/jpeg") {
thumbnailType = mimeType
}
additionalPromise = riot.loadImageElement(upload.file)
.then((img) => {
return riot.createThumbnail(img,
img.width,
img.height,
thumbnailType)
})
.catch((error) => {
console.error("neo: error getting thumbnail", error)
})
.then((thumbResult) => {
return client.uploadContent(thumbResult.thumbnail, {onlyContentUri: false})
}).then((response) => {
return {
thumbnail_url: response.content_uri,
thumbnail_info: {
mimetype: thumbnailType
}
}
})
} else if (mimeType.startsWith("video/")) {
eventType = "m.video"
} else if (mimeType.startsWith("audio/")) {
eventType = "m.audio"
} else {
// m.file
}
Promise.all([fileUploadPromise, additionalPromise]).then((result) => {
console.log(result)
let info = {
mimetype: mimeType
}
if (result[1] != undefined) {
info = Object.assign(info, result[1])
}
client.sendEvent(this.props.roomId, "m.room.message", {
body: upload.file.name,
msgtype: eventType,
info: info,
url: result[0]
})
})
})
},
sendPlain: function(string) { sendPlain: function(string) {
let content = { let content = {
body: string, body: string,

Loading…
Cancel
Save