Add more logging

This commit is contained in:
christophhagen 2020-05-28 14:34:07 +02:00
parent 824db80572
commit b52e4f5740

View File

@ -91,11 +91,13 @@ public func routes(_ router: Router) throws {
if !fm.fileExists(atPath: url.path) {
try fm.createDirectory(at: url, withIntermediateDirectories: false)
}
log("Added cap \(cap)")
} else {
caps[index] = name
log("Set name for cap \(cap)")
}
try caps.joined(separator: "\n").data(using: .utf8)!.write(to: nameFile)
log("Set name for cap \(cap) (\(caps.count) caps loaded)")
}
// Upload an image
@ -107,6 +109,7 @@ public func routes(_ router: Router) throws {
let c = try count(of: cap)
let f = file(of: cap, version: c)
guard !fm.fileExists(atPath: f.path) else {
log("Image \(c) for cap \(cap) already exists on disk")
throw CapError.dataInconsistency
}
try data.write(to: f)
@ -130,18 +133,22 @@ public func routes(_ router: Router) throws {
let cap = try request.parameters.next(Int.self)
let version = try request.parameters.next(Int.self)
guard version > 0 else {
log("Not switching cap \(cap) to image \(version)")
return
}
let file1 = file(of: cap, version: 0)
guard fm.fileExists(atPath: file1.path) else {
log("No image 0 for cap \(cap)")
throw CapError.invalidFile
}
let file2 = file(of: cap, version: version)
guard fm.fileExists(atPath: file2.path) else {
log("No image \(version) for cap \(cap)")
throw CapError.invalidFile
}
try fm.moveItem(at: file1, to: tempImageFile)
try fm.moveItem(at: file2, to: file1)
try fm.moveItem(at: tempImageFile, to: file2)
log("Switched cap \(cap) to version \(version)")
}
}