You are not logged in Log in Join
You are here: Home » Members » Stefan's Home » Patches for exUserFolder, ... » ExtFile-1.1.3.patch3 » View File

Log in
Name

Password

 

ExtFile-1.1.3.patch3

File details
Size
13 K
File type
text/plain

File contents

--- ExtFile.py	Tue Sep 11 02:04:10 2001
+++ ExtFile.py	Wed Aug 28 18:54:31 2002
@@ -62,6 +62,7 @@
 from os.path import join, isfile
 try: from cStringIO import StringIO
 except: from StringIO import StringIO
+from PIL import Image
 
 FLAT = 0
 SYNC_ZODB = 1
@@ -95,14 +96,12 @@
 		            string.rfind(id,':')
 		           )+1:]
 		id = string.translate(id, TRANSMAP)
-	self = self.this()
+	#self = self.this()
 	tempExtFile = ExtFile(id, title, descr, permission_check)
 	self._setObject(id, tempExtFile)
 	self._getOb(id).manage_file_upload(file, content_type)
 	if REQUEST is not None:
-		return MessageDialog(title = 'Created',
-			message = 'The ExtFile %s was successfully created!' % id,
-			action = './manage_main',)
+		return self.manage_main(self, REQUEST, update_menu=0)
 
 class ExtFile(CatalogAware, SimpleItem, PropertyManager): 
 	
@@ -217,7 +216,7 @@
 					else:
 						last_mod = long(0)
 					if last_mod > 0 and last_mod < mod_since:
-						RESPONSE.setStatus(304)
+						REQUEST.RESPONSE.setStatus(304)
 						return ''
 		
 		if hasattr(self,'has_preview') and self.has_preview: has_preview = 1
@@ -226,8 +225,7 @@
 		if (preview and not has_preview): icon = 1
 		
 		if icon:
-			filename = join(SOFTWARE_HOME, 'Products', 
-			                'ExtFile', self.getIconPath())
+			filename = join(Globals.package_home(globals()), self.getIconPath())
 			content_type = 'image/gif'
 		elif preview:
 			filename = self._get_filename(self.prev_filename)
@@ -241,16 +239,15 @@
 		if isfile(filename):
 			size = os.stat(filename)[6] # file size
 		else:
-			filename = join(SOFTWARE_HOME, 'Products',
-			              'ExtFile', 'icons', 'broken.gif')
+			filename = join(Globals.package_home(globals()), 'icons', 'broken.gif')
 			try: size = os.stat(filename)[6]
 			except: raise cant_read_exc, ("%s (%s)" %(self.id, filename))
 			content_type = 'image/gif'
 			icon = 1
-		data = StringIO()
+
 		if icon==0 and width is not None and height is not None:
+			data = StringIO() # hold resized image
 			try:
-				from PIL import Image
 				im = Image.open(filename) 
 				if im.mode!='RGB' and im.mode!='CMYK': im = im.convert("RGB")
 				im.draft(None,(int(width),int(height)))
@@ -258,19 +255,31 @@
 				im = im.resize((int(width),int(height)), Image.BICUBIC)
 				im.save(data, 'JPEG')
 			except:
-				self._copy(filename, data)
+				data = open(filename, 'rb')
 			else:
 				data.seek(0,2)
 				size = data.tell()
+				data.seek(0)
 				content_type = 'image/jpeg'
 		else:
-			self._copy(filename, data)
-		if REQUEST is not None:
-			last_mod = rfc1123_date(self._p_mtime)
-			REQUEST.RESPONSE.setHeader('Last-Modified', last_mod)
-			REQUEST.RESPONSE.setHeader('Content-Type', content_type)
-			REQUEST.RESPONSE.setHeader('Content-Length', size)
-		return data.getvalue()
+			data = open(filename, 'rb')
+		try:
+			if REQUEST is not None:
+				last_mod = rfc1123_date(self._p_mtime)
+				REQUEST.RESPONSE.setHeader('Last-Modified', last_mod)
+				REQUEST.RESPONSE.setHeader('Content-Type', content_type)
+				REQUEST.RESPONSE.setHeader('Content-Length', size)
+				blocksize = 2<<16
+				while 1:
+					buffer = data.read(blocksize)
+					REQUEST.RESPONSE.write(buffer)
+					if len(buffer) < blocksize:
+						break
+				return ''
+			else:
+				return data.read()
+		finally:
+			data.close()
 	
 	def view_image_or_file(self):
 		""" The default view of the contents of the File or Image. """
@@ -356,11 +365,7 @@
 		# update ZCatalog
 		self.reindex_object()
 		if REQUEST is not None:
-			return MessageDialog(
-				title = 'Edited',
-				message = "The properties of %s have been changed!" % self.id,
-				action = './manage_main',
-			)
+			return self.manage_main(self, REQUEST, manage_tabs_message='Saved changes.')                
 	
 	# File upload Interface
 	manage_uploadForm = HTMLFile('extFileUpload', globals())
@@ -373,9 +378,7 @@
 		                    self.id, content_type or self.content_type)
 		self.filename = new_fn
 		if REQUEST is not None:
-			return MessageDialog(title = 'Uploaded',
-				message = "The file was uploaded successfully!", 
-				action = './manage_main',)
+			return self.manage_main(self, REQUEST, manage_tabs_message='Upload complete.')                
 	
 	def manage_http_upload(self, url, REQUEST=None):
 		""" Upload file from http-server """
@@ -397,9 +400,7 @@
 		else:
 			self.filename = new_fn
 		if REQUEST is not None:
-			return MessageDialog(title = 'Uploaded',
-				message = "The file was uploaded successfully!", 
-				action = './manage_main',)
+			return self.manage_main(self, REQUEST, manage_tabs_message='Upload complete.')                
 	
 	manage_FTPget = index_html
 	
@@ -408,7 +409,12 @@
 		self.dav__init(REQUEST, RESPONSE)
 		content_type = REQUEST.get_header('content-type', None)
 		instream = REQUEST['BODYFILE']
-		new_fn = self._get_ufn(self.filename)
+		# XXX: creating the disk file will fail without a proper 
+		#      path. however, absolute_url() does not work here,
+		#      as we are not yet seated. as a workaround we 
+		#      introduce the path parameter to pass the location 
+		#      explicitly. also see the _get_new_ufn() method.
+		new_fn = self._get_ufn(self.filename, path=REQUEST.steps[:-2])
 		self._copy(instream, self._get_filename(new_fn))
 		try:
 			self.content_type = self._get_content_type(instream, 
@@ -537,12 +543,12 @@
 			path = join(path,filename)
 		return path
 	
-	def _get_ufn(self, filename):
+	def _get_ufn(self, filename, path=None):
 		""" If no unique filename has been generated, generate one
 		    otherwise, return the existing one.
 		"""
 		if UNDO_POLICY==ALWAYS_BACKUP or filename==[]: 
-			new_fn = self._get_new_ufn()
+			new_fn = self._get_new_ufn(path=path)
 		else: 
 			new_fn = filename[:]
 		if filename:
@@ -555,9 +561,14 @@
 				except: pass
 		return new_fn
 	
-	def _get_new_ufn(self):
+	def _get_new_ufn(self, path=None):
 		""" Create a new unique filename """
-		rel_url_list = string.split(self.absolute_url(1), '/')[:-1]
+		# XXX: absolute_url() does not work inside a PUT factory
+		#      so we pass in the path. also see the PUT() method.
+		if path is not None:
+			rel_url_list = path
+		else:
+			rel_url_list = string.split(self.absolute_url(1), '/')[:-1]
 		rel_url_list = filter(None, rel_url_list)
 		pos = string.rfind(self.id, '.')
 		if (pos+1):
--- extFileAdd.dtml	Wed Jun 27 00:20:44 2001
+++ extFileAdd.dtml	Fri Aug 23 16:35:35 2002
@@ -1,4 +1,4 @@
-<dtml-if "_.string.find(REQUEST.get('SERVER_SOFTWARE', ''), 'Zope 2.3.')>=0">
+<dtml-if "_.hasattr(this(), 'manage_page_header')">
 <dtml-var manage_page_header>
 <dtml-var "manage_form_title(this(), _,
            form_title='Add ExtFile',
@@ -59,4 +59,4 @@
 			</table>
 		</form>
 	</body>
-</html>
\ No newline at end of file
+</html>
--- extFileEdit.dtml	Wed Jun 27 00:20:48 2001
+++ extFileEdit.dtml	Fri Aug 23 16:36:51 2002
@@ -1,4 +1,4 @@
-<dtml-if "_.string.find(REQUEST.get('SERVER_SOFTWARE', ''), 'Zope 2.3.')>=0">
+<dtml-if "_.hasattr(this(), 'manage_page_header')">
 <dtml-var manage_page_header>
 <dtml-var manage_tabs>
 <dtml-else>
@@ -55,4 +55,4 @@
 			</table>
 		</form>
 	</body>
-</html>
\ No newline at end of file
+</html>
--- extFileUpload.dtml	Wed Jun 27 00:21:50 2001
+++ extFileUpload.dtml	Fri Aug 23 16:37:18 2002
@@ -1,4 +1,4 @@
-<dtml-if "_.string.find(REQUEST.get('SERVER_SOFTWARE', ''), 'Zope 2.3.')>=0">
+<dtml-if "_.hasattr(this(), 'manage_page_header')">
 <dtml-var manage_page_header>
 <dtml-var manage_tabs>
 <dtml-else>
@@ -39,4 +39,4 @@
 			</table>
 		</form>
 	</body>
-</html>
\ No newline at end of file
+</html>
--- ExtImage.py	Tue Sep 11 02:04:02 2001
+++ ExtImage.py	Wed Aug 28 15:41:06 2002
@@ -59,6 +59,7 @@
 from os.path import join, isfile
 try: from cStringIO import StringIO
 except: from StringIO import StringIO
+from PIL import Image
 
 NO_PREVIEW = 0
 GENERATE = 1
@@ -81,7 +82,7 @@
 		            string.rfind(id,':')
 		           )+1:]
 		id = string.translate(id, TRANSMAP)
-	self = self.this()
+	#self = self.this()
 	tempExtImage = ExtImage(id, title, descr, permission_check)
 	self._setObject(id, tempExtImage)
 	self._getOb(id).manage_file_upload(file, content_type, 0, create_prev, 
@@ -90,9 +91,7 @@
 		self._getOb(id).manage_file_upload(preview, content_type, 1, 
 		                                   create_prev, maxx, maxy, ratio)
 	if REQUEST is not None:
-		return MessageDialog(title = 'Created',
-			message = 'The ExtImage %s was successfully created!' % id,
-			action = './manage_main',)
+		return self.manage_main(self, REQUEST, update_menu=0)
 
 class ExtImage(ExtFile): 
 	
@@ -250,7 +249,6 @@
 	def format(self):
 		""" Get the Fileformat of the image """
 		try:
-			from PIL import Image
 			im = Image.open(self._get_filename(self.filename)) 
 			return im.format
 		except:
@@ -272,7 +270,7 @@
 		self.prev_content_type = ''
 		self.has_preview = 0
 		if REQUEST is not None:
-			return self.manage_main(self, REQUEST)
+			return self.manage_main(self, REQUEST, manage_tabs_message='Preview deleted.')
 	
 	def manage_create_prev(self, maxx=0, maxy=0, ratio=0, REQUEST=None):
 		""" Create a preview Image """
@@ -283,7 +281,7 @@
 			                    maxx, maxy, ratio)
 		if REQUEST is not None:
 			if self.has_preview: 
-				return self.manage_main(self, REQUEST)
+				return self.manage_main(self, REQUEST, manage_tabs_message='Preview created.')
 			elif maxx=='0' and maxy=='0':
 				return MessageDialog(
 					title = 'Attention',
@@ -326,9 +324,7 @@
 					self._createPreview(self.filename, prev_filename, 
 					                    maxx, maxy, ratio)
 		if REQUEST is not None:
-			return MessageDialog(title = 'Uploaded',
-				message = "The file was uploaded successfully!",
-				action = './manage_main',)
+			return self.manage_main(self, REQUEST, manage_tabs_message='Upload complete.')
 	
 	def manage_http_upload(self, url, is_preview=0, REQUEST=None):
 		""" Upload file from http-server """
@@ -352,16 +348,14 @@
 				self._createPreview(self.filename, prev_filename, 
 				                    width, height, 0)
 		if REQUEST is not None:
-			return MessageDialog(title = 'Uploaded',
-				message = "The file was uploaded successfully!",
-				action = './manage_main',)
+			return self.manage_main(self, REQUEST, manage_tabs_message='Upload complete.')
 	
 	def PUT(self, REQUEST, RESPONSE):
 		""" Handle HTTP PUT requests """
 		RESPONSE = ExtImage.inheritedAttribute("PUT")(self,REQUEST, RESPONSE)
 		if self.has_preview:
 			width, height = self._getImageSize(self.prev_filename)
-			prev_filename = self._get_ufn()
+			prev_filename = self._get_ufn(path=REQUEST.steps[:-2])
 			self._createPreview(self.filename, prev_filename, 
 			                    width, height, 0)
 		return RESPONSE
@@ -379,7 +373,6 @@
 			
 	def _getImageSize(self, filename):
 		try:
-			from PIL import Image
 			try: 
 				im = Image.open(self._get_filename(filename)) 
 			except:
@@ -394,7 +387,6 @@
 	def _createPreview(self, from_filename, to_filename, maxx, maxy, ratio):
 		""" generate a preview using PIL """
 		try:
-			from PIL import Image
 			im = Image.open(self._get_filename(from_filename)) 
 			if im.mode!='RGB' and im.mode!='CMYK': im = im.convert("RGB")
 			if ratio==1: 			# keep aspect-ratio
@@ -433,12 +425,12 @@
 				os.rename(fn+'.undo', fn)			# rename preview
 		return ExtImage.inheritedAttribute("_undo")(self)
 	
-	def _get_ufn(self, filename=None):
+	def _get_ufn(self, filename=None, path=None):
 		""" If no unique filename has been generated, generate one
 		    otherwise, return the existing one.
 		"""
 		if filename is not None:
-			return ExtImage.inheritedAttribute("_get_ufn") (self, filename)
+			return ExtImage.inheritedAttribute("_get_ufn") (self, filename, path=path)
 		new_fn = ''
 		if self.filename:
 			test_fn = self._get_filename(self.filename) + '.preview'
@@ -446,7 +438,7 @@
 				new_fn = self.filename[:]
 		if not new_fn:
 			new_fn = ExtImage.inheritedAttribute("_get_ufn") (self, 
-			                                      self.prev_filename)
+			                                      self.prev_filename, path=path)
 		if new_fn != self.prev_filename:
 			new_fn[-1] = new_fn[-1] + '.preview'
 		return new_fn
--- extImageAdd.dtml	Wed Jun 27 00:19:54 2001
+++ extImageAdd.dtml	Wed Aug 28 17:47:47 2002
@@ -1,4 +1,4 @@
-<dtml-if "_.string.find(REQUEST.get('SERVER_SOFTWARE', ''), 'Zope 2.3.')>=0">
+<dtml-if "_.hasattr(this(), 'manage_page_header')">
 <dtml-var manage_page_header>
 <dtml-var "manage_form_title(this(), _,
            form_title='Add ExtImage',
@@ -140,4 +140,4 @@
 		//-->
 		</script>
 	</body>
-</html>
\ No newline at end of file
+</html>
--- extImageEdit.dtml	Tue Sep 11 01:55:00 2001
+++ extImageEdit.dtml	Wed Aug 28 17:48:09 2002
@@ -1,4 +1,4 @@
-<dtml-if "_.string.find(REQUEST.get('SERVER_SOFTWARE', ''), 'Zope 2.3.')>=0">
+<dtml-if "_.hasattr(this(), 'manage_page_header')">
 <dtml-var manage_page_header>
 <dtml-var manage_tabs>
 <dtml-else>
@@ -89,4 +89,4 @@
 			</table>
 		</form>
 	</body>
-</html>
\ No newline at end of file
+</html>
--- extImageUpload.dtml	Wed Jun 27 00:20:14 2001
+++ extImageUpload.dtml	Wed Aug 28 17:48:22 2002
@@ -1,4 +1,4 @@
-<dtml-if "_.string.find(REQUEST.get('SERVER_SOFTWARE', ''), 'Zope 2.3.')>=0">
+<dtml-if "_.hasattr(this(), 'manage_page_header')">
 <dtml-var manage_page_header>
 <dtml-var manage_tabs>
 <dtml-else>
@@ -47,9 +47,3 @@
 		</form>
 	</body>
 </html>
-
-
-
-
-
-