comparison wiki/htdocs/applets/FCKeditor/editor/filemanager/connectors/py/fckcommands.py @ 4005:f5c322ae1c7b

add recent version of fckeditor 2.6.3
author Byeongweon [tasyblue@gmail.com]
date Mon, 11 Aug 2008 13:54:49 +0900
parents 6089ade226fb
children 1d37117f8ca6
comparison
equal deleted inserted replaced
4004:3791b2e8e471 4005:f5c322ae1c7b
1 #!/usr/bin/env python
2
3 """
4 FCKeditor - The text editor for Internet - http://www.fckeditor.net
5 Copyright (C) 2003-2008 Frederico Caldeira Knabben
6
7 == BEGIN LICENSE ==
8
9 Licensed under the terms of any of the following licenses at your
10 choice:
11
12 - GNU General Public License Version 2 or later (the "GPL")
13 http://www.gnu.org/licenses/gpl.html
14
15 - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
16 http://www.gnu.org/licenses/lgpl.html
17
18 - Mozilla Public License Version 1.1 or later (the "MPL")
19 http://www.mozilla.org/MPL/MPL-1.1.html
20
21 == END LICENSE ==
22
23 Connector for Python (CGI and WSGI).
24
25 """
26
27 import os
28 try: # Windows needs stdio set for binary mode for file upload to work.
29 import msvcrt
30 msvcrt.setmode (0, os.O_BINARY) # stdin = 0
31 msvcrt.setmode (1, os.O_BINARY) # stdout = 1
32 except ImportError:
33 pass
34
35 from fckutil import *
36 from fckoutput import *
37 import config as Config
38
39 class GetFoldersCommandMixin (object):
40 def getFolders(self, resourceType, currentFolder):
41 """
42 Purpose: command to recieve a list of folders
43 """
44 # Map the virtual path to our local server
45 serverPath = mapServerFolder(self.userFilesFolder,currentFolder)
46 s = """<Folders>""" # Open the folders node
47 for someObject in os.listdir(serverPath):
48 someObjectPath = mapServerFolder(serverPath, someObject)
49 if os.path.isdir(someObjectPath):
50 s += """<Folder name="%s" />""" % (
51 convertToXmlAttribute(someObject)
52 )
53 s += """</Folders>""" # Close the folders node
54 return s
55
56 class GetFoldersAndFilesCommandMixin (object):
57 def getFoldersAndFiles(self, resourceType, currentFolder):
58 """
59 Purpose: command to recieve a list of folders and files
60 """
61 # Map the virtual path to our local server
62 serverPath = mapServerFolder(self.userFilesFolder,currentFolder)
63 # Open the folders / files node
64 folders = """<Folders>"""
65 files = """<Files>"""
66 for someObject in os.listdir(serverPath):
67 someObjectPath = mapServerFolder(serverPath, someObject)
68 if os.path.isdir(someObjectPath):
69 folders += """<Folder name="%s" />""" % (
70 convertToXmlAttribute(someObject)
71 )
72 elif os.path.isfile(someObjectPath):
73 size = os.path.getsize(someObjectPath)
74 files += """<File name="%s" size="%s" />""" % (
75 convertToXmlAttribute(someObject),
76 os.path.getsize(someObjectPath)
77 )
78 # Close the folders / files node
79 folders += """</Folders>"""
80 files += """</Files>"""
81 return folders + files
82
83 class CreateFolderCommandMixin (object):
84 def createFolder(self, resourceType, currentFolder):
85 """
86 Purpose: command to create a new folder
87 """
88 errorNo = 0; errorMsg ='';
89 if self.request.has_key("NewFolderName"):
90 newFolder = self.request.get("NewFolderName", None)
91 newFolder = sanitizeFolderName (newFolder)
92 try:
93 newFolderPath = mapServerFolder(self.userFilesFolder, combinePaths(currentFolder, newFolder))
94 self.createServerFolder(newFolderPath)
95 except Exception, e:
96 errorMsg = str(e).decode('iso-8859-1').encode('utf-8') # warning with encodigns!!!
97 if hasattr(e,'errno'):
98 if e.errno==17: #file already exists
99 errorNo=0
100 elif e.errno==13: # permission denied
101 errorNo = 103
102 elif e.errno==36 or e.errno==2 or e.errno==22: # filename too long / no such file / invalid name
103 errorNo = 102
104 else:
105 errorNo = 110
106 else:
107 errorNo = 102
108 return self.sendErrorNode ( errorNo, errorMsg )
109
110 def createServerFolder(self, folderPath):
111 "Purpose: physically creates a folder on the server"
112 # No need to check if the parent exists, just create all hierachy
113
114 try:
115 permissions = Config.ChmodOnFolderCreate
116 if not permissions:
117 os.makedirs(folderPath)
118 except AttributeError: #ChmodOnFolderCreate undefined
119 permissions = 0755
120
121 if permissions:
122 oldumask = os.umask(0)
123 os.makedirs(folderPath,mode=0755)
124 os.umask( oldumask )
125
126 class UploadFileCommandMixin (object):
127 def uploadFile(self, resourceType, currentFolder):
128 """
129 Purpose: command to upload files to server (same as FileUpload)
130 """
131 errorNo = 0
132 if self.request.has_key("NewFile"):
133 # newFile has all the contents we need
134 newFile = self.request.get("NewFile", "")
135 # Get the file name
136 newFileName = newFile.filename
137 newFileName = sanitizeFileName( newFileName )
138 newFileNameOnly = removeExtension(newFileName)
139 newFileExtension = getExtension(newFileName).lower()
140 allowedExtensions = Config.AllowedExtensions[resourceType]
141 deniedExtensions = Config.DeniedExtensions[resourceType]
142
143 if (allowedExtensions):
144 # Check for allowed
145 isAllowed = False
146 if (newFileExtension in allowedExtensions):
147 isAllowed = True
148 elif (deniedExtensions):
149 # Check for denied
150 isAllowed = True
151 if (newFileExtension in deniedExtensions):
152 isAllowed = False
153 else:
154 # No extension limitations
155 isAllowed = True
156
157 if (isAllowed):
158 # Upload to operating system
159 # Map the virtual path to the local server path
160 currentFolderPath = mapServerFolder(self.userFilesFolder, currentFolder)
161 i = 0
162 while (True):
163 newFilePath = os.path.join (currentFolderPath,newFileName)
164 if os.path.exists(newFilePath):
165 i += 1
166 newFileName = "%s(%04d).%s" % (
167 newFileNameOnly, i, newFileExtension
168 )
169 errorNo= 201 # file renamed
170 else:
171 # Read file contents and write to the desired path (similar to php's move_uploaded_file)
172 fout = file(newFilePath, 'wb')
173 while (True):
174 chunk = newFile.file.read(100000)
175 if not chunk: break
176 fout.write (chunk)
177 fout.close()
178
179 if os.path.exists ( newFilePath ):
180 doChmod = False
181 try:
182 doChmod = Config.ChmodOnUpload
183 permissions = Config.ChmodOnUpload
184 except AttributeError: #ChmodOnUpload undefined
185 doChmod = True
186 permissions = 0755
187 if ( doChmod ):
188 oldumask = os.umask(0)
189 os.chmod( newFilePath, permissions )
190 os.umask( oldumask )
191
192 newFileUrl = self.webUserFilesFolder + currentFolder + newFileName
193
194 return self.sendUploadResults( errorNo , newFileUrl, newFileName )
195 else:
196 return self.sendUploadResults( errorNo = 203, customMsg = "Extension not allowed" )
197 else:
198 return self.sendUploadResults( errorNo = 202, customMsg = "No File" )