Skip to content

Commit b426561

Browse files
authored
Merge pull request #493 from jcrate/stable
Update Files.swift
2 parents c9bf600 + 48a7331 commit b426561

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ All notable changes to this project will be documented in this file. Changes not
2323
- 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)
2424
- Set `Content-Type` to HttpBody and Text HttpResponse. ([#474](https://github.com/httpswift/swifter/pull/474)) by [@mtgto](https://github.com/mtgto)
2525

26+
- 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)
27+
2628
## Fixed
2729

2830
* Fix misspell `serialise`. ([#473](https://github.com/httpswift/swifter/pull/473)) by [@mtgto](https://github.com/mtgto)

Xcode/Sources/Files.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,14 @@ import Foundation
1010
public func shareFile(_ path: String) -> ((HttpRequest) -> HttpResponse) {
1111
return { _ in
1212
if let file = try? path.openForReading() {
13-
return .raw(200, "OK", [:], { writer in
13+
let mimeType = path.mimeType()
14+
var responseHeader: [String: String] = ["Content-Type": mimeType]
15+
16+
if let attr = try? FileManager.default.attributesOfItem(atPath: path),
17+
let fileSize = attr[FileAttributeKey.size] as? UInt64 {
18+
responseHeader["Content-Length"] = String(fileSize)
19+
}
20+
return .raw(200, "OK", responseHeader, { writer in
1421
try? writer.write(file)
1522
file.close()
1623
})

0 commit comments

Comments
 (0)