Create thumbnails on server
This commit is contained in:
parent
cc7a3ec567
commit
848ff21134
@ -6,6 +6,8 @@ final class CapServer {
|
|||||||
|
|
||||||
private let imageSize = 360
|
private let imageSize = 360
|
||||||
|
|
||||||
|
private let thumbnailSize = 100
|
||||||
|
|
||||||
// MARK: Paths
|
// MARK: Paths
|
||||||
|
|
||||||
private let imageFolder: URL
|
private let imageFolder: URL
|
||||||
@ -36,6 +38,8 @@ final class CapServer {
|
|||||||
/// Indicates that the data is loaded
|
/// Indicates that the data is loaded
|
||||||
private(set) var isOperational = false
|
private(set) var isOperational = false
|
||||||
|
|
||||||
|
private(set) var canResizeImages = false
|
||||||
|
|
||||||
// MARK: Caps
|
// MARK: Caps
|
||||||
|
|
||||||
|
|
||||||
@ -117,7 +121,12 @@ final class CapServer {
|
|||||||
updateGridCapCount()
|
updateGridCapCount()
|
||||||
try ensureExistenceOfChangedImagesFile()
|
try ensureExistenceOfChangedImagesFile()
|
||||||
organizeImages()
|
organizeImages()
|
||||||
shrinkImages()
|
if let version = getMagickVersion() {
|
||||||
|
log("Using ImageMagick \(version.rawValue)")
|
||||||
|
canResizeImages = true
|
||||||
|
}
|
||||||
|
// shrinkImages()
|
||||||
|
createMissingThumbnails()
|
||||||
isOperational = true
|
isOperational = true
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -363,6 +372,10 @@ final class CapServer {
|
|||||||
let count = try count(of: cap)
|
let count = try count(of: cap)
|
||||||
caps[cap]!.count = count
|
caps[cap]!.count = count
|
||||||
addChangedImageToLog(cap: cap, image: id)
|
addChangedImageToLog(cap: cap, image: id)
|
||||||
|
if canResizeImages {
|
||||||
|
shrink(imageAt: capImageUrl, size: imageSize, destination: capImageUrl)
|
||||||
|
createThumbnail(for: cap)
|
||||||
|
}
|
||||||
log("Added image \(id) for cap \(cap) (\(count) total)")
|
log("Added image \(id) for cap \(cap) (\(count) total)")
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -453,6 +466,9 @@ final class CapServer {
|
|||||||
throw CapError.invalidFile
|
throw CapError.invalidFile
|
||||||
}
|
}
|
||||||
caps[cap]?.mainImage = version
|
caps[cap]?.mainImage = version
|
||||||
|
if canResizeImages {
|
||||||
|
createThumbnail(for: cap)
|
||||||
|
}
|
||||||
log("Switched cap \(cap) to version \(version)")
|
log("Switched cap \(cap) to version \(version)")
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -607,6 +623,30 @@ final class CapServer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func createMissingThumbnails() {
|
||||||
|
let thumbnailsToCreate = getListOfMissingThumbnails()
|
||||||
|
guard !thumbnailsToCreate.isEmpty else {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
guard canResizeImages else {
|
||||||
|
log("Can't create thumbnails, missing ImageMagick")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log("Creating \(thumbnailsToCreate.count) thumbnails")
|
||||||
|
for cap in thumbnailsToCreate {
|
||||||
|
createThumbnail(for: cap)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func createThumbnail(for cap: Int) {
|
||||||
|
guard let version = caps[cap]?.mainImage else {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
let mainImageUrl = imageUrl(of: cap, version: version)
|
||||||
|
let thumbnailUrl = thumbnail(of: cap)
|
||||||
|
shrink(imageAt: mainImageUrl, size: thumbnailSize, destination: thumbnailUrl)
|
||||||
|
}
|
||||||
|
|
||||||
// MARK: Monitoring
|
// MARK: Monitoring
|
||||||
|
|
||||||
private let capCountMetric: Metric<Int>
|
private let capCountMetric: Metric<Int>
|
||||||
@ -645,12 +685,10 @@ final class CapServer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func shrinkImages() {
|
func shrinkImages() {
|
||||||
guard let version = getMagickVersion() else {
|
guard canResizeImages else {
|
||||||
log("Failed to shrink images, missing dependency")
|
log("Can't resize images, missing ImageMagick")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log("Shrinking images using ImageMagick \(version.rawValue)")
|
|
||||||
|
|
||||||
let imageFolders: [URL]
|
let imageFolders: [URL]
|
||||||
do {
|
do {
|
||||||
imageFolders = try fm.contentsOfDirectory(at: imageFolder, includingPropertiesForKeys: nil)
|
imageFolders = try fm.contentsOfDirectory(at: imageFolder, includingPropertiesForKeys: nil)
|
||||||
@ -663,20 +701,20 @@ final class CapServer {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
for imageUrl in images {
|
for imageUrl in images {
|
||||||
shrink(imageAt: imageUrl)
|
shrink(imageAt: imageUrl, size: imageSize, destination: imageUrl)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private func shrink(imageAt url: URL) {
|
private func shrink(imageAt url: URL, size: Int, destination: URL) {
|
||||||
do {
|
do {
|
||||||
let command = "convert \(url.path) -resize '\(imageSize)x\(imageSize)>' \(url.path)"
|
let command = "convert \(url.path) -resize '\(size)x\(size)>' \(destination.path)"
|
||||||
let (code, output) = try safeShell(command)
|
let (code, output) = try safeShell(command)
|
||||||
if code != 0 {
|
if code != 0 {
|
||||||
print("Failed to shrink image: " + output)
|
log("Failed to shrink image \(url.path): " + output)
|
||||||
}
|
}
|
||||||
} catch {
|
} catch {
|
||||||
print("Failed to shrink image: \(error)")
|
log("Failed to shrink image \(url.path): \(error)")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -47,7 +47,11 @@ public func configure(_ app: Application) async throws {
|
|||||||
} catch {
|
} catch {
|
||||||
try await status.update(.initializationFailure)
|
try await status.update(.initializationFailure)
|
||||||
}
|
}
|
||||||
try await status.update(.nominal)
|
if server.canResizeImages {
|
||||||
|
try await status.update(.nominal)
|
||||||
|
} else {
|
||||||
|
try await status.update(.reducedFunctionality)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func log(_ message: String) {
|
func log(_ message: String) {
|
||||||
|
Loading…
Reference in New Issue
Block a user