cloud_bbb/ts/filelist.ts

101 lines
2.6 KiB
TypeScript
Raw Normal View History

2020-04-26 11:36:41 +02:00
import axios from '@nextcloud/axios';
import { generateOcsUrl, generateUrl } from '@nextcloud/router';
import { Room } from './Manager/Api';
declare const OCA: any;
class BigBlueButton {
public async getRooms(): Promise<Room[]> {
2020-04-27 16:50:45 +02:00
const response = await axios.get(OC.generateUrl('/apps/bbb/rooms'));
2020-04-26 11:36:41 +02:00
2020-04-27 16:50:45 +02:00
return response.data;
}
2020-04-26 11:36:41 +02:00
}
$(() => {
if (!OCA?.Files?.fileActions) {
return;
}
const mimeTypes = [
'application/pdf',
'application/vnd.oasis.opendocument.presentation',
'application/vnd.oasis.opendocument.text',
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'application/vnd.openxmlformats-officedocument.presentationml.presentation',
'application/msword',
'application/vnd.ms-powerpoint',
];
2020-04-27 16:50:45 +02:00
const bbb = new BigBlueButton();
bbb.getRooms().then(rooms => {
rooms.forEach(room => {
mimeTypes.forEach(mime => registerFileAction(mime, room.id, room.uid, room.name));
2020-04-27 16:50:45 +02:00
});
});
function registerFileAction(mime, id, uid, name) {
OCA.Files.fileActions.registerAction({
name: 'bbb-' + id,
displayName: name,
mime,
permissions: OC.PERMISSION_SHARE,
icon: OC.imagePath('bbb', 'app-dark.svg'),
actionHandler: (fileName, context) => {
share(context.fileInfoModel.getFullPath(), fileName, uid);
},
});
}
async function share(path: string, filename: string, roomUid) {
const id = await createShare(path);
const shareUrl = await configureShare(id);
const joinUrl = generateUrl('/apps/bbb/b/{uid}?u={url}&filename={filename}', {
uid: roomUid,
url: shareUrl + '/download',
filename,
});
window.open(joinUrl, '_blank', 'noopener,noreferrer');
}
async function createShare(path: string): Promise<number> {
const url = generateOcsUrl('apps/files_sharing/api/v1', 2) + 'shares';
const createResponse = await axios.post(url, {
path,
shareType: OC.Share.SHARE_TYPE_LINK,
note: 'foobar foobar foobar',
expireDate: '2020-07-01',
});
const { meta, data } = createResponse.data.ocs;
if (meta.statuscode !== 200) {
throw new Error('Failed to create share');
}
return data.id;
}
async function configureShare(id: number): Promise<string> {
const url = generateOcsUrl('apps/files_sharing/api/v1', 2) + 'shares/' + id;
const tomorrow = new Date();
tomorrow.setDate(new Date().getDate() + 1);
const updateResponse = await axios.put(url, {
expireDate: tomorrow.toISOString().split('T')[0],
note: 'BigBlueButton',
});
const { meta, data } = updateResponse.data.ocs;
if (meta.statuscode !== 200) {
throw new Error('Failed to configure share');
}
return data.url;
}
2020-04-26 11:36:41 +02:00
});