0001    import Foundation
0002    
0003    struct StaticFileMiddleware
Application.swift:32
        use(middleware: StaticFileMiddleware())
: Middleware { 0004 func handle(request: Request, response: Response, next: (() -> ())) { 0005 //check in file system 0006 let filePath = "Public" + request.path 0007 0008 let fileManager = NSFileManager.defaultManager() 0009 var isDir: ObjCBool = false 0010 0011 if fileManager.fileExistsAtPath(filePath, isDirectory: &isDir) { 0012 // File exists 0013 if let fileBody = NSData(contentsOfFile: filePath) { 0014 var array = [UInt8](count: fileBody.length, repeatedValue: 0) 0015 fileBody.getBytes(&array, length: fileBody.length) 0016 0017 response.status = .OK 0018 response.body = array 0019 response.contentType = .Text 0020 response.send() 0021 } else { 0022 next() 0023 } 0024 } else { 0025 0026 next() 0027 } 0028 } 0029 } 0030