Repository: vapor/vapor
File: Sources/Vapor/Utilities/FileIO.swift (Swift)

Code before:
                try? fd.close()
            }

            return done
        } catch {
            return self.request.eventLoop.makeFailedFuture(error)
        }
    }

    /// Generates a fresh ETag for a file or returns its currently cached one.
    /// - Parameters:
    ///   - path: The file's path.
    ///   - lastModified: When the file was last modified.
    /// - Returns: An `EventLoopFuture<String>` which holds the ETag.
    private func generateETagHash(path: String, lastModified: Date) -> EventLoopFuture<String> {
        var hashingDictionary = request.application.storage[FileMiddleware.ETagHashes.self] ?? [:]

        if let hash = hashingDictionary[path], hash.lastModified == lastModified {
            return request.eventLoop.makeSucceededFuture(hash.digest.hex)
        } else {
            return collectFile(at: path).map { buffer in
                let digest = SHA256.hash(data: buffer.readableBytesView)

                // update hash in dictionary
                hashingDictionary[path] = FileMiddleware.ETagHashes.FileHash(lastModified: lastModified, digest: digest)
                request.application.storage[FileMiddleware.ETagHashes.self] = hashingDictionary

                return digest.hex
            }
        }
    }
}

extension HTTPHeaders.Range.Value {
    
    fileprivate func asByteBufferBounds(withMaxSize size: Int, logger: Logger) throws -> (offset: Int64, byteCount: Int) {
        switch self {
            case .start(let value):
                guard value <= size, value >= 0 else {
                    logger.debug("Requested range start was invalid: \(value)")
                    throw Abort(.badRequest)
                }
                return (offset: numericCast(value), byteCount: size - value)
            case .tail(let value):
                guard value <= size, value >= 0 else {
                    logger.debug("Requested range end was invalid: \(value)")
                    throw Abort(.badRequest)
                }
                return (offset: numericCast(size - value), byteCount: value)
            case .within(let start, let end):
                guard start >= 0, end >= 0, start <= end, start <= size, end <= size else {

Review comment (performance):
Is there a risk here we'll overwrite the cache with multiple concurrent requests? Would it not be better to just access it directly?

Code after:
    /// - returns: `Future` that will complete when the file write is finished.
    public func writeFile(_ buffer: ByteBuffer, at path: String) -> EventLoopFuture<Void> {
        self.request.eventLoop.flatSubmit {
            do {
                let fd = try NIOFileHandle(path: path, mode: .write, flags: .allowFileCreation())
                let fdWrapper = NIOLoopBound(fd, eventLoop: self.request.eventLoop)
                let done = io.write(fileHandle: fd, buffer: buffer, eventLoop: self.request.eventLoop)
                done.whenComplete { _ in
                    try? fdWrapper.value.close()
                }
                return done
            } catch {
                return self.request.eventLoop.makeFailedFuture(error)
            }
        }
    }

    /// Generates a fresh ETag for a file or returns its currently cached one.
    /// - Parameters:
    ///   - path: The file's path.
    ///   - lastModified: When the file was last modified.
    /// - Returns: An `EventLoopFuture<String>` which holds the ETag.
    private func generateETagHash(path: String, lastModified: Date) -> EventLoopFuture<String> {
        if let hash = request.application.storage[FileMiddleware.ETagHashes.self]?[path], hash.lastModified == lastModified {
            return request.eventLoop.makeSucceededFuture(hash.digestHex)
        } else {
            return collectFile(at: path).map { buffer in
                let digest = SHA256.hash(data: buffer.readableBytesView)
                
                // update hash in dictionary
                request.application.storage[FileMiddleware.ETagHashes.self]?[path] = FileMiddleware.ETagHashes.FileHash(lastModified: lastModified, digestHex: digest.hex)

                return digest.hex
            }
        }
    }
}

extension HTTPHeaders.Range.Value {
    
    fileprivate func asByteBufferBounds(withMaxSize size: Int, logger: Logger) throws -> (offset: Int64, byteCount: Int) {
        switch self {
            case .start(let value):
                guard value <= size, value >= 0 else {
                    logger.debug("Requested range start was invalid: \(value)")
                    throw Abort(.badRequest)
                }
                return (offset: numericCast(value), byteCount: size - value)
            case .tail(let value):
                guard value <= size, value >= 0 else {
                    logger.debug("Requested range end was invalid: \(value)")