54 خطوط
1.9 KiB
Python
54 خطوط
1.9 KiB
Python
"""Private local/S3 storage. All object keys are application-generated."""
|
|
import os
|
|
import re
|
|
import secrets
|
|
from functools import lru_cache
|
|
from pathlib import Path
|
|
from .config import settings
|
|
|
|
KEY = re.compile(r'^[a-f0-9]{48}/(?:input\.SchDoc|attempt-[12]/(?:data\.json|drawing\.svg))$')
|
|
|
|
class Storage:
|
|
def __init__(self):
|
|
self.config = settings()
|
|
if self.config.s3_bucket:
|
|
import boto3
|
|
self.s3 = boto3.client('s3', endpoint_url=self.config.s3_endpoint, region_name=self.config.s3_region)
|
|
else: self.s3 = None
|
|
|
|
def path(self, key):
|
|
if not KEY.fullmatch(key): raise ValueError('Invalid storage key')
|
|
p = self.config.storage_dir / 'objects' / key
|
|
if not p.resolve().is_relative_to(self.config.storage_dir / 'objects'):
|
|
raise ValueError('Invalid storage path')
|
|
return p
|
|
|
|
def put(self, key, data, content_type='application/octet-stream'):
|
|
p = self.path(key) # validate on both backends
|
|
if self.s3:
|
|
self.s3.put_object(Bucket=self.config.s3_bucket, Key=key, Body=data, ContentType=content_type)
|
|
else:
|
|
p.parent.mkdir(parents=True, exist_ok=True, mode=0o700)
|
|
tmp = p.with_name(p.name + '.' + secrets.token_hex(8) + '.tmp')
|
|
try:
|
|
with open(tmp, 'xb') as f:
|
|
os.chmod(tmp, 0o600)
|
|
f.write(data)
|
|
os.replace(tmp, p)
|
|
finally: tmp.unlink(missing_ok=True)
|
|
|
|
def get(self, key):
|
|
p = self.path(key)
|
|
if self.s3:
|
|
with self.s3.get_object(Bucket=self.config.s3_bucket, Key=key)['Body'] as body:
|
|
return body.read()
|
|
return p.read_bytes()
|
|
|
|
def delete(self, key):
|
|
p = self.path(key)
|
|
if self.s3: self.s3.delete_object(Bucket=self.config.s3_bucket, Key=key)
|
|
else: p.unlink(missing_ok=True)
|
|
|
|
@lru_cache
|
|
def storage(): return Storage()
|