Skip to content

Commit

Permalink
Initial implementation of #92. Pending tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
supakeen committed Aug 29, 2020
1 parent 8b289bd commit 8564105
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
51 changes: 51 additions & 0 deletions pinnwand/handler/website.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import logging
import binascii
import io
import zipfile

from typing import Any
from datetime import datetime
Expand Down Expand Up @@ -370,6 +372,55 @@ async def get(self, file_id: str) -> None: # type: ignore
self.write(binascii.hexlify(file.raw.encode("utf8")))


class PasteDownload(Base):
"""Download an entire paste."""
async def get(self, paste_id: str) -> None: #type: ignore
"""Get all files from the database and download them as a zipfile."""

with database.session() as session:
paste = (
session.query(database.Paste)
.filter(database.Paste.slug == paste_id)
.first()
)

if not paste:
raise tornado.web.HTTPError(404)

if paste.exp_date < datetime.now():
session.delete(paste)
session.commit()

log.warn(
"FileRaw.get: paste was expired, is your cronjob running?"
)

raise tornado.web.HTTPError(404)

data = io.BytesIO()

with zipfile.ZipFile(data, "x") as zf:
for file in paste.files:
if file.filename:
filename = (
f"{utility.filename_clean(file.filename)}-{file.slug}.txt"
)
else:
filename = f"{file.slug}.txt"

zf.writestr(
filename,
file.raw)

data.seek(0)

self.set_header("Content-Type", "application/zip")
self.set_header(
"Content-Disposition", f"attachment; filename={paste.slug}.zip"
)
self.write(data.read())


class FileDownload(Base):
"""Download a file."""

Expand Down
3 changes: 3 additions & 0 deletions pinnwand/http.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
import secrets
import zipfile

from typing import Any, List

Expand All @@ -23,6 +24,8 @@ def make_application() -> tornado.web.Application:
(r"/([A-Z2-7]+)(?:#.+)?/hex", handler.website.FileHex),
(r"/download/([A-Z2-7]+)(?:#.+)?", handler.website.FileDownload),
(r"/([A-Z2-7]+)(?:#.+)?/download", handler.website.FileDownload),
(r"/download-archive/([A-Z2-7]+)(?:#.+)?", handler.website.PasteDownload),
(r"/([A-Z2-7]+)(?:#.+)?/download-archive", handler.website.PasteDownload),
(r"/remove/([A-Z2-7]+)", handler.website.Remove),
]

Expand Down
3 changes: 2 additions & 1 deletion pinnwand/template/show.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
<a href="/remove/{{ paste.removal }}">Remove now</a>.
{% end %}

<a href="/repaste/{{ paste.slug }}">Repaste</a> this paste.
<a href="/repaste/{{ paste.slug }}">Repaste</a>, or
<a href="/download-archive/{{ paste.slug }}">download</a> this paste.

<button class="btn-link" id="toggle-word-wrap">Toggle word wrap</button>.

Expand Down

0 comments on commit 8564105

Please sign in to comment.