-
Notifications
You must be signed in to change notification settings - Fork 10.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[NEW] WebDAV(Nextcloud/ownCloud) Storage Server Option (#11027)
Closes #9562, Part of #7791 Implementation of ownCloud/Nextcloud as file upload store feature. Since it is using generic webdav client, the store compatible with all WebDAV servers (NextCloud, ownCloud and so on). ![image](https://user-images.githubusercontent.com/14157973/41051962-3a63ef5e-69c0-11e8-9a8a-76ed36308dfb.png)
- Loading branch information
1 parent
2c28b81
commit 2bcca4f
Showing
7 changed files
with
263 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* globals FileUpload */ | ||
|
||
import _ from 'underscore'; | ||
import { FileUploadClass } from '../lib/FileUpload'; | ||
import '../../ufs/Webdav/server.js'; | ||
|
||
const get = function(file, req, res) { | ||
this.store.getReadStream(file._id, file).pipe(res); | ||
}; | ||
|
||
const copy = function(file, out) { | ||
this.store.getReadStream(file._id, file).pipe(out); | ||
}; | ||
|
||
const WebdavUploads = new FileUploadClass({ | ||
name: 'Webdav:Uploads', | ||
get, | ||
copy | ||
// store setted bellow | ||
}); | ||
|
||
const WebdavAvatars = new FileUploadClass({ | ||
name: 'Webdav:Avatars', | ||
get, | ||
copy | ||
// store setted bellow | ||
}); | ||
|
||
const WebdavUserDataFiles = new FileUploadClass({ | ||
name: 'Webdav:UserDataFiles', | ||
get, | ||
copy | ||
// store setted bellow | ||
}); | ||
|
||
const configure = _.debounce(function() { | ||
const uploadFolderPath = RocketChat.settings.get('FileUpload_Webdav_Upload_Folder_Path'); | ||
const server = RocketChat.settings.get('FileUpload_Webdav_Server_URL'); | ||
const username = RocketChat.settings.get('FileUpload_Webdav_Username'); | ||
const password = RocketChat.settings.get('FileUpload_Webdav_Password'); | ||
|
||
if (!server || !username || !password) { | ||
return; | ||
} | ||
|
||
const config = { | ||
connection: { | ||
credentials: { | ||
server, | ||
username, | ||
password | ||
} | ||
}, | ||
uploadFolderPath | ||
}; | ||
|
||
WebdavUploads.store = FileUpload.configureUploadsStore('Webdav', WebdavUploads.name, config); | ||
WebdavAvatars.store = FileUpload.configureUploadsStore('Webdav', WebdavAvatars.name, config); | ||
WebdavUserDataFiles.store = FileUpload.configureUploadsStore('Webdav', WebdavUserDataFiles.name, config); | ||
}, 500); | ||
|
||
RocketChat.settings.get(/^FileUpload_Webdav_/, configure); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import {UploadFS} from 'meteor/jalik:ufs'; | ||
|
||
export class WebdavStore extends UploadFS.Store {} | ||
|
||
// Add store to UFS namespace | ||
UploadFS.store.Webdav = WebdavStore; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
import {UploadFS} from 'meteor/jalik:ufs'; | ||
import Webdav from 'webdav'; | ||
import stream from 'stream'; | ||
/** | ||
* WebDAV store | ||
* @param options | ||
* @constructor | ||
*/ | ||
export class WebdavStore extends UploadFS.Store { | ||
|
||
constructor(options) { | ||
|
||
super(options); | ||
|
||
|
||
const client = new Webdav( | ||
options.connection.credentials.server, | ||
options.connection.credentials.username, | ||
options.connection.credentials.password, | ||
); | ||
|
||
options.getPath = function(file) { | ||
if (options.uploadFolderPath[options.uploadFolderPath.length-1] !== '/') { | ||
options.uploadFolderPath += '/'; | ||
} | ||
return options.uploadFolderPath + file._id; | ||
}; | ||
|
||
client.stat(options.uploadFolderPath).catch(function(err) { | ||
if (err.status === '404') { | ||
client.createDirectory(options.uploadFolderPath); | ||
} | ||
}); | ||
|
||
/** | ||
* Returns the file path | ||
* @param file | ||
* @return {string} | ||
*/ | ||
this.getPath = function(file) { | ||
if (file.Webdav) { | ||
return file.Webdav.path; | ||
} | ||
}; | ||
|
||
/** | ||
* Creates the file in the col lection | ||
* @param file | ||
* @param callback | ||
* @return {string} | ||
*/ | ||
this.create = function(file, callback) { | ||
check(file, Object); | ||
|
||
if (file._id == null) { | ||
file._id = Random.id(); | ||
} | ||
|
||
file.Webdav = { | ||
path: options.getPath(file) | ||
}; | ||
|
||
file.store = this.options.name; | ||
return this.getCollection().insert(file, callback); | ||
}; | ||
|
||
/** | ||
* Removes the file | ||
* @param fileId | ||
* @param callback | ||
*/ | ||
this.delete = function(fileId, callback) { | ||
const file = this.getCollection().findOne({_id: fileId}); | ||
client.deleteFile(this.getPath(file), (err, data) => { | ||
if (err) { | ||
console.error(err); | ||
} | ||
|
||
callback && callback(err, data); | ||
}); | ||
}; | ||
|
||
/** | ||
* Returns the file read stream | ||
* @param fileId | ||
* @param file | ||
* @param options | ||
* @return {*} | ||
*/ | ||
this.getReadStream = function(fileId, file, options = {}) { | ||
const range = {}; | ||
|
||
if (options.start != null) { | ||
range.start = options.start; | ||
} | ||
|
||
if (options.end != null) { | ||
range.end = options.end; | ||
} | ||
return client.createReadStream(this.getPath(file), options); | ||
}; | ||
|
||
/** | ||
* Returns the file write stream | ||
* @param fileId | ||
* @param file | ||
* @return {*} | ||
*/ | ||
this.getWriteStream = function(fileId, file) { | ||
const writeStream = new stream.PassThrough(); | ||
const webdavStream = client.createWriteStream(this.getPath(file)); | ||
|
||
//TODO remove timeout when UploadFS bug resolved | ||
const newListenerCallback = (event, listener) => { | ||
if (event === 'finish') { | ||
process.nextTick(() => { | ||
writeStream.removeListener(event, listener); | ||
writeStream.removeListener('newListener', newListenerCallback); | ||
writeStream.on(event, function() { | ||
setTimeout(listener, 500); | ||
}); | ||
}); | ||
} | ||
}; | ||
writeStream.on('newListener', newListenerCallback); | ||
|
||
writeStream.pipe(webdavStream); | ||
return writeStream; | ||
}; | ||
|
||
} | ||
} | ||
|
||
// Add store to UFS namespace | ||
UploadFS.store.Webdav = WebdavStore; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters