Verified Commit 0cb925bd authored by Jake Howard's avatar Jake Howard
Browse files

add add hotlink endpoint

parent 391da3fd
......@@ -2,6 +2,7 @@ from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^(?P<token>.*)/(?P<id>.*)/$', views.file_download, name="file_download"),
url(r'^(?P<id>.*)/hot/$', views.hotlink_file_download, name="file_hot"),
url(r'^(?P<id>.*)/(?P<token>.*)/$', views.file_download, name="file_download"),
url(r'^(?P<id>.*)/$', views.SharedFileDetails.as_view(), name="file"),
]
......@@ -2,8 +2,13 @@ from django.views.generic import TemplateView
from django.shortcuts import get_object_or_404
from django.http import HttpResponse
from .models import SharedFile, FileToken
import datetime
import mimetypes
from django.utils import timezone
from django.contrib.auth.decorators import login_required
from django.core.urlresolvers import reverse
mimetypes.init()
class SharedFileDetails(TemplateView):
......@@ -16,10 +21,22 @@ class SharedFileDetails(TemplateView):
return context
def file_download(request, token, id):
def FileResponse(file):
response = HttpResponse(file.file)
response['Content-Type'] = mimetypes.guess_type(file.get_original_filename())[0]
response['Content-Disposition'] = 'attachment; filename="{0}"'.format(file.get_original_filename())
return response
def file_download(request, id, token):
time_threshold = timezone.now() - FileToken.valid_time
FileToken.objects.filter(created__lt=time_threshold)
file = get_object_or_404(SharedFile, short_id=id)
file = get_object_or_404(SharedFile, short_id=id, published=True)
token = get_object_or_404(FileToken, token=token, file=file)
token.delete() # delete after used
return HttpResponse(file.file.name)
return FileResponse(file)
def hotlink_file_download(request, id):
file = get_object_or_404(SharedFile, short_id=id, hotlink=True, published=True)
return FileResponse(file)
......@@ -5,4 +5,5 @@
{% block content %}
<h1>{{ file }}</h1>
<h1>{{ token.token }}</h1>
<h2>{% url 'files:file_download' file.short_id token.token %}</h2>
{% endblock %}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment