Skip to content

Commit b592bf1

Browse files
committed
Update Files.swift
Sharing a file now sets content type and length
1 parent 718f82c commit b592bf1

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

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)