Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle Content-Type and Content-Disposition headers #90

Merged
merged 8 commits into from
May 14, 2021
3 changes: 3 additions & 0 deletions giftless/storage/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ def exists(self, prefix: str, oid: str) -> bool:
def get_size(self, prefix: str, oid: str) -> int:
pass

def get_mime_type(self, prefix: str, oid: str) -> str:
pass

def verify_object(self, prefix: str, oid: str, size: int):
"""Verify that an object exists
"""
Expand Down
30 changes: 28 additions & 2 deletions giftless/storage/azure.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from typing import Any, BinaryIO, Dict, Iterable, List, Optional
from urllib.parse import urlencode
from xml.sax.saxutils import escape as xml_escape
import mimetypes

from azure.core.exceptions import ResourceNotFoundError
from azure.storage.blob import BlobClient, BlobSasPermissions, BlobServiceClient, generate_blob_sas # type: ignore
Expand Down Expand Up @@ -63,10 +64,20 @@ def get_size(self, prefix: str, oid: str) -> int:
except ResourceNotFoundError:
raise ObjectNotFound("Object does not exist")

def get_mime_type(self, prefix: str, oid: str) -> Optional[str]:
try:
blob_client = self.blob_svc_client.get_blob_client(container=self.container_name,
blob=self._get_blob_path(prefix, oid))
props = blob_client.get_blob_properties()
return props.content_settings.get('content_type')
except ResourceNotFoundError:
raise ObjectNotFound("Object does not exist")


def get_upload_action(self, prefix: str, oid: str, size: int, expires_in: int,
extra: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
filename = extra.get('filename') if extra else None
return {
reply = {
"actions": {
"upload": {
"href": self._get_signed_url(prefix, oid, expires_in, filename, create=True),
Expand All @@ -78,9 +89,17 @@ def get_upload_action(self, prefix: str, oid: str, size: int, expires_in: int,
}
}

if filename:
mime_type = self._guess_mime_type_from_filename(filename)
if mime_type:
reply["actions"]["upload"]["header"]["x-ms-blob-content-type"] = mime_type

return reply

def get_download_action(self, prefix: str, oid: str, size: int, expires_in: int,
extra: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
filename = extra.get('filename') if extra else None

return {
"actions": {
"download": {
Expand All @@ -104,7 +123,6 @@ def get_multipart_actions(self, prefix: str, oid: str, size: int, part_size: int
_log.info("There are %d uncommitted blocks pre-uploaded; %d parts still need to be uploaded",
len(uncommitted), len(parts))
commit_body = self._create_commit_body(blocks)

reply: Dict[str, Any] = {
"actions": {
"commit": {
Expand All @@ -124,11 +142,19 @@ def get_multipart_actions(self, prefix: str, oid: str, size: int, part_size: int
}
}

if filename:
mime_type = self._guess_mime_type_from_filename(filename)
if mime_type:
reply["actions"]["commit"]["header"]["x-ms-blob-content-type"] = mime_type

if parts:
reply['actions']['parts'] = parts

return reply

def _guess_mime_type_from_filename(self, filename: str) -> Optional[str]:
return mimetypes.guess_type(filename)[0]

def _get_blob_path(self, prefix: str, oid: str) -> str:
"""Get the path to a blob in storage
"""
Expand Down
8 changes: 8 additions & 0 deletions giftless/storage/local_storage.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import mimetypes
import os
import shutil
from typing import Any, BinaryIO, Dict, Optional
Expand Down Expand Up @@ -42,6 +43,13 @@ def get_size(self, prefix: str, oid: str) -> int:
return os.path.getsize(self._get_path(prefix, oid))
raise exc.ObjectNotFound("Object was not found")

def get_mime_type(self, prefix: str, oid: str) -> str:
if self.exists(prefix, oid):
path = self._get_path(prefix, oid)
mime_type, encoding = mimetypes.guess_type(path)
return mime_type or "application/octet-stream"
raise exc.ObjectNotFound("Object was not found")

def get_multipart_actions(self, prefix: str, oid: str, size: int, part_size: int, expires_in: int,
extra: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
return super().get_multipart_actions(prefix, oid, size, part_size, expires_in, extra)
Expand Down
2 changes: 2 additions & 0 deletions giftless/transfer/basic_streaming.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ def get(self, organization, repo, oid):

if self.storage.exists(path, oid):
file = self.storage.get(path, oid)
mime_type = self.storage.get_mime_type(path, oid)
headers['Content-Type'] = mime_type
return Response(file, direct_passthrough=True, status=200, headers=headers)
else:
raise NotFound("The object was not found")
Expand Down