|
|
@@ -9,6 +9,25 @@ import Foundation
|
|
|
|
|
|
extension HttpHandlers {
|
|
|
|
|
|
+ public class func shareFilesFromDirectory(directoryPath: String) -> (HttpRequest -> HttpResponse) {
|
|
|
+ return { r in
|
|
|
+ guard let fileRelativePath = r.params.first else {
|
|
|
+ return .NotFound
|
|
|
+ }
|
|
|
+ let absolutePath = directoryPath + "/" + fileRelativePath.1
|
|
|
+ guard let file = try? File.openForReading(absolutePath) else {
|
|
|
+ return .NotFound
|
|
|
+ }
|
|
|
+ return .RAW(200, "OK", [:], { writer in
|
|
|
+ var buffer = [UInt8](count: 64, repeatedValue: 0)
|
|
|
+ while let count = try? file.read(&buffer) where count > 0 {
|
|
|
+ writer.write(Array(buffer[0..<count]))
|
|
|
+ }
|
|
|
+ file.close()
|
|
|
+ })
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
private static let rangePrefix = "bytes="
|
|
|
|
|
|
public class func directory(dir: String) -> (HttpRequest -> HttpResponse) {
|