-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utils.ts
120 lines (107 loc) · 4.57 KB
/
Utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import { HttpStatusCode, IHttp, IRead } from '@rocket.chat/apps-engine/definition/accessors';
import { IApiRequest, IApiResponse } from '@rocket.chat/apps-engine/definition/api';
import { RocketChatAssociationModel, RocketChatAssociationRecord } from '@rocket.chat/apps-engine/definition/metadata';
export const WOPI_FILE_MAP = new RocketChatAssociationRecord(RocketChatAssociationModel.MISC, 'wopi-file-map');
export class Utils {
public static async checkWopiRequest(request: IApiRequest, read: IRead, http: IHttp): Promise<IApiResponse> {
const token = request.query.access_token;
if (!token) {
return {
status: HttpStatusCode.UNAUTHORIZED,
content: `No access token has been found`,
};
}
const siteSetting = await read.getEnvironmentReader().getServerSettings().getOneById('Site_Url');
if (!siteSetting) {
return {
status: HttpStatusCode.NOT_FOUND,
content: `Can not retrieve Rocket.Chat server url`,
};
}
const resDecoding = await http.get(`${ siteSetting.value }/api/v1/token.decode/${ token }`);
if (!resDecoding || !resDecoding.content) {
return {
status: HttpStatusCode.UNAUTHORIZED,
content: `Attached access token is not valid`,
};
}
const decodedToken = JSON.parse(resDecoding.content);
if (!decodedToken || !decodedToken.result.payloadObj) {
return {
status: HttpStatusCode.UNAUTHORIZED,
content: `Attached access token is not valid`,
};
}
const wopiToken = decodedToken.result.payloadObj.context;
console.log(`Utils.checkWopiRequest: decoded token: ${ JSON.stringify(wopiToken) }`);
const fileId = request.params.fileId;
if (!fileId) {
return {
status: HttpStatusCode.NOT_FOUND,
content: `No file id has been specified`,
};
}
if (fileId !== wopiToken.fileId) {
return {
status: HttpStatusCode.UNAUTHORIZED,
content: `File id does not match token`,
};
}
const uploadInfo = await read.getUploadReader().getById(fileId);
if (!uploadInfo || !uploadInfo.room || !uploadInfo.user) {
return {
status: HttpStatusCode.NOT_FOUND,
content: `Upload ${ fileId } could not be found`,
};
}
if (uploadInfo.room.id !== wopiToken.rid) {
return {
status: HttpStatusCode.UNAUTHORIZED,
content: `File does not belong to the expected room`,
};
}
const userInfo = await read.getUserReader().getById(wopiToken.userId);
if (!userInfo) {
return {
status: HttpStatusCode.UNAUTHORIZED,
content: `User ${ wopiToken.userId } has not been found`,
};
}
console.log(`Utils.checkWopiRequest: user name: ${ userInfo.name }`);
return {
status: HttpStatusCode.OK,
content: {
uploadInfo,
userInfo,
},
};
}
public static async updateWopiFileMap(apiId: string, read: IRead, http: IHttp, wopiAddress?: string): Promise<boolean> {
if (!wopiAddress || wopiAddress.length === 0) {
const settings = read.getEnvironmentReader().getSettings();
const setting = await settings.getById('OnlineServerUrl');
if (!settings) {
return false;
}
wopiAddress = setting.value || setting.packageValue;
}
const siteSetting = await read.getEnvironmentReader().getServerSettings().getOneById('Site_Url');
if (!siteSetting) {
return false;
}
console.log(`Utils.updateWopifileMap: siteSetting: ${ JSON.stringify(siteSetting) }`);
const request = {
content: wopiAddress,
};
const response = await http.post(`${ siteSetting.value }/api/apps/public/${ apiId }/wopiFileMap.update`, request);
console.log(`Utils.updateWopifileMap: response status: ${ response.statusCode }`);
return response.statusCode === HttpStatusCode.OK;
}
public static getFileExtension(fileName: string): string {
const nameParts = fileName.split('.');
if (nameParts.length < 2 || nameParts[nameParts.length - 1].length === 0) {
return '';
}
return nameParts[nameParts.length - 1];
}
}