Parcourir la source

Update Files.swift

Sharing a file now sets content type and length
Jim Crate il y a 4 ans
Parent
commit
b592bf155e
1 fichiers modifiés avec 8 ajouts et 1 suppressions
  1. 8 1
      Xcode/Sources/Files.swift

+ 8 - 1
Xcode/Sources/Files.swift

@@ -10,7 +10,14 @@ import Foundation
 public func shareFile(_ path: String) -> ((HttpRequest) -> HttpResponse) {
     return { _ in
         if let file = try? path.openForReading() {
-            return .raw(200, "OK", [:], { writer in
+            let mimeType = path.mimeType()
+            var responseHeader: [String: String] = ["Content-Type": mimeType]
+            
+            if let attr = try? FileManager.default.attributesOfItem(atPath: path),
+                let fileSize = attr[FileAttributeKey.size] as? UInt64 {
+                responseHeader["Content-Length"] = String(fileSize)
+            }
+            return .raw(200, "OK", responseHeader, { writer in
                 try? writer.write(file)
                 file.close()
             })