| OLD | NEW |
| 1 # Copyright (C) 2010 Google Inc. All rights reserved. | 1 # Copyright (C) 2010 Google Inc. All rights reserved. |
| 2 # | 2 # |
| 3 # Redistribution and use in source and binary forms, with or without | 3 # Redistribution and use in source and binary forms, with or without |
| 4 # modification, are permitted provided that the following conditions are | 4 # modification, are permitted provided that the following conditions are |
| 5 # met: | 5 # met: |
| 6 # | 6 # |
| 7 # * Redistributions of source code must retain the above copyright | 7 # * Redistributions of source code must retain the above copyright |
| 8 # notice, this list of conditions and the following disclaimer. | 8 # notice, this list of conditions and the following disclaimer. |
| 9 # * Redistributions in binary form must reproduce the above | 9 # * Redistributions in binary form must reproduce the above |
| 10 # copyright notice, this list of conditions and the following disclaimer | 10 # copyright notice, this list of conditions and the following disclaimer |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 43 Args: | 43 Args: |
| 44 fields: A sequence of (name, value) elements for regular form fields. | 44 fields: A sequence of (name, value) elements for regular form fields. |
| 45 files: A sequence of (name, filename, value) elements for data to be | 45 files: A sequence of (name, filename, value) elements for data to be |
| 46 uploaded as files. | 46 uploaded as files. |
| 47 Returns: | 47 Returns: |
| 48 (content_type, body) ready for httplib.HTTP instance. | 48 (content_type, body) ready for httplib.HTTP instance. |
| 49 | 49 |
| 50 Source: | 50 Source: |
| 51 http://code.google.com/p/rietveld/source/browse/trunk/upload.py | 51 http://code.google.com/p/rietveld/source/browse/trunk/upload.py |
| 52 """ | 52 """ |
| 53 boundary = '-M-A-G-I-C---B-O-U-N-D-A-R-Y-' | 53 BOUNDARY = '-M-A-G-I-C---B-O-U-N-D-A-R-Y-' |
| 54 CRLF = '\r\n' | 54 CRLF = '\r\n' |
| 55 lines = [] | 55 lines = [] |
| 56 | 56 |
| 57 for key, value in fields: | 57 for key, value in fields: |
| 58 lines.append('--' + boundary) | 58 lines.append('--' + BOUNDARY) |
| 59 lines.append('Content-Disposition: form-data; name="%s"' % key) | 59 lines.append('Content-Disposition: form-data; name="%s"' % key) |
| 60 lines.append('') | 60 lines.append('') |
| 61 if isinstance(value, unicode): | 61 if isinstance(value, unicode): |
| 62 value = value.encode('utf-8') | 62 value = value.encode('utf-8') |
| 63 lines.append(value) | 63 lines.append(value) |
| 64 | 64 |
| 65 for key, filename, value in files: | 65 for key, filename, value in files: |
| 66 lines.append('--' + boundary) | 66 lines.append('--' + BOUNDARY) |
| 67 lines.append('Content-Disposition: form-data; name="%s"; filename="%s"'
% (key, filename)) | 67 lines.append('Content-Disposition: form-data; name="%s"; filename="%s"'
% (key, filename)) |
| 68 lines.append('Content-Type: %s' % get_mime_type(filename)) | 68 lines.append('Content-Type: %s' % get_mime_type(filename)) |
| 69 lines.append('') | 69 lines.append('') |
| 70 if isinstance(value, unicode): | 70 if isinstance(value, unicode): |
| 71 value = value.encode('utf-8') | 71 value = value.encode('utf-8') |
| 72 lines.append(value) | 72 lines.append(value) |
| 73 | 73 |
| 74 lines.append('--' + boundary + '--') | 74 lines.append('--' + BOUNDARY + '--') |
| 75 lines.append('') | 75 lines.append('') |
| 76 body = CRLF.join(lines) | 76 body = CRLF.join(lines) |
| 77 content_type = 'multipart/form-data; boundary=%s' % boundary | 77 content_type = 'multipart/form-data; boundary=%s' % BOUNDARY |
| 78 return content_type, body | 78 return content_type, body |
| 79 | 79 |
| 80 | 80 |
| 81 class FileUploader(object): | 81 class FileUploader(object): |
| 82 | 82 |
| 83 def __init__(self, url, timeout_seconds): | 83 def __init__(self, url, timeout_seconds): |
| 84 self._url = url | 84 self._url = url |
| 85 self._timeout_seconds = timeout_seconds | 85 self._timeout_seconds = timeout_seconds |
| 86 | 86 |
| 87 def upload_single_text_file(self, filesystem, content_type, filename): | 87 def upload_single_text_file(self, filesystem, content_type, filename): |
| (...skipping 10 matching lines...) Expand all Loading... |
| 98 | 98 |
| 99 def _upload_data(self, content_type, data): | 99 def _upload_data(self, content_type, data): |
| 100 def callback(): | 100 def callback(): |
| 101 # FIXME: Setting a timeout, either globally using socket.setdefaultt
imeout() | 101 # FIXME: Setting a timeout, either globally using socket.setdefaultt
imeout() |
| 102 # or in urlopen(), doesn't appear to work on Mac 10.5 with Python 2.
7. | 102 # or in urlopen(), doesn't appear to work on Mac 10.5 with Python 2.
7. |
| 103 # For now we will ignore the timeout value and hope for the best. | 103 # For now we will ignore the timeout value and hope for the best. |
| 104 request = urllib2.Request(self._url, data, {"Content-Type": content_
type}) | 104 request = urllib2.Request(self._url, data, {"Content-Type": content_
type}) |
| 105 return urllib2.urlopen(request) | 105 return urllib2.urlopen(request) |
| 106 | 106 |
| 107 return NetworkTransaction(timeout_seconds=self._timeout_seconds).run(cal
lback) | 107 return NetworkTransaction(timeout_seconds=self._timeout_seconds).run(cal
lback) |
| OLD | NEW |