aboutsummaryrefslogtreecommitdiff
path: root/server/szurubooru/func/file_uploads.py
blob: 800397df845c1de2a74094560a8fc235d6012c8e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from datetime import datetime, timedelta
from typing import Optional

from szurubooru.func import files, util

MAX_MINUTES = 60


def _get_path(checksum: str) -> str:
    return "temporary-uploads/%s.dat" % checksum


def purge_old_uploads() -> None:
    now = datetime.now()
    for file in files.scan("temporary-uploads"):
        file_time = datetime.fromtimestamp(file.stat().st_ctime)
        if now - file_time > timedelta(minutes=MAX_MINUTES):
            files.delete("temporary-uploads/%s" % file.name)


def get(checksum: str) -> Optional[bytes]:
    return files.get("temporary-uploads/%s.dat" % checksum)


def save(content: bytes) -> str:
    checksum = util.get_sha1(content)
    path = _get_path(checksum)
    if not files.has(path):
        files.save(path, content)
    return checksum

© 2015 - 2026 Jakob L. Kreuze