Search code examples
swiftvapor

Returning a binary file from a custom route (not FileMiddeleware's public folder)


I want to return a file via a route that I don't want public. My users will need to use a URL with a unique and one time code. Because of that I don't want to use the public folder via the FileMiddleware.

This is a binary executable, not a text file. I can grab the file as data with FileManager, but it seems like Future<Data> is not a valid return type for a route.

After searching StackOverflow, the only thing I've found is this reply to a similar question: How do I download a file and send a file using Vapor server side swift?

But that doesn't really fill in much on HOW to accomplish it.


Solution

  • router.get("customfile") { req -> Future<Response> in
        return try req.streamFile(at: "/path/to/file")
    }