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

Commit

Permalink
fix: improve detection of mimetype
Browse files Browse the repository at this point in the history
  • Loading branch information
sralloza committed Jun 16, 2020
1 parent a2da3f3 commit 3481f33
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
10 changes: 6 additions & 4 deletions backup_to_cloud/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from .settings import EntryType, get_settings
from .upload import backup
from .utils import list_files, log
from .utils import get_mimetype, list_files, log, ZIP_MIMETYPE


def _main():
Expand All @@ -22,7 +22,8 @@ def _main():
if not entry.zip:
files = list_files(entry.root_path, entry.filter)
for file in files:
backup(file, entry.folder)
mimetype = get_mimetype(file)
backup(file, mimetype, entry.folder)
continue

buffer = BytesIO()
Expand All @@ -47,10 +48,11 @@ def _main():
for file in files:
myzip.write(file, Path(file).relative_to(min_file))

backup(buffer, entry.folder, filename=entry.zipname)
backup(buffer, ZIP_MIMETYPE, entry.folder, filename=entry.zipname)

elif entry.type == EntryType.single_file:
backup(entry.root_path, entry.folder)
mimetype = get_mimetype(entry.root_path)
backup(entry.root_path,mimetype, entry.folder)


def main():
Expand Down
13 changes: 5 additions & 8 deletions backup_to_cloud/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
FileData = Union[Path, str, BytesIO]


def backup(file_data: FileData, folder=None, filename=None):
def backup(file_data: FileData, mimetype, folder=None, filename=None):
if isinstance(file_data, (str, Path)):
filepath = Path(file_data)
if not filepath.exists():
Expand All @@ -21,7 +21,6 @@ def backup(file_data: FileData, folder=None, filename=None):
del filepath

service = get_google_drive_services()
mimetype = mimetypes.guess_type(filename)[0]
file_metadata = {"name": filename, "mimeType": mimetype}
query = "name = %r" % (filename)

Expand All @@ -36,13 +35,12 @@ def backup(file_data: FileData, folder=None, filename=None):
raise RuntimeError("Files should not be more than one")

if ids:
return save_version(service, file_data, ids[0], filename)
return save_new_file(service, file_data, folder, filename)
return save_version(service, file_data, mimetype, ids[0], filename)
return save_new_file(service, file_data, mimetype, folder, filename)


def save_new_file(gds, file_data: BytesIO, folder=None, filename=None):
def save_new_file(gds, file_data: BytesIO, mimetype, folder=None, filename=None):
log("saving new file: %s", filename)
mimetype = get_mimetype(filename)
file_metadata = {"name": filename, "mimeType": mimetype}

if folder:
Expand All @@ -55,13 +53,12 @@ def save_new_file(gds, file_data: BytesIO, folder=None, filename=None):
return res


def save_version(gds, file_data: BytesIO, file_id: str, filename=None):
def save_version(gds, file_data: BytesIO, mimetype, file_id: str, filename=None):
log("saving new version of %s", filename)
file_metadata = {
"name": filename,
"published": True,
}
mimetype = get_mimetype(filename)
media = MediaIoBaseUpload(file_data, mimetype=mimetype)
response = (
gds.files()
Expand Down
18 changes: 16 additions & 2 deletions backup_to_cloud/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from .paths import CREDENTIALS_PATH, LOG_PATH, TOKEN_PATH

SCOPES = ["https://www.googleapis.com/auth/drive"]
ZIP_MIMETYPE = "application/octet-stream"


class TokenError(Exception):
Expand Down Expand Up @@ -63,8 +64,21 @@ def get_creds_from_token():
return creds


def get_mimetype(filename: str) -> str:
return mimetypes.guess_type(filename)[0] or "application/octet-stream"
def get_mimetype(filepath: str) -> str:
mimetype = mimetypes.guess_type(filepath)[0]
if mimetype:
return mimetype

data = Path(filepath).read_bytes()
try:
data.decode()
except UnicodeDecodeError:
try:
data.decode("utf-8")
except UnicodeDecodeError:
return "application/octet-stream"

return "text/plain"


def list_files(root_path, regex_filter):
Expand Down

0 comments on commit 3481f33

Please sign in to comment.