| 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 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 73 lines.append(value) | 73 lines.append(value) |
| 74 | 74 |
| 75 lines.append('--' + BOUNDARY + '--') | 75 lines.append('--' + BOUNDARY + '--') |
| 76 lines.append('') | 76 lines.append('') |
| 77 body = CRLF.join(lines) | 77 body = CRLF.join(lines) |
| 78 content_type = 'multipart/form-data; boundary=%s' % BOUNDARY | 78 content_type = 'multipart/form-data; boundary=%s' % BOUNDARY |
| 79 return content_type, body | 79 return content_type, body |
| 80 | 80 |
| 81 | 81 |
| 82 class FileUploader(object): | 82 class FileUploader(object): |
| 83 |
| 83 def __init__(self, url, timeout_seconds): | 84 def __init__(self, url, timeout_seconds): |
| 84 self._url = url | 85 self._url = url |
| 85 self._timeout_seconds = timeout_seconds | 86 self._timeout_seconds = timeout_seconds |
| 86 | 87 |
| 87 def upload_single_text_file(self, filesystem, content_type, filename): | 88 def upload_single_text_file(self, filesystem, content_type, filename): |
| 88 return self._upload_data(content_type, filesystem.read_text_file(filenam
e)) | 89 return self._upload_data(content_type, filesystem.read_text_file(filenam
e)) |
| 89 | 90 |
| 90 def upload_as_multipart_form_data(self, filesystem, files, attrs): | 91 def upload_as_multipart_form_data(self, filesystem, files, attrs): |
| 91 file_objs = [] | 92 file_objs = [] |
| 92 for filename, path in files: | 93 for filename, path in files: |
| 93 file_objs.append(('file', filename, filesystem.read_binary_file(path
))) | 94 file_objs.append(('file', filename, filesystem.read_binary_file(path
))) |
| 94 | 95 |
| 95 # FIXME: We should use the same variable names for the formal and actual
parameters. | 96 # FIXME: We should use the same variable names for the formal and actual
parameters. |
| 96 content_type, data = _encode_multipart_form_data(attrs, file_objs) | 97 content_type, data = _encode_multipart_form_data(attrs, file_objs) |
| 97 return self._upload_data(content_type, data) | 98 return self._upload_data(content_type, data) |
| 98 | 99 |
| 99 def _upload_data(self, content_type, data): | 100 def _upload_data(self, content_type, data): |
| 100 def callback(): | 101 def callback(): |
| 101 # FIXME: Setting a timeout, either globally using socket.setdefaultt
imeout() | 102 # 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. | 103 # 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. | 104 # For now we will ignore the timeout value and hope for the best. |
| 104 request = urllib2.Request(self._url, data, {"Content-Type": content_
type}) | 105 request = urllib2.Request(self._url, data, {"Content-Type": content_
type}) |
| 105 return urllib2.urlopen(request) | 106 return urllib2.urlopen(request) |
| 106 | 107 |
| 107 return NetworkTransaction(timeout_seconds=self._timeout_seconds).run(cal
lback) | 108 return NetworkTransaction(timeout_seconds=self._timeout_seconds).run(cal
lback) |
| OLD | NEW |