1
0
Fork 0
mirror of synced 2024-06-30 12:00:41 +12:00

make archivebox server work for urls, hashes, and timestamps

This commit is contained in:
Nick Sweeting 2019-04-30 23:13:21 -04:00
parent 95007d9137
commit d26f87efef
4 changed files with 79 additions and 24 deletions

View file

@ -1,7 +1,10 @@
from django.contrib import admin from django.contrib import admin
from django.urls import path, include from django.urls import path, include
from django.views import static
from django.conf import settings 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 from core.views import MainIndex, AddLinks, LinkDetails
@ -9,17 +12,19 @@ admin.site.site_header = 'ArchiveBox Admin'
admin.site.index_title = 'Archive Administration' admin.site.index_title = 'Archive Administration'
urlpatterns = [ urlpatterns = [
path('archive/<timestamp>/', 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/<path:path>', LinkDetails.as_view(), name='LinkAssets'),
path('add/', AddLinks.as_view(), name='AddLinks'),
path('static/<path>', views.serve),
path('accounts/', include('django.contrib.auth.urls')), path('accounts/', include('django.contrib.auth.urls')),
path('admin/', admin.site.urls), path('admin/', admin.site.urls),
path('add/', AddLinks.as_view(), name='AddLinks'),
path('', MainIndex.as_view(), name='Home'), path('', MainIndex.as_view(), name='Home'),
] ]
if settings.SERVE_STATIC:
# serve staticfiles via runserver
from django.contrib.staticfiles import views
urlpatterns += [
path('static/<path>', views.serve),
]

View file

@ -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 django.http import HttpResponse
from .config import OUTPUT_DIR 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): class MainIndex(View):
@ -17,6 +23,8 @@ class MainIndex(View):
'updated': meta_info['updated'], 'updated': meta_info['updated'],
'num_links': meta_info['num_links'], 'num_links': meta_info['num_links'],
'links': all_links, 'links': all_links,
'VERSION': VERSION,
'FOOTER_INFO': FOOTER_INFO,
} }
return render(template_name=self.template, request=request, context=context) return render(template_name=self.template, request=request, context=context)
@ -41,7 +49,45 @@ class AddLinks(View):
class LinkDetails(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): try:
return render(template_name=self.template, request=request, context={}) 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,
)

View file

@ -208,20 +208,24 @@
<tbody> <tbody>
{% for link in links %} {% for link in links %}
<tr> <tr>
<td title="$timestamp">{{link.bookmarked_date}}</td> <td title="{{link.timestamp}}">{{link.bookmarked_date}}</td>
<td class="title-col"> <td class="title-col">
<a href="$archive_path/index.html"><img src="$favicon_url" class="link-favicon" decoding="async"></a> {% if link.is_archived %}
<a href="$archive_path/$wget_url" title="{{link.title}}"> <a href="archive/{{link.timestamp}}/index.html"><img src="archive/{{link.timestamp}}/favicon.ico" class="link-favicon" decoding="async"></a>
<span data-title-for="{{link.url}}" data-archived="$is_archived">{{link.title}}</span> {% else %}
<a href="archive/{{link.timestamp}}/index.html"><img src="{% static 'spinner.gif' %}" class="link-favicon" decoding="async"></a>
{% endif %}
<a href="archive/{{link.timestamp}}/{{link.canonical_outputs.wget_path}}" title="{{link.title}}">
<span data-title-for="{{link.url}}" data-archived="{{link.is_archived}}">{{link.title}}</span>
<small style="float:right">{{link.tags|default:''}}</small> <small style="float:right">{{link.tags|default:''}}</small>
</a> </a>
</td> </td>
<td> <td>
<a href="$archive_path/index.html">📄 <a href="archive/{{link.timestamp}}/index.html">📄
<span data-number-for="{{link.url}}" title="Fetching any missing files...">{{link.num_outputs}} <img src="{% static 'spinner.gif' %}" class="files-spinner" decoding="async"/></span> <span data-number-for="{{link.url}}" title="Fetching any missing files...">{{link.num_outputs}} <img src="{% static 'spinner.gif' %}" class="files-spinner" decoding="async"/></span>
</a> </a>
</td> </td>
<td style="text-align:left"><a href="$url">{{link.url}}</a></td> <td style="text-align:left"><a href="{{link.url}}">{{link.url}}</a></td>
</tr> </tr>
{% endfor %} {% endfor %}
</tbody> </tbody>
@ -231,10 +235,10 @@
<center> <center>
<small> <small>
Archive created using <a href="https://github.com/pirate/ArchiveBox" title="Github">ArchiveBox</a> Archive created using <a href="https://github.com/pirate/ArchiveBox" title="Github">ArchiveBox</a>
version <a href="https://github.com/pirate/ArchiveBox/tree/v$version" title="Git commit">v$version</a> &nbsp; | &nbsp; version <a href="https://github.com/pirate/ArchiveBox/tree/v{{VERSION}}" title="Git commit">v{{VERSION}}</a> &nbsp; | &nbsp;
Download index as <a href="index.json" title="JSON summary of archived links.">JSON</a> Download index as <a href="index.json" title="JSON summary of archived links.">JSON</a>
<br/><br/> <br/><br/>
$footer_info {{FOOTER_INFO}}
</small> </small>
</center> </center>
<br/> <br/>

View file

@ -246,7 +246,7 @@
</a> </a>
</div> </div>
<div class="col-lg-8"> <div class="col-lg-8">
<img src="$link_dir/favicon.ico" alt="Favicon"> <img src="favicon.ico" alt="Favicon">
&nbsp;&nbsp; &nbsp;&nbsp;
$title $title
&nbsp;&nbsp; &nbsp;&nbsp;