diff --git a/gns3server/controller/symbols.py b/gns3server/controller/symbols.py index 45ac4bfc..d1cf7c37 100644 --- a/gns3server/controller/symbols.py +++ b/gns3server/controller/symbols.py @@ -43,7 +43,7 @@ class Symbols: 'builtin': True, }) self._symbols_path[symbol_id] = os.path.join(get_resource("symbols"), file) - directory = self._symbol_path() + directory = self.symbols_path() if directory: for file in os.listdir(directory): if file.startswith('.'): @@ -56,12 +56,11 @@ class Symbols: }) self._symbols_path[symbol_id] = os.path.join(get_resource("symbols"), file) - symbols.sort(key=lambda x: x["filename"]) return symbols - def _symbol_path(self): + def symbols_path(self): directory = os.path.expanduser(Config.instance().get_section_config("Server").get("symbols_path", "~/GNS3/symbols")) if directory: os.makedirs(directory, exist_ok=True) diff --git a/gns3server/handlers/api/controller/symbol_handler.py b/gns3server/handlers/api/controller/symbol_handler.py index 7ede100f..c6237836 100644 --- a/gns3server/handlers/api/controller/symbol_handler.py +++ b/gns3server/handlers/api/controller/symbol_handler.py @@ -15,6 +15,7 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . +import os from gns3server.web.route import Route from gns3server.controller import Controller @@ -50,3 +51,22 @@ class SymbolHandler: yield from response.file(controller.symbols.get_path(request.match_info["symbol_id"])) except KeyError: response.set_status(404) + + @Route.post( + r"/symbols/{symbol_id:.+}/raw", + description="Write the symbol file", + status_codes={ + 200: "Symbol returned" + }, + raw=True) + def upload(request, response): + controller = Controller.instance() + path = os.path.join(controller.symbols.symbols_path(), os.path.basename(request.match_info["symbol_id"])) + with open(path, 'wb+') as f: + while True: + packet = yield from request.content.read(512) + if not packet: + break + f.write(packet) + response.set_status(204) + diff --git a/tests/controller/test_symbols.py b/tests/controller/test_symbols.py index dfea1e74..30a91faa 100644 --- a/tests/controller/test_symbols.py +++ b/tests/controller/test_symbols.py @@ -25,7 +25,6 @@ from gns3server.utils.get_resource import get_resource def test_list(symbols_dir): - print(symbols_dir) with open(os.path.join(symbols_dir, "linux.svg"), "w+") as f: pass @@ -45,6 +44,3 @@ def test_list(symbols_dir): def test_get_path(): symbols = Symbols() assert symbols.get_path(':/symbols/firewall.svg') == get_resource("symbols/firewall.svg") - - - diff --git a/tests/handlers/api/controller/test_symbol.py b/tests/handlers/api/controller/test_symbol.py index 04483887..8b2b0cec 100644 --- a/tests/handlers/api/controller/test_symbol.py +++ b/tests/handlers/api/controller/test_symbol.py @@ -15,6 +15,7 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . +import os import urllib.parse from gns3server.config import Config @@ -39,3 +40,13 @@ def test_get(http_controller): response = http_controller.get('/symbols/404.png/raw') assert response.status == 404 + + +def test_upload(http_controller, symbols_dir): + response = http_controller.post("/symbols/test2/raw", body="TEST", raw=True) + assert response.status == 204 + + with open(os.path.join(symbols_dir, "test2")) as f: + assert f.read() == "TEST" + +