2020-10-02 06:37:50 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
2020-10-14 00:19:29 +00:00
|
|
|
# Copyright (C) 2020 GNS3 Technologies Inc.
|
2020-10-02 06:37:50 +00:00
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
"""
|
2020-11-19 04:51:03 +00:00
|
|
|
API routes for symbols.
|
2020-10-02 06:37:50 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
2021-08-10 12:23:21 +00:00
|
|
|
from fastapi import APIRouter, Request, Depends, Response, status
|
2020-10-02 06:37:50 +00:00
|
|
|
from fastapi.responses import FileResponse
|
2021-04-18 06:10:38 +00:00
|
|
|
from typing import List
|
2020-10-02 06:37:50 +00:00
|
|
|
|
|
|
|
from gns3server.controller import Controller
|
2020-10-31 05:32:21 +00:00
|
|
|
from gns3server import schemas
|
2020-10-02 06:37:50 +00:00
|
|
|
from gns3server.controller.controller_error import ControllerError, ControllerNotFoundError
|
|
|
|
|
2021-06-16 10:52:49 +00:00
|
|
|
from .dependencies.authentication import get_current_active_user
|
|
|
|
|
2020-10-02 06:37:50 +00:00
|
|
|
import logging
|
2021-04-13 09:16:50 +00:00
|
|
|
|
2020-10-02 06:37:50 +00:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
|
|
|
2020-10-19 04:30:41 +00:00
|
|
|
@router.get("")
|
2021-04-18 06:10:38 +00:00
|
|
|
def get_symbols() -> List[str]:
|
2020-10-02 06:37:50 +00:00
|
|
|
|
|
|
|
controller = Controller.instance()
|
|
|
|
return controller.symbols.list()
|
|
|
|
|
|
|
|
|
2021-04-13 09:16:50 +00:00
|
|
|
@router.get(
|
|
|
|
"/{symbol_id:path}/raw", responses={404: {"model": schemas.ErrorMessage, "description": "Could not find symbol"}}
|
|
|
|
)
|
2021-04-18 06:10:38 +00:00
|
|
|
async def get_symbol(symbol_id: str) -> FileResponse:
|
2020-10-14 00:19:29 +00:00
|
|
|
"""
|
|
|
|
Download a symbol file.
|
|
|
|
"""
|
2020-10-02 06:37:50 +00:00
|
|
|
|
|
|
|
controller = Controller.instance()
|
|
|
|
try:
|
|
|
|
symbol = controller.symbols.get_path(symbol_id)
|
|
|
|
return FileResponse(symbol)
|
|
|
|
except (KeyError, OSError) as e:
|
2021-06-16 10:52:49 +00:00
|
|
|
raise ControllerNotFoundError(f"Could not get symbol file: {e}")
|
2020-10-02 06:37:50 +00:00
|
|
|
|
|
|
|
|
2021-04-13 09:16:50 +00:00
|
|
|
@router.get(
|
|
|
|
"/{symbol_id:path}/dimensions",
|
|
|
|
responses={404: {"model": schemas.ErrorMessage, "description": "Could not find symbol"}},
|
|
|
|
)
|
2021-04-18 06:10:38 +00:00
|
|
|
async def get_symbol_dimensions(symbol_id: str) -> dict:
|
2021-04-10 03:31:23 +00:00
|
|
|
"""
|
|
|
|
Get a symbol dimensions.
|
|
|
|
"""
|
|
|
|
|
|
|
|
controller = Controller.instance()
|
|
|
|
try:
|
|
|
|
width, height, _ = controller.symbols.get_size(symbol_id)
|
2021-04-13 09:16:50 +00:00
|
|
|
symbol_dimensions = {"width": width, "height": height}
|
2021-04-10 03:31:23 +00:00
|
|
|
return symbol_dimensions
|
|
|
|
except (KeyError, OSError, ValueError) as e:
|
2021-06-16 10:52:49 +00:00
|
|
|
raise ControllerNotFoundError(f"Could not get symbol file: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
@router.get("/default_symbols")
|
|
|
|
def get_default_symbols() -> dict:
|
|
|
|
"""
|
|
|
|
Return all default symbols.
|
|
|
|
"""
|
|
|
|
|
|
|
|
controller = Controller.instance()
|
|
|
|
return controller.symbols.default_symbols()
|
2021-04-10 03:31:23 +00:00
|
|
|
|
|
|
|
|
2021-06-16 10:52:49 +00:00
|
|
|
@router.post(
|
|
|
|
"/{symbol_id:path}/raw",
|
|
|
|
dependencies=[Depends(get_current_active_user)],
|
|
|
|
status_code=status.HTTP_204_NO_CONTENT
|
|
|
|
)
|
2021-08-10 12:23:21 +00:00
|
|
|
async def upload_symbol(symbol_id: str, request: Request) -> Response:
|
2020-10-02 06:37:50 +00:00
|
|
|
"""
|
|
|
|
Upload a symbol file.
|
|
|
|
"""
|
|
|
|
|
|
|
|
controller = Controller.instance()
|
|
|
|
path = os.path.join(controller.symbols.symbols_path(), os.path.basename(symbol_id))
|
|
|
|
|
|
|
|
try:
|
|
|
|
with open(path, "wb") as f:
|
|
|
|
f.write(await request.body())
|
|
|
|
except (UnicodeEncodeError, OSError) as e:
|
2021-04-13 09:07:58 +00:00
|
|
|
raise ControllerError(f"Could not write symbol file '{path}': {e}")
|
2020-10-02 06:37:50 +00:00
|
|
|
|
|
|
|
# Reset the symbol list
|
|
|
|
controller.symbols.list()
|
2021-08-10 12:23:21 +00:00
|
|
|
|
|
|
|
return Response(status_code=status.HTTP_204_NO_CONTENT)
|