mirror of
https://github.com/GNS3/gns3-server.git
synced 2026-11-03 22:11:14 +02:00
API for list & download files of a project
This commit is contained in:
1 parent
3c4a60cd0a
commit
6f9f004ebb
6 files changed
+205
-4
No files matched your search
@@ -15,13 +15,16 @@
|
||||
# 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 asyncio
|
||||
import json
|
||||
import os
|
||||
|
||||
from ...web.route import Route
|
||||
from ...schemas.project import PROJECT_OBJECT_SCHEMA, PROJECT_CREATE_SCHEMA, PROJECT_UPDATE_SCHEMA
|
||||
from ...schemas.project import PROJECT_OBJECT_SCHEMA, PROJECT_CREATE_SCHEMA, PROJECT_UPDATE_SCHEMA, PROJECT_FILE_LIST_SCHEMA
|
||||
from ...modules.project_manager import ProjectManager
|
||||
from ...modules import MODULES
|
||||
from ...utils.asyncio import wait_run_in_executor
|
||||
|
||||
import logging
|
||||
log = logging.getLogger()
|
||||
@@ -198,3 +201,71 @@ class ProjectHandler:
|
||||
response.write("{\"action\": \"ping\"}\n".encode("utf-8"))
|
||||
project.stop_listen_queue(queue)
|
||||
ProjectHandler._notifications_listening -= 1
|
||||
|
||||
@classmethod
|
||||
@Route.get(
|
||||
r"/projects/{project_id}/files",
|
||||
description="List files of a project",
|
||||
parameters={
|
||||
"project_id": "The UUID of the project",
|
||||
},
|
||||
status_codes={
|
||||
200: "Return list of files",
|
||||
404: "The project doesn't exist"
|
||||
},
|
||||
output=PROJECT_FILE_LIST_SCHEMA)
|
||||
def list_files(request, response):
|
||||
|
||||
pm = ProjectManager.instance()
|
||||
project = pm.get_project(request.match_info["project_id"])
|
||||
files = yield from project.list_files()
|
||||
response.json(files)
|
||||
response.set_status(200)
|
||||
|
||||
@classmethod
|
||||
@Route.get(
|
||||
r"/projects/{project_id}/files/{path:.+}",
|
||||
description="Get a file of a project",
|
||||
parameters={
|
||||
"project_id": "The UUID of the project",
|
||||
},
|
||||
status_codes={
|
||||
200: "Return the file",
|
||||
403: "Permission denied",
|
||||
404: "The file doesn't exist"
|
||||
})
|
||||
def get_file(request, response):
|
||||
|
||||
pm = ProjectManager.instance()
|
||||
project = pm.get_project(request.match_info["project_id"])
|
||||
path = request.match_info["path"]
|
||||
path = os.path.normpath(path)
|
||||
|
||||
# Raise error if user try to escape
|
||||
if path[0] == ".":
|
||||
raise aiohttp.web.HTTPForbidden
|
||||
path = os.path.join(project.path, path)
|
||||
|
||||
response.content_type = "application/octet-stream"
|
||||
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
|
||||
|
||||
try:
|
||||
yield from wait_run_in_executor(ProjectHandler._read_file, path, request, response)
|
||||
except FileNotFoundError:
|
||||
raise aiohttp.web.HTTPNotFound()
|
||||
except PermissionError:
|
||||
raise aiohttp.web.HTTPForbidden
|
||||
|
||||
@staticmethod
|
||||
def _read_file(path, request, response):
|
||||
|
||||
with open(path, "rb") as f:
|
||||
response.start(request)
|
||||
while True:
|
||||
data = f.read(4096)
|
||||
if not data:
|
||||
break
|
||||
response.write(data)
|
||||
@@ -19,6 +19,7 @@ import aiohttp
|
||||
import os
|
||||
import shutil
|
||||
import asyncio
|
||||
import hashlib
|
||||
|
||||
from uuid import UUID, uuid4
|
||||
from .port_manager import PortManager
|
||||
@@ -457,3 +458,42 @@ class Project:
|
||||
"""Stop sending notification to this clients"""
|
||||
|
||||
self._listeners.remove(queue)
|
||||
|
||||
@asyncio.coroutine
|
||||
def list_files(self):
|
||||
"""
|
||||
:returns: Array of files in project without temporary files. The files are dictionnary {"path": "test.bin", "md5sum": "aaaaa"}
|
||||
"""
|
||||
|
||||
files = []
|
||||
for (dirpath, dirnames, filenames) in os.walk(self.path):
|
||||
for filename in filenames:
|
||||
if not filename.endswith(".ghost"):
|
||||
path = os.path.relpath(dirpath, self.path)
|
||||
path = os.path.join(path, filename)
|
||||
path = os.path.normpath(path)
|
||||
file_info = {"path": path}
|
||||
|
||||
try:
|
||||
file_info["md5sum"] = yield from wait_run_in_executor(self._hash_file, os.path.join(dirpath, filename))
|
||||
except OSError:
|
||||
continue
|
||||
files.append(file_info)
|
||||
|
||||
return files
|
||||
|
||||
def _hash_file(self, path):
|
||||
"""
|
||||
Compute and md5 hash for file
|
||||
|
||||
:returns: hexadecimal md5
|
||||
"""
|
||||
|
||||
m = hashlib.md5()
|
||||
with open(path, "rb") as f:
|
||||
while True:
|
||||
buf = f.read(128)
|
||||
if not buf:
|
||||
break
|
||||
m.update(buf)
|
||||
return m.hexdigest()
|
||||
@@ -103,3 +103,26 @@ PROJECT_OBJECT_SCHEMA = {
|
||||
"additionalProperties": False,
|
||||
"required": ["location", "project_id", "temporary"]
|
||||
}
|
||||
|
||||
PROJECT_FILE_LIST_SCHEMA = {
|
||||
"$schema": "http://json-schema.org/draft-04/schema#",
|
||||
"description": "List files in the project",
|
||||
"type": "array",
|
||||
"items": [
|
||||
{
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"path": {
|
||||
"description": "File path",
|
||||
"type": ["string"]
|
||||
},
|
||||
"md5sum": {
|
||||
"description": "MD5 hash of the file",
|
||||
"type": ["string"]
|
||||
},
|
||||
|
||||
},
|
||||
}
|
||||
],
|
||||
"additionalProperties": False,
|
||||
}
|
||||
Reference in new issue
Block a user