Skip to content
This repository has been archived by the owner on Jul 7, 2022. It is now read-only.

Fall back to Python's mimetype module if libmagic is not available #19

Merged
merged 1 commit into from
Dec 7, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions pushbullet/filetype.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
def _magic_get_file_type(f, _):
file_type = magic.from_buffer(f.read(1024), mime=True)
f.seek(0)
return file_type


def _guess_file_type(_, filename):
return mimetypes.guess_type(filename)[0]


try:
import magic
except ImportError:
import mimetypes
get_file_type = _guess_file_type
else:
get_file_type = _magic_get_file_type
10 changes: 6 additions & 4 deletions pushbullet/pushbullet.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import json

import requests
import magic


from .device import Device
from .contact import Contact
from .filetype import get_file_type



class PushBullet(object):

Expand Down Expand Up @@ -143,9 +146,8 @@ def delete_push(self, iden):

def upload_file(self, f, file_name, file_type=None):
if not file_type:
file_type = magic.from_buffer(f.read(1024), mime=True)
f.seek(0)

file_type = get_file_type(f, file_name)

data = {"file_name": file_name, "file_type": file_type}

# Request url for file upload
Expand Down