2015-01-24 22:32:58 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# Copyright (C) 2015 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
|
2015-03-10 03:46:23 +00:00
|
|
|
import aiohttp
|
2015-02-23 16:21:39 +00:00
|
|
|
import stat
|
|
|
|
|
|
|
|
from ..config import Config
|
2015-02-23 10:27:07 +00:00
|
|
|
from ..web.route import Route
|
2015-01-24 22:32:58 +00:00
|
|
|
|
|
|
|
|
2015-02-23 10:27:07 +00:00
|
|
|
class UploadHandler:
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
@Route.get(
|
|
|
|
r"/upload",
|
|
|
|
description="Manage upload of GNS3 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:
|
2015-02-23 22:49:05 +00:00
|
|
|
for root, _, files in os.walk(UploadHandler.image_directory()):
|
|
|
|
for filename in files:
|
2015-02-23 22:56:10 +00:00
|
|
|
image_file = os.path.join(root, filename)
|
|
|
|
if os.access(image_file, os.X_OK):
|
2015-03-13 02:56:10 +00:00
|
|
|
uploaded_files.append(image_file)
|
2015-02-23 22:49:05 +00:00
|
|
|
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
|
|
|
|
|
|
|
@classmethod
|
|
|
|
@Route.post(
|
|
|
|
r"/upload",
|
|
|
|
description="Manage upload of GNS3 images",
|
|
|
|
api_version=None
|
|
|
|
)
|
|
|
|
def upload(request, response):
|
|
|
|
data = yield from request.post()
|
|
|
|
|
2015-02-23 22:49:05 +00:00
|
|
|
if not data["file"]:
|
|
|
|
response.redirect("/upload")
|
|
|
|
return
|
2015-02-23 16:21:39 +00:00
|
|
|
|
2015-03-07 13:38:38 +00:00
|
|
|
if data["type"] not in ["IOU", "IOURC", "QEMU", "IOS"]:
|
2015-03-10 03:46:23 +00:00
|
|
|
raise aiohttp.web.HTTPForbidden("You are not authorized to upload this kind of image {}".format(data["type"]))
|
2015-03-07 12:52:40 +00:00
|
|
|
|
2015-03-07 13:38:38 +00:00
|
|
|
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)
|
2015-02-23 16:21:39 +00:00
|
|
|
try:
|
2015-03-07 12:52:40 +00:00
|
|
|
os.makedirs(destination_dir, exist_ok=True)
|
2015-02-23 16:21:39 +00:00
|
|
|
with open(destination_path, "wb+") as f:
|
2015-02-23 16:28:17 +00:00
|
|
|
chunk = data["file"].file.read()
|
|
|
|
f.write(chunk)
|
2015-02-23 16:21:39 +00:00
|
|
|
st = os.stat(destination_path)
|
|
|
|
os.chmod(destination_path, st.st_mode | stat.S_IXUSR)
|
|
|
|
except OSError as e:
|
|
|
|
response.html("Could not upload file: {}".format(e))
|
|
|
|
response.set_status(500)
|
|
|
|
return
|
|
|
|
response.redirect("/upload")
|
2015-01-24 22:32:58 +00:00
|
|
|
|
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"))
|