Skip to content

Commit

Permalink
Refactored mimetype handling.
Browse files Browse the repository at this point in the history
- We now allow no mimetype to be sent in mimetype creations.
- We now default to try using the file mimetype for downloads or, if there is none, octet-stream.
- We now alo try to guess a mimetype based on the extension if we're otherwise not going to send a mimetype in creations (was there before but unimplemented).

Fixes #160
Fixes #148
  • Loading branch information
dsoprea committed Jun 30, 2016
1 parent 6c65e42 commit 29c2de2
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 35 deletions.
3 changes: 0 additions & 3 deletions gdrivefs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ class Conf(object):
file_jobthread_max_idle_time = 60
file_chunk_size_kb = 1024
file_download_temp_max_age_s = 86400
file_default_mime_type = 'application/octet-stream'
change_check_frequency_s = 3
hidden_flags_list_local = [u'trashed', u'restricted']
hidden_flags_list_remote = [u'trashed']
Expand All @@ -38,7 +37,6 @@ class Conf(object):

google_discovery_service_url = DISCOVERY_URI
default_buffer_read_blocksize = 65536
default_mimetype = 'application/octet-stream'
directory_mimetype = u'application/vnd.google-apps.folder'
default_perm_folder = '777'
default_perm_file_editable = '666'
Expand All @@ -58,4 +56,3 @@ def set(key, value):
raise KeyError(key)

setattr(Conf, key, value)

17 changes: 7 additions & 10 deletions gdrivefs/gdfs/gdfuse.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
LoggingMixIn
from time import mktime, time
from sys import argv, exit, excepthook
from mimetypes import guess_type
from datetime import datetime
from os.path import split

Expand Down Expand Up @@ -299,16 +298,14 @@ def __create(self, filepath, mode=None):
filepath)
raise FuseOSError(EIO)

distilled_filepath = build_filepath(path, filename)

# Try to guess at a mime-type, if not otherwise given.
if mime_type is None:
(mimetype_guess, _) = guess_type(filename, True)

if mimetype_guess is not None:
mime_type = mimetype_guess
else:
mime_type = Conf.get('default_mimetype')
_, ext = os.path.splitext(filename)
if ext != '':
ext = ext[1:]

mime_type = utility.get_first_mime_type_by_extension(ext)

distilled_filepath = build_filepath(path, filename)

gd = get_gdrive()

Expand Down
36 changes: 23 additions & 13 deletions gdrivefs/gdtool/drive.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import apiclient.http
import apiclient.errors

import gdrivefs.constants
import gdrivefs.config
import gdrivefs.conf
import gdrivefs.gdtool.chunked_download
Expand Down Expand Up @@ -395,8 +396,8 @@ def list_files(self, query_contains_string=None, query_is_string=None,
return entries

@_marshall
def download_to_local(self, output_file_path, normalized_entry, mime_type,
allow_cache=True):
def download_to_local(self, output_file_path, normalized_entry,
mime_type=None, allow_cache=True):
"""Download the given file. If we've cached a previous download and the
mtime hasn't changed, re-use. The third item returned reflects whether
the data has changed since any prior attempts.
Expand All @@ -405,6 +406,22 @@ def download_to_local(self, output_file_path, normalized_entry, mime_type,
_logger.info("Downloading entry with ID [%s] and mime-type [%s] to "
"[%s].", normalized_entry.id, mime_type, output_file_path)

if mime_type is None:
if normalized_entry.mime_type in normalized_entry.download_links:
mime_type = normalized_entry.mime_type

_logger.debug("Electing file mime-type for download: [%s]",
normalized_entry.mime_type)
elif gdrivefs.constants.OCTET_STREAM_MIMETYPE \
in normalized_entry.download_links:
mime_type = gdrivefs.constants.OCTET_STREAM_MIMETYPE

_logger.debug("Electing octet-stream for download.")
else:
raise ValueError("Could not determine what to fallback to for "
"the mimetype: {}".format(
normalized_entry.mime_type))

if mime_type != normalized_entry.mime_type and \
mime_type not in normalized_entry.download_links:
message = ("Entry with ID [%s] can not be exported to type [%s]. "
Expand Down Expand Up @@ -508,13 +525,6 @@ def create_file(self, filename, parents, mime_type, data_filepath=None,
# without having one. We don't want to impose this when acting like a
# normal FS.

# If no data and no mime-type was given, default it.
if mime_type == None:
mime_type = gdrivefs.conf.Conf.get('file_default_mime_type')
_logger.debug("No mime-type was presented for file "
"create/update. Defaulting to [%s].",
mime_type)

return self.__insert_entry(
True,
filename,
Expand Down Expand Up @@ -646,10 +656,10 @@ def update_entry(self, normalized_entry, filename=None, data_filepath=None,

body = {}

if mime_type is not None:
body['mimeType'] = mime_type
else:
body['mimeType'] = normalized_entry.mime_type
if mime_type is None:
mime_type = normalized_entry.mime_type

body['mimeType'] = mime_type

if filename is not None:
body['title'] = filename
Expand Down
4 changes: 0 additions & 4 deletions gdrivefs/gdtool/normal_entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@


class NormalEntry(object):
__default_general_mime_type = Conf.get('default_mimetype')
__directory_mimetype = Conf.get('directory_mimetype')

__properties_extra = [
Expand Down Expand Up @@ -167,9 +166,6 @@ def normalize_download_mimetype(self, specific_mimetype=None):
mimetype_candidate in self.download_links:
mime_type = mimetype_candidate

elif NormalEntry.__default_general_mime_type in self.download_links:
mime_type = NormalEntry.__default_general_mime_type

# If there's only one download link, resort to using it (perhaps it was
# an uploaded file, assigned only one type).
elif len(self.download_links) == 1:
Expand Down
11 changes: 6 additions & 5 deletions gdrivefs/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,12 @@ def __load_mappings(self):

def get_first_mime_type_by_extension(self, extension):

found = [ mime_type
for mime_type, temp_extension
in self.default_extensions.iteritems()
if temp_extension == extension ]
found = [
mime_type
for mime_type, temp_extension
in self.default_extensions.iteritems()
if temp_extension == extension
]

if not found:
return None
Expand Down Expand Up @@ -110,4 +112,3 @@ def make_safe_for_filename(self, text):
return re.sub('[^a-z0-9\-_\.]+', '', text)

utility = _DriveUtility()

0 comments on commit 29c2de2

Please sign in to comment.