Kaynağa Gözat

Merge pull request #493 from jcrate/stable

Update Files.swift
Victor Sigler 4 yıl önce
ebeveyn
işleme
b426561e42
2 değiştirilmiş dosya ile 10 ekleme ve 1 silme
  1. 2 0
      CHANGELOG.md
  2. 8 1
      Xcode/Sources/Files.swift

+ 2 - 0
CHANGELOG.md

@@ -23,6 +23,8 @@ All notable changes to this project will be documented in this file. Changes not
 - Add support for using `**` as a catch-all at the end of a route. ([#479](https://github.com/httpswift/swifter/pull/479)) by [@michaelenger](https://github.com/michaelenger)
 - Set `Content-Type` to HttpBody and Text HttpResponse. ([#474](https://github.com/httpswift/swifter/pull/474)) by [@mtgto](https://github.com/mtgto)
 
+- The `shareFile` function now sets `Content-Type` and `Content-Length` headers like `shareFilesFromDirectory`. ([#493](https://github.com/httpswift/swifter/pull/493)) by [@jcrate](https://github.com/jcrate)
+
 ## Fixed
 
 * Fix misspell `serialise`. ([#473](https://github.com/httpswift/swifter/pull/473)) by [@mtgto](https://github.com/mtgto)

+ 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()
             })