def _finish_change(node, change_file, user, uploadfile, req): if change_file in ["yes", "no"]: # check that the correct filetype is uploaded # note: only the suffix of the filename is checked not the file content uploadfile_type = getMimeType(uploadfile.filename)[1] if uploadfile_type != node.type and uploadfile_type != node.get_upload_filetype( ): req.setStatus(httpstatus.HTTP_NOT_ACCEPTABLE) return # sys files are always cleared to delete remaining thumbnails, presentation images etc. for f in node.files: if f.filetype in node.get_sys_filetypes(): node.files.remove(f) file = importFile(uploadfile.filename, uploadfile.tempname) # add new file file.filetype = node.get_upload_filetype() node.files.append(file) # this should re-create all dependent files node.event_files_changed() logg.info(u"%s changed file of node %s to %s (%s)", user.login_name, node.id, uploadfile.filename, uploadfile.tempname) return attpath = "" for f in node.files: if f.mimetype == "inode/directory": attpath = f.base_name break if change_file == "attdir": # add attachmentdir if attpath == "": # add attachment directory attpath = req.params.get("inputname") if not os.path.exists(getImportDir() + "/" + attpath): os.mkdir(getImportDir() + "/" + attpath) node.files.append( File(getImportDir() + "/" + attpath, "attachment", "inode/directory")) importFileIntoDir(getImportDir() + "/" + attpath, uploadfile.tempname) # add new file if change_file == "attfile": # add file as attachment if attpath == "": # no attachment directory existing file = importFile(uploadfile.filename, uploadfile.tempname) # add new file file.mimetype = "inode/file" file.filetype = "attachment" node.files.append(file) else: # import attachment file into existing attachment directory importFileIntoDir(getImportDir() + "/" + attpath, uploadfile.tempname) # add new file
def close(self): self.file.close() try: f = fileutils.importFile(self.realname, self.filename, "ftp_") os.remove(self.filename) self.node.addFile(f) except: pass
def _finish_change(node, change_file, user, uploadfile, req): if change_file in ["yes", "no"]: # check that the correct filetype is uploaded # note: only the suffix of the filename is checked not the file content uploadfile_type = getMimeType(uploadfile.filename)[1] if uploadfile_type != node.type and uploadfile_type != node.get_upload_filetype(): req.setStatus(httpstatus.HTTP_NOT_ACCEPTABLE) return # sys files are always cleared to delete remaining thumbnails, presentation images etc. for f in node.files: if f.filetype in node.get_sys_filetypes(): node.files.remove(f) file = importFile(uploadfile.filename, uploadfile.tempname) # add new file file.filetype = node.get_upload_filetype() node.files.append(file) # this should re-create all dependent files node.event_files_changed() logg.info(u"%s changed file of node %s to %s (%s)", user.login_name, node.id, uploadfile.filename, uploadfile.tempname) return attpath = "" for f in node.files: if f.mimetype == "inode/directory": attpath = f.base_name break if change_file == "attdir": # add attachmentdir if attpath == "": # add attachment directory attpath = req.params.get("inputname") if not os.path.exists(getImportDir() + "/" + attpath): os.mkdir(getImportDir() + "/" + attpath) node.files.append(File(getImportDir() + "/" + attpath, "attachment", "inode/directory")) importFileIntoDir(getImportDir() + "/" + attpath, uploadfile.tempname) # add new file if change_file == "attfile": # add file as attachment if attpath == "": # no attachment directory existing file = importFileToRealname(uploadfile.filename, uploadfile.tempname) # add new file file.mimetype = "inode/file" file.filetype = "attachment" node.files.append(file) else: # import attachment file into existing attachment directory importFileIntoDir(getImportDir() + "/" + attpath, uploadfile.tempname) # add new file
def upload_new_node(req, path, params, data): try: uploadfile = params['data'] del params['data'] except KeyError: uploadfile = None # get the user and verify the signature if params.get('user'): # user=users.getUser(params.get('user')) #userAccess = AccessData(user=user) _user = users.getUser(params.get('user')) if not _user: # user of dynamic class dummyuser: # dummy user class # return all groups with given dynamic user def getGroups(self): return [ g.name for g in tree.getRoot('usergroups').getChildren() if g.get('allow_dynamic') == '1' and params.get('user') in g.get('dynamic_users') ] def getName(self): return params.get('user') def getDirID(self): # unique identifier return params.get('user') def isAdmin(self): return 0 _user = dummyuser() userAccess = AccessData(user=_user) if userAccess.user: user = userAccess.user if not userAccess.verify_request_signature(req.fullpath + '?', params): userAccess = None else: userAccess = None else: user = users.getUser(config.get('user.guestuser')) userAccess = AccessData(user=user) parent = tree.getNode(params.get('parent')) # check user access if userAccess and userAccess.hasAccess(parent, "write"): pass else: s = "No Access" req.write(s) d = { 'status': 'fail', 'html_response_code': '403', 'errormessage': 'no access' } logger.error("user has no edit permission for node %s" % parent) return d['html_response_code'], len(s), d datatype = params.get('type') uploaddir = users.getUploadDir(user) n = tree.Node(name=params.get('name'), type=datatype) if isinstance(uploadfile, types.InstanceType): # file object used nfile = importFile(uploadfile.filename, uploadfile.tempname) else: # string used nfile = importFileFromData('uploadTest.jpg', base64.b64decode(uploadfile)) if nfile: n.addFile(nfile) else: logger.error("error in file uploadservice") try: # test metadata metadata = json.loads(params.get('metadata')) except ValueError: metadata = dict() # set provided metadata for key, value in metadata.iteritems(): n.set(u(key), u(value)) # service flags n.set("creator", user.getName()) n.set("creationtime", format_date()) parent.addChild(n) # process the file, we've added to the new node if hasattr(n, "event_files_changed"): try: n.event_files_changed() except OperationException as e: for file in n.getFiles(): if os.path.exists(file.retrieveFile()): os.remove(file.retrieveFile()) raise OperationException(e.value) # make sure the new node is visible immediately from the web service and # the search index gets updated n.setDirty() tree.remove_from_nodecaches(parent) d = { 'status': 'Created', 'html_response_code': '201', 'build_response_end': time.time() } s = "Created" # provide the uploader with the new node ID req.reply_headers['NodeID'] = n.id # we need to write in case of POST request, send as buffer will not work req.write(s) return d['html_response_code'], len(s), d
def upload_for_html(req): user = users.getUserFromRequest(req) datatype = req.params.get("datatype", "image") id = req.params.get("id") node = tree.getNode(id) access = AccessData(req) if not (access.hasAccess(node, 'read') and access.hasAccess(node, 'write') and access.hasAccess(node, 'data')): return 403 for key in req.params.keys(): if key.startswith("delete_"): filename = key[7:-2] for file in n.getFiles(): if file.getName() == filename: n.removeFile(file) if "file" in req.params.keys(): # file # file upload via (possibly disabled) upload form in custom image # browser file = req.params["file"] del req.params["file"] if hasattr(file, "filesize") and file.filesize > 0: try: logger.info( user.name + " upload " + file.filename + " (" + file.tempname + ")") nodefile = importFile(file.filename, file.tempname) node.addFile(nodefile) req.request["Location"] = req.makeLink( "nodefile_browser/%s/" % id, {}) except EncryptionException: req.request["Location"] = req.makeLink("content", { "id": id, "tab": "tab_editor", "error": "EncryptionError_" + datatype[:datatype.find("/")]}) except: logException("error during upload") req.request["Location"] = req.makeLink("content", { "id": id, "tab": "tab_editor", "error": "PostprocessingError_" + datatype[:datatype.find("/")]}) return send_nodefile_tal(req) if "upload" in req.params.keys(): # NewFile # file upload via CKeditor Image Properties / Upload tab file = req.params["upload"] del req.params["upload"] if hasattr(file, "filesize") and file.filesize > 0: try: logger.info( user.name + " upload via ckeditor " + file.filename + " (" + file.tempname + ")") nodefile = importFile(file.filename, file.tempname) node.addFile(nodefile) except EncryptionException: req.request["Location"] = req.makeLink("content", { "id": id, "tab": "tab_editor", "error": "EncryptionError_" + datatype[:datatype.find("/")]}) except: logException("error during upload") req.request["Location"] = req.makeLink("content", { "id": id, "tab": "tab_editor", "error": "PostprocessingError_" + datatype[:datatype.find("/")]}) url = '/file/' + id + '/' + file.tempname.split('/')[-1] res = """<script type="text/javascript"> // Helper function to get parameters from the query string. function getUrlParam(paramName) { var reParam = new RegExp('(?:[\?&]|&)' + paramName + '=([^&]+)', 'i') ; var match = window.location.search.match(reParam) ; return (match && match.length > 1) ? match[1] : '' ; } funcNum = getUrlParam('CKEditorFuncNum'); window.parent.CKEDITOR.tools.callFunction(funcNum, "%(fileUrl)s","%(customMsg)s"); </script>;""" % { 'fileUrl': url.replace('"', '\\"'), 'customMsg': (t(lang(req), "edit_fckeditor_cfm_uploadsuccess")), } return res return send_nodefile_tal(req)
def buildStatAll_(collection_ids, collection_ids_keys, data, period="", fname=None): # period format = yyyy-mm # read data from logfiles def getStatFile(col_id, timestamp, type, period=period): f = None node = col_id.collection orig_file = None for file in node.getFiles(): if file.getType() == u"statistic": try: if file.getName() == u"stat_{}_{}_{}.xml".format(node.id, timestamp, type): if timestamp == format_date(now(), "yyyy-mm") or timestamp == period: # update current month or given period # orig_file = file.retrieveFile() if os.path.exists(file.retrieveFile()): print 'removing %s' % file.retrieveFile() os.remove(file.retrieveFile()) orig_file = file.retrieveFile() # node.files.remove(file) f = None break else: # old month, do nothing print 'old file doing nothing' return None except: return None if not f: # create new file f_name = config.get("paths.tempdir") + u"stat_{}_{}_{}.xml".format(node.id, timestamp, type) # create new file and write header:j print 'creating writing headers %s' % f_name f = codecs.open(f_name, "w", encoding='utf8') f.write('<?xml version="1.0" encoding="utf-8" ?>\n') f.write('<nodelist created="' + format_date(now(), "yyyy-mm-dd HH:MM:SS") + '">\n') if f_name not in col_id.statfiles: col_id.statfiles.append((f_name, orig_file)) return f print "buildStatAll_ called for %d collections" % len(collection_ids_keys) gi = GeoIP(flags=MEMORY_CACHE) time0 = time.time() last_access = None count = 0 for access in data: if (count % 10000) == 0: print "writing stat files: %d lines from %d processed: %d%%" % (count, len(data), count * 100 / len(data)) count += 1 if last_access and last_access.getID() == access.getID(): pass else: for col in collection_ids_keys: col_id = collection_ids[col] if not col_id.first_frontend: col_id.fin_frontend.write("\t</node>\n") col_id.first_frontend = True if not col_id.first_download: col_id.fin_download.write("\t</node>\n") col_id.first_download = True if not col_id.first_edit: col_id.fin_edit.write("\t</node>\n") col_id.first_edit = True col_id.in_ids = access.getID() in col_id.ids_set last_access = access for col in collection_ids_keys: col_id = collection_ids[col] if not col_id.in_ids: continue if not col_id.files_open: col_id.fin_frontend = getStatFile(col_id, period, "frontend", period) col_id.fin_download = getStatFile(col_id, period, "download", period) col_id.fin_edit = getStatFile(col_id, period, "edit", period) col_id.files_open = True first = False if access.inttype == FRONTEND: fin = col_id.fin_frontend if col_id.first_frontend: first = True col_id.first_frontend = False elif access.inttype == DOWNLOAD: fin = col_id.fin_download if col_id.first_download: first = True col_id.first_download = False elif access.inttype == EDIT: fin = col_id.fin_edit if col_id.first_edit: first = True col_id.first_edit = False if first: fin.write('\t<node id="%d">\n' % access.getID()) try: country_code = gi.country_code_by_name(access.getIp()) except Exception as e: print access.getIp(), e country_code = "" fin.write('\t\t<access date="%s" time="%s" country="%s" visitor_number="%s" bot="%s"/>\n' % (access.getDate(), access.getTime(), country_code, access.get_visitor_number1(col_id), access.is_google_bot())) for col in collection_ids_keys: col_id = collection_ids[col] if not col_id.first_frontend: col_id.fin_frontend.write("\t</node>\n") if not col_id.first_download: col_id.fin_download.write("\t</node>\n") if not col_id.first_edit: col_id.fin_edit.write("\t</node>\n") if col_id.files_open: col_id.fin_frontend.write("</nodelist>\n") col_id.fin_download.write("</nodelist>\n") col_id.fin_edit.write("</nodelist>\n") col_id.fin_frontend.close() col_id.fin_download.close() col_id.fin_edit.close() time1 = time.time() print "read collection: %f" % (time1 - time0) for col in collection_ids_keys: col_id = collection_ids[col] for file, orig_file in col_id.statfiles: if orig_file: destfile = os.path.join(config.get("paths.datadir"), orig_file) shutil.copyfile(file, destfile) print "copy %s to %s" % (file, destfile) else: print "importFile %s" % file statfile = importFile(file.split("/")[-1], file) if statfile: statfile.filetype = u"statistic" col_id.collection.files.append(statfile) db.session.commit() try: os.remove(file) except: pass
def _finish_change(node, change_file, user, uploadfile, req): if change_file in ["yes", "no"]: # check that the correct filetype is uploaded # note: only the suffix of the filename is checked not the file content uploadfile_type = getMimeType(uploadfile.filename)[1] if uploadfile_type != node.type and uploadfile_type != node.get_upload_filetype( ): req.setStatus(httpstatus.HTTP_NOT_ACCEPTABLE) return # sys files are always cleared to delete remaining thumbnails, presentation images etc. for f in node.files: if f.filetype in node.get_sys_filetypes(): node.files.remove(f) file = importFile(uploadfile.filename, uploadfile.tempname) # add new file file.filetype = node.get_upload_filetype() node.files.append(file) # this should re-create all dependent files node.event_files_changed() logg.info(u"%s changed file of node %s to %s (%s)", user.login_name, node.id, uploadfile.filename, uploadfile.tempname) return attpath = "" for f in node.files: if f.mimetype == "inode/directory": attpath = f.base_name break if change_file == "attfile": # add file as attachment if attpath == "": # no attachment directory existing file = importFileToRealname(uploadfile.filename, uploadfile.tempname) # add new file file.mimetype = "inode/file" file.filetype = "attachment" node.files.append(file) else: # import attachment file into existing attachment directory importFileIntoDir(getImportDir() + "/" + attpath, uploadfile.tempname) # add new file # this should re-create all dependent files node.event_files_changed() logg.info(u"%s changed file of node %s to %s (%s)", user.login_name, node.id, uploadfile.filename, uploadfile.tempname) if change_file == "addthumb": # create new thumbanil from uploaded file thumbname = os.path.join( getImportDir(), hashlib.md5(ustr(random.random())).hexdigest()[0:8]) + ".thumb" file = importFile(thumbname, uploadfile.tempname) # add new file make_thumbnail_image(file.abspath, thumbname) make_presentation_image(file.abspath, thumbname + "2") if os.path.exists(file.abspath): # remove uploaded original os.remove(file.abspath) for f in node.files: if f.type in ["thumb", "presentation"]: if os.path.exists(f.abspath): os.remove(f.abspath) node.files.remove(f) node.files.append(File(thumbname, "thumb", "image/jpeg")) node.files.append(File(thumbname + "2", "presentation", "image/jpeg")) logg.info("%s changed thumbnail of node %s", user.login_name, node.id) # this should re-create all dependent files node.event_files_changed() logg.info(u"%s changed file of node %s to %s (%s)", user.login_name, node.id, uploadfile.filename, uploadfile.tempname)
def buildStatAll_(collection_ids, collection_ids_keys, data, period="", fname=None): # period format = yyyy-mm # read data from logfiles def getStatFile(col_id, timestamp, type, period=period): f = None node = col_id.collection orig_file = None for file in node.getFiles(): if file.getType() == u"statistic": try: if file.getName() == u"stat_{}_{}_{}.xml".format( node.id, timestamp, type): if timestamp == format_date( now(), "yyyy-mm" ) or timestamp == period: # update current month or given period # orig_file = file.retrieveFile() if os.path.exists(file.retrieveFile()): print 'removing %s' % file.retrieveFile() os.remove(file.retrieveFile()) orig_file = file.retrieveFile() # node.files.remove(file) f = None break else: # old month, do nothing print 'old file doing nothing' return None except: return None if not f: # create new file f_name = config.get("paths.tempdir") + u"stat_{}_{}_{}.xml".format( node.id, timestamp, type) # create new file and write header:j print 'creating writing headers %s' % f_name f = codecs.open(f_name, "w", encoding='utf8') f.write('<?xml version="1.0" encoding="utf-8" ?>\n') f.write('<nodelist created="' + format_date(now(), "yyyy-mm-dd HH:MM:SS") + '">\n') if f_name not in col_id.statfiles: col_id.statfiles.append((f_name, orig_file)) return f print "buildStatAll_ called for %d collections" % len(collection_ids_keys) gi = GeoIP(flags=MEMORY_CACHE) time0 = time.time() last_access = None count = 0 for access in data: if (count % 10000) == 0: print "writing stat files: %d lines from %d processed: %d%%" % ( count, len(data), count * 100 / len(data)) count += 1 if last_access and last_access.getID() == access.getID(): pass else: for col in collection_ids_keys: col_id = collection_ids[col] if not col_id.first_frontend: col_id.fin_frontend.write("\t</node>\n") col_id.first_frontend = True if not col_id.first_download: col_id.fin_download.write("\t</node>\n") col_id.first_download = True if not col_id.first_edit: col_id.fin_edit.write("\t</node>\n") col_id.first_edit = True col_id.in_ids = access.getID() in col_id.ids_set last_access = access for col in collection_ids_keys: col_id = collection_ids[col] if not col_id.in_ids: continue if not col_id.files_open: col_id.fin_frontend = getStatFile(col_id, period, "frontend", period) col_id.fin_download = getStatFile(col_id, period, "download", period) col_id.fin_edit = getStatFile(col_id, period, "edit", period) col_id.files_open = True first = False if access.inttype == FRONTEND: fin = col_id.fin_frontend if col_id.first_frontend: first = True col_id.first_frontend = False elif access.inttype == DOWNLOAD: fin = col_id.fin_download if col_id.first_download: first = True col_id.first_download = False elif access.inttype == EDIT: fin = col_id.fin_edit if col_id.first_edit: first = True col_id.first_edit = False if first: fin.write('\t<node id="%d">\n' % access.getID()) try: country_code = gi.country_code_by_name(access.getIp()) except Exception as e: print access.getIp(), e country_code = "" fin.write( '\t\t<access date="%s" time="%s" country="%s" visitor_number="%s" bot="%s"/>\n' % (access.getDate(), access.getTime(), country_code, access.get_visitor_number1(col_id), access.is_google_bot())) for col in collection_ids_keys: col_id = collection_ids[col] if not col_id.first_frontend: col_id.fin_frontend.write("\t</node>\n") if not col_id.first_download: col_id.fin_download.write("\t</node>\n") if not col_id.first_edit: col_id.fin_edit.write("\t</node>\n") if col_id.files_open: col_id.fin_frontend.write("</nodelist>\n") col_id.fin_download.write("</nodelist>\n") col_id.fin_edit.write("</nodelist>\n") col_id.fin_frontend.close() col_id.fin_download.close() col_id.fin_edit.close() time1 = time.time() print "read collection: %f" % (time1 - time0) for col in collection_ids_keys: col_id = collection_ids[col] for file, orig_file in col_id.statfiles: if orig_file: destfile = os.path.join(config.get("paths.datadir"), orig_file) shutil.copyfile(file, destfile) print "copy %s to %s" % (file, destfile) else: print "importFile %s" % file statfile = importFile(file.split("/")[-1], file) if statfile: statfile.filetype = u"statistic" col_id.collection.files.append(statfile) db.session.commit() try: os.remove(file) except: pass
def upload_new_node(req, path, params, data): try: uploadfile = params['data'] del params['data'] except KeyError: uploadfile = None # get the user and verify the signature if params.get('user'): # user=users.getUser(params.get('user')) #userAccess = AccessData(user=user) _user = users.getUser(params.get('user')) if not _user: # user of dynamic class dummyuser: # dummy user class # return all groups with given dynamic user def getGroups(self): return [g.name for g in tree.getRoot('usergroups').getChildren() if g.get( 'allow_dynamic') == '1' and params.get('user') in g.get('dynamic_users')] def getName(self): return params.get('user') def getDirID(self): # unique identifier return params.get('user') def isAdmin(self): return 0 _user = dummyuser() userAccess = AccessData(user=_user) if userAccess.user: user = userAccess.user if not userAccess.verify_request_signature( req.fullpath + '?', params): userAccess = None else: userAccess = None else: user = users.getUser(config.get('user.guestuser')) userAccess = AccessData(user=user) parent = tree.getNode(params.get('parent')) # check user access if userAccess and userAccess.hasAccess(parent, "write"): pass else: s = "No Access" req.write(s) d = { 'status': 'fail', 'html_response_code': '403', 'errormessage': 'no access'} logger.error("user has no edit permission for node %s" % parent) return d['html_response_code'], len(s), d datatype = params.get('type') uploaddir = users.getUploadDir(user) n = tree.Node(name=params.get('name'), type=datatype) if isinstance(uploadfile, types.InstanceType): # file object used nfile = importFile(uploadfile.filename, uploadfile.tempname) else: # string used nfile = importFileFromData( 'uploadTest.jpg', base64.b64decode(uploadfile)) if nfile: n.addFile(nfile) else: logger.error("error in file uploadservice") try: # test metadata metadata = json.loads(params.get('metadata')) except ValueError: metadata = dict() # set provided metadata for key, value in metadata.iteritems(): n.set(u(key), u(value)) # service flags n.set("creator", user.getName()) n.set("creationtime", format_date()) parent.addChild(n) # process the file, we've added to the new node if hasattr(n, "event_files_changed"): try: n.event_files_changed() except OperationException as e: for file in n.getFiles(): if os.path.exists(file.retrieveFile()): os.remove(file.retrieveFile()) raise OperationException(e.value) # make sure the new node is visible immediately from the web service and # the search index gets updated n.setDirty() tree.remove_from_nodecaches(parent) d = { 'status': 'Created', 'html_response_code': '201', 'build_response_end': time.time()} s = "Created" # provide the uploader with the new node ID req.reply_headers['NodeID'] = n.id # we need to write in case of POST request, send as buffer will not work req.write(s) return d['html_response_code'], len(s), d
def getContent(req, ids): ret = "" user = users.getUserFromRequest(req) node = tree.getNode(ids[0]) update_error = False access = acl.AccessData(req) msg = "%s|web.edit.modules.files.getContend|req.fullpath=%r|req.path=%r|req.params=%r|ids=%r" % (get_user_id(req), req.fullpath, req.path, req.params, ids) log.debug(msg) if not access.hasWriteAccess(node) or "files" in users.getHideMenusForUser(user): req.setStatus(httpstatus.HTTP_FORBIDDEN) return req.getTAL("web/edit/edit.html", {}, macro="access_error") if 'data' in req.params: if req.params.get('data') == 'children': # get formated list of childnodes of selected directory excludeid = req.params.get('excludeid', None) if excludeid: grandchildren = [] for child in node.getChildren(): for grandchild in child.getChildren(): if not grandchild.isContainer(): grandchildren.append(grandchild) req.writeTAL("web/edit/modules/files.html", {'children': [c for c in node.getChildren() if str(c.id) != excludeid], 'grandchildren': grandchildren}, macro="edit_files_popup_children") else: grandchildren = [] for child in node.getChildren(): for grandchild in child.getChildren(): if not grandchild.isContainer(): grandchildren.append(grandchild) req.writeTAL("web/edit/modules/files.html", {'children': [c for c in node.getChildren() if str(c.id) != excludeid], 'grandchildren': grandchildren}, macro="edit_files_popup_children") elif req.params.get('data') =='grandchildren': grandchildren = [] for child in node.getChildren(): if not child.isContainer(): for grandchild in child.getChildren(): if not grandchild.isContainer(): if not grandchild.isContainer(): grandchildren.append(grandchild) if len(node.getChildren())==0: req.writeTAL("web/edit/modules/files.html", {'grandchildren': []}, macro="edit_files_popup_grandchildren") else: req.writeTAL("web/edit/modules/files.html", {'grandchildren': grandchildren}, macro="edit_files_popup_grandchildren") if req.params.get('data') == 'additems': # add selected node as children for childid in req.params.get('items').split(";"): if childid.strip() != "": childnode = tree.getNode(childid.strip()) for p in childnode.getParents(): if p.isContainer(): logging.getLogger('editor').info("Removed childnode: {} from node: {}. - caused by adding".format(childnode.id, node.id)) p.removeChild(childnode) node.addChild(childnode) req.writeTAL("web/edit/modules/files.html", {'children': node.getChildren(), 'node': node}, macro="edit_files_children_list") if req.params.get('data') == 'removeitem': # remove selected childnode node try: remnode = tree.getNode(req.params.get('remove')) if len(remnode.getParents()) == 1: users.getUploadDir(user).addChild(remnode) node.removeChild(remnode) except: # node not found pass req.writeTAL("web/edit/modules/files.html", {'children': node.getChildren(), 'node': node}, macro="edit_files_children_list") if req.params.get('data') == 'reorder': i = 0 for id in req.params.get('order').split(","): if id != "": n = tree.getNode(id) n.setOrderPos(i) i += 1 if req.params.get('data') == 'translate': req.writeTALstr('<tal:block i18n:translate="" tal:content="msgstr"/>', {'msgstr': req.params.get('msgstr')}) return "" if req.params.get("style") == "popup": v = {"basedirs": [tree.getRoot('home'), tree.getRoot('collections')]} id = req.params.get("id", tree.getRoot().id) v["script"] = "var currentitem = '%s';\nvar currentfolder = '%s';\nvar node = %s;" %(id, req.params.get('parent'), id) v["idstr"] = ",".join(ids) v["node"] = node if node.type != 'directory': try: if v['current_id'] == None: v['current_id'] = node.id except KeyError: pass req.writeTAL("web/edit/modules/files.html", v, macro="edit_files_popup_selection") return "" if "operation" in req.params: op = req.params.get("operation") if op == "delete": for key in req.params.keys(): # delete file if key.startswith("del|"): filename = key[4:-2].split("|") for file in node.getFiles(): if file.getName() == filename[1] and file.type == filename[0]: # remove all files in directory if file.getMimeType() == "inode/directory": for root, dirs, files in os.walk(file.retrieveFile()): for name in files: try: os.remove(root + "/" + name) except: pass os.removedirs(file.retrieveFile()+"/") if len([f for f in node.getFiles() if f.getName()==filename[1] and f.type==filename[0]]) > 1: # remove single file from database if there are duplicates node.removeFile(file, single=True) else: # remove single file node.removeFile(file) try: os.remove(file.retrieveFile()) except: pass break break elif key.startswith("delatt|"): for file in node.getFiles(): if file.getMimeType() == "inode/directory": try: os.remove(file.retrieveFile() + "/" + key.split("|")[2][:-2]) except: pass break break elif op=="change": uploadfile = req.params.get("updatefile") if uploadfile: create_version_error = False # Create new version when change file if (req.params.get('generate_new_version') and not hasattr(node, "metaFields")): if (req.params.get('version_comment', '').strip()=='' or req.params.get('version_comment', '').strip()==' '): create_version_error = True req.setStatus(httpstatus.HTTP_INTERNAL_SERVER_ERROR) ret += req.getTAL("web/edit/modules/files.html", {}, macro="version_error") else: current = node node = node.createNewVersion(user) for attr, value in current.items(): if node.get(attr)!="": # do not overwrite attributes pass else: node.set(attr, value) req.setStatus(httpstatus.HTTP_MOVED_TEMPORARILY) ret += req.getTAL("web/edit/modules/metadata.html", {'url':'?id='+node.id+'&tab=files', 'pid':None}, macro="redirect") if req.params.get("change_file")=="yes" and not create_version_error: # remove old files if uploadfile.filename: if getMimeType(uploadfile.filename)[0] == 'other': return '<h2 style="width:100%; text-align:center ;color:red">file-format is not supported (upload canceled / use attachment)</h2>' for f in node.getFiles(): if f.getType() in node.getSysFiles(): node.removeFile(f) node.set("system.version.comment", '('+t(req, "edit_files_new_version_exchanging_comment")+')\n'+req.params.get('version_comment', '')) if req.params.get("change_file")=="no" and not create_version_error: node.set("system.version.comment", '('+t(req, "edit_files_new_version_adding_comment")+')\n'+req.params.get('version_comment', '')) if req.params.get("change_file") in ["yes", "no"] and not create_version_error: if uploadfile.filename: if getMimeType(uploadfile.filename)[0] == 'other': return '<h2 style="width:100%; text-align:center ;color:red">file-format is not supported (upload canceled / use attachment)</h2>' file = importFile(uploadfile.filename, uploadfile.tempname) # add new file node.addFile(file) logging.getLogger('usertracing').info(user.name+" changed file of node "+node.id+" to "+uploadfile.filename+" ("+uploadfile.tempname+")") attpath = "" for f in node.getFiles(): if f.getMimeType()=="inode/directory": attpath = f.getName() break if req.params.get("change_file")=="attdir" and not create_version_error: # add attachmentdir dirname = req.params.get("inputname") if attpath=="": # add attachment directory attpath = req.params.get("inputname") if not os.path.exists(getImportDir() + "/" + attpath): os.mkdir(getImportDir() + "/" + attpath) node.addFile(tree.FileNode(name=getImportDir() + "/" + attpath, mimetype="inode/directory", type="attachment")) file = importFileIntoDir(getImportDir() + "/" + attpath, uploadfile.tempname) # add new file node.set("system.version.comment", '('+t(req, "edit_files_new_version_attachment_directory_comment")+')\n'+req.params.get('version_comment', '')) pass if req.params.get("change_file")=="attfile" and not create_version_error: # add file as attachment if attpath=="": # no attachment directory existing file = importFile(uploadfile.filename, uploadfile.tempname) # add new file file.mimetype = "inode/file" file.type = "attachment" node.addFile(file) else: # import attachment file into existing attachment directory file = importFileIntoDir(getImportDir() + "/" + attpath, uploadfile.tempname) # add new file node.set("system.version.comment", '('+t(req, "edit_files_new_version_attachment_comment")+')\n'+req.params.get('version_comment', '')) pass elif op == "addthumb": # create new thumbanil from uploaded file uploadfile = req.params.get("updatefile") if uploadfile: thumbname = os.path.join(getImportDir(), hashlib.md5(str(random.random())).hexdigest()[0:8]) + ".thumb" file = importFile(thumbname, uploadfile.tempname) # add new file makeThumbNail(file.retrieveFile(), thumbname) makePresentationFormat(file.retrieveFile(), thumbname + "2") if os.path.exists(file.retrieveFile()): # remove uploaded original os.remove(file.retrieveFile()) for f in node.getFiles(): if f.type in ["thumb", "presentation", "presentati"]: if os.path.exists(f.retrieveFile()): os.remove(f.retrieveFile()) node.removeFile(f) node.addFile(tree.FileNode(name=thumbname, type="thumb", mimetype="image/jpeg")) node.addFile(tree.FileNode(name=thumbname + "2", type="presentation", mimetype="image/jpeg")) logging.getLogger('usertracing').info(user.name + " changed thumbnail of node " + node.id) elif op == "postprocess": if hasattr(node, "event_files_changed"): try: node.event_files_changed() logging.getLogger('usertracing').info(user.name + " postprocesses node " + node.id) except: update_error = True v = {"id": req.params.get("id", "0"), "tab": req.params.get("tab", ""), "node": node, "update_error": update_error, "user": user, "files": filter(lambda x: x.type != 'statistic', node.getFiles()), "statfiles": filter(lambda x: x.type == 'statistic', node.getFiles()), "attfiles": filter(lambda x: x.type == 'attachment', node.getFiles()), "att": [], "nodes": [node], "access": access} for f in v["attfiles"]: # collect all files in attachment directory if f.getMimeType() == "inode/directory": for root, dirs, files in os.walk(f.retrieveFile()): for name in files: af = tree.FileNode(root + "/" + name, "attachmentfile", getMimeType(name)[0]) v["att"].append(af) return req.getTAL("web/edit/modules/files.html", v, macro="edit_files_file")
def getContent(req, ids): ret = "" user = current_user node = q(Node).get(ids[0]) update_error = False update_error_extension = False logg.debug( "%s|web.edit.modules.files.getContend|req.fullpath=%s|req.path=%s|req.params=%s|ids=%s", get_user_id(req), req.fullpath, req.path, req.params, ids) if not node.has_write_access( ) or "files" in current_user.hidden_edit_functions: req.setStatus(httpstatus.HTTP_FORBIDDEN) return req.getTAL("web/edit/edit.html", {}, macro="access_error") if 'data' in req.params: if 'data' in req.params: from contenttypes.container import Container if req.params.get( 'data' ) == 'children': # get formated list of childnodes of selected directory excludeid = str(req.params.get('excludeid', None)) if excludeid: grandchildren = [] for child in node.getChildren(): for grandchild in child.children.all(): if not isinstance(grandchild, Container): grandchildren.append(grandchild) req.writeTAL("web/edit/modules/files.html", { 'children': [ c for c in node.children.all() if str(c.id) != excludeid ], 'grandchildren': grandchildren }, macro="edit_files_popup_children") else: grandchildren = [] for child in node.children.all(): for grandchild in child.children.all(): if not isinstance(grandchild, Container): grandchildren.append(grandchild) req.writeTAL("web/edit/modules/files.html", { 'children': [ c for c in node.getChildren() if str(c.id) != excludeid ], 'grandchildren': grandchildren }, macro="edit_files_popup_children") elif req.params.get('data') == 'grandchildren': grandchildren = [] for child in node.children.all(): if not isinstance(child, Container): for grandchild in child.children.all(): if not isinstance(grandchild, Container): grandchildren.append(grandchild) if len(node.getChildren()) == 0: req.writeTAL("web/edit/modules/files.html", {'grandchildren': []}, macro="edit_files_popup_grandchildren") else: req.writeTAL("web/edit/modules/files.html", {'grandchildren': grandchildren}, macro="edit_files_popup_grandchildren") if req.params.get( 'data') == 'additems': # add selected node as children for childid in req.params.get('items').split(";"): if childid.strip() != "": childnode = q(Node).get(childid.strip()) # don't try to add node as child to itself ! if childnode != node: for p in childnode.parents: if isinstance(p, Container): p.children.remove(childnode) node.children.append(childnode) req.writeTAL("web/edit/modules/files.html", { 'children': node.children, 'node': node }, macro="edit_files_children_list") if req.params.get( 'data') == 'removeitem': # remove selected childnode node try: remnode = q(Node).get(req.params.get('remove')) if len(remnode.parents) == 1: users.getUploadDir(user).children.append(remnode) node.children.remove(remnode) except: # node not found logg.exception( "exception in getContent, node not found? ignore") pass req.writeTAL("web/edit/modules/files.html", { 'children': node.children, 'node': node }, macro="edit_files_children_list") if req.params.get('data') == 'reorder': i = 0 for id in req.params.get('order').split(","): if id != "": n = q(Node).get(id) n.setOrderPos(i) i += 1 if req.params.get('data') == 'translate': req.writeTALstr( '<tal:block i18n:translate="" tal:content="msgstr"/>', {'msgstr': req.params.get('msgstr')}) db.session.commit() return "" if req.params.get("style") == "popup": v = {"basedirs": [q(Home).one(), q(Collections).one()]} id = req.params.get("id", q(Root).one().id) v["script"] = "var currentitem = '%s';\nvar currentfolder = '%s';\nvar node = %s;" % ( id, req.params.get('parent'), id) v["idstr"] = ",".join(ids) v["node"] = node req.writeTAL("web/edit/modules/files.html", v, macro="edit_files_popup_selection") return "" if "operation" in req.params: op = req.params.get("operation") if op == "delete": for key in req.params.keys(): # delete file if key.startswith("del|"): filename = key[4:-2].split("|") for file in node.files: if file.base_name == filename[ 1] and file.filetype == filename[0]: # remove all files in directory if file.mimetype == "inode/directory": for root, dirs, files in os.walk(file.abspath): for name in files: try: os.remove(root + "/" + name) except: logg.exception( "exception while removing file, ignore" ) os.removedirs(file.abspath + "/") node.files.remove(file) try: os.remove(file.abspath) except: pass break break elif key.startswith("delatt|"): for file in node.files: if file.mimetype == "inode/directory": try: os.remove(file.abspath + "/" + key.split("|")[2][:-2]) except: logg.exception( "exception while removing file, ignore") break break elif op == "change": _handle_change(node, req) if req.reply_code != httpstatus.HTTP_OK and req.reply_code != httpstatus.HTTP_MOVED_TEMPORARILY: if req.reply_code is httpstatus.HTTP_NOT_ACCEPTABLE: update_error_extension = True else: update_error = True elif op == "addthumb": # create new thumbanil from uploaded file uploadfile = req.params.get("updatefile") if uploadfile: thumbname = os.path.join( getImportDir(), hashlib.md5(ustr( random.random())).hexdigest()[0:8]) + ".thumb" file = importFile(thumbname, uploadfile.tempname) # add new file make_thumbnail_image(file.abspath, thumbname) make_presentation_image(file.abspath, thumbname + "2") if os.path.exists(file.abspath): # remove uploaded original os.remove(file.abspath) for f in node.files: if f.type in ["thumb", "presentation"]: if os.path.exists(f.abspath): os.remove(f.abspath) node.files.remove(f) node.files.append(File(thumbname, "thumb", "image/jpeg")) node.files.append( File(thumbname + "2", "presentation", "image/jpeg")) logg.info("%s changed thumbnail of node %s", user.login_name, node.id) elif op == "postprocess": try: node.event_files_changed() logg.info("%s postprocesses node %s", user.login_name, node.id) except: update_error = True db.session.commit() v = { "id": req.params.get("id", "0"), "tab": req.params.get("tab", ""), "node": node, "update_error": update_error, "update_error_extension": update_error_extension, "user": user, "files": filter(lambda x: x.type != 'statistic', node.files), "statfiles": filter(lambda x: x.type == 'statistic', node.files), "attfiles": filter(lambda x: x.type == 'attachment', node.files), "att": [], "nodes": [node], } for f in v["attfiles"]: # collect all files in attachment directory if f.mimetype == "inode/directory": for root, dirs, files in os.walk(f.abspath): for name in files: af = File(root + "/" + name, "attachmentfile", getMimeType(name)[0]) v["att"].append(af) return req.getTAL("web/edit/modules/files.html", v, macro="edit_files_file")
def buildStat(collection, period="", fname=None): # period format = yyyy-mm gi = GeoIP() logging.getLogger('editor').info("update stats for node %s and period %s" % (collection.id, period)) statfiles = [] # read data from logfiles def getStatFile(node, timestamp, type, period=period): f = None for file in node.getFiles(): if file.getType() == "statistic": try: if file.getName() == "stat_{}_{}_{}.xml".format( node.id, timestamp, type): if timestamp == str( format_date(now(), "yyyy-mm") ) or timestamp == period: # update current month or given period if os.path.exists(file.retrieveFile()): print 'removing %s' % file.retrieveFile() os.remove(file.retrieveFile()) node.removeFile( file) # remove old file and create new f = None break else: # old month, do nothing print 'old file doing nothing' return None except: return None if not f: # create new file f_name = config.get("paths.tempdir") + "stat_{}_{}_{}.xml".format( node.id, timestamp, type) if os.path.exists(f_name): f = open(f_name, "a") else: # create new file and write header: print 'creating writing headers %s' % f_name f = open(f_name, "w") f.write('<?xml version="1.0" encoding="utf-8" ?>\n') f.write('<nodelist created="' + str(format_date(now(), "yyyy-mm-dd HH:MM:SS")) + '">\n') if f_name not in statfiles: statfiles.append(f_name) return f def writeFooters(): for file in statfiles: f = open(file, "a") f.write("</nodelist>\n") f.close() ids = [] items = collection.getAllChildren() for item in items: ids.append(item.id) data = readLogFiles(period, fname) gi = GeoIP() for timestamp in data.keys(): for id in data[timestamp].keys(): if id in ids: for type in ["frontend", "edit", "download"]: fin = getStatFile(collection, timestamp, type, period) if fin and len(data[timestamp][id][type]) > 0: fin.write('\t<node id="%s">\n' % str(id)) for access in data[timestamp][id][type]: fin.write( '\t\t<access date="%s" time="%s" country="%s" visitor_number="%s" bot="%s"/>\n' % (str(access.getDate()), str(access.getTime()), gi.country_code_by_name(access.getIp()), str(access.get_visitor_number()), str(access.is_google_bot()))) fin.write("\t</node>\n") fin.close() for file in statfiles: f = open(file, "a") f.write("</nodelist>\n") f.close() statfile = importFile(file.split("/")[-1], file) if statfile: statfile.type = "statistic" collection.addFile(statfile) try: os.remove(file) except: pass
def getContent(req, ids): user = current_user node = q(Node).get(ids[0]) if "logo" in current_user.hidden_edit_functions or not node.has_write_access(): req.setStatus(httpstatus.HTTP_FORBIDDEN) return req.getTAL("web/edit/edit.html", {}, macro="access_error") # delete logo file if "action" in req.params and req.params.get('action') == "delete": file = req.params.get('file').split("/")[-1] for f in node.files: if f.abspath.endswith(file): node.files.remove(f) db.session.commit() req.write('ok') return None req.write('not found') return None # add logo file if "addfile" in req.params.keys(): file = req.params.get("updatefile") if file: mimetype = "application/x-download" type = "file" mimetype, type = getMimeType(file.filename.lower()) if mimetype not in ("image/jpeg", "image/gif", "image/png"): # wrong file type (jpeg, jpg, gif, png) req.setStatus(httpstatus.HTTP_INTERNAL_SERVER_ERROR) return req.getTAL("web/edit/modules/logo.html", {}, macro="filetype_error") else: file = importFile(file.filename, file.tempname) node.files.append(file) db.session.commit() # save logo if "logo_save" in req.params.keys(): # save url if req.params.get("logo_link", "") == "": if 'url' in node.attrs: del node.attrs['url'] else: node.set('url', req.params.get("logo_link")) # save filename if req.params.get('logo') == "/img/empty.gif": # remove logo from current node node.set("system.logo", "") logg.info("%s cleared logo for node %s (%s, %s)", user.login_name, node.id, node.name, node.type) else: node.set("system.logo", req.params.get("logo").split("/")[-1]) logg.info("%s set logo for node %s (%s, %s) to %s", user.login_name, node.id, node.name, node.type, node.get("system.logo")) db.session.commit() logofiles = [] for f in node.files: if f.filetype == "image": logofiles.append(splitpath(f.abspath)) v = { "id": req.params.get("id", "0"), "tab": req.params.get("tab", ""), "node": node, "logofiles": logofiles, "logo": node.getLogoPath(), "language": lang(req), "t": translation_t, "csrf": req.csrf_token.current_token } return req.getTAL("web/edit/modules/logo.html", v, macro="edit_logo")
def getContent(req, ids): ret = "" user = users.getUserFromRequest(req) node = tree.getNode(ids[0]) update_error = False access = acl.AccessData(req) msg = "%s|web.edit.modules.files.getContend|req.fullpath=%r|req.path=%r|req.params=%r|ids=%r" % ( get_user_id(req), req.fullpath, req.path, req.params, ids) log.debug(msg) if not access.hasWriteAccess(node) or "files" in users.getHideMenusForUser( user): req.setStatus(httpstatus.HTTP_FORBIDDEN) return req.getTAL("web/edit/edit.html", {}, macro="access_error") if 'data' in req.params: if req.params.get( 'data' ) == 'children': # get formated list of childnodes of selected directory req.writeTAL("web/edit/modules/files.html", {'children': node.getChildren()}, macro="edit_files_popup_children") if req.params.get( 'data') == 'additems': # add selected node as children for childid in req.params.get('items').split(";"): if childid.strip() != "": childnode = tree.getNode(childid.strip()) for p in childnode.getParents(): p.removeChild(childnode) node.addChild(childnode) req.writeTAL("web/edit/modules/files.html", { 'children': node.getChildren(), 'node': node }, macro="edit_files_children_list") if req.params.get( 'data') == 'removeitem': # remove selected childnode node try: remnode = tree.getNode(req.params.get('remove')) if len(remnode.getParents()) == 1: users.getUploadDir(user).addChild(remnode) node.removeChild(remnode) except: # node not found pass req.writeTAL("web/edit/modules/files.html", { 'children': node.getChildren(), 'node': node }, macro="edit_files_children_list") if req.params.get('data') == 'reorder': i = 0 for id in req.params.get('order').split(","): if id != "": n = tree.getNode(id) n.setOrderPos(i) i += 1 if req.params.get('data') == 'translate': req.writeTALstr( '<tal:block i18n:translate="" tal:content="msgstr"/>', {'msgstr': req.params.get('msgstr')}) return "" if req.params.get("style") == "popup": v = {"basedirs": [tree.getRoot('home'), tree.getRoot('collections')]} id = req.params.get("id", tree.getRoot().id) v["script"] = "var currentitem = '%s';\nvar currentfolder = '%s';\nvar node = %s;" % ( id, req.params.get('parent'), id) v["idstr"] = ",".join(ids) v["node"] = node req.writeTAL("web/edit/modules/files.html", v, macro="edit_files_popup_selection") return "" if "operation" in req.params: op = req.params.get("operation") if op == "delete": for key in req.params.keys(): # delete file if key.startswith("del|"): filename = key[4:-2].split("|") for file in node.getFiles(): if file.getName( ) == filename[1] and file.type == filename[0]: # remove all files in directory if file.getMimeType() == "inode/directory": for root, dirs, files in os.walk( file.retrieveFile()): for name in files: try: os.remove(root + "/" + name) except: pass os.removedirs(file.retrieveFile() + "/") if len([ f for f in node.getFiles() if f.getName() == filename[1] and f.type == filename[0] ]) > 1: # remove single file from database if there are duplicates node.removeFile(file, single=True) else: # remove single file node.removeFile(file) try: os.remove(file.retrieveFile()) except: pass break break elif key.startswith("delatt|"): for file in node.getFiles(): if file.getMimeType() == "inode/directory": try: os.remove(file.retrieveFile() + "/" + key.split("|")[2][:-2]) except: pass break break elif op == "change": uploadfile = req.params.get("updatefile") if uploadfile: create_version_error = False # Create new version when change file if (req.params.get('generate_new_version') and not hasattr(node, "metaFields")): if (req.params.get('version_comment', '').strip() == '' or req.params.get('version_comment', '').strip() == ' '): create_version_error = True req.setStatus(httpstatus.HTTP_INTERNAL_SERVER_ERROR) ret += req.getTAL("web/edit/modules/files.html", {}, macro="version_error") else: current = node node = node.createNewVersion(user) for attr, value in current.items(): if node.get( attr) != "": # do not overwrite attributes pass else: node.set(attr, value) req.setStatus(httpstatus.HTTP_MOVED_TEMPORARILY) ret += req.getTAL("web/edit/modules/metadata.html", { 'url': '?id=' + node.id + '&tab=files', 'pid': None }, macro="redirect") if req.params.get( "change_file" ) == "yes" and not create_version_error: # remove old files for f in node.getFiles(): if f.getType() in node.getSysFiles(): node.removeFile(f) node.set( "system.version.comment", '(' + t(req, "edit_files_new_version_exchanging_comment") + ')\n' + req.params.get('version_comment', '')) if req.params.get( "change_file") == "no" and not create_version_error: node.set( "system.version.comment", '(' + t(req, "edit_files_new_version_adding_comment") + ')\n' + req.params.get('version_comment', '')) if req.params.get("change_file") in [ "yes", "no" ] and not create_version_error: file = importFile(uploadfile.filename, uploadfile.tempname) # add new file node.addFile(file) logging.getLogger('usertracing').info( user.name + " changed file of node " + node.id + " to " + uploadfile.filename + " (" + uploadfile.tempname + ")") attpath = "" for f in node.getFiles(): if f.getMimeType() == "inode/directory": attpath = f.getName() break if req.params.get( "change_file" ) == "attdir" and not create_version_error: # add attachmentdir dirname = req.params.get("inputname") if attpath == "": # add attachment directory attpath = req.params.get("inputname") if not os.path.exists(getImportDir() + "/" + attpath): os.mkdir(getImportDir() + "/" + attpath) node.addFile( tree.FileNode(name=getImportDir() + "/" + attpath, mimetype="inode/directory", type="attachment")) file = importFileIntoDir( getImportDir() + "/" + attpath, uploadfile.tempname) # add new file node.set( "system.version.comment", '(' + t( req, "edit_files_new_version_attachment_directory_comment" ) + ')\n' + req.params.get('version_comment', '')) pass if req.params.get( "change_file" ) == "attfile" and not create_version_error: # add file as attachment if attpath == "": # no attachment directory existing file = importFile(uploadfile.filename, uploadfile.tempname) # add new file file.mimetype = "inode/file" file.type = "attachment" node.addFile(file) else: # import attachment file into existing attachment directory file = importFileIntoDir( getImportDir() + "/" + attpath, uploadfile.tempname) # add new file node.set( "system.version.comment", '(' + t(req, "edit_files_new_version_attachment_comment") + ')\n' + req.params.get('version_comment', '')) pass elif op == "addthumb": # create new thumbanil from uploaded file uploadfile = req.params.get("updatefile") if uploadfile: thumbname = os.path.join( getImportDir(), hashlib.md5(str( random.random())).hexdigest()[0:8]) + ".thumb" file = importFile(thumbname, uploadfile.tempname) # add new file makeThumbNail(file.retrieveFile(), thumbname) makePresentationFormat(file.retrieveFile(), thumbname + "2") if os.path.exists( file.retrieveFile()): # remove uploaded original os.remove(file.retrieveFile()) for f in node.getFiles(): if f.type in ["thumb", "presentation", "presentati"]: if os.path.exists(f.retrieveFile()): os.remove(f.retrieveFile()) node.removeFile(f) node.addFile( tree.FileNode(name=thumbname, type="thumb", mimetype="image/jpeg")) node.addFile( tree.FileNode(name=thumbname + "2", type="presentation", mimetype="image/jpeg")) logging.getLogger('usertracing').info( user.name + " changed thumbnail of node " + node.id) elif op == "postprocess": if hasattr(node, "event_files_changed"): try: node.event_files_changed() logging.getLogger('usertracing').info( user.name + " postprocesses node " + node.id) except: update_error = True v = { "id": req.params.get("id", "0"), "tab": req.params.get("tab", ""), "node": node, "update_error": update_error, "user": user, "files": filter(lambda x: x.type != 'statistic', node.getFiles()), "statfiles": filter(lambda x: x.type == 'statistic', node.getFiles()), "attfiles": filter(lambda x: x.type == 'attachment', node.getFiles()), "att": [], "nodes": [node], "access": access } for f in v["attfiles"]: # collect all files in attachment directory if f.getMimeType() == "inode/directory": for root, dirs, files in os.walk(f.retrieveFile()): for name in files: af = tree.FileNode(root + "/" + name, "attachmentfile", getMimeType(name)[0]) v["att"].append(af) return req.getTAL("web/edit/modules/files.html", v, macro="edit_files_file")
def upload_for_html(req): user = current_user datatype = req.params.get("datatype", "image") id = req.params.get("id") node = q(Node).get(id) if not (node.has_read_access() and node.has_write_access() and node.has_data_access()): return 403 for key in req.params.keys(): if key.startswith("delete_"): filename = key[7:-2] # XXX: dead code? for file in node.files: if file.base_name == filename: node.files.remove(file) db.session.commit() if "file" in req.params.keys(): # file # file upload via (possibly disabled) upload form in custom image # browser file = req.params["file"] del req.params["file"] if hasattr(file, "filesize") and file.filesize > 0: try: logg.info("file %s (temp %s) uploaded by user %s (%s)", file.filename, file.tempname, user.login_name, user.id) nodefile = importFile(file.filename, file.tempname) node.files.append(nodefile) db.session.commit() req.request["Location"] = req.makeLink( "nodefile_browser/%s/" % id, {}) except EncryptionException: req.request["Location"] = req.makeLink("content", { "id": id, "tab": "tab_editor", "error": "EncryptionError_" + datatype[:datatype.find("/")]}) except: logg.exception("error during upload") req.request["Location"] = req.makeLink("content", { "id": id, "tab": "tab_editor", "error": "PostprocessingError_" + datatype[:datatype.find("/")]}) return send_nodefile_tal(req) if "upload" in req.params.keys(): # NewFile # file upload via CKeditor Image Properties / Upload tab file = req.params["upload"] del req.params["upload"] if hasattr(file, "filesize") and file.filesize > 0: try: logg.info("%s upload via ckeditor %s (%s)", user.login_name , file.filename, file.tempname) nodefile = importFile(file.filename, file.tempname) node.files.append(nodefile) db.session.commit() except EncryptionException: req.request["Location"] = req.makeLink("content", { "id": id, "tab": "tab_editor", "error": "EncryptionError_" + datatype[:datatype.find("/")]}) except: logg.exception("error during upload") req.request["Location"] = req.makeLink("content", { "id": id, "tab": "tab_editor", "error": "PostprocessingError_" + datatype[:datatype.find("/")]}) url = '/file/' + id + '/' + file.tempname.split('/')[-1] res = """<script type="text/javascript"> // Helper function to get parameters from the query string. function getUrlParam(paramName) { var reParam = new RegExp('(?:[\?&]|&)' + paramName + '=([^&]+)', 'i') ; var match = window.location.search.match(reParam) ; return (match && match.length > 1) ? match[1] : '' ; } funcNum = getUrlParam('CKEditorFuncNum'); window.parent.CKEDITOR.tools.callFunction(funcNum, "%(fileUrl)s","%(customMsg)s"); </script>;""" % { 'fileUrl': url.replace('"', '\\"'), 'customMsg': (t(lang(req), "edit_fckeditor_cfm_uploadsuccess")), } return res return send_nodefile_tal(req)
def show_workflow_node(self, node, req): error = "" for key in req.params.keys(): if key.startswith("delete_"): filename = key[7:-2] all = 0 for file in node.getFiles(): if file.getName() == filename: if file.type in ['document', 'image']: # original -> delete all all = 1 node.removeFile(file) if all == 1: # delete all files for file in node.getFiles(): node.removeFile(file) if "file" in req.params: file = req.params["file"] if not file: error = t(req, "workflowstep_file_not_uploaded") else: del req.params["file"] fileExtension = os.path.splitext(file.filename)[1][1:].strip().lower() if fileExtension in self.get("limit").lower().split(";") or self.get("limit").strip() in ['*', '']: orig_filename = file.filename if hasattr(file, "filename") and file.filename: file = fileutils.importFile(file.filename, file.tempname) node.addFile(file) node.setName(orig_filename) if hasattr(node, "event_files_changed"): try: node.event_files_changed() except OperationException as ex: error = ex.value else: error = t(req, "WorkflowStep_InvalidFileType") if "gotrue" in req.params: if hasattr(node, "event_files_changed"): node.event_files_changed() if len(node.getFiles()) > 0: return self.forwardAndShow(node, True, req) elif not error: error = t(req, "no_file_transferred") if "gofalse" in req.params: if hasattr(node, "event_files_changed"): node.event_files_changed() # if len(node.getFiles())>0: return self.forwardAndShow(node, False, req) # else: # error = t(req, "no_file_transferred") filelist = mkfilelist(node, 1, request=req) filelistshort = mkfilelistshort(node, 1, request=req) return req.getTAL("workflow/upload.html", {"obj": node.id, "id": self.id, "prefix": self.get("prefix"), "suffix": self.get("suffix"), "limit": self.get("limit"), "filelist": filelist, "filelistshort": filelistshort, "node": node, "buttons": self.tableRowButtons(node), "singlefile": self.get('singleobj'), "error": error, "pretext": self.getPreText(lang(req)), "posttext": self.getPostText(lang(req))}, macro="workflow_upload")
def getContent(req, ids): user = users.getUserFromRequest(req) node = tree.getNode(ids[0]) access = acl.AccessData(req) if "logo" in users.getHideMenusForUser(user) or not access.hasWriteAccess(node): req.setStatus(httpstatus.HTTP_FORBIDDEN) return req.getTAL("web/edit/edit.html", {}, macro="access_error") # delete logo file if "action" in req.params and req.params.get('action') == "delete": file = req.params.get('file').split("/")[-1] for f in node.getFiles(): if f.retrieveFile().endswith(file): node.removeFile(f) req.write('ok') return None req.write('not found') return None # add logo file if "addfile" in req.params.keys(): file = req.params.get("updatefile") if file: mimetype = "application/x-download" type = "file" mimetype, type = getMimeType(file.filename.lower()) if mimetype not in ("image/jpeg", "image/gif", "image/png"): # wrong file type (jpeg, jpg, gif, png) req.setStatus(httpstatus.HTTP_INTERNAL_SERVER_ERROR) return req.getTAL("web/edit/modules/logo.html", {}, macro="filetype_error") else: file = importFile(file.filename, file.tempname) node.addFile(file) # save logo if "logo_save" in req.params.keys(): # save url if req.params.get("logo_link", "") == "": node.removeAttribute("url") else: node.set('url', req.params.get("logo_link")) # save filename if req.params.get('logo') == "/img/empty.gif": # remove logo from current node node.set("system.logo", "") msg = "%s cleared logo for node %r (%r, %r)" % (user.getName(), node.id, node.name, node.type) logger.info(msg) logger_e.info(msg) else: node.set("system.logo", req.params.get("logo").split("/")[-1]) msg = "%s set logo for node %r (%r, %r) to %r" % (user.getName(), node.id, node.name, node.type, node.get("system.logo")) logger.info(msg) logger_e.info(msg) logofiles = [] for f in node.getFiles(): if f.getType() == "image": logofiles.append(splitpath(f.retrieveFile())) v = { "id": req.params.get("id", "0"), "tab": req.params.get("tab", ""), "node": node, "logofiles": logofiles, "logo": node.getLogoPath(), "language": lang(req), "t": translation_t } return req.getTAL("web/edit/modules/logo.html", v, macro="edit_logo")
def show_workflow_node(self, node, req): error = "" for key in req.params.keys(): if key.startswith("delete_"): filename = key[7:-2] all = 0 for file in node.getFiles(): if file.getName() == filename: if file.type in ['document', 'image']: # original -> delete all all = 1 node.removeFile(file) if all == 1: # delete all files for file in node.getFiles(): node.removeFile(file) if "file" in req.params: file = req.params["file"] if not file: error = t(req, "workflowstep_file_not_uploaded") else: del req.params["file"] fileExtension = os.path.splitext( file.filename)[1][1:].strip().lower() if fileExtension in self.get("limit").lower().split( ";") or self.get("limit").strip() in ['*', '']: orig_filename = file.filename if hasattr(file, "filename") and file.filename: file = fileutils.importFile(file.filename, file.tempname) node.addFile(file) node.setName(orig_filename) if hasattr(node, "event_files_changed"): try: node.event_files_changed() except OperationException as ex: error = ex.value else: error = t(req, "WorkflowStep_InvalidFileType") if "gotrue" in req.params: if hasattr(node, "event_files_changed"): node.event_files_changed() if len(node.getFiles()) > 0: return self.forwardAndShow(node, True, req) elif not error: error = t(req, "no_file_transferred") if "gofalse" in req.params: if hasattr(node, "event_files_changed"): node.event_files_changed() # if len(node.getFiles())>0: return self.forwardAndShow(node, False, req) # else: # error = t(req, "no_file_transferred") filelist = mkfilelist(node, 1, request=req) filelistshort = mkfilelistshort(node, 1, request=req) return req.getTAL("workflow/upload.html", { "obj": node.id, "id": self.id, "prefix": self.get("prefix"), "suffix": self.get("suffix"), "limit": self.get("limit"), "filelist": filelist, "filelistshort": filelistshort, "node": node, "buttons": self.tableRowButtons(node), "singlefile": self.get('singleobj'), "error": error, "pretext": self.getPreText(lang(req)), "posttext": self.getPostText(lang(req)) }, macro="workflow_upload")
def getContent(req, ids): ret = "" user = current_user node = q(Node).get(ids[0]) update_error = False update_error_extension = False logg.debug("%s|web.edit.modules.files.getContend|req.fullpath=%s|req.path=%s|req.params=%s|ids=%s", get_user_id(req), req.fullpath, req.path, req.params, ids) if not node.has_write_access() or "files" in current_user.hidden_edit_functions: req.setStatus(httpstatus.HTTP_FORBIDDEN) return req.getTAL("web/edit/edit.html", {}, macro="access_error") if 'data' in req.params: if 'data' in req.params: from contenttypes.container import Container if req.params.get('data') == 'children': # get formated list of childnodes of selected directory excludeid = str(req.params.get('excludeid', None)) if excludeid: grandchildren = [] for child in node.getChildren(): for grandchild in child.children.all(): if not isinstance(grandchild, Container): grandchildren.append(grandchild) req.writeTAL("web/edit/modules/files.html", {'children': [c for c in node.children.all() if str(c.id) != excludeid], 'grandchildren': grandchildren, "csrf": req.csrf_token.current_token}, macro="edit_files_popup_children") else: grandchildren = [] for child in node.children.all(): for grandchild in child.children.all(): if not isinstance(grandchild, Container): grandchildren.append(grandchild) req.writeTAL("web/edit/modules/files.html", {'children': [c for c in node.getChildren() if str(c.id) != excludeid], 'grandchildren': grandchildren, "csrf": req.csrf_token.current_token}, macro="edit_files_popup_children") elif req.params.get('data') =='grandchildren': grandchildren = [] for child in node.children.all(): if not isinstance(child, Container): for grandchild in child.children.all(): if not isinstance(grandchild, Container): grandchildren.append(grandchild) if len(node.getChildren())==0: req.writeTAL("web/edit/modules/files.html", {'grandchildren': [], "csrf": req.csrf_token.current_token}, macro="edit_files_popup_grandchildren") else: req.writeTAL("web/edit/modules/files.html", {'grandchildren': grandchildren, "csrf": req.csrf_token.current_token}, macro="edit_files_popup_grandchildren") if req.params.get('data') == 'additems': # add selected node as children for childid in req.params.get('items').split(";"): if childid.strip() != "": childnode = q(Node).get(childid.strip()) # don't try to add node as child to itself ! if childnode != node: for p in childnode.parents: if isinstance(p, Container): p.children.remove(childnode) node.children.append(childnode) req.writeTAL("web/edit/modules/files.html", {'children': node.children, 'node': node, "csrf": req.csrf_token.current_token}, macro="edit_files_children_list") if req.params.get('data') == 'removeitem': # remove selected childnode node with suppress(Exception): remnode = q(Node).get(req.params.get('remove')) if len(remnode.parents) == 1: users.getUploadDir(user).children.append(remnode) node.children.remove(remnode) req.writeTAL("web/edit/modules/files.html", {'children': node.children, 'node': node, "csrf": req.csrf_token.current_token}, macro="edit_files_children_list") if req.params.get('data') == 'reorder': i = 0 for id in req.params.get('order').split(","): if id != "": n = q(Node).get(id) n.setOrderPos(i) i += 1 if req.params.get('data') == 'translate': req.writeTALstr('<tal:block i18n:translate="" tal:content="msgstr"/>', {'msgstr': req.params.get('msgstr'), "csrf": req.csrf_token.current_token}) db.session.commit() return "" if req.params.get("style") == "popup": v = {"basedirs": [q(Home).one(), q(Collections).one()]} id = req.params.get("id", q(Root).one().id) v["script"] = "var currentitem = '%s';\nvar currentfolder = '%s';\nvar node = %s;" % (id, req.params.get('parent'), id) v["idstr"] = ",".join(ids) v["node"] = node v["csrf"] = req.csrf_token.current_token req.writeTAL("web/edit/modules/files.html", v, macro="edit_files_popup_selection") return "" if "operation" in req.params: op = req.params.get("operation") if op == "delete": for key in req.params.keys(): # delete file if key.startswith("del|"): filename = key[4:-2].split("|") for file in node.files: if file.base_name == filename[1] and file.filetype == filename[0]: # remove all files in directory if file.mimetype == "inode/directory": for root, dirs, files in os.walk(file.abspath): for name in files: try: os.remove(root + "/" + name) except: logg.exception("exception while removing file, ignore") os.removedirs(file.abspath + "/") node.files.remove(file) with suppress(Exception, warn=False): os.remove(file.abspath) break break elif key.startswith("delatt|"): for file in node.files: if file.mimetype == "inode/directory": try: os.remove(file.abspath + "/" + key.split("|")[2][:-2]) except: logg.exception("exception while removing file, ignore") break break elif op == "change": _handle_change(node, req) if req.reply_code != httpstatus.HTTP_OK and req.reply_code != httpstatus.HTTP_MOVED_TEMPORARILY: if req.reply_code is httpstatus.HTTP_NOT_ACCEPTABLE: update_error_extension = True else: update_error = True elif op == "addthumb": # create new thumbanil from uploaded file uploadfile = req.params.get("updatefile") if uploadfile: thumbname = os.path.join(getImportDir(), hashlib.md5(ustr(random.random())).hexdigest()[0:8]) + ".thumb" file = importFile(thumbname, uploadfile.tempname) # add new file make_thumbnail_image(file.abspath, thumbname) make_presentation_image(file.abspath, thumbname + "2") if os.path.exists(file.abspath): # remove uploaded original os.remove(file.abspath) for f in node.files: if f.type in ["thumb", "presentation"]: if os.path.exists(f.abspath): os.remove(f.abspath) node.files.remove(f) node.files.append(File(thumbname, "thumb", "image/jpeg")) node.files.append(File(thumbname + "2", "presentation", "image/jpeg")) logg.info("%s changed thumbnail of node %s", user.login_name, node.id) elif op == "postprocess": try: node.event_files_changed() logg.info("%s postprocesses node %s", user.login_name, node.id) except: update_error = True db.session.commit() v = {"id": req.params.get("id", "0"), "tab": req.params.get("tab", ""), "node": node, "update_error": update_error, "update_error_extension": update_error_extension, "user": user, "files": filter(lambda x: x.type != 'statistic', node.files), "statfiles": filter(lambda x: x.type == 'statistic', node.files), "attfiles": filter(lambda x: x.type == 'attachment', node.files), "att": [], "nodes": [node], "csrf": req.csrf_token.current_token } for f in v["attfiles"]: # collect all files in attachment directory if f.mimetype == "inode/directory": for root, dirs, files in os.walk(f.abspath): for name in files: af = File(root + "/" + name, "attachmentfile", getMimeType(name)[0]) v["att"].append(af) return req.getTAL("web/edit/modules/files.html", v, macro="edit_files_file")
def upload_for_html(req): user = users.getUserFromRequest(req) datatype = req.params.get("datatype", "image") id = req.params.get("id") node = tree.getNode(id) access = AccessData(req) if not (access.hasAccess(node, 'read') and access.hasAccess(node, 'write') and access.hasAccess(node, 'data')): return 403 for key in req.params.keys(): if key.startswith("delete_"): filename = key[7:-2] for file in n.getFiles(): if file.getName() == filename: n.removeFile(file) if "file" in req.params.keys(): # file # file upload via (possibly disabled) upload form in custom image # browser file = req.params["file"] del req.params["file"] if hasattr(file, "filesize") and file.filesize > 0: try: logger.info(user.name + " upload " + file.filename + " (" + file.tempname + ")") nodefile = importFile(file.filename, file.tempname) node.addFile(nodefile) req.request["Location"] = req.makeLink( "nodefile_browser/%s/" % id, {}) except EncryptionException: req.request["Location"] = req.makeLink( "content", { "id": id, "tab": "tab_editor", "error": "EncryptionError_" + datatype[:datatype.find("/")] }) except: logException("error during upload") req.request["Location"] = req.makeLink( "content", { "id": id, "tab": "tab_editor", "error": "PostprocessingError_" + datatype[:datatype.find("/")] }) return send_nodefile_tal(req) if "upload" in req.params.keys(): # NewFile # file upload via CKeditor Image Properties / Upload tab file = req.params["upload"] del req.params["upload"] if hasattr(file, "filesize") and file.filesize > 0: try: logger.info(user.name + " upload via ckeditor " + file.filename + " (" + file.tempname + ")") nodefile = importFile(file.filename, file.tempname) node.addFile(nodefile) except EncryptionException: req.request["Location"] = req.makeLink( "content", { "id": id, "tab": "tab_editor", "error": "EncryptionError_" + datatype[:datatype.find("/")] }) except: logException("error during upload") req.request["Location"] = req.makeLink( "content", { "id": id, "tab": "tab_editor", "error": "PostprocessingError_" + datatype[:datatype.find("/")] }) url = '/file/' + id + '/' + file.tempname.split('/')[-1] res = """<script type="text/javascript"> // Helper function to get parameters from the query string. function getUrlParam(paramName) { var reParam = new RegExp('(?:[\?&]|&)' + paramName + '=([^&]+)', 'i') ; var match = window.location.search.match(reParam) ; return (match && match.length > 1) ? match[1] : '' ; } funcNum = getUrlParam('CKEditorFuncNum'); window.parent.CKEDITOR.tools.callFunction(funcNum, "%(fileUrl)s","%(customMsg)s"); </script>;""" % { 'fileUrl': url.replace('"', '\\"'), 'customMsg': (t(lang(req), "edit_fckeditor_cfm_uploadsuccess")), } return res return send_nodefile_tal(req)
def getContent(req, ids): user = current_user node = q(Node).get(ids[0]) if "logo" in current_user.hidden_edit_functions or not node.has_write_access( ): req.setStatus(httpstatus.HTTP_FORBIDDEN) return req.getTAL("web/edit/edit.html", {}, macro="access_error") # delete logo file if "action" in req.params and req.params.get('action') == "delete": file = req.params.get('file').split("/")[-1] for f in node.files: if f.abspath.endswith(file): node.files.remove(f) db.session.commit() req.write('ok') return None req.write('not found') return None # add logo file if "addfile" in req.params.keys(): file = req.params.get("updatefile") if file: mimetype = "application/x-download" type = "file" mimetype, type = getMimeType(file.filename.lower()) if mimetype not in ("image/jpeg", "image/gif", "image/png"): # wrong file type (jpeg, jpg, gif, png) req.setStatus(httpstatus.HTTP_INTERNAL_SERVER_ERROR) return req.getTAL("web/edit/modules/logo.html", {}, macro="filetype_error") else: file = importFile(file.filename, file.tempname) node.files.append(file) db.session.commit() # save logo if "logo_save" in req.params.keys(): # save url if req.params.get("logo_link", "") == "": if 'url' in node.attrs: del node.attrs['url'] else: node.set('url', req.params.get("logo_link")) # save filename if req.params.get('logo') == "/img/empty.gif": # remove logo from current node node.set("system.logo", "") logg.info("%s cleared logo for node %s (%s, %s)", user.login_name, node.id, node.name, node.type) else: node.set("system.logo", req.params.get("logo").split("/")[-1]) logg.info("%s set logo for node %s (%s, %s) to %s", user.login_name, node.id, node.name, node.type, node.get("system.logo")) db.session.commit() logofiles = [] for f in node.files: if f.filetype == "image": logofiles.append(splitpath(f.abspath)) v = { "id": req.params.get("id", "0"), "tab": req.params.get("tab", ""), "node": node, "logofiles": logofiles, "logo": node.getLogoPath(), "language": lang(req), "t": translation_t } return req.getTAL("web/edit/modules/logo.html", v, macro="edit_logo")
def buildStat(collection, period="", fname=None): # period format = yyyy-mm gi = GeoIP() logging.getLogger('editor').info("update stats for node %s and period %s" % (collection.id, period)) statfiles = [] # read data from logfiles def getStatFile(node, timestamp, type, period=period): f = None for file in node.getFiles(): if file.getType() == "statistic": try: if file.getName() == "stat_{}_{}_{}.xml".format(node.id, timestamp, type): if timestamp == str(format_date(now(), "yyyy-mm")) or timestamp == period: # update current month or given period if os.path.exists(file.retrieveFile()): print 'removing %s' % file.retrieveFile() os.remove(file.retrieveFile()) node.removeFile(file) # remove old file and create new f = None break else: # old month, do nothing print 'old file doing nothing' return None except: return None if not f: # create new file f_name = config.get("paths.tempdir") + "stat_{}_{}_{}.xml".format(node.id, timestamp, type) if os.path.exists(f_name): f = open(f_name, "a") else: # create new file and write header: print 'creating writing headers %s' % f_name f = open(f_name, "w") f.write('<?xml version="1.0" encoding="utf-8" ?>\n') f.write('<nodelist created="' + str(format_date(now(), "yyyy-mm-dd HH:MM:SS")) + '">\n') if f_name not in statfiles: statfiles.append(f_name) return f def writeFooters(): for file in statfiles: f = open(file, "a") f.write("</nodelist>\n") f.close() ids = [] items = collection.getAllChildren() for item in items: ids.append(item.id) data = readLogFiles(period, fname) gi = GeoIP() for timestamp in data.keys(): for id in data[timestamp].keys(): if id in ids: for type in ["frontend", "edit", "download"]: fin = getStatFile(collection, timestamp, type, period) if fin and len(data[timestamp][id][type]) > 0: fin.write('\t<node id="%s">\n' % str(id)) for access in data[timestamp][id][type]: fin.write('\t\t<access date="%s" time="%s" country="%s" visitor_number="%s" bot="%s"/>\n' % (str(access.getDate()), str(access.getTime()), gi.country_code_by_name(access.getIp()), str(access.get_visitor_number()), str(access.is_google_bot()))) fin.write("\t</node>\n") fin.close() for file in statfiles: f = open(file, "a") f.write("</nodelist>\n") f.close() statfile = importFile(file.split("/")[-1], file) if statfile: statfile.type = "statistic" collection.addFile(statfile) try: os.remove(file) except: pass