1
/**//*
2
* FCKeditor - The text editor for internet
3
* Copyright (C) 2003-2005 Frederico Caldeira Knabben
4
*
5
* Licensed under the terms of the GNU Lesser General Public License:
6
* http://www.opensource.org/licenses/lgpl-license.php
7
*
8
* For further information visit:
9
* http://www.fckeditor.net/
10
*
11
* "Support Open Source software. What about a donation today?"
12
*
13
* File Name: FileBrowserConnector.cs
14
* This is the code behind of the connector.aspx page used by the
15
* File Browser.
16
*
17
* File Authors:
18
* Frederico Caldeira Knabben (fredck@fckeditor.net)
19
*/
20
21
using System;
22
using System.Globalization;
23
using System.Xml;
24
using System.Web;
25
26
namespace FredCK.FCKeditorV2
27
...{
28
public class FileBrowserConnector : FileWorkerBase
29
...{
30
protected override void OnLoad(EventArgs e)
31
...{
32
// Get the main request informaiton.
33
string sCommand = Request.QueryString["Command"];
34
if (sCommand == null) return;
35
36
string sResourceType = Request.QueryString["Type"];
37
if (sResourceType == null) return;
38
39
string sCurrentFolder = Request.QueryString["CurrentFolder"];
40
if (sCurrentFolder == null) return;
41
42
// Check the current folder syntax (must begin and start with a slash).
43
if (!sCurrentFolder.EndsWith("/"))
44
sCurrentFolder += "/";
45
if (!sCurrentFolder.StartsWith("/"))
46
sCurrentFolder = "/" + sCurrentFolder;
47
48
// File Upload doesn't have to return XML, so it must be intercepted before anything.
49
if (sCommand == "FileUpload")
50
...{
51
this.FileUpload(sResourceType, sCurrentFolder);
52
return;
53
}
54
55
// Cleans the response buffer.
56
Response.ClearHeaders();
57
Response.Clear();
58
59
// Prevent the browser from caching the result.
60
Response.CacheControl = "no-cache";
61
62
// Set the response format.
63
Response.ContentEncoding = System.Text.UTF8Encoding.UTF8;
64
Response.ContentType = "text/xml";
65
66
XmlDocument oXML = new XmlDocument();
67
XmlNode oConnectorNode = CreateBaseXml(oXML, sCommand, sResourceType, sCurrentFolder);
68
69
// Execute the required command.
70
switch (sCommand)
71
...{
72
case "GetFolders":
73
this.GetFolders(oConnectorNode, sResourceType, sCurrentFolder);
74
break;
75
case "GetFoldersAndFiles":
76
this.GetFolders(oConnectorNode, sResourceType, sCurrentFolder);
77
this.GetFiles(oConnectorNode, sResourceType, sCurrentFolder);
78
break;
79
case "CreateFolder":
80
this.CreateFolder(oConnectorNode, sResourceType, sCurrentFolder);
81
break;
82
}
83
84
// Output the resulting XML.
85
Response.Write(oXML.OuterXml);
86
87
Response.End();
88
}
89
90
Base XML Creation#region Base XML Creation
91
92
private XmlNode CreateBaseXml(XmlDocument xml, string command, string resourceType, string currentFolder)
93
...{
94
// Create the XML document header.
95
xml.AppendChild(xml.CreateXmlDeclaration("1.0", "utf-8", null));
96
97
// Create the main "Connector" node.
98
XmlNode oConnectorNode = XmlUtil.AppendElement(xml, "Connector");
99
XmlUtil.SetAttribute(oConnectorNode, "command", command);
100
XmlUtil.SetAttribute(oConnectorNode, "resourceType", resourceType);
101
102
// Add the current folder node.
103
XmlNode oCurrentNode = XmlUtil.AppendElement(oConnectorNode, "CurrentFolder");
104
XmlUtil.SetAttribute(oCurrentNode, "path", currentFolder);
105
XmlUtil.SetAttribute(oCurrentNode, "url", GetUrlFromPath(resourceType, currentFolder));
106
107
return oConnectorNode;
108
}
109
110
#endregion
111
112
Command Handlers#region Command Handlers
113
114
private void GetFolders(XmlNode connectorNode, string resourceType, string currentFolder)
115
...{
116
// Map the virtual path to the local server path.
117
string sServerDir = this.ServerMapFolder(resourceType, currentFolder);
118
119
// Create the "Folders" node.
120
XmlNode oFoldersNode = XmlUtil.AppendElement(connectorNode, "Folders");
121
122
System.IO.DirectoryInfo oDir = new System.IO.DirectoryInfo(sServerDir);
123
System.IO.DirectoryInfo[] aSubDirs = oDir.GetDirectories();
124
125
for (int i = 0; i < aSubDirs.Length; i++)
126
...{
127
// Create the "Folders" node.
128
XmlNode oFolderNode = XmlUtil.AppendElement(oFoldersNode, "Folder");
129
XmlUtil.SetAttribute(oFolderNode, "name", aSubDirs[i].Name);
130
}
131
}
132
133
private void GetFiles(XmlNode connectorNode, string resourceType, string currentFolder)
134
...{
135
// Map the virtual path to the local server path.
136
string sServerDir = this.ServerMapFolder(resourceType, currentFolder);
137
138
// Create the "Files" node.
139
XmlNode oFilesNode = XmlUtil.AppendElement(connectorNode, "Files");
140
141
System.IO.DirectoryInfo oDir = new System.IO.DirectoryInfo(sServerDir);
142
System.IO.FileInfo[] aFiles = oDir.GetFiles();
143
144
for (int i = 0; i < aFiles.Length; i++)
145
...{
146
Decimal iFileSize = Math.Round((Decimal)aFiles[i].Length / 1024);
147
if (iFileSize < 1 && aFiles[i].Length != 0) iFileSize = 1;
148
149
// Create the "File" node.
150
XmlNode oFileNode = XmlUtil.AppendElement(oFilesNode, "File");
151
XmlUtil.SetAttribute(oFileNode, "name", aFiles[i].Name);
152
XmlUtil.SetAttribute(oFileNode, "size", iFileSize.ToString(CultureInfo.InvariantCulture));
153
}
154
}
155
156
private void CreateFolder(XmlNode connectorNode, string resourceType, string currentFolder)
157
...{
158
string sErrorNumber = "0";
159
160
string sNewFolderName = Request.QueryString["NewFolderName"];
161
162
if (sNewFolderName == null || sNewFolderName.Length == 0)
163
sErrorNumber = "102";
164
else
165
...{
166
// Map the virtual path to the local server path of the current folder.
167
string sServerDir = this.ServerMapFolder(resourceType, currentFolder);
168
169
try
170
...{
171
Util.CreateDirectory(System.IO.Path.Combine(sServerDir, sNewFolderName));
172
}
173
catch (ArgumentException)
174
...{
175
sErrorNumber = "102";
176
}
177
catch (System.IO.PathTooLongException)
178
...{
179
sErrorNumber = "102";
180
}
181
catch (System.IO.IOException)
182
...{
183
sErrorNumber = "101";
184
}
185
catch (System.Security.SecurityException)
186
...{
187
sErrorNumber = "103";
188
}
189
catch (Exception)
190
...{
191
sErrorNumber = "110";
192
}
193
}
194
195
// Create the "Error" node.
196
XmlNode oErrorNode = XmlUtil.AppendElement(connectorNode, "Error");
197
XmlUtil.SetAttribute(oErrorNode, "number", sErrorNumber);
198
}
199
200
private void FileUpload(string resourceType, string currentFolder)
201
...{
202
HttpPostedFile oFile = Request.Files["NewFile"];
203
204
string sErrorNumber = "0";
205
string sFileName = "";
206
207
if (oFile != null)
208
...{
209
// Map the virtual path to the local server path.
210
string sServerDir = this.ServerMapFolder(resourceType, currentFolder);
211
212
// Get the uploaded file name.
213
sFileName = System.IO.Path.GetFileName(oFile.FileName);
214
215
int iCounter = 0;
216
217
while (true)
218
...{
219
string sFilePath = System.IO.Path.Combine(sServerDir, sFileName);
220
//判断文件是否可以上传
221
if (CheckUploadFileExtension(sFileName))
222
...{
223
if (System.IO.File.Exists(sFilePath))
224
...{
225
iCounter++;
226
sFileName =
227
System.IO.Path.GetFileNameWithoutExtension(oFile.FileName) +
228
"(" + iCounter + ")" +
229
System.IO.Path.GetExtension(oFile.FileName);
230
231
sErrorNumber = "201";
232
}
233
else
234
...{
235
oFile.SaveAs(sFilePath);
236
break;
237
}
238
}
239
else
240
...{
241
//不允许上传
242
sErrorNumber = "202";
243
break;
244
}
245
}
246
}
247
else
248
sErrorNumber = "202";
249
250
Response.Clear();
251
252
Response.Write("<script type=\"text/javascript\">");
253
Response.Write("window.parent.frames['frmUpload'].OnUploadCompleted(" + sErrorNumber + ",'" + sFileName.Replace("'", "\\'") + "') ;");
254
Response.Write("</script>");
255
256
Response.End();
257
}
258
259
#endregion
260
261
Directory Mapping#region Directory Mapping
262
263
private string ServerMapFolder(string resourceType, string folderPath)
264
...{
265
// Get the resource type directory.
266
string sResourceTypePath = System.IO.Path.Combine(this.UserFilesDirectory, resourceType);
267
268
// Ensure that the directory exists.
269
Util.CreateDirectory(sResourceTypePath);
270
271
// Return the resource type directory combined with the required path.
272
return System.IO.Path.Combine(sResourceTypePath, folderPath.TrimStart('/'));
273
}
274
275
private string GetUrlFromPath(string resourceType, string folderPath)
276
...{
277
if (resourceType == null || resourceType.Length == 0)
278
return this.UserFilesPath.TrimEnd('/') + folderPath;
279
else
280
return this.UserFilesPath + resourceType + folderPath;
281
}
282
283
#endregion
284
}
285
}
286