Move more import code to the server

https://github.com/GNS3/gns3-gui/issues/1156
This commit is contained in:
Julien Duponchelle committed 2016-04-05 18:32:48 +02:00
1 parent f8ffd078a8
commit 9ed15e55af
4 files changed
+63 -9

No files matched your search

+7 -6
View File
@@ -21,7 +21,6 @@ import json
import os
import psutil
import tempfile
import zipfile
from ...web.route import Route
from ...schemas.project import PROJECT_OBJECT_SCHEMA, PROJECT_CREATE_SCHEMA, PROJECT_UPDATE_SCHEMA, PROJECT_FILE_LIST_SCHEMA, PROJECT_LIST_SCHEMA
@@ -58,6 +57,7 @@ class ProjectHandler:
description="Create a new project on the server",
status_codes={
201: "Project created",
403: "You are not allowed to modify this property",
409: "Project already created"
},
output=PROJECT_OBJECT_SCHEMA,
@@ -382,14 +382,16 @@ class ProjectHandler:
"project_id": "The UUID of the project",
},
raw=True,
output=PROJECT_OBJECT_SCHEMA,
status_codes={
200: "Return the file"
200: "Project imported",
403: "You are not allowed to modify this property"
})
def import_project(request, response):
pm = ProjectManager.instance()
project_id = request.match_info["project_id"]
project = pm.create_project(project_id=project_id)
project = pm.get_project(project_id)
# We write the content to a temporary location
# and after extract all. It could be more optimal to stream
@@ -403,10 +405,9 @@ class ProjectHandler:
if not packet:
break
temp.write(packet)
with zipfile.ZipFile(temp) as myzip:
myzip.extractall(project.path)
project.import_zip(temp)
except OSError as e:
raise aiohttp.web.HTTPInternalServerError(text="Could not import the project: {}".format(e))
response.json(project)
response.set_status(201)
+25 -1
View File
@@ -15,13 +15,14 @@
# 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 aiohttp
import os
import aiohttp
import shutil
import asyncio
import hashlib
import zipstream
import zipfile
import json
from uuid import UUID, uuid4
from .port_manager import PortManager
@@ -171,6 +172,8 @@ class Project:
@name.setter
def name(self, name):
if "/" in name or "\\" in name:
raise aiohttp.web.HTTPForbidden(text="Name can not contain path separator")
self._name = name
@property
@@ -540,3 +543,24 @@ class Project:
else:
z.write(path, os.path.relpath(path, self._path))
return z
def import_zip(self, stream):
"""
Import a project contain in a zip file
:params: A io.BytesIO of the zifile
"""
with zipfile.ZipFile(stream) as myzip:
myzip.extractall(self.path)
project_file = os.path.join(self.path, "project.gns3")
if os.path.exists(project_file):
with open(project_file) as f:
topology = json.load(f)
topology["project_id"] = self.id
topology["name"] = self.name
with open(project_file, "w") as f:
json.dump(topology, f, indent=4)
shutil.move(project_file, os.path.join(self.path, self.name + ".gns3"))