Skip to Content.
Sympa Menu

idok-commit - [idok-commit] idok commit r40 - trunk/python/pydok/test

idok-commit AT lists.psi.ch

Subject: Commit emails of the iDok project

List archive

[idok-commit] idok commit r40 - trunk/python/pydok/test


Chronological Thread 
  • From: "AFS account Roman Geus" <geus AT savannah.psi.ch>
  • To: idok-commit AT lists.psi.ch
  • Subject: [idok-commit] idok commit r40 - trunk/python/pydok/test
  • Date: Wed, 12 Mar 2008 11:47:39 +0100
  • List-archive: <https://lists.web.psi.ch/pipermail/idok-commit/>
  • List-id: Commit emails of the iDok project <idok-commit.lists.psi.ch>

Author: geus
Date: Wed Mar 12 11:47:38 2008
New Revision: 40

Log:
Improved upload to iDok CGI application: implemented workaround for small
files, improved parsing of filename for Windows clients, implemented
standalone, iframe and json modes

Modified:
trunk/python/pydok/test/idok_upload_cgi.py

Modified: trunk/python/pydok/test/idok_upload_cgi.py
==============================================================================
--- trunk/python/pydok/test/idok_upload_cgi.py (original)
+++ trunk/python/pydok/test/idok_upload_cgi.py Wed Mar 12 11:47:38 2008
@@ -22,7 +22,7 @@
"""Simple web application for uploading files to iDok
"""

-import cgi, os, sys, urllib, StringIO
+import cgi, os, sys, urllib, StringIO, tempfile

# Enable HTML formatted stack traces
import cgitb; cgitb.enable()
@@ -45,13 +45,12 @@
cgi.FieldStorage.__init__(self, *args, **kwargs)

def __del__(self):
- "Delete all temporary files"
+ "Close (and delete) all temporary files"
for tempf in self.opened_files:
- os.unlink(tempf.name)
+ tempf.close()

def make_file(self, binary=None):
"Override method of base class"
- import tempfile
tempf = tempfile.NamedTemporaryFile("w+b")
self.opened_files.append(tempf)
return tempf
@@ -66,24 +65,28 @@
cli_service.sendCommand(command)
return file_buf.getvalue()

+def generate_dest_url(base_url, pathname):
+ """Generate the destination URL of the uploaded file
+
+ base_url: URL referring to the destination iDok folder
+ pathname: the path of uploaded file valid on the client. Depending on
the
+ client, it may be a foreign path (e.g. Windows or Mac OS).
+ """
+ idx = pathname.rfind("/")
+ if idx == -1:
+ idx = pathname.rfind("\\")
+ if idx == -1:
+ idx = pathname.rfind(":")
+ return base_url + pathname[idx+1:]
+
def main():
"Main CGI function"
form = MyFieldStorage()
- json_mode = form.getfirst("json_mode")
+ output_mode = form.getfirst("output_mode")
callback = form.getfirst("callback")
twiki_topic = form.getfirst("topic")
twiki_web = form.getfirst("web")
- if json_mode:
- fo = StringIO.StringIO()
- print "Content-Type: application/x-javascript; charset=utf-8"
- print
- else:
- fo = sys.stdout
- print "Content-Type: text/html; charset=utf-8"
- print
- print """<html>
- <title>Upload to iDok</title>
- <body>"""
+ fo = StringIO.StringIO()
fo.write("<h1>Upload to iDok</h1>")
if twiki_topic:
fo.write("<p><b>TWiki Topic</b>:%s</p>" % twiki_topic)
@@ -101,39 +104,66 @@
fo.write('<input type="hidden" name="topic" value="%s">' %
twiki_topic)
if twiki_web:
fo.write('<input type="hidden" name="web" value="%s">' % twiki_web)

- if json_mode:
- fo.write('<input type="hidden" name="json_mode" value="1">')
+ if output_mode:
+ fo.write('<input type="hidden" name="output_mode" value="%s">' %
output_mode)
fo.write("</form>")
if form.has_key("upload_file"):
fileitem = form["upload_file"]
+ # Test if the CGI file upload was successful
if fileitem.file and fileitem.filename and fileitem.done != -1:
- head, tail = os.path.split(fileitem.filename)
- dest_url = BASE_URL + urllib.pathname2url(tail)
- file_size = os.stat(fileitem.file.name).st_size
+ dest_url = generate_dest_url(BASE_URL, fileitem.filename)
+ tempf = None
+ if not hasattr(fileitem.file, 'fileno'):
+ # Small files are stored in a string buffer instead of on the
+ # file system. To proceed with the upload, we make a
temporary
+ # copy on the file system.
+ tempf = tempfile.NamedTemporaryFile("w+b")
+ tempf.write(fileitem.file.getvalue())
+ tempf.seek(0)
+ temp_file_name = tempf.name
+ else:
+ temp_file_name = fileitem.file.name
+
+ file_size = os.stat(temp_file_name).st_size
command = 'put -m "Imported by idok_upload_cgi.py" %s %s' % \
- (fileitem.file.name, dest_url)
+ (temp_file_name, dest_url)
try:
output = idok_cli(command)
fo.write("<h2>iDok CLI service output</h2>")
fo.write("<pre>%s</pre>" % output)
fo.write('<p>Local file <i>%s</i> uploaded to <a
href="%s">%s</a> (%d bytes)</p>' % \
(fileitem.filename, dest_url, dest_url, file_size))
+ if output_mode == "iframe":
+ output_mode = "location"
except ServiceException, se:
fo.write("<h2>iDok CLI service output</h2>")
fo.write("<p><b>iDok CLI service returned the following
error</b>: %s</p>" % se.userMessage)
if se.detailedMessage:
fo.write("<h3>Detailed message</h3><pre>%s</pre>" %
se.detailedMessage)
if se.stackTrace:
- fo.write("<h3>Stack trace</h3><pre>%s</pre>" %
se.stackTrace)
- if json_mode:
+ fo.write("<h3>Stack trace</h3><pre>%s</pre>" %
se.stackTrace)
+ if tempf:
+ # Close and remove the temporary file
+ tempf.close()
+ if output_mode == "js" and callback:
import json
- if callback:
- print "%s(%s)" % (callback,
- json.write({"#search": fo.getvalue()}))
- else:
- fo.write("""</body>
+ print "Content-Type: application/x-javascript; charset=utf-8"
+ print
+ print "%s(%s)" % (callback,
+ json.write({"#search": fo.getvalue()}))
+ elif output_mode == "location":
+ print "Location:
https://ait.web.psi.ch/docu/internal/twiki/pub/Projects/TWikitoDMSUploadAjax/ok.html?%s";
% urllib.pathname2url(dest_url)
+ print
+ else:
+ print "Content-Type: text/html; charset=utf-8"
+ print
+ print """<html>
+ <title>Upload to iDok</title>
+ <body>"""
+ print fo.getvalue()
+ print """</body>
</html>
- """)
+ """

if __name__ == "__main__":
main()



  • [idok-commit] idok commit r40 - trunk/python/pydok/test, AFS account Roman Geus, 03/12/2008

Archive powered by MHonArc 2.6.19.

Top of Page