Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions PhotosExporter/exporter/PhotosExporter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

import Foundation
import MediaLibrary
import CoreImage
import AppKit
import Photos


enum PhotosExporterError: Error {
Expand Down Expand Up @@ -82,6 +85,8 @@ class PhotosExporter {
var exportCurrent = true
// set to true if original photos should be exported
var exportOriginals = true
// set to true if HEIC photos should be converted to JPG
var convertHeic2Jpg = false

let fileManager = FileManager.default

Expand Down Expand Up @@ -436,6 +441,36 @@ class PhotosExporter {
}
statistics.countLinkedFiles += 1
stopWatchFileManagerLinkItem.stop()

if sourceUrl.pathExtension.lowercased() == "heic" && convertHeic2Jpg {

if let image = CIImage(contentsOf: sourceUrl) {
let context = CIContext()
let data = context.jpegRepresentation(of: image, colorSpace: CGColorSpaceCreateDeviceRGB(), options: [kCGImageDestinationLossyCompressionQuality as CIImageRepresentationOption:0.8])
var targetJPEGUrl = URL.init(fileURLWithPath: "\(targetPath)/\(fotoName).jpg")
logger.debug("Convert HEIC foto: \(fotoName) to \(targetJPEGUrl)")
var i = 1
while fileManager.fileExists(atPath: targetJPEGUrl.path) {
targetJPEGUrl = URL(fileURLWithPath: "\(targetJPEGUrl)/\(fotoName) (\(i)).jpg")
i += 1
}
logger.debug("convert jpg image: \(targetJPEGUrl.lastPathComponent)")
do {
try data?.write(to: targetJPEGUrl)
} catch {
logger.error("\(String(describing: index)): Unable to convert heic to jpg : \(fotoName) to \(targetJPEGUrl). Error: \(error)")
}

do {
if let creationDate = try fileManager.attributesOfItem(atPath: sourceUrl.path)[FileAttributeKey.creationDate] as? Date {
try fileManager.setAttributes([FileAttributeKey.creationDate:creationDate], ofItemAtPath: targetJPEGUrl.path)
}
} catch {
logger.error("\(String(describing: index)): Unable to set creationDate to jpg : \(fotoName) to \(targetJPEGUrl). Error: \(error)")
}
}
}

} else {
logger.warn("Source URL of mediaObject unknown: \(mediaObject)")
}
Expand Down
3 changes: 3 additions & 0 deletions PhotosExporter/exporter/PhotosExporterFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ class PhotosExporterFactory {
if let exportOriginals = plan.exportOriginals {
photosExporter.exportOriginals = exportOriginals
}
if let convertHeic2Jpg = plan.convertHeic2Jpg {
photosExporter.convertHeic2Jpg = convertHeic2Jpg
}

return photosExporter
}
Expand Down
1 change: 1 addition & 0 deletions PhotosExporter/preferences/PreferencesReader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ class PreferencesReader {
}
plan.exportCurrent = planDict["exportCurrent"]?.bool
plan.exportOriginals = planDict["exportOriginals"]?.bool
plan.convertHeic2Jpg = planDict["convertHeic2Jpg"]?.bool

if let plan = plan as? FileSystemExportPlan {
plan.targetFolder = planDict["targetFolder"]?.string
Expand Down
6 changes: 6 additions & 0 deletions PhotosExporter/preferences/model/Plan.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ class Plan {
// exports the original assets
public var exportOriginals: Bool?

// convert heic to jpg
public var convertHeic2Jpg: Bool?


public var mediaObjectFilter = MediaObjectFilter()

Expand Down Expand Up @@ -49,6 +52,9 @@ class Plan {
if let exportOriginals = exportOriginals {
result += "exportOriginals: \(exportOriginals)\n".indent(indent)
}
if let convertHeic2Jpg = convertHeic2Jpg {
result += "convertHeic2Jpg: \(convertHeic2Jpg)\n".indent(indent)
}
result += mediaObjectFilter.toYaml(indent: indent)

return result
Expand Down