The communications platform that puts data protection first.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
Rocket.Chat/app/webdav/server/methods/uploadFileToWebdav.js

52 lines
1.8 KiB

import { Meteor } from 'meteor/meteor';
import { settings } from '../../../settings';
import { Logger } from '../../../logger';
import { getWebdavCredentials } from './getWebdavCredentials';
import { WebdavAccounts } from '../../../models';
import { WebdavClientAdapter } from '../lib/webdavClientAdapter';
const logger = new Logger('WebDAV_Upload', {});
Meteor.methods({
async uploadFileToWebdav(accountId, fileData, name) {
if (!Meteor.userId()) {
throw new Meteor.Error('error-invalid-user', 'Invalid User', { method: 'uploadFileToWebdav' });
}
if (!settings.get('Webdav_Integration_Enabled')) {
throw new Meteor.Error('error-not-allowed', 'WebDAV Integration Not Allowed', { method: 'uploadFileToWebdav' });
}
const account = WebdavAccounts.findOne({ _id: accountId });
if (!account) {
throw new Meteor.Error('error-invalid-account', 'Invalid WebDAV Account', { method: 'uploadFileToWebdav' });
}
const uploadFolder = 'Rocket.Chat Uploads/';
const buffer = new Buffer(fileData);
try {
const cred = getWebdavCredentials(account);
const client = new WebdavClientAdapter(account.server_url, cred);
await client.createDirectory(uploadFolder).catch(() => {});
await client.putFileContents(`${ uploadFolder }/${ name }`, buffer, { overwrite: false });
return { success: true };
} catch (error) {
logger.error(error);
if (error.response) {
const { status } = error.response;
if (status === 404) {
return { success: false, message: 'webdav-server-not-found' };
}
if (status === 401) {
return { success: false, message: 'error-invalid-account' };
}
if (status === 412) {
return { success: false, message: 'Duplicate_file_name_found' };
}
}
return { success: false, message: 'FileUpload_Error' };
}
},
});