1
0
mirror of https://github.com/GNS3/gns3-server synced 2024-11-28 03:08:14 +00:00

API for uploading symbols

This commit is contained in:
Julien Duponchelle 2016-06-28 21:15:22 +02:00
parent bf154049d2
commit e77445e860
No known key found for this signature in database
GPG Key ID: CE8B29639E07F5E8
4 changed files with 33 additions and 7 deletions

View File

@ -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)

View File

@ -15,6 +15,7 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
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)

View File

@ -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")

View File

@ -15,6 +15,7 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
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"