Compare commits

..

No commits in common. "f956b8b4d2260da9f33da825b1b8158c476dd563" and "df89f4eabd20cd9c0f9f0fbf09b0edb96621614d" have entirely different histories.

2 changed files with 21 additions and 42 deletions

View File

@ -184,10 +184,10 @@ final class CapServer: ServerOwner {
}
private func organizeImages() {
caps.values.sorted().forEach(organizeImages)
caps.values.sorted().forEach(organizeImage)
}
private func organizeImages(for cap: Cap) {
private func organizeImage(for cap: Cap) {
var cap = cap
guard let images = try? images(in: folder(of: cap.id)) else {
log("Failed to get image urls for cap \(cap.id)")
@ -204,7 +204,7 @@ final class CapServer: ServerOwner {
continue
}
let lastImage = sorted.popLast()!
let newUrl = imageUrl(of: cap.id, version: version)
let newUrl = file(of: cap.id, version: version)
do {
try fm.moveItem(at: lastImage.url, to: newUrl)
} catch {
@ -230,7 +230,7 @@ final class CapServer: ServerOwner {
thumbnailFolder.appendingPathComponent(String(format: "%04d.jpg", cap))
}
func imageUrl(of cap: Int, version: Int) -> URL {
func file(of cap: Int, version: Int) -> URL {
folder(of: cap).appendingPathComponent(String(format: "%04d-%02d.jpg", cap, version))
}
@ -246,6 +246,19 @@ final class CapServer: ServerOwner {
}
// MARK: Counts
private func updateCounts() throws {
do {
caps = try caps.mapValues {
var cap = $0
cap.count = try count(of: $0.id)
return cap
}
} catch {
log("Failed to update counts: \(error)")
throw error
}
}
private func images(in folder: URL) throws -> [URL] {
try fm.contentsOfDirectory(at: folder, includingPropertiesForKeys: nil)
@ -279,11 +292,11 @@ final class CapServer: ServerOwner {
}
var id = 0
let capFolder = folder(of: cap)
var f = imageUrl(of: cap, version: id)
var f = file(of: cap, version: id)
if fm.fileExists(atPath: capFolder.path) {
while fm.fileExists(atPath: f.path) {
id += 1
f = imageUrl(of: cap, version: id)
f = file(of: cap, version: id)
}
} else {
try fm.createDirectory(at: capFolder, withIntermediateDirectories: true)
@ -357,7 +370,7 @@ final class CapServer: ServerOwner {
}
func switchMainImage(to version: Int, for cap: Int) throws {
let file2 = imageUrl(of: cap, version: version)
let file2 = file(of: cap, version: version)
guard fm.fileExists(atPath: file2.path) else {
log("No image \(version) for cap \(cap)")
throw CapError.invalidFile
@ -392,7 +405,7 @@ final class CapServer: ServerOwner {
if cap.name != "" {
updatedCap.name = cap.name
}
let url = imageUrl(of: existingCap.id, version: cap.mainImage)
let url = file(of: existingCap.id, version: cap.mainImage)
if fm.fileExists(atPath: url.path) {
updatedCap.mainImage = cap.mainImage
}
@ -403,20 +416,6 @@ final class CapServer: ServerOwner {
log("Updated cap \(existingCap.id)")
}
func deleteImage(version: Int, for capId: Int) -> Bool {
guard let cap = caps[capId] else {
return false
}
let url = imageUrl(of: capId, version: version)
guard fm.fileExists(atPath: url.path) else {
return false
}
organizeImages(for: cap)
return true
}
// MARK: Classifier
func updateTrainedClasses(content: String) {
let trainedCaps = content
.components(separatedBy: "\n")
@ -441,8 +440,6 @@ final class CapServer: ServerOwner {
log("Updated classifier to version \(version)")
}
// MARK: Grid
func getListOfMissingThumbnails() -> [Int] {
caps.keys.filter { !fm.fileExists(atPath: thumbnail(of: $0).path) }
}

View File

@ -84,32 +84,14 @@ func routes(_ app: Application) {
return missingThumbnails.map(String.init).joined(separator: ",")
}
// Upload the thumbnail of a cap
app.postCatching("thumbnails", ":cap") { request in
guard let cap = request.parameters.get("cap", as: Int.self) else {
log("Invalid cap parameter for thumbnail upload")
throw Abort(.badRequest)
}
try authorize(request)
let data = try request.getBodyData()
server.saveThumbnail(data, for: cap)
}
// Delete the image of a cap
app.postCatching("delete", ":cap", ":version") { request in
guard let cap = request.parameters.get("cap", as: Int.self) else {
log("Invalid cap parameter for image deletion")
throw Abort(.badRequest)
}
try authorize(request)
guard let version = request.parameters.get("version", as: Int.self) else {
log("Invalid version parameter for image deletion")
throw Abort(.badRequest)
}
guard server.deleteImage(version: version, for: cap) else {
throw Abort(.gone)
}
}
}
private extension Request {