Browse Source

Add shareFile which shares a single file

Tae Won Ha 9 năm trước cách đây
mục cha
commit
cc6f6f7c1d
1 tập tin đã thay đổi với 12 bổ sung0 xóa
  1. 12 0
      Sources/Files.swift

+ 12 - 0
Sources/Files.swift

@@ -7,6 +7,18 @@
 
 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) {
     return { r in
         guard let fileRelativePath = r.params.first else {