/
addWebdavAccount.js
55 lines (46 loc) · 1.39 KB
/
addWebdavAccount.js
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
import { Meteor } from 'meteor/meteor';
import { Match, check } from 'meteor/check';
import { settings } from 'meteor/rocketchat:settings';
import { WebdavAccounts } from 'meteor/rocketchat:models';
import { createClient } from 'webdav';
Meteor.methods({
async addWebdavAccount(formData) {
const userId = Meteor.userId();
if (!userId) {
throw new Meteor.Error('error-invalid-user', 'Invalid User', { method: 'addWebdavAccount' });
}
if (!settings.get('Webdav_Integration_Enabled')) {
throw new Meteor.Error('error-not-allowed', 'WebDAV Integration Not Allowed', { method: 'addWebdavAccount' });
}
check(formData, Match.ObjectIncluding({
serverURL: String,
username: String,
pass: String,
}));
const client = createClient(
formData.serverURL,
{
username: formData.username,
password: formData.pass,
}
);
try {
await client.stat('/');
} catch (error) {
return { success: false, message: 'could-not-access-webdav', error };
}
const accountData = {
user_id: userId,
server_url: formData.serverURL,
username: formData.username,
password: formData.pass,
name: formData.name,
};
try {
WebdavAccounts.insert(accountData);
return { success: true, message: 'webdav-account-saved' };
} catch (error) {
return { success: false, message: error.code === 11000 ? 'duplicated-account' : 'unknown-write-error', error };
}
},
});