[Zope-dev] patch to allow importing through file upload

Ava ava@dde974.equipement.gouv.fr
Tue, 30 Nov 1999 10:54:30 +0400


This message is in MIME format. Since your mail reader does not understand
this format, some or all of this message may not be legible.

------_=_NextPart_000_01BF3AFF.C0C131D0
Content-Type: text/plain;
	charset="iso-8859-1"

Hello,

I've written a patch to allow one to upload files in the export/import tab.
the file limit is set to 64Kb.

I hope this is useful
Again, comments are welcome (please send them to minf7@educ.univ-reunion.fr
instead of ava@dde.equipement.gouv.fr)

Regards,
Jephte CLAIN

There are 2 patches, one against ObjectManager.py, one against
importExport.dtml, both located in lib/python/OFS
Cordialement,


------_=_NextPart_000_01BF3AFF.C0C131D0
Content-Type: application/octet-stream;
	name="importExport.dtml.patch"
Content-Disposition: attachment;
	filename="importExport.dtml.patch"

--- importExport.dtml.orig	Thu Aug 26 06:20:18 1999
+++ importExport.dtml	Tue Nov 30 06:23:16 1999
@@ -51,14 +51,40 @@
 the "import" directory in the root of your Zope installation 
 if it does not yet exist.
 </P>
+<P>
+You can also upload the file to be imported. However, the file must not be
+over 64Kb in size. Note: if you wan't to upload the file to be imported,
+don't forget to check the &quot;Upload file to import&quot; radio button
+before you click on &quot;Import&quot;.
+</P>
 
-<FORM ACTION="manage_importObject" METHOD="POST">
+<FORM ACTION="manage_importObject" METHOD="POST" ENCTYPE="multipart/form-data">
 <TABLE CELLSPACING="2">
 
 <TR>
+  <TD></TD>
+  <TD ALIGN="LEFT" VALIGN="TOP">
+  <INPUT TYPE="RADIO" NAME="upload:int" VALUE="0" CHECKED>
+  Import from file on server
+  </TD>
+</TR>
+<TR>
   <TH ALIGN="LEFT" VALIGN="TOP">Import file name</TH>
   <TD ALIGN="LEFT" VALIGN="TOP">
   <INPUT TYPE="text" NAME="file" SIZE="25" VALUE="">
+  </TD>
+</TR>
+<TR>
+  <TD></TD>
+  <TD ALIGN="LEFT" VALIGN="TOP">
+  <INPUT TYPE="RADIO" NAME="upload:int" VALUE="1">
+  Upload file to import
+  </TD>
+</TR>
+<TR>
+  <TH ALIGN="LEFT" VALIGN="TOP">Attach file here</TH>
+  <TD ALIGN="LEFT" VALIGN="TOP">
+  <INPUT TYPE="file" NAME="uploadfile" SIZE="25">
   </TD>
 </TR>
 <TR>

------_=_NextPart_000_01BF3AFF.C0C131D0
Content-Type: application/octet-stream;
	name="ObjectManager.py.patch"
Content-Disposition: attachment;
	filename="ObjectManager.py.patch"

--- ObjectManager.py.orig	Thu Jul 22 11:19:22 1999
+++ ObjectManager.py	Tue Nov 30 06:24:26 1999
@@ -464,14 +464,28 @@
 
     manage_importExportForm=HTMLFile('importExport',globals())
 
-    def manage_importObject(self, file, REQUEST=None):
+    def manage_importObject(self, file=None, uploadfile=None, upload=None, REQUEST=None):
         """Import an object from a file"""
-        dirname, file=os.path.split(file)
-        if dirname:
-            raise 'Bad Request', 'Invalid file name %s' % file
-        file=os.path.join(INSTANCE_HOME, 'import', file)
-        if not os.path.exists(file):
-            raise 'Bad Request', 'File does not exist: %s' % file
+        if upload:
+            file=uploadfile
+            if file and file.filename:
+                # check file size
+                # We do not need to check anything else, because if the file is invalid,
+                # the import machinery will raise an error
+                file.seek(0, 2)
+                if file.tell() > 65536: # the limit is 64K. is this enough or too much ???
+                    raise 'Bad Request', 'Files imported can not be over 64K in size'
+                file.seek(0, 0)
+            else:
+                raise 'Bad Request', 'You must specify the file to upload'
+        else:
+            dirname, file=os.path.split(file)
+            if dirname:
+                raise 'Bad Request', 'Invalid file name %s' % file
+            file=os.path.join(INSTANCE_HOME, 'import', file)
+            if not os.path.exists(file):
+                raise 'Bad Request', 'File does not exist: %s' % file
+
         ob=self._p_jar.importFile(file)
         if REQUEST: self._verifyObjectPaste(ob, REQUEST)
         id=ob.id

------_=_NextPart_000_01BF3AFF.C0C131D0--