-
Notifications
You must be signed in to change notification settings - Fork 554
Expand file tree
/
Copy pathFiles.swift
More file actions
106 lines (98 loc) · 3.73 KB
/
Files.swift
File metadata and controls
106 lines (98 loc) · 3.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
//
// HttpHandlers+Files.swift
// Swifter
//
// Copyright (c) 2014-2016 Damian Kołakowski. All rights reserved.
//
import Foundation
public func shareFile(_ path: String) -> ((HttpRequest) -> HttpResponse) {
return { _ in
if let file = try? path.openForReading() {
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()
})
}
return .notFound()
}
}
public func shareFilesFromDirectory(_ directoryPath: String, defaults: [String] = ["index.html", "default.html"]) -> ((HttpRequest) -> HttpResponse) {
return { request in
guard let fileRelativePath = request.params.first else {
return .notFound()
}
if fileRelativePath.value.isEmpty {
for path in defaults {
if let file = try? (directoryPath + String.pathSeparator + path).openForReading() {
return .raw(200, "OK", [:], { writer in
try? writer.write(file)
file.close()
})
}
}
}
let filePath = directoryPath + String.pathSeparator + fileRelativePath.value
if let file = try? filePath.openForReading() {
let mimeType = fileRelativePath.value.mimeType()
var responseHeader: [String: String] = ["Content-Type": mimeType]
if let attr = try? FileManager.default.attributesOfItem(atPath: filePath),
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()
})
}
return .notFound()
}
}
public func directoryBrowser(_ dir: String) -> ((HttpRequest) -> HttpResponse) {
return { request in
guard let (_, value) = request.params.first else {
return .notFound()
}
let filePath = dir + String.pathSeparator + value
do {
guard try filePath.exists() else {
return .notFound()
}
if try filePath.directory() {
var files = try filePath.files()
files.sort(by: {$0.lowercased() < $1.lowercased()})
return scopes {
html {
body {
table(files) { file in
tr {
td {
a {
href = request.path + "%2F" + file
inner = file
}
}
}
}
}
}
}(request)
} else {
guard let file = try? filePath.openForReading() else {
return .notFound()
}
return .raw(200, "OK", [:], { writer in
try? writer.write(file)
file.close()
})
}
} catch {
return HttpResponse.internalServerError(.text("Internal Server Error"))
}
}
}