Commit 327eb18c authored by murban's avatar murban
Browse files

filesystem updates

parent f257473c
# -*- coding: utf-8 -*-
# imports
from vispa import models, vfs
from vispa import models, filesystem
from vispa.plugins.rpc import RpcProcess
import cherrypy
......@@ -115,12 +115,12 @@ class AbstractController(object):
# error case
raise Exception('TypeError: conversion with unknown type flag.')
def _get_vfs_instance(self, username, workspace_id=None):
def _get_fs_instance(self, username, workspace_id=None):
workspace_id = workspace_id or self.get('workspace_id') or cherrypy.config.get('rpc.default_workspace_id', 0)
key = (str(username), workspace_id)
if key in self._vfs_instances.keys():
instance = self._vfs_instances[key]
if key in self._fs_instances.keys():
instance = self._fs_instances[key]
if isinstance(instance, RpcProcess):
if instance.online():
return instance
......@@ -129,11 +129,11 @@ class AbstractController(object):
if workspace_id:
workspace = models.workspace.Workspace.get_by_id(cherrypy.request.db, workspace_id)
instance = cherrypy.engine.publish('rpc_get', 'vispa.vfs.VirtualFileSystem', False, None, workspace_id).pop()
instance = cherrypy.engine.publish('rpc_get', 'vispa.filesystem.FileSystem', False, None, workspace_id).pop()
instance.setup(str(workspace.basedir), str(username))
else:
instance = vfs.VirtualFileSystem()
instance.setup(cherrypy.vispaServer.var_dir, str(username))
instance = filesystem.FileSystem()
instance.setup(cherrypy.vispaServer.var_dir)
self._vfs_instances[key] = instance
self._fs_instances[key] = instance
return instance
......@@ -5,7 +5,7 @@ import vispa
from vispa.helpers import browser
from vispa.controller import AbstractController, StaticController
from vispa.controller.ajax import AjaxController
from vispa.controller.vfs import VFSController
from vispa.controller.filesystem import FSController
from vispa.controller.error import ErrorController
from vispa.controller.bus import BusController
from vispa.models.stats import AccessStats
......@@ -25,7 +25,7 @@ class PlatformController(AbstractController):
'''
AbstractController.__init__(self, platform)
self.ajax = AjaxController(platform)
self.vfs = VFSController(platform)
self.vfs = FSController(platform)
self.error = ErrorController()
self.static = StaticController('static')
self.extensions = StaticController('vispa/extensions')
......
# @package FileSystem
# @package FileSystem
# Imports
import os
......@@ -24,31 +25,20 @@ class FileSystem(object):
# allowed extensions
self.allowed_extensions = FileSystem.FILE_EXTENSIONS
def setup(self, basedir, username):
# the data path
self.data_path = os.path.join(basedir, "data")
# the main paths
self.user_path = os.path.join(self.data_path, "user")
self.public_path = os.path.join(self.data_path, "public")
self.shared_path = os.path.join(self.data_path, "shared")
self.examples_path = os.path.join(self.data_path, "examples")
self.exercises_path = os.path.join(self.data_path, "exercises")
paths = [self.data_path, self.user_path, self.public_path, self.shared_path, self.examples_path, self.exercises_path]
def setup(self, basedir):
if not os.path.isdir(basedir):
raise Exception("Basedir ("+str(basedir)+") does not exist!")
# the basedir
self.basedir = os.path.join(basedir, ".vispa")
# the job file path
self.job_file_path = os.path.join(self.basedir, ".jobs")
paths = [self.basedir, self.job_file_path]
for path in paths:
if not os.path.isdir(path):
os.makedirs(path, 0700)
if username is None or username == "":
return
upath = os.path.join(self.user_path, username)
if not os.path.isdir(upath):
os.makedirs(upath, 0700)
upath = os.path.join(self.public_path, username)
if not os.path.isdir(upath):
os.makedirs(upath, 0700)
def getMimeType(self, filepath):
def get_mime_type(self, filepath):
mime, encoding = guess_type(filepath)
if mime is not None:
return mime
......@@ -57,7 +47,7 @@ class FileSystem(object):
return FileSystem.ADDITIONAL_MIMES[ext]
return None
def checkFileExtension(self, path, extensions=[]):
def check_file_extension(self, path, extensions=[]):
if (len(extensions) == 0):
return True
for elem in extensions:
......@@ -66,7 +56,7 @@ class FileSystem(object):
return True
return False
def exists(self, username, path, pathtype=None, fileextension=None, permissionmode="r"):
def exists(self, path, pathtype=None, fileextension=None, permissionmode="r"):
if isinstance(permissionmode, str) and permissionmode.lower() not in ["r", "w"]:
return False
......@@ -102,7 +92,7 @@ class FileSystem(object):
return True
def getFileList(self, username, path, deep, ext_filter=[], reverse=False):
def get_file_list(self, path, deep, ext_filter=[], reverse=False):
dirlist = os.listdir(path)
returnlist = []
for elem in dirlist:
......@@ -118,7 +108,7 @@ class FileSystem(object):
continue
returnlist.append({'name': elem, 'type': 'folder', 'parent': path + '/', 'extension': '', 'mtime': mtime, 'size': size, "path": os.path.join(path, elem)})
if deep:
returnlist.extend(self.getFileList(username, fullPath, deep, ext_filter, reverse))
returnlist.extend(self.get_file_list(fullPath, deep, ext_filter, reverse))
else:
if elem.startswith("."):
continue
......@@ -141,14 +131,14 @@ class FileSystem(object):
return filelist
def cutSlashs(self, path):
def cut_slashs(self, path):
path = path[1:] if path.startswith("/") else path
if path == "":
return path
path = path[:-1] if path.endswith("/") else path
return path
def createFolder(self, path, name):
def create_folder(self, path, name):
# folder with the same name existent?
fullpath = os.path.join(path, name)
if os.path.isdir(fullpath):
......@@ -159,7 +149,7 @@ class FileSystem(object):
#raise Exception("You don't have the permission to create this folder!")
raise Exception(str(e))
def createFile(self, path, name):
def create_file(self, path, name):
# file with the same name existent?
fullpath = os.path.join(path, name)
if os.path.exists(fullpath):
......@@ -171,7 +161,7 @@ class FileSystem(object):
raise Exception(str(e))
def renameFolder(self, path, name):
def rename_folder(self, path, name):
# file or folder
if not os.path.isdir(path):
raise Exception("Renaming file with folder function!")
......@@ -184,7 +174,7 @@ class FileSystem(object):
os.renames(path, fullpath)
def renameFile(self, path, name):
def rename_file(self, path, name):
# file or folder
if os.path.isdir(path):
raise Exception("Renaming folder with file function!")
......@@ -196,8 +186,8 @@ class FileSystem(object):
os.renames(path, fullpath)
def remove(self, vpath):
if isinstance(vpath, list):
def remove(self, path):
if isinstance(path, list):
for p in path:
self.remove(p)
return True
......@@ -220,11 +210,11 @@ class FileSystem(object):
archive = ZipFile(fullpath, "w")
vpath = vpath[1:] if vpath.startswith("/") else vpath
path = path if path.startswith("/") else path
for p in paths:
if p is None or p == "":
continue
p = p[1:] if vp.startswith("/") else p
p = p if p.startswith(os.sep) else os.sep + path
if os.path.isdir(p):
for elem in os.listdir(p):
fullp = os.path.join(p, elem)
......@@ -239,71 +229,53 @@ class FileSystem(object):
archive.close()
def paste(self, vpath, vtarget, cut):
if isinstance(vtarget, list):
for vp in vtarget:
self.paste(username, vpath, vp, cut)
def paste(self, path, target, cut):
if isinstance(target, list):
for p in target:
self.paste(path, p, cut)
return True
# permission?
if not self.checkPermission(username, vpath, 'w'):
raise Exception("You don't have the permission in this folder!")
ppath = self.translateVirtualPath(username, vpath)
ptarget = self.translateVirtualPath(username, vtarget)
fulltarget = os.path.join(ppath, ptarget.split("/")[-1])
fulltarget = os.path.join(path, target.split("/")[-1])
if os.path.exists(fulltarget):
raise Exception("Name already in use!")
if os.path.isdir(ptarget):
shutil.copytree(ptarget, fulltarget)
if os.path.isdir(target):
shutil.copytree(target, fulltarget)
if cut:
shutil.rmtree(ptarget)
shutil.rmtree(target)
else:
shutil.copy2(ptarget, ppath)
shutil.copy2(target, path)
if cut:
os.remove(ptarget)
def saveFileContent(self, username, vPath, content, force=True):
#check write permissions
if not self.checkPermission(username, vPath, 'w'):
return False, "Permission denied!"
os.remove(target)
pPath = self.translateVirtualPath(username, vPath)
def save_file_content(self, path, content, force=True):
#check if file already exists
if os.path.exists(pPath) and not force:
return False, "The file '%s' already exists!" % vPath
if os.path.exists(path) and not force:
return False, "The file '%s' already exists!" % path
out = open(pPath, "w")
out = open(path, "w")
for line in content.data:
out.write(line)
out.close()
return True, "File saved!"
def getFileContent(self, username, vPath):
# permissions?
if not self.checkPermission(username, vPath, 'r'):
return False, "Permission denied!"
pPath = self.translateVirtualPath(username, vPath)
f = open(pPath, "r")
def get_file_content(self, path):
f = open(path, "r")
content = f.read()
f.close()
content = " " if content == "" else content
return xmlrpclib.Binary(content), "File opened!"
def isbrowserfile(self, path):
def is_browser_file(self, path):
extension = path.split(".")[-1]
return extension in VirtualFileSystem.BROWSER_EXTENSIONS
return extension in FileSystem.BROWSER_EXTENSIONS
def handleFileNameCollision(self, name, pPath):
def handle_file_name_collision(self, name, path):
# collision?
files = os.listdir(pPath)
files = os.listdir(path)
if name not in files:
return name
......@@ -339,33 +311,7 @@ class FileSystem(object):
newname = "%s_1%s" % (prename, "" if extension == "" else "." + extension)
# return
return self.handleFileNameCollision(newname, pPath)
"""
def handleDownload(self, username, vPath):
if not self.exists(username, vPath, pathtype="file"):
raise Exception("The file '%s' does not exist or<br />you don't have the permission to read this file."%vPath)
return None
pPath = self.translateVirtualPath(username, vPath)
payload = open(pPath, "r")
output = cStringIO.StringIO()
for line in payload:
output.write(line)
output.seek(0)
data = output.read()
output.close()
# get the content type depending on the file extension
ext = vPath.split(".")[-1]
mimetype = self.getMimeType(vPath)
if mimetype is None:
raise Exception("The file extension '%s' is not supported by this server"%ext)
return None
return data, mimetype, self.isbrowserfile(vPath)
"""
return self.handle_file_name_collision(newname, path)
##
# Creates a unique ID
......
......@@ -12,7 +12,7 @@ import vispa
import vispa.extensions
from vispa.extensions import AbstractExtension
from vispa.models.workspace import Workspace
from vispa.vfs import VirtualFileSystem
from vispa.filesystem import FileSystem
from vispa.helpers import browser
from vispa.models.user import User
from vispa.controller.platform import PlatformController
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment