add add hotlink endpoint
This commit is contained in:
parent
391da3fd27
commit
0cb925bdad
3 changed files with 24 additions and 5 deletions
|
@ -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 %}
|
||||
|
|
Reference in a new issue