mirror of
https://github.com/GNS3/gns3-server
synced 2024-11-28 11:18:11 +00:00
API for uploading symbols
This commit is contained in:
parent
bf154049d2
commit
e77445e860
@ -43,7 +43,7 @@ class Symbols:
|
|||||||
'builtin': True,
|
'builtin': True,
|
||||||
})
|
})
|
||||||
self._symbols_path[symbol_id] = os.path.join(get_resource("symbols"), file)
|
self._symbols_path[symbol_id] = os.path.join(get_resource("symbols"), file)
|
||||||
directory = self._symbol_path()
|
directory = self.symbols_path()
|
||||||
if directory:
|
if directory:
|
||||||
for file in os.listdir(directory):
|
for file in os.listdir(directory):
|
||||||
if file.startswith('.'):
|
if file.startswith('.'):
|
||||||
@ -56,12 +56,11 @@ class Symbols:
|
|||||||
})
|
})
|
||||||
self._symbols_path[symbol_id] = os.path.join(get_resource("symbols"), file)
|
self._symbols_path[symbol_id] = os.path.join(get_resource("symbols"), file)
|
||||||
|
|
||||||
|
|
||||||
symbols.sort(key=lambda x: x["filename"])
|
symbols.sort(key=lambda x: x["filename"])
|
||||||
|
|
||||||
return symbols
|
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"))
|
directory = os.path.expanduser(Config.instance().get_section_config("Server").get("symbols_path", "~/GNS3/symbols"))
|
||||||
if directory:
|
if directory:
|
||||||
os.makedirs(directory, exist_ok=True)
|
os.makedirs(directory, exist_ok=True)
|
||||||
|
@ -15,6 +15,7 @@
|
|||||||
# You should have received a copy of the GNU General Public License
|
# You should have received a copy of the GNU General Public License
|
||||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
import os
|
||||||
from gns3server.web.route import Route
|
from gns3server.web.route import Route
|
||||||
from gns3server.controller import Controller
|
from gns3server.controller import Controller
|
||||||
|
|
||||||
@ -50,3 +51,22 @@ class SymbolHandler:
|
|||||||
yield from response.file(controller.symbols.get_path(request.match_info["symbol_id"]))
|
yield from response.file(controller.symbols.get_path(request.match_info["symbol_id"]))
|
||||||
except KeyError:
|
except KeyError:
|
||||||
response.set_status(404)
|
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)
|
||||||
|
|
||||||
|
@ -25,7 +25,6 @@ from gns3server.utils.get_resource import get_resource
|
|||||||
|
|
||||||
def test_list(symbols_dir):
|
def test_list(symbols_dir):
|
||||||
|
|
||||||
print(symbols_dir)
|
|
||||||
with open(os.path.join(symbols_dir, "linux.svg"), "w+") as f:
|
with open(os.path.join(symbols_dir, "linux.svg"), "w+") as f:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@ -45,6 +44,3 @@ def test_list(symbols_dir):
|
|||||||
def test_get_path():
|
def test_get_path():
|
||||||
symbols = Symbols()
|
symbols = Symbols()
|
||||||
assert symbols.get_path(':/symbols/firewall.svg') == get_resource("symbols/firewall.svg")
|
assert symbols.get_path(':/symbols/firewall.svg') == get_resource("symbols/firewall.svg")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -15,6 +15,7 @@
|
|||||||
# You should have received a copy of the GNU General Public License
|
# You should have received a copy of the GNU General Public License
|
||||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
import os
|
||||||
import urllib.parse
|
import urllib.parse
|
||||||
|
|
||||||
from gns3server.config import Config
|
from gns3server.config import Config
|
||||||
@ -39,3 +40,13 @@ def test_get(http_controller):
|
|||||||
|
|
||||||
response = http_controller.get('/symbols/404.png/raw')
|
response = http_controller.get('/symbols/404.png/raw')
|
||||||
assert response.status == 404
|
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"
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user