Improve logging, fix image upload
This commit is contained in:
parent
8a42dddb2c
commit
fe0fdec03d
@ -35,10 +35,10 @@ extension CapServer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Upload an image
|
// Upload an image
|
||||||
app.postCatching("images", ":cap") { request in
|
app.postCatching("images", .parameter("cap")) { request in
|
||||||
try self.ensureOperability()
|
try self.ensureOperability()
|
||||||
try authenticator.authorize(request)
|
try authenticator.authorize(request)
|
||||||
guard let cap = request.parameters.get("n", as: Int.self) else {
|
guard let cap = request.parameters.get("cap", as: Int.self) else {
|
||||||
log("/images/:cap: Invalid 'cap' parameter")
|
log("/images/:cap: Invalid 'cap' parameter")
|
||||||
throw Abort(.badRequest)
|
throw Abort(.badRequest)
|
||||||
}
|
}
|
||||||
|
@ -149,6 +149,10 @@ final class CapServer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private func loadCaps() throws {
|
private func loadCaps() throws {
|
||||||
|
guard exists(dbFile) else {
|
||||||
|
log("No cap database found")
|
||||||
|
return
|
||||||
|
}
|
||||||
do {
|
do {
|
||||||
let data = try Data(contentsOf: dbFile)
|
let data = try Data(contentsOf: dbFile)
|
||||||
caps = try JSONDecoder().decode([Cap].self, from: data)
|
caps = try JSONDecoder().decode([Cap].self, from: data)
|
||||||
@ -341,8 +345,17 @@ final class CapServer {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
let handle = try FileHandle(forWritingTo: changedImagesFile)
|
try ensureExistenceOfChangedImagesFile()
|
||||||
|
|
||||||
|
let handle: FileHandle
|
||||||
|
do {
|
||||||
|
handle = try FileHandle(forWritingTo: changedImagesFile)
|
||||||
try handle.seekToEnd()
|
try handle.seekToEnd()
|
||||||
|
} catch {
|
||||||
|
log("Failed to open changed images file for writing: \(error)")
|
||||||
|
throw error
|
||||||
|
}
|
||||||
|
|
||||||
var entries = unwrittenImageChanges
|
var entries = unwrittenImageChanges
|
||||||
defer {
|
defer {
|
||||||
unwrittenImageChanges = entries
|
unwrittenImageChanges = entries
|
||||||
@ -351,7 +364,12 @@ final class CapServer {
|
|||||||
let dateString = changedImageEntryDateFormatter.string(from: Date())
|
let dateString = changedImageEntryDateFormatter.string(from: Date())
|
||||||
while let entry = entries.popLast() {
|
while let entry = entries.popLast() {
|
||||||
let content = "\(dateString):\(entry.cap):\(entry.image)\n".data(using: .utf8)!
|
let content = "\(dateString):\(entry.cap):\(entry.image)\n".data(using: .utf8)!
|
||||||
|
do {
|
||||||
try handle.write(contentsOf: content)
|
try handle.write(contentsOf: content)
|
||||||
|
} catch {
|
||||||
|
log("Failed to write entry to changed images file: \(error)")
|
||||||
|
throw error
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -377,6 +395,10 @@ final class CapServer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func removeAllEntriesInImageChangeList(before date: Date) {
|
func removeAllEntriesInImageChangeList(before date: Date) {
|
||||||
|
guard exists(changedImagesFile) else {
|
||||||
|
log("No file for changed images to update")
|
||||||
|
return
|
||||||
|
}
|
||||||
do {
|
do {
|
||||||
try String(contentsOf: changedImagesFile)
|
try String(contentsOf: changedImagesFile)
|
||||||
.components(separatedBy: "\n")
|
.components(separatedBy: "\n")
|
||||||
@ -418,6 +440,7 @@ final class CapServer {
|
|||||||
|
|
||||||
private func add(_ cap: Cap) throws {
|
private func add(_ cap: Cap) throws {
|
||||||
guard cap.mainImage == 0 else {
|
guard cap.mainImage == 0 else {
|
||||||
|
log("Attempting to add cap \(cap.id) with main image \(cap.mainImage)")
|
||||||
throw CapError.invalidData
|
throw CapError.invalidData
|
||||||
}
|
}
|
||||||
var cap = cap
|
var cap = cap
|
||||||
@ -447,10 +470,12 @@ final class CapServer {
|
|||||||
|
|
||||||
func deleteImage(version: Int, for capId: Int) -> Bool {
|
func deleteImage(version: Int, for capId: Int) -> Bool {
|
||||||
guard let cap = caps[capId] else {
|
guard let cap = caps[capId] else {
|
||||||
|
log("Attempting to delete image \(version) of unknown cap \(capId)")
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
let capImageUrl = imageUrl(of: capId, version: version)
|
let capImageUrl = imageUrl(of: capId, version: version)
|
||||||
guard exists(capImageUrl) else {
|
guard exists(capImageUrl) else {
|
||||||
|
log("Attempting to delete missing image \(version) of cap \(capId)")
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
organizeImages(for: cap)
|
organizeImages(for: cap)
|
||||||
@ -476,7 +501,7 @@ final class CapServer {
|
|||||||
do {
|
do {
|
||||||
try classifier.write(to: classifierFile)
|
try classifier.write(to: classifierFile)
|
||||||
} catch {
|
} catch {
|
||||||
log("Failed to write classifier: \(error)")
|
log("Failed to write classifier \(version): \(error)")
|
||||||
throw Abort(.internalServerError)
|
throw Abort(.internalServerError)
|
||||||
}
|
}
|
||||||
classifierVersion = version
|
classifierVersion = version
|
||||||
|
Loading…
Reference in New Issue
Block a user