2014-03-11 21:45:04 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# Copyright (C) 2014 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/>.
|
|
|
|
|
|
|
|
"""
|
|
|
|
Simple file upload & listing handler.
|
|
|
|
"""
|
|
|
|
|
2015-01-20 12:04:20 +00:00
|
|
|
# TODO: file upload with aiohttp
|
2015-01-14 00:05:26 +00:00
|
|
|
|
2014-03-11 21:45:04 +00:00
|
|
|
|
|
|
|
import os
|
2014-03-30 03:29:10 +00:00
|
|
|
import stat
|
2014-03-11 21:45:04 +00:00
|
|
|
import tornado.web
|
2014-09-04 04:12:34 +00:00
|
|
|
from .auth_handler import GNS3BaseHandler
|
2014-04-20 00:37:10 +00:00
|
|
|
from ..version import __version__
|
2014-03-11 21:45:04 +00:00
|
|
|
from ..config import Config
|
|
|
|
|
|
|
|
import logging
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2014-09-04 04:12:34 +00:00
|
|
|
class FileUploadHandler(GNS3BaseHandler):
|
2015-01-20 12:24:00 +00:00
|
|
|
|
2014-03-11 21:45:04 +00:00
|
|
|
"""
|
|
|
|
File upload handler.
|
|
|
|
|
|
|
|
:param application: Tornado Application instance
|
|
|
|
:param request: Tornado Request instance
|
|
|
|
"""
|
|
|
|
|
2014-05-28 12:26:20 +00:00
|
|
|
def __init__(self, application, request, **kwargs):
|
2014-03-11 21:45:04 +00:00
|
|
|
|
2014-05-28 12:26:20 +00:00
|
|
|
super().__init__(application, request, **kwargs)
|
2014-03-11 21:45:04 +00:00
|
|
|
config = Config.instance()
|
|
|
|
server_config = config.get_default_section()
|
2014-05-28 12:26:20 +00:00
|
|
|
self._upload_dir = os.path.expandvars(
|
2014-06-29 16:14:15 +00:00
|
|
|
os.path.expanduser(server_config.get("upload_directory", "~/GNS3/images")))
|
2014-04-20 00:37:10 +00:00
|
|
|
self._host = request.host
|
2014-04-12 22:46:02 +00:00
|
|
|
try:
|
|
|
|
os.makedirs(self._upload_dir)
|
|
|
|
log.info("upload directory '{}' created".format(self._upload_dir))
|
|
|
|
except FileExistsError:
|
|
|
|
pass
|
|
|
|
except OSError as e:
|
|
|
|
log.error("could not create the upload directory {}: {}".format(self._upload_dir, e))
|
2014-03-11 21:45:04 +00:00
|
|
|
|
2014-09-04 04:12:34 +00:00
|
|
|
@tornado.web.authenticated
|
2014-03-11 21:45:04 +00:00
|
|
|
def get(self):
|
|
|
|
"""
|
|
|
|
Invoked on GET request.
|
|
|
|
"""
|
|
|
|
|
|
|
|
items = []
|
|
|
|
path = self._upload_dir
|
|
|
|
for filename in os.listdir(path):
|
|
|
|
items.append(filename)
|
|
|
|
|
2014-04-20 00:37:10 +00:00
|
|
|
self.render("upload.html",
|
|
|
|
version=__version__,
|
|
|
|
host=self._host,
|
|
|
|
path=path,
|
|
|
|
items=items)
|
2014-03-11 21:45:04 +00:00
|
|
|
|
2014-09-04 04:12:34 +00:00
|
|
|
@tornado.web.authenticated
|
2014-03-11 21:45:04 +00:00
|
|
|
def post(self):
|
|
|
|
"""
|
|
|
|
Invoked on POST request.
|
|
|
|
"""
|
|
|
|
|
2014-04-16 18:43:59 +00:00
|
|
|
if "file" in self.request.files:
|
|
|
|
fileinfo = self.request.files["file"][0]
|
|
|
|
destination_path = os.path.join(self._upload_dir, fileinfo['filename'])
|
2014-05-18 00:39:37 +00:00
|
|
|
try:
|
|
|
|
with open(destination_path, 'wb') as f:
|
|
|
|
f.write(fileinfo['body'])
|
|
|
|
except OSError as e:
|
|
|
|
self.write("Could not upload {}: {}".format(fileinfo['filename'], e))
|
|
|
|
return
|
2014-04-16 18:43:59 +00:00
|
|
|
st = os.stat(destination_path)
|
|
|
|
os.chmod(destination_path, st.st_mode | stat.S_IXUSR)
|
2014-03-11 21:45:04 +00:00
|
|
|
self.redirect("/upload")
|