Skip to content

Commit

Permalink
* Create a presigned url when dowloading. This avoids the document be…
Browse files Browse the repository at this point in the history
…ing pulled onto the machine before sending it across the wire.
  • Loading branch information
bgroff committed Aug 5, 2017
1 parent c269cd4 commit f1b4564
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions django_kala/documents/models.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from django.conf import settings
from django.db import models
from django.http import HttpResponse
from django.shortcuts import redirect
from django.utils import timezone
from uuid import uuid4
from django_kala.managers import ActiveManager
from taggit.managers import TaggableManager

from .defs import get_icon_for_mime, get_alt_for_mime
import boto3 as boto3


class Document(models.Model):
Expand Down Expand Up @@ -120,11 +121,16 @@ def delete(self, using=None):
super(DocumentVersion, self).delete(using)

def http_response(self):
response = HttpResponse(self.file.read(), content_type=self.mime)
response['Content-Length'] = self.file.size
response['Content-Disposition'] = 'attachment; filename={0}'.format(self.name)
response['Content-Type'] = self.mime
return response
s3 = boto3.client('s3')
url = s3.generate_presigned_url(
ClientMethod='get_object',
Params={
'Bucket': settings.S3_STORAGE_BUCKET,
'Key': 'media/{0}'.format(self.file.name)
}
)
return redirect(url)


def get_icon(self):
return get_icon_for_mime(self.mime)
Expand Down

0 comments on commit f1b4564

Please sign in to comment.