diff --git a/archivebox/core/urls.py b/archivebox/core/urls.py index 4784d0de..2a001f6b 100644 --- a/archivebox/core/urls.py +++ b/archivebox/core/urls.py @@ -1,7 +1,10 @@ from django.contrib import admin from django.urls import path, include +from django.views import static from django.conf import settings +from django.contrib.staticfiles import views +from django.views.generic.base import RedirectView from core.views import MainIndex, AddLinks, LinkDetails @@ -9,17 +12,19 @@ admin.site.site_header = 'ArchiveBox Admin' admin.site.index_title = 'Archive Administration' urlpatterns = [ - path('archive//', LinkDetails.as_view(), name='LinkDetails'), + path('index.html', RedirectView.as_view(url='/')), + path('index.json', static.serve, {'document_root': settings.OUTPUT_DIR, 'path': 'index.json'}), + path('robots.txt', static.serve, {'document_root': settings.OUTPUT_DIR, 'path': 'robots.txt'}), + path('favicon.ico', static.serve, {'document_root': settings.OUTPUT_DIR, 'path': 'favicon.ico'}), + + path('archive/', RedirectView.as_view(url='/')), + path('archive/', LinkDetails.as_view(), name='LinkAssets'), + path('add/', AddLinks.as_view(), name='AddLinks'), + + path('static/', views.serve), path('accounts/', include('django.contrib.auth.urls')), path('admin/', admin.site.urls), - path('add/', AddLinks.as_view(), name='AddLinks'), path('', MainIndex.as_view(), name='Home'), ] -if settings.SERVE_STATIC: - # serve staticfiles via runserver - from django.contrib.staticfiles import views - urlpatterns += [ - path('static/', views.serve), - ] diff --git a/archivebox/core/views.py b/archivebox/core/views.py index 7411ab9c..9edb9557 100644 --- a/archivebox/core/views.py +++ b/archivebox/core/views.py @@ -1,9 +1,15 @@ -from django.shortcuts import render +__package__ = 'archivebox.core' -from django.views import View +from django.shortcuts import render, redirect -from .index import load_main_index, load_main_index_meta -from .config import OUTPUT_DIR +from django.http import HttpResponse +from django.views import View, static + +from core.models import Page + +from ..index import load_main_index, load_main_index_meta +from ..config import OUTPUT_DIR, VERSION, FOOTER_INFO +from ..util import base_url class MainIndex(View): @@ -17,6 +23,8 @@ class MainIndex(View): 'updated': meta_info['updated'], 'num_links': meta_info['num_links'], 'links': all_links, + 'VERSION': VERSION, + 'FOOTER_INFO': FOOTER_INFO, } return render(template_name=self.template, request=request, context=context) @@ -41,7 +49,45 @@ class AddLinks(View): class LinkDetails(View): - template = 'link_details.html' + def get(self, request, path): + # missing trailing slash -> redirect to index + if '/' not in path: + return redirect(f'{path}/index.html') - def get(self, request): - return render(template_name=self.template, request=request, context={}) + try: + slug, archivefile = path.split('/', 1) + except (IndexError, ValueError): + slug, archivefile = path.split('/', 1)[0], 'index.html' + + all_pages = list(Page.objects.all()) + + # slug is a timestamp + by_ts = {page.timestamp: page for page in all_pages} + try: + return static.serve(request, archivefile, by_ts[slug].link_dir, show_indexes=True) + except KeyError: + pass + + # slug is a hash + by_hash = {page.url_hash: page for page in all_pages} + try: + timestamp = by_hash[slug].timestamp + return redirect(f'/archive/{timestamp}/{archivefile}') + except KeyError: + pass + + # slug is a URL + by_url = {page.base_url: page for page in all_pages} + try: + # TODO: add multiple snapshot support by showing index of all snapshots + # for given url instead of redirecting to timestamp index + timestamp = by_url[base_url(path)].timestamp + return redirect(f'/archive/{timestamp}/index.html') + except KeyError: + pass + + return HttpResponse( + 'No archived link matches the given timestamp or hash.', + content_type="text/plain", + status=404, + ) diff --git a/archivebox/themes/default/main_index.html b/archivebox/themes/default/main_index.html index 936511a1..4ad00be7 100644 --- a/archivebox/themes/default/main_index.html +++ b/archivebox/themes/default/main_index.html @@ -208,20 +208,24 @@ {% for link in links %} - {{link.bookmarked_date}} + {{link.bookmarked_date}} - - - {{link.title}} + {% if link.is_archived %} + + {% else %} + + {% endif %} + + {{link.title}} {{link.tags|default:''}} - 📄 + 📄 {{link.num_outputs}} - {{link.url}} + {{link.url}} {% endfor %} @@ -231,10 +235,10 @@
Archive created using ArchiveBox - version v$version   |   + version v{{VERSION}}   |   Download index as JSON

- $footer_info + {{FOOTER_INFO}}

diff --git a/archivebox/themes/legacy/link_details.html b/archivebox/themes/legacy/link_details.html index f9019926..1d3b201d 100644 --- a/archivebox/themes/legacy/link_details.html +++ b/archivebox/themes/legacy/link_details.html @@ -246,7 +246,7 @@
- Favicon + Favicon    $title