66 lines
1.6 KiB
Python
66 lines
1.6 KiB
Python
from typing import TypedDict
|
|
|
|
from flask import request
|
|
|
|
from src.lib.api import (
|
|
create_api_v2_response,
|
|
create_api_v2_not_found_error_response,
|
|
get_api_v2_request_data,
|
|
create_api_v2_invalid_body_error_response,
|
|
create_api_v2_client_error_response,
|
|
TDAPIError,
|
|
)
|
|
from src.lib.files import get_archive_files, try_set_password
|
|
|
|
|
|
def get_file_overview(file_hash: str):
|
|
archive = get_archive_files(file_hash)
|
|
|
|
if not archive:
|
|
return create_api_v2_not_found_error_response(600)
|
|
|
|
response = create_api_v2_response(archive)
|
|
|
|
response.headers["Cache-Control"] = "s-maxage=600"
|
|
|
|
return response
|
|
|
|
|
|
class TDFileUpdate(TypedDict):
|
|
password: str
|
|
|
|
|
|
def set_file_password(file_hash: str):
|
|
data: TDFileUpdate = get_api_v2_request_data(request)
|
|
|
|
if not isinstance(data, dict):
|
|
return create_api_v2_invalid_body_error_response()
|
|
|
|
password = data.get("password")
|
|
|
|
if not password:
|
|
error = TDAPIError(type="api_invalid_body_data", message="Password is required.")
|
|
|
|
return create_api_v2_client_error_response(error)
|
|
|
|
archive = get_archive_files(file_hash)
|
|
|
|
if not archive:
|
|
return create_api_v2_not_found_error_response()
|
|
|
|
if archive.password:
|
|
error = TDAPIError(type="api_invalid_body_data", message="Password is already set.")
|
|
|
|
return create_api_v2_client_error_response(error, 409)
|
|
|
|
is_set = try_set_password(file_hash, [password])
|
|
|
|
if not is_set:
|
|
error = TDAPIError(type="api_invalid_body_data", message="Invalid password.")
|
|
|
|
return create_api_v2_client_error_response(error)
|
|
|
|
response = create_api_v2_response(file_hash)
|
|
|
|
return response
|