-
Notifications
You must be signed in to change notification settings - Fork 1
/
browser.js
132 lines (105 loc) · 4.49 KB
/
browser.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
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
121
122
123
124
125
126
127
128
129
130
131
132
var util = require('./file_manager/utils');
var xml = require('./file_manager/xml_preparing');
var FileManagerFactory = require('./file_manager/file_manager_factory');
var browserCommand = new FileManagerFactory({fs:'disk'})
var path = require('path');
var rootPath = path.resolve(process.cwd());
var config = require('./file_manager/config');
util.prepareDirs(rootPath, ['/public/'+ config.user_file_path]);
function strtr(str){
str = str.replace(/\\\\/g, '\\\\');
str = str.replace(/\"/g, '\\"');
return str;
}
module.exports = new function(){
this.browse = function(req, res){
var command = req.query.Command;
var resourceType = req.query.Type;
var currentFolder = req.query.CurrentFolder;
if ( !command || !resourceType || !currentFolder ){
return ;
}
// Check if it is an allowed command
if ( ! util.isAllowedCommand( command ) ){
util.setXmlHeaders(res)
return res.send(xml.renderError( 1, 'The "' + command + '" command isn\'t allowed'))
}
// Check if it is an allowed type.
if ( ! util.isAllowedType( resourceType ) ){
util.setXmlHeaders(res)
return res.send(xml.renderError( 1, 'Invalid type specified')) ;
}
// File Upload doesn't have to Return XML, so it must be intercepted before anything.
if ( command == 'FileUpload' ) {
return browserCommand.fileUpload(req, resourceType, currentFolder, command, function(err, result){
if (result.errorNumber && result.errorNumber != 201) {
result.fileUrl = "";
result.fileName = "";
}
return res.send("<script type='text/javascript'>\
(function(){var d=document.domain;while (true){try{var A=window.parent.document.domain;break;}catch(e) {};d=d.replace(/.*?(?:\.|$)/,'');if (d.length==0) break;try{document.domain=d;}catch (e){break;}}})();\
window.parent.OnUploadCompleted(" + result.errorNumber + ",'" + strtr( result.fileUrl ) + "','" + strtr( result.fileName ) + "', '" + strtr( result.customMsg ) + "') ;\
</script>");
}) ;
} else if( command == 'CreateFolder'){
util.setXmlHeaders(res)
var newFolderName = req.query.NewFolderName;
var errorObj = browserCommand.createFolder( resourceType, currentFolder, newFolderName ) ;
return res.send(xml.renderError( errorObj.number, errorObj.text))
}
var foldersAndFiles = {};
switch ( command ){
case 'GetFolders' :
foldersAndFiles = browserCommand.getFolders( resourceType, currentFolder ) ;
break ;
case 'GetFoldersAndFiles' :
foldersAndFiles = browserCommand.getFoldersAndFiles( resourceType, currentFolder ) ;
break ;
}
util.setXmlHeaders(res)
res.send(xml.renderXml(command, resourceType, currentFolder, foldersAndFiles));
}
this.upload = function(req, res){
var command = 'QuickUpload';
var resourceType = req.query.Type || 'File';
var currentFolder = "/" ;
// Check if it is an allowed command
if ( ! util.isAllowedCommand( command ) ){
return res.send(xml.renderError( 1, 'The "' + command + '" command isn\'t allowed'))
}
// Check if it is an allowed type.
if ( ! util.isAllowedType( resourceType ) ){
return res.send(xml.renderError( 1, 'Invalid type specified')) ;
}
var CKEcallback = req.query.CKEditorFuncNum;
browserCommand.fileUpload(req, resourceType, currentFolder, command, function(err, result){
if (result.errorNumber && result.errorNumber != 201) {
result.fileUrl = "";
result.fileName= "";
}
var msg = "";
switch (result.errorNumber )
{
case 0 :
msg = "Upload successful";
break;
case 1 : // Custom error.
msg = result.customMsg;
break ;
case 201 :
msg = 'A file with the same name is already available. The uploaded file has been renamed to "' + result.fileName + '"' ;
break ;
case 202 :
msg = 'Invalid file' ;
break ;
default :
msg = 'Error on file upload. Error number: ' + result.errorNumber ;
break ;
}
res.send("<script type='text/javascript'>\
(function(){var d=document.domain;while (true){try{var A=window.parent.document.domain;break;}catch(e) {};d=d.replace(/.*?(?:\.|$)/,'');if (d.length==0) break;try{document.domain=d;}catch (e){break;}}})();\
window.parent.CKEDITOR.tools.callFunction('" + CKEcallback + "','" + strtr(result.fileUrl) + "', '" + strtr( msg) + "');\
</script>")
});
}
}