// // Handlers.swift // Swifter // Copyright (c) 2014 Damian KoĊ‚akowski. All rights reserved. // import Foundation public class HttpHandlers { public 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 } } public 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 ) { do { let files = try fileManager.contentsOfDirectoryAtPath(filePath) var response = "

\(filePath)


" response += files.map({ ""}).joinWithSeparator("") response += "
\($0)
" return HttpResponse.OK(.HTML(response)) } catch { return HttpResponse.NotFound } } else { if let fileBody = NSData(contentsOfFile: filePath) { return HttpResponse.RAW(200, fileBody) } } } } return HttpResponse.NotFound } } } private extension String { var stringByExpandingTildeInPath: String { return (self as NSString).stringByExpandingTildeInPath } func stringByAppendingPathComponent(str: String) -> String { return (self as NSString).stringByAppendingPathComponent(str) } }