gns3-server/gns3server/handlers/upload_handler.py

161 lines
6.0 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
#
2016-05-14 00:00:07 +00:00
# Copyright (C) 2016 GNS3 Technologies Inc.
#
# 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/>.
2015-02-23 16:21:39 +00:00
import os
import aiohttp
2015-02-23 16:21:39 +00:00
import stat
import io
import tarfile
import asyncio
2015-02-23 16:21:39 +00:00
2016-05-14 00:00:07 +00:00
from gns3server.config import Config
from gns3server.web.route import Route
from gns3server.utils.images import remove_checksum, md5sum
class UploadHandler:
@Route.get(
r"/upload",
2016-05-14 00:00:07 +00:00
description="List binary images",
api_version=None
)
def index(request, response):
2015-03-13 02:56:10 +00:00
uploaded_files = []
2015-02-23 16:32:55 +00:00
try:
for root, _, files in os.walk(UploadHandler.image_directory()):
for filename in files:
if not filename.startswith(".") and not filename.endswith(".md5sum"):
image_file = os.path.join(root, filename)
uploaded_files.append(image_file)
except OSError:
2015-02-23 16:32:55 +00:00
pass
2015-03-13 02:56:10 +00:00
iourc_path = os.path.join(os.path.expanduser("~/"), ".iourc")
if os.path.exists(iourc_path):
uploaded_files.append(iourc_path)
response.template("upload.html", files=uploaded_files)
2015-02-23 16:21:39 +00:00
@Route.post(
r"/upload",
2016-05-14 00:00:07 +00:00
description="Upload binary images",
2015-02-23 16:21:39 +00:00
api_version=None
)
def upload(request, response):
data = yield from request.post()
if not data["file"]:
response.redirect("/upload")
return
2015-02-23 16:21:39 +00:00
2015-07-21 14:14:03 +00:00
if data["type"] not in ["IOU", "IOURC", "QEMU", "IOS", "IMAGES", "PROJECTS"]:
raise aiohttp.web.HTTPForbidden(text="You are not authorized to upload this kind of image {}".format(data["type"]))
2015-02-23 16:21:39 +00:00
try:
2015-07-21 14:14:03 +00:00
if data["type"] == "IMAGES":
UploadHandler._restore_directory(data["file"], UploadHandler.image_directory())
elif data["type"] == "PROJECTS":
UploadHandler._restore_directory(data["file"], UploadHandler.project_directory())
else:
if data["type"] == "IOURC":
destination_dir = os.path.expanduser("~/")
destination_path = os.path.join(destination_dir, ".iourc")
else:
destination_dir = os.path.join(UploadHandler.image_directory(), data["type"])
destination_path = os.path.join(destination_dir, data["file"].filename)
os.makedirs(destination_dir, exist_ok=True)
remove_checksum(destination_path)
with open(destination_path, "wb+") as f:
while True:
chunk = data["file"].file.read(512)
if not chunk:
break
2015-07-21 14:14:03 +00:00
f.write(chunk)
md5sum(destination_path)
st = os.stat(destination_path)
os.chmod(destination_path, st.st_mode | stat.S_IXUSR)
2015-02-23 16:21:39 +00:00
except OSError as e:
response.html("Could not upload file: {}".format(e))
response.set_status(200)
2015-02-23 16:21:39 +00:00
return
response.redirect("/upload")
@Route.get(
2015-07-21 14:14:03 +00:00
r"/backup/images.tar",
2016-05-14 00:00:07 +00:00
description="Backup binary images",
api_version=None
)
def backup_images(request, response):
yield from UploadHandler._backup_directory(request, response, UploadHandler.image_directory())
@Route.get(
r"/backup/projects.tar",
description="Backup GNS3 projects",
api_version=None
)
def backup_projects(request, response):
yield from UploadHandler._backup_directory(request, response, UploadHandler.project_directory())
2015-07-21 14:14:03 +00:00
@staticmethod
def _restore_directory(file, directory):
"""
Extract from HTTP stream the content of a tar
"""
destination_path = os.path.join(directory, "archive.tar")
os.makedirs(directory, exist_ok=True)
with open(destination_path, "wb+") as f:
chunk = file.file.read()
f.write(chunk)
t = tarfile.open(destination_path)
t.extractall(directory)
t.close()
os.remove(destination_path)
@staticmethod
@asyncio.coroutine
def _backup_directory(request, response, directory):
2015-07-21 14:14:03 +00:00
"""
Return a tar archive from a directory
"""
response.content_type = 'application/x-gtar'
response.set_status(200)
response.enable_chunked_encoding()
# Very important: do not send a content length otherwise QT close the connection but curl can consume the Feed
response.content_length = None
response.start(request)
buffer = io.BytesIO()
with tarfile.open('arch.tar', 'w', fileobj=buffer) as tar:
for root, dirs, files in os.walk(directory):
for file in files:
path = os.path.join(root, file)
tar.add(os.path.join(root, file), arcname=os.path.relpath(path, directory))
response.write(buffer.getvalue())
yield from response.drain()
buffer.truncate(0)
buffer.seek(0)
yield from response.write_eof()
2015-02-23 16:21:39 +00:00
@staticmethod
def image_directory():
server_config = Config.instance().get_section_config("Server")
2015-02-24 00:08:34 +00:00
return os.path.expanduser(server_config.get("images_path", "~/GNS3/images"))
@staticmethod
def project_directory():
server_config = Config.instance().get_section_config("Server")
2015-07-21 18:19:29 +00:00
return os.path.expanduser(server_config.get("projects_path", "~/GNS3/projects"))