def put(self, dir_name): self.log.logDebug('put called', dir_name) dir_name = PathUtils.sanitizeFilename(dir_name) if not request.form or (not request.form['doc_path'] and not request.form['root_dir']): self.log.logWarn('directories put was called with invalid data.') abort(400, 'Invalid Parameters') newPath = PathUtils.sanitizePathList( [basePath, request.form['doc_path'], dir_name]) self.log.logDebug('saving path', newPath) Path(newPath).mkdir() self.log.logDebug( 'mkdir:', {'path': PathUtils.sanitizePathString(newPath, True)}) return {'path': PathUtils.sanitizePathString(newPath, True)}
def getResponseObject(self, file): fstats = os.stat(file.name) return { 'path': PathUtils.sanitizePathString(os.path.dirname(file.name), True), 'name': os.path.basename(file.name), 'last_edited': fstats[ST_MTIME] }
def get(self): # ATTENTION: Tree debug is quite spammy and deactivated by default. If you want to debug it, remove the silenced=True parameter log = Logger('directories.get', True) directoryStructure = {'name': '', 'dirs': [], 'files': []} treeBase = request.args.get('base') log.logDebug('========== GET TREE =========') log.logDebug('os.getcwd:', os.getcwd()) log.logDebug('base:', treeBase) log.logDebug('==========') if (treeBase and not os.path.exists( PathUtils.sanitizePathList([basePath, treeBase]))): abort(400, 'given treeBase is not part of the dir structure') for (dirpath, dirnames, filenames) in os.walk(basePath): filePath = PathUtils.sanitizePathString(dirpath, True) path = filePath.split('/') log.logDebug('starting with', path, dirpath) if (metaSubPath in dirnames): log.logDebug( 'removing metaSubPath from dirnames to exclude them from walking' ) dirnames.remove(metaSubPath) # do not visit metaSubPaths if (len(path) == 0 or metaSubPath in path): log.logDebug('skipping path', path) continue # skip meta paths if (treeBase and len(path) > 1 and treeBase not in path): log.logDebug('skipping path since its not part of treeBase', path) continue if len(path) == 1 and path[0] == '': currDir = directoryStructure log.logDebug( 'Got tree root, using directory Structure directly') else: log.logDebug('walking for', dirpath, path) currDir = self.walkToDir(directoryStructure, path) if currDir == None: log.logWarn('Could not find dir for path, skipping', dirpath) continue log.logDebug('crawling content', currDir, filePath) self.crawlDirContent(dirnames, filenames, currDir, filePath) if request.args.get('root_only'): break result = directoryStructure if (treeBase): result = next( (x for x in directoryStructure['dirs'] if x['name'] == treeBase), result) return json.dumps(result)
def test_sanitizePathString(self): testData = { "this/is/a/path": "this/is/a/path", "this/is/a/pathWithDouble/Slashes": "this/is/a///pathWithDouble//Slashes", "path/With/LeadingAndEnding/Slash": "/path/With/LeadingAndEnding/Slash/", "windowsstyled/path/withBackslashes": "\\windowsstyled\\path\\withBackslashes", "": "" } for result, testData in testData.items(): self.assertEqual(PathUtils.sanitizePathString(testData), result)