1

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.

RogerTheShrubber
  • 986
  • 8
  • 19

2 Answers2

2
router.get("customfile") { req -> Future<Response> in
    return try req.streamFile(at: "/path/to/file")
}
imike
  • 5,515
  • 2
  • 37
  • 44
0

This works for me.

func routes(_ app: Application) throws {
  app.get("downloadthefile") { req -> Response in
    let filePath = "path/to/the/file"
    let result = req.fileio.streamFile(at: filePath)
    return result
  }
}
zekel
  • 9,227
  • 10
  • 65
  • 96