2020-04-26 11:36:41 +02:00
import axios from '@nextcloud/axios' ;
import { generateOcsUrl , generateUrl } from '@nextcloud/router' ;
2022-03-17 14:59:47 +01:00
import { showSuccess , showWarning , showError } from '@nextcloud/dialogs' ;
import '@nextcloud/dialogs/styles/toast' ;
2020-10-22 11:00:15 +02:00
import { api } from './Common/Api' ;
2022-03-21 11:23:54 +01:00
import './filelist.scss' ;
2020-04-26 11:36:41 +02:00
2022-03-16 15:11:31 +01:00
type OC_Dialogs_Message = ( content : string , title : string , dialogType : 'notice' | 'alert' | 'warn' | 'none' , buttons? : number , callback ? : ( ) = > void , modal? : boolean , allowHtml? : boolean ) = > Promise < void > ;
type ExtendedDialogs = typeof OC . dialogs & { message : OC_Dialogs_Message } ;
2020-10-22 11:00:15 +02:00
const mimeTypes = [
'application/pdf' ,
'application/vnd.oasis.opendocument.presentation' ,
'application/vnd.oasis.opendocument.text' ,
'application/vnd.oasis.opendocument.spreadsheet' ,
'application/vnd.oasis.opendocument.graphics' ,
'application/vnd.openxmlformats-officedocument.wordprocessingml.document' ,
'application/vnd.openxmlformats-officedocument.presentationml.presentation' ,
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' ,
'application/msword' ,
'application/vnd.ms-powerpoint' ,
'application/vnd.ms-excel' ,
'image/jpeg' ,
'image/png' ,
'text/plain' ,
'text/rtf' ,
2022-03-16 15:11:31 +01:00
] as const ;
type MimeTypes = typeof mimeTypes [ number ] ;
2020-04-26 11:36:41 +02:00
2020-10-22 11:00:15 +02:00
async function createDirectShare ( fileId : number ) : Promise < string > {
2022-03-16 11:27:11 +01:00
const url = generateOcsUrl ( 'apps/dav/api/v1/' , undefined , {
ocsVersion : 1 ,
escape : true ,
noRewrite : true ,
} ) + 'direct' ;
2020-10-22 11:00:15 +02:00
const createResponse = await axios . post ( url , {
fileId ,
} ) ;
return createResponse . data ? . ocs ? . data ? . url ;
2020-04-26 11:36:41 +02:00
}
2022-03-17 14:59:47 +01:00
async function createRoomWithFile ( shareUrl : string , filename : string , roomUid : string ) {
2020-10-22 11:00:15 +02:00
const joinUrl = generateUrl ( '/apps/bbb/b/{uid}?u={url}&filename={filename}' , {
uid : roomUid ,
url : shareUrl ,
filename ,
} ) ;
window . open ( joinUrl , '_blank' , 'noopener,noreferrer' ) ;
}
2020-04-26 11:36:41 +02:00
2022-03-17 14:59:47 +01:00
function insertDocumentToRoom ( shareUrl : string , filename : string , roomUid : string ) {
return api . insertDocument ( roomUid , shareUrl , filename ) ;
}
async function sendFile ( fileId : number , filename : string , roomUid : string ) {
const shareUrl = await createDirectShare ( fileId ) ;
const isRunning = await api . isRunning ( roomUid ) ;
if ( isRunning ) {
try {
const success = await insertDocumentToRoom ( shareUrl , filename , roomUid ) ;
if ( success ) {
showSuccess ( t ( 'bbb' , 'The file "{filename}" was uploaded to your room.' , { filename } ) ) ;
} else {
showWarning ( t ( 'bbb' , 'The file "{filename}" could not be uploaded to your room.' , { filename } ) ) ;
}
} catch {
showError ( t ( 'bbb' , 'The file "{filename}" could not be uploaded to your room. Maybe your BigBlueButton server does not support this action.' , { filename } ) ) ;
}
} else {
createRoomWithFile ( shareUrl , filename , roomUid ) ;
}
}
2022-03-16 15:11:31 +01:00
async function openDialog ( fileId : number , filename : string ) {
const initContent = '<div id="bbb-file-action"><span className="icon icon-loading-small icon-visible"></span></div>' ;
const title = t ( 'bbb' , 'Send file to BBB' ) ;
await ( OC . dialogs as ExtendedDialogs ) . message ( initContent , title , 'none' , - 1 , undefined , true , true ) ;
const rooms = await api . getRooms ( ) ;
const container = $ ( '#bbb-file-action' ) . empty ( ) ;
const table = $ ( '<table>' ) . appendTo ( container ) ;
table . attr ( 'style' , 'margin-top: 1em; width: 100%;' ) ;
for ( const room of rooms ) {
const row = $ ( '<tr>' ) ;
const button = $ ( '<button>' ) ;
2022-03-17 14:59:47 +01:00
if ( ! OC . debug ) {
button . prop ( 'disabled' , room . running ) ;
}
2022-03-16 16:12:33 +01:00
button . text ( room . running ? t ( 'bbb' , 'Send to' ) : t ( 'bbb' , 'Start with' ) ) ;
2022-03-21 11:23:54 +01:00
button . addClass ( room . running ? 'success' : 'primary' ) ;
2022-03-16 15:11:31 +01:00
button . attr ( 'type' , 'button' ) ;
button . on ( 'click' , ( ev ) = > {
ev . preventDefault ( ) ;
2022-03-21 11:23:54 +01:00
table . find ( 'button' ) . prop ( 'disabled' , true ) ;
$ ( ev . target ) . addClass ( 'icon-loading-small' ) ;
2022-03-16 15:11:31 +01:00
2022-03-21 11:23:54 +01:00
sendFile ( fileId , filename , room . uid ) . then ( ( ) = > {
container . parents ( '.oc-dialog' ) . find ( '.oc-dialog-close' ) . trigger ( 'click' ) ;
} ) ;
2022-03-16 15:11:31 +01:00
} ) ;
row . append ( $ ( '<td>' ) . append ( button ) ) ;
row . append ( $ ( '<td>' ) . attr ( 'style' , 'width: 100%;' ) . text ( room . name ) ) ;
row . appendTo ( table ) ;
}
if ( rooms . length > 0 ) {
const description = t ( 'bbb' , 'Please select the room in which you like to use the file "{filename}".' , { filename } ) ;
container . append ( description ) ;
container . append ( table ) ;
} else {
container . append ( $ ( 'p' ) . text ( t ( 'bbb' , 'No rooms available!' ) ) ) ;
}
}
function registerFileAction ( fileActions : any , mime : MimeTypes ) {
2021-02-24 15:11:19 +01:00
fileActions . registerAction ( {
2022-03-16 15:11:31 +01:00
name : 'bbb' ,
displayName : t ( 'bbb' , 'Send to BBB' ) ,
2020-10-22 11:00:15 +02:00
mime ,
permissions : OC.PERMISSION_SHARE ,
icon : OC.imagePath ( 'bbb' , 'app-dark.svg' ) ,
actionHandler : ( fileName , context ) = > {
2022-03-16 15:11:31 +01:00
console . log ( 'Action handler' ) ;
openDialog ( context . fileInfoModel . id , fileName ) ;
2020-10-22 11:00:15 +02:00
} ,
} ) ;
}
2021-02-24 15:11:19 +01:00
const BBBFileListPlugin = {
ignoreLists : [
'trashbin' ,
] ,
2020-10-22 11:00:15 +02:00
2021-02-24 15:11:19 +01:00
attach ( fileList ) {
if ( this . ignoreLists . includes ( fileList . id ) || ! OC . currentUser ) {
return ;
}
2020-04-27 16:47:32 +02:00
2022-03-16 15:11:31 +01:00
mimeTypes . forEach ( mime = > registerFileAction ( fileList . fileActions , mime ) ) ;
2021-02-24 15:11:19 +01:00
} ,
} ;
2020-04-27 16:47:32 +02:00
2021-02-24 15:11:19 +01:00
OC . Plugins . register ( 'OCA.Files.FileList' , BBBFileListPlugin ) ;