- Added type hinting (for now, 3.5-compatible) - Split `db` namespace into `db` module and `model` namespace - Changed elastic search to be created lazily for each operation - Changed to class based approach in entity serialization to allow stronger typing - Removed `required` argument from `context.get_*` family of functions; now it's implied if `default` argument is omitted - Changed `unalias_dict` implementation to use less magic inputs
30 lines
803 B
Python
30 lines
803 B
Python
from typing import Optional
|
|
from datetime import datetime, timedelta
|
|
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
|