def __init__(self, name, path): self.name = name self.path = path if path: self.size = format_filesize(get_filesize(path)) else: self.size = 0
def getOverviewData(req): conn = getConnection() num = {} num['nodes'] = str(conn.runQuery("select count(*) from node")[0][0]) num['metadata'] = str(conn.runQuery("select count(*) from nodeattribute")[0][0]) num['files'] = str(conn.runQuery("select count(*) from nodefile where type='document' or type='image'")[0][0]) num['size'] = format_filesize(getTotalSize(conn)) return num
def getOverviewData(req): conn = getConnection() num = {} num['nodes'] = str(conn.runQuery("select count(*) from node")[0][0]) num['metadata'] = str( conn.runQuery("select count(*) from nodeattribute")[0][0]) num['files'] = str( conn.runQuery( "select count(*) from nodefile where type='document' or type='image'" )[0][0]) num['size'] = format_filesize(getTotalSize(conn)) return num
def view(req): gotopage = req.params.get("gotopage", "") if gotopage == "searchconfig": searchconfig_action(req) page = req.params.get("page", "") gotopage = req.params.get("gotopage", "") v = {} v["gotopage"] = req.params.get("gotopage", "") v["subitem"] = req.params.get("editsubitem", "") if page == "python": # python information v['copyright'] = sys.copyright v['platform'] = sys.platform v['version'] = sys.version v['platform'] = sys.platform if sys.platform.startswith("win"): v['plat_version'] = sys.getwindowsversion() else: v['plat_version'] = '' return req.getTAL("web/admin/modules/settings.html", v, macro="view_python") elif page == "mediatum": # mediatum information fi = open(os.path.join(config.basedir, 'mediatum.cfg'), "rb") v['mediatum_cfg'] = fi.readlines() v["mediatum_version"] = mediatum_version return req.getTAL("web/admin/modules/settings.html", v, macro="view_mediatum") elif page == "database": if config.get("database.type") == "sqlite": # sqlite v['db_driver'] = 'PySQLite' v['db_connector_version'] = 'n.a.' else: # mysql import MySQLdb v['db_driver'] = 'MySQLdb' v['db_connector_version'] = ('%i.%i.%i %s %i' % MySQLdb.version_info) from core.tree import db v['db_status'] = db.getStatus() v['db_size'] = format_filesize(db.getDBSize()) return req.getTAL("web/admin/modules/settings.html", v, macro="view_database") elif page == "search": # search if config.get("config.searcher") == "fts3": v['search_driver'] = 'sqlite with fts3 support' else: v['search_driver'] = 'magpy' from core.tree import searcher v['search_info'] = searcher.getSearchInfo() v['search_size'] = format_filesize(searcher.getSearchSize()) return req.getTAL("web/admin/modules/settings.html", v, macro="view_search") elif page == "searchconfig": node = tree.getRoot() file = None sections = ["chars", "words"] data = {"chars": [], "words": []} for f in node.getFiles(): if f.retrieveFile().endswith("searchconfig.txt"): file = f break if file and os.path.exists(file.retrieveFile()): section = "" for line in open(file.retrieveFile(), "r"): line = line[:-1] if line.startswith("[") and line.endswith("]"): section = line[1:-1] continue if section in sections: data[section].append(line.split("=")) v["data"] = data return req.getTAL("web/admin/modules/settings.html", v, macro="view_searchconfig") elif page == "archive": try: v['a_managers'] = core.archivemanager.getManager() except: req.setStatus(httpstatus.HTTP_INTERNAL_SERVER_ERROR) return req.getTAL("web/admin/modules/settings.html", v, macro="view_error") v['archive_interval'] = config.get('archive.interval') v['archive_activated'] = config.get('archive.activate') return req.getTAL("web/admin/modules/settings.html", v, macro="view_archive") else: return req.getTAL("web/admin/modules/settings.html", v, macro="view")
def filebrowser(node, req): filesize = 0 ret = list() paths = [] for f in node.getFiles(): if f.getType() == "attachment": paths.append(f._path) # break if len(paths) == 1 and os.path.isdir(config.get("paths.datadir") + paths[0]): # single file with no path path = paths[0] elif len(paths) > 0: # some single files files = [] for path in paths: file = {} if not os.path.isdir(config.get("paths.datadir") + path): # file file["mimetype"], file["type"] = getMimeType(config.get("paths.datadir") + path) file["icon"] = fileicons[file["mimetype"]] file["path"] = path file["name"] = f.getName() if os.path.exists(config.get("paths.datadir") + path): size = os.path.getsize(config.get("paths.datadir") + path) else: size = 0 file["size"] = format_filesize(size) filesize += int(size) files.append(file) return files, filesize else: path = "" if path == "": # no attachment directory -> test for single file file = {} for f in node.getFiles(): if f.getType() not in node.getSysFiles(): file["mimetype"], file["type"] = getMimeType(f.getName()) file["icon"] = fileicons[file["mimetype"]] file["path"] = f._path file["name"] = f.getName() size = f.getSize() or 0 file["size"] = format_filesize(size) filesize += f.getSize() ret.append(file) return ret, filesize if not path.endswith("/") and not req.params.get("path", "").startswith("/"): path += "/" path += req.params.get("path", "") if req.params.get("path", "") != "": file = {} file["type"] = "back" file["mimetype"] = "back" file["icon"] = fileicons[file["mimetype"]] file["name"] = ".." file["path"] = req.params.get("path", "") file["req_path"] = req.params.get("path", "")[:req.params.get("path", "").rfind("/")] ret.append(file) for name in os.listdir(config.settings["paths.datadir"] + path + "/"): if name.endswith(".thumb") or name.endswith(".thumb2"): continue file = {} file_path = os.path.join(config.settings["paths.datadir"] + path, name) if os.path.isdir(file_path): # directory file["type"] = "dir" file["mimetype"] = "directory" else: # file file["mimetype"], file["type"] = getMimeType(name) file["size"] = format_filesize(os.path.getsize(file_path)) filesize += os.path.getsize(file_path) file["icon"] = fileicons[file["mimetype"]] file["path"] = os.path.join(path, name) file["name"] = name file["req_path"] = req.params.get("path", "") + "/" + file["name"] ret.append(file) return ret, format_filesize(filesize)
def getContent(req, ids): node = tree.getNode(ids[0]) user = users.getUserFromRequest(req) access = AccessData(req) if not access.hasWriteAccess( node) or "editor" in users.getHideMenusForUser(user): req.setStatus(httpstatus.HTTP_FORBIDDEN) return req.getTAL("web/edit/edit.html", {}, macro="access_error") if req.params.get('file') == "config": # configuration file for ckeditor req.reply_headers['Content-Type'] = "application/javascript" return req.writeTAL("web/edit/modules/startpages.html", { 'id': ids[0], 'lang': lang(req) }, macro="ckconfig") if "action" in req.params: if req.params.get('action') == "getfile": # deliver filecontent data = "" for f in [f for f in node.getFiles() if f.mimetype == "text/html"]: filepath = f.retrieveFile().replace( config.get("paths.datadir"), '') if req.params.get('filename') == filepath and os.path.exists( config.get("paths.datadir") + filepath): with open(config.get("paths.datadir") + filepath, "r") as fil: data = fil.read() msg = "%s opened startpage %r for node %s (%r, %r)" % ( user.name, filepath, node.id, node.name, node.type) logger.info(msg) break req.write(json.dumps({'filecontent': data})) if req.params.get('action') == "save": # save filedata if req.params.get('filename') == "add": # add new file maxid = 0 for f in [f for f in node.getFiles() if f.type == "content"]: try: if int(f.retrieveFile()[:-5].split("_")[-1]) >= maxid: maxid = int( f.retrieveFile()[:-5].split("_")[-1]) + 1 except ValueError: pass filename = 'html/%s_%s.html' % (req.params.get('id'), maxid) while os.path.exists(config.get("paths.datadir") + filename): maxid = maxid + 1 filename = 'html/%s_%s.html' % (req.params.get('id'), maxid) with open(config.get("paths.datadir") + filename, "w") as fil: fil.write(req.params.get('data')) node.addFile(FileNode(filename, "content", "text/html")) req.write(json.dumps({'filename': '', 'state': 'ok'})) msg = "%s added startpage %r for node %s (%r, %r)" % ( user.name, filename, node.id, node.name, node.type) logger.info(msg) return None else: for f in [ f for f in node.getFiles() if f.mimetype == "text/html" ]: filepath = f.retrieveFile().replace( config.get("paths.datadir"), '') if req.params.get( 'filename') == filepath and os.path.exists( config.get("paths.datadir") + filepath): with open(config.get("paths.datadir") + filepath, "w") as fil: fil.write(req.params.get('data')) req.write( json.dumps({ 'filesize': format_filesize( os.path.getsize( config.get("paths.datadir") + filepath)), 'filename': req.params.get('filename'), 'state': 'ok' })) msg = "%s saved startpage %r for node %s (%r, %r)" % ( user.name, filepath, node.id, node.name, node.type) logger.info(msg) break return None if "option" in req.params: if req.params.get("option") == "filebrowser": # open filebrowser msg = "%s opening ckeditor filebrowser for node %s (%r, %r)" % ( user.name, node.id, node.name, node.type) logger.info(msg) req.write(send_nodefile_tal(req)) return "" if req.params.get("option") == "htmlupload": # use fileupload msg = "%s going to use ckeditor fileupload (htmlupload) for node %s (%r, %r)" % ( user.name, node.id, node.name, node.type) logger.info(msg) req.write(upload_for_html(req)) return "" if "delete" in req.params: # delete file via CKeditor for f in node.getFiles(): if f.retrieveFile().endswith(req.params.get('option')): filepath = f.retrieveFile().replace( config.get("paths.datadir"), '') msg = "%s going to delete ckeditor filebrowser file %r for node %s (%r, %r)" % ( user.name, filepath, node.id, node.name, node.type) logger.info(msg) if os.path.exists(f.retrieveFile()): os.remove(f.retrieveFile()) node.removeFile(f) break return "" for key in req.params.keys(): if key.startswith("delete_"): # delete page page = key[7:-2] try: file_shortpath = page.replace(config.get("paths.datadir"), "") fullpath = os.path.join(config.get("paths.datadir"), page) if os.path.exists(fullpath): os.remove(fullpath) logger.info("%s removed file %r from disk" % (user.name, fullpath)) else: logger.warning( "%s could not remove file %r from disk: not existing" % (user.name, fullpath)) filenode = FileNode(page, "", "text/html") node.removeAttribute("startpagedescr." + file_shortpath) node.set( "startpage.selector", node.get("startpage.selector").replace(file_shortpath, "")) node.removeFile(filenode) logger.info( user.name + " - startpages - deleted FileNode and file for node %s (%s): %s, %s, %s, %s" % (node.id, node.name, page, filenode.getName(), filenode.type, filenode.mimetype)) except: logger.error( user.name + " - startpages - error while delete FileNode and file for " + page) logger.error("%s - %s" % (sys.exc_info()[0], sys.exc_info()[1])) break if "save_page" in req.params: # save page content = "" for key in req.params.keys(): if key.startswith("page_content"): content = req.params.get(key, "") break with open(req.params.get('file_path'), "w") as fi: fi.writelines(content) del req.params['save_page'] del req.params['file_to_edit'] req.params['tab'] = 'startpages' return getContent(req, [node.id]) if "cancel_page" in req.params: del req.params['file_to_edit'] del req.params['cancel_page'] return getContent(req, [node.id]) filelist = [] for f in node.getFiles(): if f.mimetype == 'text/html' and f.getType() in ['content']: filelist.append(f) languages = [ language.strip() for language in config.get("i18n.languages").split(",") ] if "startpages_save" in req.params.keys( ): # user saves startpage configuration msg = "%s going to save startpage configuration for node %s (%r, %r): %r" % ( user.name, node.id, node.name, node.type, req.params) logger.info(msg) sidebar = "" for k in [k for k in req.params if k.startswith('sidebar_')]: sidebar += "%s:%s;" % (k[8:], req.params[k]) node.set('system.sidebar', sidebar) for k in [k for k in req.params if k.startswith('descr.')]: node.set('startpage' + k, req.params[k]) # build startpage_selector startpage_selector = "" for language in languages: startpage_selector += "%s:%s;" % ( language, req.params.get('radio_' + language)) node.set('startpage.selector', startpage_selector[0:-1]) named_filelist = [] for f in filelist: long_path = f.retrieveFile() short_path = long_path.replace(config.get("paths.datadir"), '') file_exists = os.path.isfile(long_path) file_size = "-" if file_exists: file_size = os.path.getsize(long_path) langlist = [] sidebar = [] for language in languages: spn = node.getStartpageFileNode(language) if spn and spn.retrieveFile() == long_path: langlist.append(language) if node.get('system.sidebar').find(language + ":" + short_path) >= 0: sidebar.append(language) named_filelist.append( (short_path, node.get('startpagedescr.' + short_path), f.type, f, file_exists, format_filesize(file_size), long_path, langlist, "/file/%s/%s" % (req.params.get("id", "0"), short_path.split('/')[-1]), sidebar)) lang2file = node.getStartpageDict() # compatibility: there may be old startpages in the database that # are not described by node attributes initial = filelist and not lang2file # node may not have startpage set for some language # compatibilty: node may not have attribute startpage.selector # build startpage_selector and wriote back to node startpage_selector = "" for language in languages: if initial: lang2file[language] = named_filelist[0][0] else: lang2file[language] = lang2file.setdefault(language, '') startpage_selector += "%s:%s;" % (language, lang2file[language]) node.set('startpage.selector', startpage_selector[0:-1]) v = { "id": req.params.get("id", "0"), "tab": req.params.get("tab", ""), "node": node, "named_filelist": named_filelist, "languages": languages, "lang2file": lang2file, "types": ['content'], "d": lang2file and True } return req.getTAL("web/edit/modules/startpages.html", v, macro="edit_startpages")
def getContent(req, ids): node = q(Node).get(ids[0]) user = current_user if not node.has_write_access() or "editor" in user.hidden_edit_functions: req.setStatus(httpstatus.HTTP_FORBIDDEN) return req.getTAL("web/edit/edit.html", {}, macro="access_error") if req.params.get('file') == "config": # configuration file for ckeditor req.reply_headers['Content-Type'] = "application/javascript" return req.writeTAL("web/edit/modules/startpages.html", { 'id': ids[0], 'lang': lang(req) }, macro="ckconfig") if "action" in req.params: if req.params.get('action') == "getfile": # deliver filecontent data = "" for f in [f for f in node.files if f.mimetype == "text/html"]: filepath = f.abspath.replace(config.get("paths.datadir"), '') if req.params.get('filename') == filepath and os.path.exists( config.get("paths.datadir") + filepath): with codecs.open(config.get("paths.datadir") + filepath, "r", encoding='utf8') as fil: data = fil.read() logg.info("%s opened startpage %s for node %s (%s, %s)", user.login_name, filepath, node.id, node.name, node.type) break req.write(json.dumps({'filecontent': data}, ensure_ascii=False)) if req.params.get('action') == "save": # save filedata if req.params.get('filename') == "add": # add new file maxid = 0 for f in [f for f in node.files if f.type == "content"]: try: if int(f.abspath[:-5].split("_")[-1]) >= maxid: maxid = int(f.abspath[:-5].split("_")[-1]) + 1 except ValueError: pass filename = 'html/%s_%s.html' % (req.params.get('id'), maxid) while os.path.exists(config.get("paths.datadir") + filename): maxid = maxid + 1 filename = 'html/%s_%s.html' % (req.params.get('id'), maxid) with codecs.open(config.get("paths.datadir") + filename, "w", encoding='utf8') as fil: fil.write(req.params.get('data')) node.files.append(File(filename, u"content", u"text/html")) db.session.commit() req.write(json.dumps({'filename': '', 'state': 'ok'})) logg.info("%s added startpage %s for node %s (%s, %s)", user.login_name, filename, node.id, node.name, node.type) return None else: for f in [f for f in node.files if f.mimetype == "text/html"]: filepath = f.abspath.replace(config.get("paths.datadir"), '') if req.params.get( 'filename') == filepath and os.path.exists( config.get("paths.datadir") + filepath): with open(config.get("paths.datadir") + filepath, "w") as fil: fil.write(req.params.get('data')) req.write( json.dumps( { 'filesize': format_filesize( os.path.getsize( config.get("paths.datadir") + filepath)), 'filename': req.params.get('filename'), 'state': 'ok' }, ensure_ascii=False)) logg.info("%s saved startpage %s for node %s (%s, %s)", user.login_name, filepath, node.id, node.name, node.type) break return None if "option" in req.params: if req.params.get("option") == "filebrowser": # open filebrowser logg.info("%s opening ckeditor filebrowser for node %s (%r, %r)", user.login_name, node.id, node.name, node.type) req.write(send_nodefile_tal(req)) return "" if req.params.get("option") == "htmlupload": # use fileupload logg.info( "%s going to use ckeditor fileupload (htmlupload) for node %s (%s, %s)", user.login_name, node.id, node.name, node.type) req.write(upload_for_html(req)) return "" if "delete" in req.params: # delete file via CKeditor for f in node.files: if f.abspath.endswith(req.params.get('option')): filepath = f.abspath.replace(config.get("paths.datadir"), '') logg.info( "%s going to delete ckeditor filebrowser file %s for node %s (%s, %s)", user.login_name, filepath, node.id, node.name, node.type) if os.path.exists(f.abspath): os.remove(f.abspath) node.files.remove(f) break db.session.commit() return "" for key in req.params.keys(): if key.startswith("delete_"): # delete page page = key[7:-2] try: file_shortpath = page.replace(config.get("paths.datadir"), "") fullpath = os.path.join(config.get("paths.datadir"), page) if os.path.exists(fullpath): os.remove(fullpath) logg.info("%s removed file %s from disk", user.login_name, fullpath) else: logg.warn( "%s could not remove file %s from disk: not existing", user.login_name, fullpath) filenode = q(File).filter_by(path=page, mimetype=u"text/html").one() try: del node.system_attrs["startpagedescr." + file_shortpath] except KeyError: pass node.system_attrs["startpage_selector"] = node.system_attrs[ "startpage_selector"].replace(file_shortpath, "") node.files.remove(filenode) db.session.commit() logg.info( "%s - startpages - deleted File and file for node %s (%s): %s, %s, %s, %s", user.login_name, node.id, node.name, page, filenode.path, filenode.filetype, filenode.mimetype) except: logg.exception( "%s - startpages - error while delete File and file for %s, exception ignored", user.login_name, page) break if "save_page" in req.params: # save page content = "" for key in req.params.keys(): if key.startswith("page_content"): content = req.params.get(key, "") break with open(req.params.get('file_path'), "w", encoding='utf8') as fi: fi.writelines(content) del req.params['save_page'] del req.params['file_to_edit'] req.params['tab'] = 'startpages' return getContent(req, [node.id]) if "cancel_page" in req.params: del req.params['file_to_edit'] del req.params['cancel_page'] return getContent(req, [node.id]) filelist = [] for f in node.files: if f.mimetype == 'text/html' and f.getType() in ['content']: filelist.append(f) db.session.commit() if "startpages_save" in req.params.keys( ): # user saves startpage configuration logg.info( "%s going to save startpage configuration for node %s (%s, %s): %s", user.login_name, node.id, node.name, node.type, req.params) sidebar = "" for k in [k for k in req.params if k.startswith('sidebar_')]: sidebar += "%s:%s;" % (k[8:], req.params[k]) node.set('system.sidebar', sidebar) for k in [k for k in req.params if k.startswith('descr.')]: node.system_attrs['startpage' + k] = req.params[k] # build startpage_selector startpage_selector = "" for language in config.languages: startpage_selector += "%s:%s;" % ( language, req.params.get('radio_' + language)) node.system_attrs['startpage_selector'] = startpage_selector[0:-1] named_filelist = [] for f in filelist: long_path = f.abspath short_path = long_path.replace(config.get("paths.datadir"), '') file_exists = os.path.isfile(long_path) file_size = "-" if file_exists: file_size = os.path.getsize(long_path) langlist = [] sidebar = [] for language in config.languages: spn = node.getStartpageFileNode(language) if spn and spn.abspath == long_path: langlist.append(language) if node.system_attrs.get( 'sidebar', '').find(language + ":" + short_path) >= 0: sidebar.append(language) named_filelist.append( (short_path, node.system_attrs.get('startpagedescr.' + short_path), f.type, f, file_exists, format_filesize(file_size), long_path, langlist, "/file/%s/%s" % (req.params.get("id", "0"), short_path.split('/')[-1]), sidebar)) lang2file = node.getStartpageDict() # compatibility: there may be old startpages in the database that # are not described by node attributes initial = filelist and not lang2file # node may not have startpage set for some language # compatibilty: node may not have system attribute startpage_selector # build startpage_selector and write back to node startpage_selector = "" for language in config.languages: if initial: lang2file[language] = named_filelist[0][0] else: lang2file[language] = lang2file.setdefault(language, '') startpage_selector += "%s:%s;" % (language, lang2file[language]) node.system_attrs['startpage_selector'] = startpage_selector[0:-1] db.session.commit() named_filelist.sort(lambda x, y: cmp(x[1], y[1])) v = { "id": req.params.get("id", "0"), "tab": req.params.get("tab", ""), "node": node, "named_filelist": named_filelist, "languages": config.languages, "lang2file": lang2file, "types": ['content'], "d": lang2file and True } return req.getTAL("web/edit/modules/startpages.html", v, macro="edit_startpages")
def calc_size(file): for f in file.getFiles(): if f.getType() == "document": return format_filesize(f.getSize()) return ""
def filebrowser(node, req): filesize = 0 ret = list() paths = [] for f in node.getFiles(): if f.getType() == "attachment": paths.append(f._path) # break if len(paths) == 1 and os.path.isdir( config.get("paths.datadir") + paths[0]): # single file with no path path = paths[0] elif len(paths) > 0: # some single files files = [] for path in paths: file = {} if not os.path.isdir(config.get("paths.datadir") + path): # file file["mimetype"], file["type"] = getMimeType( config.get("paths.datadir") + path) file["icon"] = fileicons[file["mimetype"]] file["path"] = path file["name"] = f.getName() if os.path.exists(config.get("paths.datadir") + path): size = os.path.getsize(config.get("paths.datadir") + path) else: size = 0 file["size"] = format_filesize(size) filesize += int(size) files.append(file) return files, filesize else: path = "" if path == "": # no attachment directory -> test for single file file = {} for f in node.getFiles(): if f.getType() not in node.getSysFiles(): file["mimetype"], file["type"] = getMimeType(f.getName()) file["icon"] = fileicons[file["mimetype"]] file["path"] = f._path file["name"] = f.getName() size = f.getSize() or 0 file["size"] = format_filesize(size) filesize += f.getSize() ret.append(file) return ret, filesize if not path.endswith("/") and not req.params.get("path", "").startswith("/"): path += "/" path += req.params.get("path", "") if req.params.get("path", "") != "": file = {} file["type"] = "back" file["mimetype"] = "back" file["icon"] = fileicons[file["mimetype"]] file["name"] = ".." file["path"] = req.params.get("path", "") file["req_path"] = req.params.get( "path", "")[:req.params.get("path", "").rfind("/")] ret.append(file) for name in os.listdir(config.settings["paths.datadir"] + path + "/"): if name.endswith(".thumb") or name.endswith(".thumb2"): continue file = {} file_path = os.path.join(config.settings["paths.datadir"] + path, name) if os.path.isdir(file_path): # directory file["type"] = "dir" file["mimetype"] = "directory" else: # file file["mimetype"], file["type"] = getMimeType(name) file["size"] = format_filesize(os.path.getsize(file_path)) filesize += os.path.getsize(file_path) file["icon"] = fileicons[file["mimetype"]] file["path"] = os.path.join(path, name) file["name"] = name file["req_path"] = req.params.get("path", "") + "/" + file["name"] ret.append(file) return ret, format_filesize(filesize)
def getContent(req, ids): node = tree.getNode(ids[0]) user = users.getUserFromRequest(req) access = AccessData(req) if not access.hasWriteAccess(node) or "editor" in users.getHideMenusForUser(user): req.setStatus(httpstatus.HTTP_FORBIDDEN) return req.getTAL("web/edit/edit.html", {}, macro="access_error") if req.params.get('file') == "config": # configuration file for ckeditor req.reply_headers['Content-Type'] = "application/javascript" return req.writeTAL("web/edit/modules/startpages.html", {'id': ids[0], 'lang': lang(req)}, macro="ckconfig") if "action" in req.params: if req.params.get('action') == "getfile": # deliver filecontent data = "" for f in [f for f in node.getFiles() if f.mimetype == "text/html"]: filepath = f.retrieveFile().replace(config.get("paths.datadir"), '') if req.params.get('filename') == filepath and os.path.exists(config.get("paths.datadir") + filepath): with open(config.get("paths.datadir") + filepath, "r") as fil: data = fil.read() msg = "%s opened startpage %r for node %s (%r, %r)" % (user.name, filepath, node.id, node.name, node.type) logger.info(msg) break req.write(json.dumps({'filecontent': data})) if req.params.get('action') == "save": # save filedata if req.params.get('filename') == "add": # add new file maxid = 0 for f in [f for f in node.getFiles() if f.type == "content"]: try: if int(f.retrieveFile()[:-5].split("_")[-1]) >= maxid: maxid = int(f.retrieveFile()[:-5].split("_")[-1]) + 1 except ValueError: pass filename = 'html/%s_%s.html' % (req.params.get('id'), maxid) while os.path.exists(config.get("paths.datadir") + filename): maxid = maxid + 1 filename = 'html/%s_%s.html' % (req.params.get('id'), maxid) with open(config.get("paths.datadir") + filename, "w") as fil: fil.write(req.params.get('data')) node.addFile(FileNode(filename, "content", "text/html")) req.write(json.dumps({'filename': '', 'state': 'ok'})) msg = "%s added startpage %r for node %s (%r, %r)" % (user.name, filename, node.id, node.name, node.type) logger.info(msg) return None else: for f in [f for f in node.getFiles() if f.mimetype == "text/html"]: filepath = f.retrieveFile().replace(config.get("paths.datadir"), '') if req.params.get('filename') == filepath and os.path.exists(config.get("paths.datadir") + filepath): with open(config.get("paths.datadir") + filepath, "w") as fil: fil.write(req.params.get('data')) req.write(json.dumps( {'filesize': format_filesize(os.path.getsize(config.get("paths.datadir") + filepath)), 'filename': req.params.get('filename'), 'state': 'ok'})) msg = "%s saved startpage %r for node %s (%r, %r)" % (user.name, filepath, node.id, node.name, node.type) logger.info(msg) break return None if "option" in req.params: if req.params.get("option") == "filebrowser": # open filebrowser msg = "%s opening ckeditor filebrowser for node %s (%r, %r)" % (user.name, node.id, node.name, node.type) logger.info(msg) req.write(send_nodefile_tal(req)) return "" if req.params.get("option") == "htmlupload": # use fileupload msg = "%s going to use ckeditor fileupload (htmlupload) for node %s (%r, %r)" % (user.name, node.id, node.name, node.type) logger.info(msg) req.write(upload_for_html(req)) return "" if "delete" in req.params: # delete file via CKeditor for f in node.getFiles(): if f.retrieveFile().endswith(req.params.get('option')): filepath = f.retrieveFile().replace(config.get("paths.datadir"), '') msg = "%s going to delete ckeditor filebrowser file %r for node %s (%r, %r)" % (user.name, filepath, node.id, node.name, node.type) logger.info(msg) if os.path.exists(f.retrieveFile()): os.remove(f.retrieveFile()) node.removeFile(f) break return "" for key in req.params.keys(): if key.startswith("delete_"): # delete page page = key[7:-2] try: file_shortpath = page.replace(config.get("paths.datadir"), "") fullpath = os.path.join(config.get("paths.datadir"), page) if os.path.exists(fullpath): os.remove(fullpath) logger.info("%s removed file %r from disk" % (user.name, fullpath)) else: logger.warning("%s could not remove file %r from disk: not existing" % (user.name, fullpath)) filenode = FileNode(page, "", "text/html") node.removeAttribute("startpagedescr." + file_shortpath) node.set("startpage.selector", node.get("startpage.selector").replace(file_shortpath, "")) node.removeFile(filenode) logger.info( user.name + " - startpages - deleted FileNode and file for node %s (%s): %s, %s, %s, %s" % ( node.id, node.name, page, filenode.getName(), filenode.type, filenode.mimetype)) except: logger.error(user.name + " - startpages - error while delete FileNode and file for " + page) logger.error("%s - %s" % (sys.exc_info()[0], sys.exc_info()[1])) break if "save_page" in req.params: # save page content = "" for key in req.params.keys(): if key.startswith("page_content"): content = req.params.get(key, "") break with open(req.params.get('file_path'), "w") as fi: fi.writelines(content) del req.params['save_page'] del req.params['file_to_edit'] req.params['tab'] = 'startpages' return getContent(req, [node.id]) if "cancel_page" in req.params: del req.params['file_to_edit'] del req.params['cancel_page'] return getContent(req, [node.id]) filelist = [] for f in node.getFiles(): if f.mimetype == 'text/html' and f.getType() in ['content']: filelist.append(f) languages = [language.strip() for language in config.get("i18n.languages").split(",")] if "startpages_save" in req.params.keys(): # user saves startpage configuration msg = "%s going to save startpage configuration for node %s (%r, %r): %r" % (user.name, node.id, node.name, node.type, req.params) logger.info(msg) sidebar = "" for k in [k for k in req.params if k.startswith('sidebar_')]: sidebar += "%s:%s;" % (k[8:], req.params[k]) node.set('system.sidebar', sidebar) for k in [k for k in req.params if k.startswith('descr.')]: node.set('startpage' + k, req.params[k]) # build startpage_selector startpage_selector = "" for language in languages: startpage_selector += "%s:%s;" % (language, req.params.get('radio_' + language)) node.set('startpage.selector', startpage_selector[0:-1]) named_filelist = [] for f in filelist: long_path = f.retrieveFile() short_path = long_path.replace(config.get("paths.datadir"), '') file_exists = os.path.isfile(long_path) file_size = "-" if file_exists: file_size = os.path.getsize(long_path) langlist = [] sidebar = [] for language in languages: spn = node.getStartpageFileNode(language) if spn and spn.retrieveFile() == long_path: langlist.append(language) if node.get('system.sidebar').find(language + ":" + short_path) >= 0: sidebar.append(language) named_filelist.append((short_path, node.get('startpagedescr.' + short_path), f.type, f, file_exists, format_filesize(file_size), long_path, langlist, "/file/%s/%s" % (req.params.get("id", "0"), short_path.split('/')[-1]), sidebar)) lang2file = node.getStartpageDict() # compatibility: there may be old startpages in the database that # are not described by node attributes initial = filelist and not lang2file # node may not have startpage set for some language # compatibilty: node may not have attribute startpage.selector # build startpage_selector and wriote back to node startpage_selector = "" for language in languages: if initial: lang2file[language] = named_filelist[0][0] else: lang2file[language] = lang2file.setdefault(language, '') startpage_selector += "%s:%s;" % (language, lang2file[language]) node.set('startpage.selector', startpage_selector[0:-1]) v = {"id": req.params.get("id", "0"), "tab": req.params.get("tab", ""), "node": node, "named_filelist": named_filelist, "languages": languages, "lang2file": lang2file, "types": ['content'], "d": lang2file and True} return req.getTAL("web/edit/modules/startpages.html", v, macro="edit_startpages")
def getContent(req, ids): node = q(Node).get(ids[0]) user = current_user if not node.has_write_access() or "editor" in user.hidden_edit_functions: req.setStatus(httpstatus.HTTP_FORBIDDEN) return req.getTAL("web/edit/edit.html", {}, macro="access_error") if req.params.get('file') == "config": # configuration file for ckeditor req.reply_headers['Content-Type'] = "application/javascript" return req.writeTAL("web/edit/modules/startpages.html", {'id': ids[0], 'lang': lang(req)}, macro="ckconfig") if "action" in req.params: if req.params.get('action') == "getfile": # deliver filecontent data = "" for f in [f for f in node.files if f.mimetype == "text/html"]: filepath = f.abspath.replace(config.get("paths.datadir"), '') if req.params.get('filename') == filepath and os.path.exists(config.get("paths.datadir") + filepath): with codecs.open(config.get("paths.datadir") + filepath, "r", encoding='utf8') as fil: data = fil.read() logg.info("%s opened startpage %s for node %s (%s, %s)", user.login_name, filepath, node.id, node.name, node.type) break req.write(json.dumps({'filecontent': data}, ensure_ascii=False)) if req.params.get('action') == "save": # save filedata if req.params.get('filename') == "add": # add new file maxid = 0 for f in [f for f in node.files if f.type == "content"]: with suppress(ValueError, warn=False): if int(f.abspath[:-5].split("_")[-1]) >= maxid: maxid = int(f.abspath[:-5].split("_")[-1]) + 1 filename = 'html/%s_%s.html' % (req.params.get('id'), maxid) while os.path.exists(config.get("paths.datadir") + filename): maxid = maxid + 1 filename = 'html/%s_%s.html' % (req.params.get('id'), maxid) with codecs.open(config.get("paths.datadir") + filename, "w", encoding='utf8') as fil: fil.write(req.params.get('data')) node.files.append(File(filename, u"content", u"text/html")) db.session.commit() req.write(json.dumps({'filename': '', 'state': 'ok'})) logg.info("%s added startpage %s for node %s (%s, %s)", user.login_name, filename, node.id, node.name, node.type) return None else: for f in [f for f in node.files if f.mimetype == "text/html"]: filepath = f.abspath.replace(config.get("paths.datadir"), '') if req.params.get('filename') == filepath and os.path.exists(config.get("paths.datadir") + filepath): with open(config.get("paths.datadir") + filepath, "w") as fil: try: fil.write(req.params.get('data')) except UnicodeEncodeError: # some unicode characters like 'Black Circle' ● are not translated in the # html entity by the current ckeditor version fil.write(req.params.get('data').encode('ascii', 'xmlcharrefreplace')) req.write(json.dumps( {'filesize': format_filesize(os.path.getsize(config.get("paths.datadir") + filepath)), 'filename': req.params.get('filename'), 'state': 'ok'}, ensure_ascii=False)) logg.info("%s saved startpage %s for node %s (%s, %s)", user.login_name, filepath, node.id, node.name, node.type) break return None if "option" in req.params: if req.params.get("option") == "filebrowser": # open filebrowser logg.info("%s opening ckeditor filebrowser for node %s (%r, %r)", user.login_name, node.id, node.name, node.type) req.write(send_nodefile_tal(req)) return "" if req.params.get("option") == "htmlupload": # use fileupload logg.info("%s going to use ckeditor fileupload (htmlupload) for node %s (%s, %s)", user.login_name, node.id, node.name, node.type) req.write(upload_for_html(req)) return "" if "delete" in req.params: # delete file via CKeditor for f in node.files: if f.abspath.endswith(req.params.get('option')): filepath = f.abspath.replace(config.get("paths.datadir"), '') logg.info("%s going to delete ckeditor filebrowser file %s for node %s (%s, %s)", user.login_name, filepath, node.id, node.name, node.type) if os.path.exists(f.abspath): os.remove(f.abspath) node.files.remove(f) break db.session.commit() return "" for key in req.params.keys(): if key.startswith("delete_"): # delete page page = key[7:-2] try: file_shortpath = page.replace(config.get("paths.datadir"), "") fullpath = os.path.join(config.get("paths.datadir"), page) if os.path.exists(fullpath): os.remove(fullpath) logg.info("%s removed file %s from disk", user.login_name, fullpath) else: logg.warn("%s could not remove file %s from disk: not existing", user.login_name, fullpath) filenode = q(File).filter_by(path=page, mimetype=u"text/html").one() with suppress(KeyError, warn=False): del node.system_attrs["startpagedescr." + file_shortpath] node.system_attrs["startpage_selector"] = node.system_attrs["startpage_selector"].replace(file_shortpath, "") node.files.remove(filenode) db.session.commit() logg.info("%s - startpages - deleted File and file for node %s (%s): %s, %s, %s, %s", user.login_name, node.id, node.name, page, filenode.path, filenode.filetype, filenode.mimetype) except: logg.exception("%s - startpages - error while delete File and file for %s, exception ignored", user.login_name, page) break if "save_page" in req.params: # save page content = "" for key in req.params.keys(): if key.startswith("page_content"): content = req.params.get(key, "") break with open(req.params.get('file_path'), "w", encoding='utf8') as fi: fi.writelines(content) del req.params['save_page'] del req.params['file_to_edit'] req.params['tab'] = 'startpages' return getContent(req, [node.id]) if "cancel_page" in req.params: del req.params['file_to_edit'] del req.params['cancel_page'] return getContent(req, [node.id]) filelist = [] for f in node.files: if f.mimetype == 'text/html' and f.getType() in ['content']: filelist.append(f) db.session.commit() if "startpages_save" in req.params.keys(): # user saves startpage configuration logg.info("%s going to save startpage configuration for node %s (%s, %s): %s", user.login_name, node.id, node.name, node.type, req.params) sidebar = "" for k in [k for k in req.params if k.startswith('sidebar_')]: sidebar += "%s:%s;" % (k[8:], req.params[k]) node.set('system.sidebar', sidebar) for k in [k for k in req.params if k.startswith('descr.')]: node.system_attrs['startpage' + k] = req.params[k] # build startpage_selector startpage_selector = "" for language in config.languages: startpage_selector += "%s:%s;" % (language, req.params.get('radio_' + language)) node.system_attrs['startpage_selector'] = startpage_selector[0:-1] named_filelist = [] for f in filelist: long_path = f.abspath short_path = long_path.replace(config.get("paths.datadir"), '') file_exists = os.path.isfile(long_path) file_size = "-" if file_exists: file_size = os.path.getsize(long_path) langlist = [] sidebar = [] for language in config.languages: spn = node.getStartpageFileNode(language) if spn and spn.abspath == long_path: langlist.append(language) if node.system_attrs.get('sidebar', '').find(language + ":" + short_path) >= 0: sidebar.append(language) named_filelist.append((short_path, node.system_attrs.get('startpagedescr.' + short_path), f.type, f, file_exists, format_filesize(file_size), long_path, langlist, "/file/%s/%s" % (req.params.get("id", "0"), short_path.split('/')[-1]), sidebar)) lang2file = node.getStartpageDict() # compatibility: there may be old startpages in the database that # are not described by node attributes initial = filelist and not lang2file # node may not have startpage set for some language # compatibilty: node may not have system attribute startpage_selector # build startpage_selector and write back to node startpage_selector = "" for language in config.languages: if initial: lang2file[language] = named_filelist[0][0] else: lang2file[language] = lang2file.setdefault(language, '') startpage_selector += "%s:%s;" % (language, lang2file[language]) node.system_attrs['startpage_selector'] = startpage_selector[0:-1] db.session.commit() named_filelist.sort(lambda x, y: cmp(x[1], y[1])) v = {"id": req.params.get("id", "0"), "tab": req.params.get("tab", ""), "node": node, "named_filelist": named_filelist, "languages": config.languages, "lang2file": lang2file, "types": ['content'], "d": lang2file and True, "csrf": req.csrf_token.current_token } return req.getTAL("web/edit/modules/startpages.html", v, macro="edit_startpages")
def filebrowser(node, req): filesize = 0 ret = list() if isinstance(node, Node): file_entity = File else: file_entity = version_class(File) paths = [ t[0] for t in node.files.with_entities(file_entity.path).filter_by( filetype=u"attachment") ] if len(paths) == 1 and os.path.isdir( config.get("paths.datadir") + paths[0]): # single file with no path path = paths[0] elif len(paths) > 0: # some single files files = [] for path in paths: file = {} if not os.path.isdir(config.get("paths.datadir") + path): # file file["mimetype"], file["type"] = getMimeType( config.get("paths.datadir") + path) icon = fileicons.get(file["mimetype"]) if not icon: icon = fileicons["other"] file["icon"] = icon file["path"] = path file["name"] = os.path.basename(path) if os.path.exists(config.get("paths.datadir") + path): size = os.path.getsize(config.get("paths.datadir") + path) else: size = 0 file["size"] = format_filesize(size) filesize += int(size) files.append(file) return files, filesize else: path = "" if path == "": # no attachment directory -> test for single file for f in node.files.filter( ~file_entity.filetype.in_(node.get_sys_filetypes())): file = {} file["mimetype"], file["type"] = getMimeType(f.getName()) file["icon"] = fileicons[file["mimetype"]] file["path"] = f.path file["name"] = f.base_name file["size"] = format_filesize(f.size) filesize += f.size ret.append(file) return ret, filesize if not path.endswith("/") and not req.params.get("path", "").startswith("/"): path += "/" path += req.params.get("path", "") if req.params.get("path", "") != "": file = {} file["type"] = "back" file["mimetype"] = "back" file["icon"] = fileicons[file["mimetype"]] file["name"] = ".." file["path"] = req.params.get("path", "") file["req_path"] = req.params.get( "path", "")[:req.params.get("path", "").rfind("/")] ret.append(file) for name in os.listdir(config.settings["paths.datadir"] + path + "/"): if name.endswith(".thumb") or name.endswith(".thumb2"): continue file = {} file_path = os.path.join(config.settings["paths.datadir"] + path, name) if os.path.isdir(file_path): # directory file["type"] = "dir" file["mimetype"] = "directory" else: # file file["mimetype"], file["type"] = getMimeType(name) file["size"] = format_filesize(os.path.getsize(file_path)) filesize += os.path.getsize(file_path) file["icon"] = fileicons[file["mimetype"]] file["path"] = os.path.join(path, name) file["name"] = name file["req_path"] = req.params.get("path", "") + "/" + file["name"] ret.append(file) return ret, format_filesize(filesize)