archive
/
static-share
Archived
1
Fork 0
This repository has been archived on 2023-03-26. You can view files and clone it, but cannot push or open issues or pull requests.
static-share/project/files/views.py

49 lines
1.6 KiB
Python
Raw Normal View History

2016-09-06 08:49:02 +01:00
from django.views.generic import TemplateView
from django.shortcuts import get_object_or_404
2016-09-12 21:32:44 +01:00
from django.http import FileResponse as BaseFileResponse
2016-09-06 08:49:02 +01:00
from .models import SharedFile, FileToken
2016-09-06 14:06:30 +01:00
import mimetypes
2016-09-06 08:49:02 +01:00
from django.utils import timezone
2016-09-06 14:06:30 +01:00
from django.contrib.auth.decorators import login_required
mimetypes.init()
2016-09-05 08:36:06 +01:00
2016-09-06 08:49:02 +01:00
class SharedFileDetails(TemplateView):
template_name = "file.html"
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['file'] = get_object_or_404(SharedFile, short_id=kwargs['id'])
context['token'] = FileToken.objects.create(file=context['file'])
return context
2016-09-06 14:06:30 +01:00
def FileResponse(file):
2016-09-12 21:32:44 +01:00
response = BaseFileResponse(file.file)
2016-09-06 14:06:30 +01:00
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):
2016-09-06 08:49:02 +01:00
time_threshold = timezone.now() - FileToken.valid_time
FileToken.objects.filter(created__lt=time_threshold)
2016-09-06 14:06:30 +01:00
file = get_object_or_404(SharedFile, short_id=id, published=True)
2016-09-06 08:49:02 +01:00
token = get_object_or_404(FileToken, token=token, file=file)
token.delete() # delete after used
2016-09-06 14:06:30 +01:00
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)
2016-09-06 17:48:43 +01:00
@login_required
def uploaded_file(request, filename):
file = get_object_or_404(SharedFile, file=request.get_full_path()[1:]) # strip preceding slash
response = FileResponse(file)
return response