1
1
import * as Icons from "./icons.js"
2
2
import { h , createRef , Fragment , Component } from 'preact' ;
3
3
import * as CommonMark from 'commonmark'
4
- import { loadImageElement , loadVideoElement , createThumbnail , blurhashFromFile } from "./utils/media.js"
4
+ import { loadImageElement , loadMediaElement , createThumbnail , blurhashFromFile } from "./utils/media.js"
5
5
import { spaceChild , spaceParent , mscLocation , mscParent , mscMarkupMsgKey , mscPdfText , mscMediaFragment , mscPdfHighlight , populusHighlight } from "./constants.js"
6
6
import { processRegex } from './processRegex.js'
7
7
import { UserColor } from './utils/colors.js'
@@ -182,6 +182,8 @@ class FileUploadInput extends Component {
182
182
183
183
theForm = createRef ( )
184
184
185
+ secondaryAudio = createRef ( )
186
+
185
187
getFile = _ => this . props . file || this . fileLoader . current . files [ 0 ]
186
188
187
189
validateFile = file => {
@@ -199,6 +201,16 @@ class FileUploadInput extends Component {
199
201
previewUrl : URL . createObjectURL ( file ) ,
200
202
mediaType : "image" ,
201
203
} )
204
+ } else if ( / ^ a u d i o / . test ( file . type ) ) {
205
+ loadMediaElement ( file , "audio" ) . then ( elt => {
206
+ this . mediaElement = elt
207
+ const stream = elt . mozCaptureStream ?. ( ) || elt . captureStream ( )
208
+ this . setState ( {
209
+ stream,
210
+ previewUrl : URL . createObjectURL ( file ) ,
211
+ mediaType : "audio" ,
212
+ } )
213
+ } )
202
214
} else {
203
215
this . setState ( { mediaType : "default" } )
204
216
}
@@ -209,12 +221,26 @@ class FileUploadInput extends Component {
209
221
switch ( this . state . mediaType ) {
210
222
case "image" : await this . submitImage ( ) ; break
211
223
case "video" : await this . submitVideo ( ) ; break
224
+ case "audio" : await this . submitAudio ( ) ; break
212
225
case "default" : await this . submitDefault ( ) ; break
213
226
}
214
227
}
215
228
this . props . done ( )
216
229
}
217
230
231
+ handleMediaClick = _ => {
232
+ console . log ( this . mediaElement . current )
233
+ if ( this . mediaElement . paused ) {
234
+ this . mediaElement . currentTime = 0
235
+ this . mediaElement . play ( )
236
+ this . secondaryAudio . current ?. play ( )
237
+ }
238
+ else {
239
+ this . mediaElement . pause ( )
240
+ this . secondaryAudio . current ?. pause ( )
241
+ }
242
+ }
243
+
218
244
submitDefault = async _ => {
219
245
const theFile = this . getFile ( )
220
246
const mxc = await Client . client . uploadContent ( theFile , { progressHandler : this . progressHandler } ) . catch ( e => console . log ( e ) )
@@ -234,8 +260,8 @@ class FileUploadInput extends Component {
234
260
235
261
submitVideo = async _ => {
236
262
const theVideo = this . getFile ( )
237
- window . theVideo = theVideo
238
- const videoElt = await loadVideoElement ( theVideo )
263
+ // window.theVideo = theVideo
264
+ const videoElt = await loadMediaElement ( theVideo , "video" )
239
265
const thumbContent = await createThumbnail ( videoElt , videoElt . videoWidth , videoElt . videoHeight , "image/jpeg" )
240
266
const thumbMxc = await Client . client . uploadContent ( thumbContent . thumbnail , {
241
267
name : `${ theVideo . name } _800x600` ,
@@ -265,6 +291,25 @@ class FileUploadInput extends Component {
265
291
this . props . handlePending ( theContent , eventI )
266
292
}
267
293
294
+ submitAudio = async _ => {
295
+ const theAudio = this . getFile ( )
296
+ console . log ( "upload audio" )
297
+ const audioMxc = await Client . client . uploadContent ( theAudio , { progressHandler : this . progressHandler } )
298
+ const duration = Math . round ( this . mediaElement . duration * 1000 )
299
+ const theContent = {
300
+ body : theAudio . name ,
301
+ info : {
302
+ mimetype : theAudio . type ,
303
+ size : theAudio . size
304
+ } ,
305
+ msgtype : "m.audio" ,
306
+ url : audioMxc ,
307
+ }
308
+ if ( duration < Infinity ) theContent . duration = duration
309
+ const eventI = await Client . client . sendMessage ( this . props . roomId , theContent )
310
+ this . props . handlePending ( theContent , eventI )
311
+ }
312
+
268
313
submitImage = async _ => {
269
314
const theImage = this . getFile ( )
270
315
const { width, height, img} = await loadImageElement ( theImage )
@@ -304,6 +349,13 @@ class FileUploadInput extends Component {
304
349
? < video ref = { this . mediaPreview } class = "video-message-preview media-message-thumbnail" controls src = { state . previewUrl } />
305
350
: state . mediaType === "image"
306
351
? < img ref = { this . mediaPreview } class = "image-message-preview media-message-thumbnail" src = { this . state . previewUrl } />
352
+ : state . mediaType === "audio" && state . stream
353
+ ? < Fragment >
354
+ { //workaround for firefox bug: https://bugzilla-dev.allizom.org/show_bug.cgi?id=1178751
355
+ this . mediaElement . mozCaptureStream ? < audio ref = { this . secondaryAudio } srcObject = { state . stream } /> : null
356
+ }
357
+ < AudioVisualizer class = "audio-message-preview media-message-thumbnail" height = "100" onclick = { this . handleMediaClick } stream = { state . stream } />
358
+ </ Fragment >
307
359
: state . mediaType === "default"
308
360
? < div id = "file-uploader-preview" >
309
361
< span > { Icons . file } </ span >
0 commit comments