Prechádzať zdrojové kódy

Merge pull request #217 from qvacua/stable

Add shareFile() which shares a single file
Damian Kołakowski 9 rokov pred
rodič
commit
e4ee35f975
1 zmenil súbory, kde vykonal 12 pridanie a 0 odobranie
  1. 12 0
      Sources/Files.swift

+ 12 - 0
Sources/Files.swift

@@ -7,6 +7,18 @@
 
 
 import Foundation
 import Foundation
 
 
+public func shareFile(_ path: String) -> ((HttpRequest) -> HttpResponse) {
+    return { r in
+        if let file = try? path.openForReading() {
+            return .raw(200, "OK", [:], { writer in
+                try? writer.write(file)
+                file.close()
+            })
+        }
+        return .notFound
+    }
+}
+
 public func shareFilesFromDirectory(_ directoryPath: String, defaults: [String] = ["index.html", "default.html"]) -> ((HttpRequest) -> HttpResponse) {
 public func shareFilesFromDirectory(_ directoryPath: String, defaults: [String] = ["index.html", "default.html"]) -> ((HttpRequest) -> HttpResponse) {
     return { r in
     return { r in
         guard let fileRelativePath = r.params.first else {
         guard let fileRelativePath = r.params.first else {