Commit f8426b5c authored by Stavros Sachtouris's avatar Stavros Sachtouris
Browse files

Guess mimetype and use it in uploads

Refs: #4196
parent cedde35d
......@@ -40,7 +40,7 @@ from kamaki.cli.command_tree import CommandTree
from kamaki.cli.errors import raiseCLIError, CLISyntaxError, CLIBaseUrlError
from kamaki.cli.utils import (
format_size, to_bytes, print_dict, print_items, pretty_keys, pretty_dict,
page_hold, bold, ask_user, get_path_size, print_json)
page_hold, bold, ask_user, get_path_size, print_json, guess_mime_type)
from kamaki.cli.argument import FlagArgument, ValueArgument, IntArgument
from kamaki.cli.argument import KeyValueArgument, DateArgument
from kamaki.cli.argument import ProgressBarArgument
......@@ -983,11 +983,12 @@ class file_manifest(_file_container_command, _optional_output_cmd):
@errors.pithos.container
@errors.pithos.object_path
def _run(self):
ctype, cenv = guess_mime_type(self.path)
self._optional_output(self.client.create_object_by_manifestation(
self.path,
content_encoding=self['content_encoding'],
content_encoding=self['content_encoding'] or cenc,
content_disposition=self['content_disposition'],
content_type=self['content_type'],
content_type=self['content_type'], or ctype,
sharing=self['sharing'],
public=self['public']))
......@@ -1135,6 +1136,10 @@ class file_upload(_file_container_command, _optional_output_cmd):
container_info_cache = dict()
for f, rpath in self._path_pairs(local_path, remote_path):
print('%s --> %s:%s' % (f.name, self.client.container, rpath))
if not (self['content_type'] and self['content_encoding']):
ctype, cenc = guess_mime_type(f.name)
params['content_type'] = self['content_type'] or ctype
params['content_encoding'] = self['content_encoding'] or cenc
if self['unchunked']:
r = self.client.upload_object_unchunked(
rpath, f,
......
......@@ -75,6 +75,20 @@ def suggest_missing(miss=None, exclude=[]):
print('')
def guess_mime_type(
filename,
default_content_type='application/octet-stream',
default_encoding=None):
assert filename, 'Cannot guess mimetype for empty filename'
try:
from mimetypes import guess_type
ctype, cenc = guess_type(filename)
return ctype or default_content_type, cenc or default_encoding
except ImportError:
print 'WARNING: Cannot import mimetypes, using defaults'
return (default_content_type, default_encoding)
def remove_colors():
global bold
global red
......
......@@ -456,6 +456,7 @@ class PithosClient(PithosRestClient):
format='json',
hashmap=True,
content_type=content_type,
content_encoding=content_encoding,
if_etag_match=if_etag_match,
if_etag_not_match='*' if if_not_exist else None,
etag=etag,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment