// // Handlers.swift // Swifter // Copyright (c) 2014 Damian KoĊ‚akowski. All rights reserved. // import Foundation class HttpHandlers { class func directory(dir: String) -> ( HttpRequest -> HttpResponse ) { return { request in if let localPath = request.capturedUrlGroups.first { let filesPath = dir.stringByExpandingTildeInPath.stringByAppendingPathComponent(localPath) if let fileBody = NSData(contentsOfFile: filesPath) { return HttpResponse.RAW(200, fileBody) } } return HttpResponse.NotFound } } class func directoryBrowser(dir: String) -> ( HttpRequest -> HttpResponse ) { return { request in if let pathFromUrl = request.capturedUrlGroups.first { let filePath = dir.stringByExpandingTildeInPath.stringByAppendingPathComponent(pathFromUrl) let fileManager = NSFileManager.defaultManager() var isDir: ObjCBool = false; if ( fileManager.fileExistsAtPath(filePath, isDirectory: &isDir) ) { if ( isDir ) { if let files = fileManager.contentsOfDirectoryAtPath(filePath, error: nil) { var response = "

\(filePath)


" response += join("", map(files, { ""})) response += "
\($0)
" return HttpResponse.OK(.HTML(response)) } } else { if let fileBody = NSData(contentsOfFile: filePath) { return HttpResponse.RAW(200, fileBody) } } } } return HttpResponse.NotFound } } }