| 7 | |
| 8 | |
| 9 | class _FormDataPart: |
| 10 | def __init__(self, header, files_path): |
| 11 | header_str = header.decode('utf-8') |
| 12 | if '\r\n' in header_str: |
| 13 | header_str = header_str[:header_str.index('\r\n')] |
| 14 | |
| 15 | (value, sub_headers) = parse_header(header_str) |
| 16 | |
| 17 | self.name = sub_headers['name'] |
| 18 | |
| 19 | self.filename = sub_headers.get('filename') |
| 20 | |
| 21 | if self.filename: |
| 22 | self.path = os.path.join(files_path, self.filename) |
| 23 | self.path = file_utils.create_unique_filename(self.path) |
| 24 | # touch file |
| 25 | open(self.path, 'w').close() |
| 26 | else: |
| 27 | self.value = '' |
| 28 | |
| 29 | def write(self, chunk): |
| 30 | if self.filename: |
| 31 | with open(self.path, 'ab') as f: |
| 32 | f.write(chunk) |
| 33 | return |
| 34 | |
| 35 | self.value += chunk.decode('utf-8') |
| 36 | |
| 37 | |
| 38 | HttpFormFile = namedtuple('HttpFormFile', ['filename', 'path']) |