Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def parse_file_item(self, item):
fn = item.value
try:
with open(os.path.expanduser(fn), 'rb') as f:
contents = f.read()
except IOError as e:
raise ParseError('"%s": %s' % (item.orig, e))
return os.path.basename(fn), BytesIO(contents), get_content_type(fn)
if self.args.files and not self.args.form:
# `http url @/path/to/file`
file_fields = list(self.args.files.keys())
if file_fields != ['']:
self.error(
'Invalid file fields (perhaps you meant --form?): %s'
% ','.join(file_fields))
fn, fd, ct = self.args.files['']
self.args.files = {}
self._body_from_file(fd)
if 'Content-Type' not in self.args.headers:
content_type = get_content_type(fn)
if content_type:
self.args.headers['Content-Type'] = content_type
def process_file_upload_arg(arg: KeyValueArg) -> Tuple[str, IO, str]:
filename = arg.value
try:
with open(os.path.expanduser(filename), 'rb') as f:
contents = f.read()
except IOError as e:
raise ParseError('"%s": %s' % (arg.orig, e))
return (
os.path.basename(filename),
BytesIO(contents),
get_content_type(filename),
)
def parse_file_item_chunked(arg: KeyValueArg):
fn = arg.value
try:
f = open(os.path.expanduser(fn), 'rb')
except IOError as e:
raise ParseError('"%s": %s' % (arg.orig, e))
return os.path.basename(fn), f, get_content_type(fn)
def parse_file_item_chunked(self, item):
fn = item.value
try:
f = open(os.path.expanduser(fn), 'rb')
except IOError as e:
raise ParseError('"%s": %s' % (item.orig, e))
return os.path.basename(fn), f, get_content_type(fn)
if self.args.files and not self.args.form:
# `http url @/path/to/file`
file_fields = list(self.args.files.keys())
if file_fields != ['']:
self.error(
'Invalid file fields (perhaps you meant --form?): %s'
% ','.join(file_fields))
fn, fd, ct = self.args.files['']
self.args.files = {}
self._body_from_file(fd)
if 'Content-Type' not in self.args.headers:
content_type = get_content_type(fn)
if content_type:
self.args.headers['Content-Type'] = content_type