mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-26 09:56:17 -05:00
A new start
Signed-off-by: Roland <roland@learningselfreliance.com>
This commit is contained in:
commit
9b899f6fc0
135 changed files with 15764 additions and 0 deletions
42
modules/map/api.py
Normal file
42
modules/map/api.py
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
import asyncio
|
||||
from http import HTTPStatus
|
||||
from pathlib import Path
|
||||
from urllib.parse import urlparse
|
||||
|
||||
from sanic import Blueprint, response
|
||||
|
||||
from modules.map.schema import PBFPostRequest, PBFPostResponse
|
||||
from wrolpi.common import create_websocket_feed
|
||||
from wrolpi.schema import validate_doc
|
||||
|
||||
NAME = 'map'
|
||||
|
||||
api_bp = Blueprint('Map', url_prefix='/map')
|
||||
|
||||
download_queue, download_event = create_websocket_feed('pbf_progress', '/feeds/pbf_progress', api_bp)
|
||||
|
||||
|
||||
@api_bp.route('/pbf', methods=['POST'])
|
||||
@validate_doc(
|
||||
summary='Queue a PBF file for download and processing',
|
||||
consumes=PBFPostRequest,
|
||||
produces=PBFPostResponse,
|
||||
)
|
||||
def pbf_post(request, data: dict):
|
||||
pbf_url = data.get('pbf_url')
|
||||
parsed = urlparse(pbf_url)
|
||||
if not parsed.scheme or not parsed.netloc or not parsed.path:
|
||||
return response.json({'error': 'Invalid PBF url'}, HTTPStatus.BAD_REQUEST)
|
||||
|
||||
# Get the size of the PBF file. Also, check that it is accessible.
|
||||
try:
|
||||
size, filename = get_http_file_info(pbf_url)
|
||||
except LookupError:
|
||||
# Couldn't get the size of the file, probably doesn't exist
|
||||
return response.json({'error': 'Failed to get file size. Does it exists?', 'url': pbf_url},
|
||||
HTTPStatus.NOT_FOUND)
|
||||
|
||||
destination = Path('/tmp') / filename
|
||||
coro = download_file(pbf_url, size, destination)
|
||||
asyncio.ensure_future(coro)
|
||||
return response.json({'success': 'File download started'})
|
||||
Loading…
Add table
Add a link
Reference in a new issue