imagemagick for preview creation

main 1.3.1
mr-elbe5 2022-08-01 10:31:04 +02:00
parent 13fa45d556
commit e5546d5b89
5 changed files with 48 additions and 26 deletions

View File

@ -12,6 +12,10 @@ import Foundation
public class Configuration: DataContainer{
public static var instance = Configuration()
public static let defaultHost = "localhost"
public static let defaultName = "SwiftyBandika"
public static let magickDefaultPath = "/opt/homebrew/bin/magick"
public static func initialize(){
Log.info("initializing configuration")
@ -53,20 +57,20 @@ public class Configuration: DataContainer{
private let configSemaphore = DispatchSemaphore(value: 1)
public required init(){
host = "localhost"
host = Configuration.defaultHost
webPort = 8080
applicationName = "SwiftyBandika"
imageMagickPath = "/opt/homebrew/bin/magick"
applicationName = Configuration.defaultName
imageMagickPath = Configuration.magickDefaultPath
super.init()
checkImageMagick()
}
public required init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
host = try values.decodeIfPresent(String.self, forKey: .host) ?? "localhost"
host = try values.decodeIfPresent(String.self, forKey: .host) ?? Configuration.defaultHost
webPort = try values.decodeIfPresent(Int.self, forKey: .webPort) ?? 0
imageMagickPath = try values.decodeIfPresent(String.self, forKey: .imageMagickPath) ?? "/opt/homebrew/bin/magick"
applicationName = try values.decodeIfPresent(String.self, forKey: .applicationName) ?? "SwiftyBandika"
imageMagickPath = try values.decodeIfPresent(String.self, forKey: .imageMagickPath) ?? Configuration.magickDefaultPath
applicationName = try values.decodeIfPresent(String.self, forKey: .applicationName) ?? Configuration.defaultName
autostart = try values.decodeIfPresent(Bool.self, forKey: .autostart) ?? false
try super.init(from: decoder)
checkImageMagick()

View File

@ -16,12 +16,13 @@ import Cocoa
open class ImageFactory {
public static var instance : ImageFactory = ImageFactory()
open func createPreview(fileData: FileData, original: MemoryFile) -> MemoryFile? {
@discardableResult
open func createPreview(original: DiskFile, previewFileName: String) -> Bool {
#if os(macOS)
return createMacOSPreview(fileData: fileData, original: original)
return createMacOSPreview(original: original, previewFileName: previewFileName)
#elseif os(Linux)
return nil
return createMagickPreview(original: original, previewFileName: previewFileName)
#else
return nil
#endif
@ -31,29 +32,30 @@ open class ImageFactory {
#if os(macOS)
return true
#elseif os(Linux)
return false
return Configuration.instance.isImageMagickEnabled
#else
return false
#endif
}
#if os(macOS)
open func createMacOSPreview(fileData: FileData, original: MemoryFile) -> MemoryFile? {
if let src = NSImage(data: original.data) {
if let previewImage: NSImage = resizeImage(original: src, toSize: NSSize(width: FileData.MAX_PREVIEW_SIDE, height: FileData.MAX_PREVIEW_SIDE)) {
open func createMacOSPreview(original: DiskFile, previewFileName: String) -> Bool {
if let memoryFile = original.readFromDisk(), let src = NSImage(data: memoryFile.data) {
if let previewImage: NSImage = resizeNSImage(original: src, toSize: NSSize(width: FileData.MAX_PREVIEW_SIDE, height: FileData.MAX_PREVIEW_SIDE)) {
if let tiff = previewImage.tiffRepresentation, let tiffData = NSBitmapImageRep(data: tiff) {
if let previewData = tiffData.representation(using: .jpeg, properties: [:]) {
let preview = MemoryFile(name: fileData.previewFileName, data: previewData)
let preview = MemoryFile(name: previewFileName, data: previewData)
preview.contentType = "image/jpeg"
return preview
let previewFile = DiskFile(name: previewFileName, live: false)
return previewFile.writeToDisk(preview)
}
}
}
}
return nil
return false
}
open func resizeImage(original: NSImage, toSize: NSSize) -> NSImage? {
open func resizeNSImage(original: NSImage, toSize: NSSize) -> NSImage? {
let newSize: NSSize
let widthRatio = toSize.width / original.size.width
let heightRatio = toSize.height / original.size.height
@ -76,5 +78,12 @@ open class ImageFactory {
}
#endif
open func createMagickPreview(original: DiskFile, previewFileName: String) -> Bool {
if Configuration.instance.isImageMagickEnabled{
return ImageMagick.copy(from: original.path, to: previewFileName, newSize: NSSize(width: FileData.MAX_PREVIEW_SIDE, height: FileData.MAX_PREVIEW_SIDE))
}
return false
}
}

View File

@ -37,6 +37,15 @@ public struct ImageMagick{
])
}
@discardableResult
public static func copy(from: String, to: String, newSize: NSSize) -> Bool{
return run(args: [
from,
"-resize \(Int(newSize.width))x\(Int(newSize.height))",
to
])
}
@discardableResult
public static func run(args : [String]) -> Bool{
let task = Process()

View File

@ -164,20 +164,13 @@ public class FileData: BaseData {
if let memoryFile = request.getFile("file") {
fileName = memoryFile.name
contentType = memoryFile.contentType
fileType = FileType.fromContentType(contentType: contentType)
file = DiskFile(name: idFileName, live: false)
if !file.writeToDisk(memoryFile) {
request.addFormError("could not create file")
return;
}
if isImage, ImageFactory.instance.canCreatePreview() {
previewFile = DiskFile(name: previewFileName, live: false)
if let memoryPreviewFile = ImageFactory.instance.createPreview(fileData: self, original: memoryFile) {
if !previewFile!.writeToDisk(memoryPreviewFile) {
request.addFormError("could not create file")
return
}
}
ImageFactory.instance.createPreview(original: file, previewFileName: previewFileName)
}
if displayName.isEmpty {
displayName = fileName.pathWithoutExtension()

View File

@ -34,6 +34,13 @@ public class DiskFile{
public func exists() -> Bool{
Files.fileExists(path: path)
}
public func readFromDisk() -> MemoryFile?{
if Files.fileExists(path: path), let data = Files.readFile(path: path){
return MemoryFile(name: name, data: data)
}
return nil
}
public func writeToDisk(_ memoryFile: MemoryFile) -> Bool{
if Files.fileExists(path: path){