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