2019-05-01 03:13:21 +00:00
|
|
|
__package__ = 'archivebox.core'
|
2019-04-02 20:36:41 +00:00
|
|
|
|
2019-05-01 03:13:21 +00:00
|
|
|
from django.shortcuts import render, redirect
|
2019-04-17 09:42:21 +00:00
|
|
|
|
2019-05-01 03:13:21 +00:00
|
|
|
from django.http import HttpResponse
|
|
|
|
from django.views import View, static
|
2019-05-02 23:15:16 +00:00
|
|
|
from django.conf import settings
|
2019-05-01 03:13:21 +00:00
|
|
|
|
2019-05-01 03:44:51 +00:00
|
|
|
from core.models import Snapshot
|
2019-05-01 03:13:21 +00:00
|
|
|
|
2020-07-01 17:23:59 +00:00
|
|
|
from contextlib import redirect_stdout
|
|
|
|
from io import StringIO
|
|
|
|
|
2019-05-01 03:13:21 +00:00
|
|
|
from ..index import load_main_index, load_main_index_meta
|
2019-05-02 23:15:16 +00:00
|
|
|
from ..config import (
|
|
|
|
OUTPUT_DIR,
|
|
|
|
VERSION,
|
|
|
|
FOOTER_INFO,
|
|
|
|
PUBLIC_INDEX,
|
|
|
|
PUBLIC_SNAPSHOTS,
|
|
|
|
)
|
2020-07-01 17:23:59 +00:00
|
|
|
from ..util import base_url, ansi_to_html
|
2020-03-27 12:37:31 +00:00
|
|
|
from .. main import add
|
2019-04-22 23:08:01 +00:00
|
|
|
|
2020-07-02 20:54:25 +00:00
|
|
|
from .forms import AddLinkForm
|
|
|
|
|
2019-04-17 09:42:21 +00:00
|
|
|
|
|
|
|
class MainIndex(View):
|
|
|
|
template = 'main_index.html'
|
|
|
|
|
|
|
|
def get(self, request):
|
2019-05-02 23:15:16 +00:00
|
|
|
if not request.user.is_authenticated and not PUBLIC_INDEX:
|
|
|
|
return redirect(f'/admin/login/?next={request.path}')
|
|
|
|
|
2019-04-22 23:08:01 +00:00
|
|
|
all_links = load_main_index(out_dir=OUTPUT_DIR)
|
|
|
|
meta_info = load_main_index_meta(out_dir=OUTPUT_DIR)
|
|
|
|
|
|
|
|
context = {
|
|
|
|
'updated': meta_info['updated'],
|
|
|
|
'num_links': meta_info['num_links'],
|
|
|
|
'links': all_links,
|
2019-05-01 03:13:21 +00:00
|
|
|
'VERSION': VERSION,
|
|
|
|
'FOOTER_INFO': FOOTER_INFO,
|
2019-04-22 23:08:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return render(template_name=self.template, request=request, context=context)
|
|
|
|
|
|
|
|
|
|
|
|
class AddLinks(View):
|
|
|
|
template = 'add_links.html'
|
|
|
|
|
|
|
|
def get(self, request):
|
2019-05-02 23:15:16 +00:00
|
|
|
if not request.user.is_authenticated and not PUBLIC_INDEX:
|
|
|
|
return redirect(f'/admin/login/?next={request.path}')
|
|
|
|
|
2020-07-02 20:54:25 +00:00
|
|
|
context = {
|
|
|
|
"form": AddLinkForm()
|
|
|
|
}
|
2019-04-22 23:08:01 +00:00
|
|
|
|
|
|
|
return render(template_name=self.template, request=request, context=context)
|
|
|
|
|
|
|
|
def post(self, request):
|
2020-07-08 19:46:31 +00:00
|
|
|
if not request.user.is_authenticated and not PUBLIC_INDEX:
|
|
|
|
return redirect(f'/admin/login/?next={request.path}')
|
2020-07-02 20:54:25 +00:00
|
|
|
form = AddLinkForm(request.POST)
|
|
|
|
if form.is_valid():
|
|
|
|
url = form.cleaned_data["url"]
|
2020-07-01 17:29:56 +00:00
|
|
|
print(f'[+] Adding URL: {url}')
|
2020-07-08 13:17:47 +00:00
|
|
|
depth = 0 if form.cleaned_data["source"] == "url" else 1
|
2020-07-02 20:54:25 +00:00
|
|
|
input_kwargs = {
|
2020-07-08 13:17:47 +00:00
|
|
|
"url": url,
|
|
|
|
"depth": depth,
|
2020-07-02 20:54:25 +00:00
|
|
|
"update_all": False,
|
|
|
|
"out_dir": OUTPUT_DIR,
|
|
|
|
}
|
2020-07-01 17:29:56 +00:00
|
|
|
add_stdout = StringIO()
|
|
|
|
with redirect_stdout(add_stdout):
|
2020-07-02 20:54:25 +00:00
|
|
|
extracted_links = add(**input_kwargs)
|
2020-07-01 17:29:56 +00:00
|
|
|
print(add_stdout.getvalue())
|
|
|
|
|
|
|
|
context = {
|
2020-07-02 20:54:25 +00:00
|
|
|
"stdout": ansi_to_html(add_stdout.getvalue()),
|
|
|
|
"form": AddLinkForm()
|
2020-07-01 17:29:56 +00:00
|
|
|
}
|
|
|
|
else:
|
2020-07-02 20:54:25 +00:00
|
|
|
context = {"form": form}
|
2020-07-01 17:23:59 +00:00
|
|
|
|
|
|
|
return render(template_name=self.template, request=request, context=context)
|
2019-04-17 09:42:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
class LinkDetails(View):
|
2019-05-01 03:13:21 +00:00
|
|
|
def get(self, request, path):
|
|
|
|
# missing trailing slash -> redirect to index
|
|
|
|
if '/' not in path:
|
|
|
|
return redirect(f'{path}/index.html')
|
|
|
|
|
2019-05-02 23:15:16 +00:00
|
|
|
if not request.user.is_authenticated and not PUBLIC_SNAPSHOTS:
|
|
|
|
return redirect(f'/admin/login/?next={request.path}')
|
|
|
|
|
2019-05-01 03:13:21 +00:00
|
|
|
try:
|
|
|
|
slug, archivefile = path.split('/', 1)
|
|
|
|
except (IndexError, ValueError):
|
|
|
|
slug, archivefile = path.split('/', 1)[0], 'index.html'
|
|
|
|
|
2019-05-01 03:44:51 +00:00
|
|
|
all_pages = list(Snapshot.objects.all())
|
2019-05-01 03:13:21 +00:00
|
|
|
|
|
|
|
# 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,
|
|
|
|
)
|