def guess_file_name_stream_type_header(args): """ Guess filename, file stream, file type, file header from args. :param args: may be string (filepath), 2-tuples (filename, fileobj), 3-tuples (filename, fileobj, contentype) or 4-tuples (filename, fileobj, contentype, custom_headers). :return: filename, file stream, file type, file header """ ftype = None fheader = None if isinstance(args, (tuple, list)): if len(args) == 2: fname, fstream = args elif len(args) == 3: fname, fstream, ftype = args else: fname, fstream, ftype, fheader = args else: fname, fstream = guess_filename_stream(args) ftype = guess_content_type(fname) if isinstance(fstream, (str, bytes, bytearray)): fdata = fstream else: fdata = fstream.read() return fname, fdata, ftype, fheader
def setUpClass(cls): cls.filename, cls.stream = guess_filename_stream( "tests/resources/file.pdf") MetashieldCleanUp.set_default_host(settings.HOST) if settings.PROXY is not None: MetashieldCleanUp.set_default_proxy(settings.PROXY) cls.api = MetashieldCleanUp(settings.DATASET["app_id"], settings.DATASET["secret_key"])
def test_encode_multipart_data_files_as_2tuple_parameter(self): filename, stream = guess_filename_stream("tests/resources/file.pdf") filename2, stream2 = guess_filename_stream("tests/resources/file.png") files = {"file_upload1": (filename, stream), "file_upload2": (filename2, stream2)} data = {"param1": "value1", "param2": "value2"} r = MultiPartRenderer() body, content_type = r.encode_params(data, files) self.assertEqual(content_type, "multipart/form-data; boundary=----------ThIs_Is_tHe_bouNdaRY_$") self.assertIn(b"param1", body) self.assertIn(b"value1", body) self.assertIn(b"param2", body) self.assertIn(b"value2", body) self.assertIn(b"file_upload", body) self.assertIn(b"file.pdf", body) self.assertNotIn(b"application/pdf", body) self.assertIn(b"file_upload2", body) self.assertIn(b"file.png", body) self.assertNotIn(b"Content-Type: image/png", body)
def test_encode_multipart_data_files_as_2tuple_parameter(self): filename, stream = guess_filename_stream("tests/resources/file.pdf") filename2, stream2 = guess_filename_stream("tests/resources/file.png") files = {"file_upload1": (filename, stream), "file_upload2": (filename2, stream2)} data = {"param1": "value1", "param2": "value2"} r = MultiPartRenderer() body, content_type = r.encode_params(data, files) self.assertEqual(content_type, "multipart/form-data; boundary=----------ThIs_Is_tHe_bouNdaRY") self.assertIn(b"param1", body) self.assertIn(b"value1", body) self.assertIn(b"param2", body) self.assertIn(b"value2", body) self.assertIn(b"file_upload", body) self.assertIn(b"file.pdf", body) self.assertNotIn(b"application/pdf", body) self.assertIn(b"file_upload2", body) self.assertIn(b"file.png", body) self.assertNotIn(b"Content-Type: image/png", body)
def test_encode_multipart_data_files_as_4tuple_parameter(self): filename, stream = guess_filename_stream("tests/resources/file.pdf") files = {"file_upload1": (filename, stream, "application/xxx", {"x-header": "value", "time": "now"})} data = {"param1": "value1", "param2": "value2"} r = MultiPartRenderer() body, content_type = r.encode_params(data, files) self.assertEqual(content_type, "multipart/form-data; boundary=----------ThIs_Is_tHe_bouNdaRY_$") self.assertIn(b"param1", body) self.assertIn(b"value1", body) self.assertIn(b"param2", body) self.assertIn(b"value2", body) self.assertIn(b"file_upload", body) self.assertIn(b"file.pdf", body) self.assertIn(b"Content-Type: application/xxx", body) self.assertIn(b"x-header: value", body) self.assertIn(b"time: now", body)
def test_encode_multipart_data_files_as_4tuple_parameter(self): filename, stream = guess_filename_stream("tests/resources/file.pdf") files = {"file_upload1": (filename, stream, "application/xxx", {"x-header": "value", "time": "now"})} data = {"param1": "value1", "param2": "value2"} r = MultiPartRenderer() body, content_type = r.encode_params(data, files) self.assertEqual(content_type, "multipart/form-data; boundary=----------ThIs_Is_tHe_bouNdaRY") self.assertIn(b"param1", body) self.assertIn(b"value1", body) self.assertIn(b"param2", body) self.assertIn(b"value2", body) self.assertIn(b"file_upload", body) self.assertIn(b"file.pdf", body) self.assertIn(b"Content-Type: application/xxx", body) self.assertIn(b"x-header: value", body) self.assertIn(b"time: now", body)
def _hash_file(context): (k, v) = context.files[0] if isinstance(v, (tuple, list)): if len(v) == 2: fn, fp = v elif len(v) == 3: fn, fp, ft = v else: fn, fp, ft, fh = v else: fn, fp = guess_filename_stream(v) if isinstance(fp, (str, bytes, bytearray)): fdata = fp else: fdata = fp.read() return sha1(fdata).hexdigest()
def encode_params(self, data=None, files=None, **kwargs): """ Build the body for a multipart/form-data request. Will successfully encode files when passed as a dict or a list of tuples. Order is retained if data is a list of tuples but arbitrary if parameters are supplied as a dict. The tuples may be string (filepath), 2-tuples (filename, fileobj), 3-tuples (filename, fileobj, contentype) or 4-tuples (filename, fileobj, contentype, custom_headers). """ if isinstance(data, basestring): raise ValueError("Data must not be a string.") # optional args boundary = kwargs.get("boundary", None) output_str = kwargs.get("output_str", self.output_str) new_fields = [] fields = to_key_val_list(data or {}) files = to_key_val_list(files or {}) for field, value in fields: ctype = None if isinstance(value, (tuple, list)) and len(value) == 2: val, ctype = value else: val = value if isinstance(val, basestring) or not hasattr(val, '__iter__'): val = [val] for v in val: # Don't call str() on bytestrings: in Py3 it all goes wrong. if not isinstance(v, bytes): v = to_string(v, lang=output_str) field = field.decode('utf-8') if isinstance(field, bytes) else field v = v.encode('utf-8') if isinstance(v, str) else v rf = RequestField(name=field, data=v) rf.make_multipart(content_type=ctype) new_fields.append(rf) for (k, v) in files: # support for explicit filename ft = None fh = None if isinstance(v, (tuple, list)): if len(v) == 2: fn, fp = v elif len(v) == 3: fn, fp, ft = v else: fn, fp, ft, fh = v else: fn, fp = guess_filename_stream(v) ft = guess_content_type(fn) if isinstance(fp, (str, bytes, bytearray)): fdata = fp else: fdata = fp.read() rf = RequestField(name=k, data=fdata, filename=fn, headers=fh) rf.make_multipart(content_type=ft) new_fields.append(rf) if boundary is None: boundary = self.boundary body, content_type = encode_multipart_formdata(new_fields, boundary=boundary) return body, content_type
def test_create_file(self): fname, fstream = guess_filename_stream("tests/resources/file.pdf") response = self.api.create_file_11paths_auth( fname, fstream, "235hWLEETQ46KWLnAg48", "lBc4BSeqtGkidJZXictc3yiHbKBS87hjE078rswJ") self.assertEqual(404, response.status)