pyazo core: rewrite download views

parent 76c5456b
......@@ -75,17 +75,17 @@
</div>
<div class="nav-group-children nav-group-children-hidden">
<clr-vertical-nav-group-children>
<a class="nav-link" href="{% url 'download_client_windows' %}">
<a class="nav-link" href="{% url 'download-client' client='windows' %}">
<span class="nav-text">
{% trans "Windows" %}
</span>
</a>
<a class="nav-link" href="{% url 'download_client_macos' %}">
<a class="nav-link" href="{% url 'download-client' client='macos' %}">
<span class="nav-text">
{% trans 'macOS' %}
</span>
</a>
<a class="nav-link" href="{% url 'download-sxcu' %}">
<a class="nav-link" href="{% url 'download-client' client='sharex' %}">
<span class="nav-text">
{% trans "ShareX" %}
</span>
......
......@@ -21,7 +21,7 @@ class DownloadViewTests(TestCase):
with open(exe_path, 'w') as _file:
_file.write('test')
self.client.login(**test_auth())
response = self.client.get(reverse('download_client_windows'))
response = self.client.get(reverse('download-client', kwargs={'client': 'windows'}))
self.assertEqual(response['Content-Type'], 'application/octet-stream')
self.assertEqual(response.status_code, 200)
......@@ -30,13 +30,13 @@ class DownloadViewTests(TestCase):
exe_path = os.path.join(settings.BASE_DIR+"/", 'bin/', 'Pyazo.exe')
os.unlink(exe_path)
self.client.login(**test_auth())
response = self.client.get(reverse('download_client_windows'))
response = self.client.get(reverse('download-client', kwargs={'client': 'windows'}))
self.assertEqual(response.status_code, 404)
def test_sharex(self):
"""Test shareX"""
self.client.login(**test_auth())
response = self.client.get(reverse('download-sxcu'))
response = self.client.get(reverse('download-client', kwargs={'client': 'sharex'}))
self.assertEqual(response.status_code, 200)
def test_macos(self):
......@@ -45,7 +45,7 @@ class DownloadViewTests(TestCase):
with open(exe_path, 'w') as _file:
_file.write('test')
self.client.login(**test_auth())
response = self.client.get(reverse('download_client_macos'))
response = self.client.get(reverse('download-client', kwargs={'client': 'macos'}))
self.assertEqual(response['Content-Type'], 'application/x-apple-diskimage')
self.assertEqual(response.status_code, 200)
......@@ -54,5 +54,5 @@ class DownloadViewTests(TestCase):
exe_path = os.path.join(settings.BASE_DIR+"/", 'bin/', 'pyazo.dmg')
os.unlink(exe_path)
self.client.login(**test_auth())
response = self.client.get(reverse('download_client_macos'))
response = self.client.get(reverse('download-client', kwargs={'client': 'macos'}))
self.assertEqual(response.status_code, 404)
......@@ -6,7 +6,7 @@ from django.contrib.auth import views
from django.urls import include, path
from django.views.generic.base import RedirectView
from pyazo.core.views import core, download, upload, view
from pyazo.core.views import clients, core, upload, view
admin.site.index_title = 'Pyazo Admin'
admin.site.site_title = 'pyazo'
......@@ -18,9 +18,7 @@ urlpatterns = [
path('accounts/allauth/', include('allauth.urls')),
path('accounts/login/', views.LoginView.as_view(), name='accounts-login'),
path('accounts/logout/', views.LogoutView.as_view(), name='accounts-logout'),
path('download/win/', download.client_windows, name='download_client_windows'),
path('download/sharex/', download.sxcu, name='download-sxcu'),
path('download/macos/', download.client_macos, name='download_client_macos'),
path('download/<slug:client>/', clients.ClientDownloadView.as_view(), name='download-client'),
# Legacy upload URL
path('gyazo.php', upload.LegacyObjectView.as_view(), name='upload'),
path('upload/', upload.LegacyObjectView.as_view(), name='upload'),
......
"""pyazo download views"""
from logging import getLogger
import os.path
from urllib.parse import urlparse
from django.conf import settings
from django.contrib.auth.mixins import LoginRequiredMixin
from django.http import Http404, HttpRequest, HttpResponse, JsonResponse
from django.views.generic import View
LOGGER = getLogger(__name__)
class ClientDownloadView(LoginRequiredMixin, View):
"""View to download clients"""
def get(self, request: HttpRequest, client: str) -> HttpResponse:
"""Proxy request to fitting method"""
client_names = {
'windows': self.client_windows,
'sharex': self.client_share_x,
'macos': self.client_mac_os,
}
if client in client_names:
return client_names[client](request)
raise Http404
def client_windows(self, request: HttpRequest) -> HttpResponse:
"""Download windows client"""
client_path = os.path.join(settings.BASE_DIR+"/", 'bin/', 'Pyazo.exe')
host = urlparse(request.build_absolute_uri()).netloc
filename = "Pyazo_%s.exe" % host
if os.path.isfile(client_path):
with open(client_path, 'rb') as _file:
response = HttpResponse(
_file.read(), content_type="application/octet-stream")
response['Content-Disposition'] = 'inline; filename=%s' % filename
return response
else:
LOGGER.warning("File %s not found", client_path)
raise Http404
def client_share_x(self, request: HttpRequest) -> HttpResponse:
"""Download ShareX custom uploader"""
data = {
'Name': 'Pyazo %s',
'DestinationType': 'ImageUploader',
'RequestURL': '%s://%s/upload/',
'FileFormName': 'imagedata',
'Arguments': {
'id': '%rn',
'username': '%uln',
}
}
url = urlparse(request.build_absolute_uri())
scheme = 'https' if request.is_secure() else 'http'
data['RequestURL'] = data['RequestURL'] % (scheme, url.netloc)
data['Name'] = data['Name'] % url.netloc
response = JsonResponse(data)
response['Content-Disposition'] = 'attachment; filename=pyazo.sxcu'
return response
def client_mac_os(self, request: HttpRequest) -> HttpResponse:
"""Download Client (macOS)"""
client_path = os.path.join(settings.BASE_DIR+"/", 'bin/', 'pyazo.dmg')
if os.path.isfile(client_path):
with open(client_path, 'rb') as _file:
response = HttpResponse(
_file.read(), content_type="application/x-apple-diskimage")
response['Content-Disposition'] = 'inline; filename=pyazo.dmg'
return response
else:
LOGGER.warning("File %s not found", client_path)
raise Http404
"""pyazo download views"""
import copy
import logging
import os.path
from urllib.parse import urlparse
from django.conf import settings
from django.contrib.auth.decorators import login_required
from django.http import Http404, HttpRequest, HttpResponse, JsonResponse
LOGGER = logging.getLogger(__name__)
SXCU_BASE = {
'Name': 'Pyazo %s',
'DestinationType': 'ImageUploader',
'RequestURL': '%s://%s/upload/',
'FileFormName': 'imagedata',
'Arguments': {
'id': '%rn',
'username': '%uln',
}
}
@login_required
def client_windows(request: HttpRequest) -> HttpResponse:
"""Download Client (Windows)"""
client_path = os.path.join(settings.BASE_DIR+"/", 'bin/', 'Pyazo.exe')
host = urlparse(request.build_absolute_uri()).netloc
filename = "Pyazo_%s.exe" % host
if os.path.isfile(client_path):
with open(client_path, 'rb') as _file:
response = HttpResponse(
_file.read(), content_type="application/octet-stream")
response['Content-Disposition'] = 'inline; filename=%s' % filename
return response
raise Http404
@login_required
def sxcu(request: HttpRequest) -> HttpResponse:
"""Download ShareX Custom Uploader"""
url = urlparse(request.build_absolute_uri())
data = copy.deepcopy(SXCU_BASE)
scheme = 'https' if request.is_secure() else 'http'
data['RequestURL'] = data['RequestURL'] % (scheme, url.netloc)
data['Name'] = data['Name'] % url.netloc
response = JsonResponse(data)
response['Content-Disposition'] = 'attachment; filename=pyazo.sxcu'
return response
@login_required
def client_macos(request: HttpRequest) -> HttpResponse:
"""Download Client (macOS)"""
client_path = os.path.join(settings.BASE_DIR+"/", 'bin/', 'pyazo.dmg')
if os.path.isfile(client_path):
with open(client_path, 'rb') as _file:
response = HttpResponse(
_file.read(), content_type="application/x-apple-diskimage")
response['Content-Disposition'] = 'inline; filename=pyazo.dmg'
return response
raise Http404
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