Repository: vapor/vapor
File: Sources/Vapor/Middleware/FileMiddleware.swift (Swift)

Code before:
            return request.eventLoop.makeFailedFuture(Abort(.forbidden))
        }

        // create absolute file path
        let filePath = self.publicDirectory + path

        // check if input exists and whether it is a directory
        var isDir: ObjCBool = false
        let fileExists = FileManager.default.fileExists(atPath: filePath, isDirectory: &isDir)
        
        guard fileExists else {
            return next.respond(to: request)
        }
        
        if !isDir.boolValue {
            // stream the file
            let res = request.fileio.streamFile(at: filePath)
            return request.eventLoop.makeSucceededFuture(res)
        } else {
            // Check for the default file
            guard let defaultFileName = defaultFile else {
                return next.respond(to: request)
            }
            
            var defaultFilePath = filePath.hasSuffix("/") ? filePath : filePath + "/"
            defaultFilePath = defaultFilePath + defaultFileName
            
            guard FileManager.default.fileExists(atPath: defaultFilePath) else {
                return next.respond(to: request)
            }
            
            // stream the file
            let res = request.fileio.streamFile(at: defaultFilePath)
            return request.eventLoop.makeSucceededFuture(res)
        }
    }
}


Review comment (suggestion):
Ideally we should explicitly handle the cases where `defaultFileName` starts with `/`.
In those cases, `defaultFilePath` should be `publicDirectory + defaultFileName`.

Code after:
        }

        // create absolute path
        var absPath = self.publicDirectory + path

        // check if path exists and whether it is a directory
        var isDir: ObjCBool = false
        guard FileManager.default.fileExists(atPath: absPath, isDirectory: &isDir) else {
            return next.respond(to: request)
        }
        
        if isDir.boolValue {
            // If a directory, check for the default file
            guard let defaultFile = defaultFile else {
                return next.respond(to: request)
            }
            
            if defaultFile.isAbsolute() {
                absPath = self.publicDirectory + defaultFile.removeLeadingSlashes()
            } else {
                absPath = absPath.addTrailingSlash() + defaultFile
            }
            
            // If the default file doesn't exist, pass on request
            guard FileManager.default.fileExists(atPath: absPath) else {
                return next.respond(to: request)
            }
        }
        
        // stream the file
        let res = request.fileio.streamFile(at: absPath)
        return request.eventLoop.makeSucceededFuture(res)
    }
}

fileprivate extension String {
    /// Determines if input path is absolute based on a leading slash
    func isAbsolute() -> Bool {
        return self.hasPrefix("/")
    }

    /// Makes a path relative by removing all leading slashes
    func removeLeadingSlashes() -> String {
        var newPath = self
        while newPath.hasPrefix("/") {
            newPath.removeFirst()
        }
        return newPath
    }

    /// Adds a trailing slash to the path if one is not already present