Rushabh Mehta | bdda977 | 2011-09-15 16:14:01 +0530 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | try: |
| 4 | |
| 5 | import sys, os |
| 6 | |
| 7 | sys.path.append('../lib/py') |
| 8 | sys.path.append('../erpnext') |
| 9 | |
| 10 | def getTraceback(): |
| 11 | import sys, traceback, string |
| 12 | type, value, tb = sys.exc_info() |
| 13 | body = "Traceback (innermost last):\n" |
| 14 | list = traceback.format_tb(tb, None) \ |
| 15 | + traceback.format_exception_only(type, value) |
| 16 | body = body + "%-20s %s" % (string.join(list[:-1], ""), list[-1]) |
| 17 | return body |
| 18 | |
| 19 | import cgi |
| 20 | import webnotes |
| 21 | import webnotes.auth |
| 22 | import webnotes.utils |
| 23 | import webnotes.utils.file_manager |
| 24 | import webnotes.db |
| 25 | import webnotes.defs |
| 26 | |
| 27 | sys.path.append(webnotes.defs.modules_path) |
| 28 | |
| 29 | form = cgi.FieldStorage() |
| 30 | webnotes.form_dict = {} |
| 31 | |
| 32 | for each in form.keys(): |
| 33 | webnotes.form_dict[each] = form.getvalue(each) |
| 34 | |
| 35 | n = form.getvalue('name') |
| 36 | |
| 37 | # authenticate |
| 38 | webnotes.auth.HTTPRequest() |
| 39 | |
| 40 | # get file |
| 41 | res = webnotes.utils.file_manager.get_file(n) |
| 42 | |
| 43 | fname = res[0] |
| 44 | if hasattr(res[1], 'tostring'): |
| 45 | fcontent = res[1].tostring() |
| 46 | else: |
| 47 | fcontent = res[1] |
| 48 | |
| 49 | if form.getvalue('thumbnail'): |
| 50 | tn = webnotes.utils.cint(form.getvalue('thumbnail')) |
| 51 | try: |
| 52 | from PIL import Image |
| 53 | import cStringIO |
| 54 | |
| 55 | fobj = cStringIO.StringIO(fcontent) |
| 56 | image = Image.open(fobj) |
| 57 | image.thumbnail((tn,tn*2), Image.ANTIALIAS) |
| 58 | outfile = cStringIO.StringIO() |
| 59 | |
| 60 | if image.mode != "RGB": |
| 61 | image = image.convert("RGB") |
| 62 | |
| 63 | image.save(outfile, 'JPEG') |
| 64 | outfile.seek(0) |
| 65 | fcontent = outfile.read() |
| 66 | except: |
| 67 | pass |
| 68 | |
| 69 | import mimetypes |
| 70 | print "Content-Type: %s" % (mimetypes.guess_type(fname)[0] or 'application/unknown') |
| 71 | print "Content-Disposition: filename="+fname.replace(' ', '_') |
| 72 | print "Cache-Control: max-age=3600" |
| 73 | print |
| 74 | print fcontent |
| 75 | |
| 76 | except Exception, e: |
| 77 | print "Content-Type: text/html" |
| 78 | try: |
| 79 | out = {'message':'', 'exc':getTraceback().replace('\n','<br>')} |
| 80 | except: |
| 81 | out = {'exc': e} |
| 82 | print |
| 83 | print str(out) |