feat: initial Swift version
This commit is contained in:
156
src/AppCategories.swift
Normal file
156
src/AppCategories.swift
Normal file
@@ -0,0 +1,156 @@
|
||||
import Foundation
|
||||
|
||||
/*
|
||||
#!/usr/bin/env python3
|
||||
# download: https://itunes.apple.com/WebObjects/MZStoreServices.woa/ws/genres
|
||||
import json
|
||||
ids = {}
|
||||
|
||||
def fn(data):
|
||||
for k, v in data.items():
|
||||
ids[k] = v['name']
|
||||
if 'subgenres' in v:
|
||||
fn(v['subgenres'])
|
||||
|
||||
with open('genres.json', 'r') as fp:
|
||||
for cat in json.load(fp).values():
|
||||
if 'App Store' in cat['name']:
|
||||
fn(cat['subgenres'])
|
||||
|
||||
print(',\n'.join(f'{k}: "{v}"' for k, v in ids.items()))
|
||||
print(len(ids))
|
||||
*/
|
||||
|
||||
let AppCategories: [Int: String] = [
|
||||
// MARK: iOS
|
||||
6018: "Books",
|
||||
6000: "Business",
|
||||
6022: "Catalogs",
|
||||
6026: "Developer Tools",
|
||||
6017: "Education",
|
||||
6016: "Entertainment",
|
||||
6015: "Finance",
|
||||
6023: "Food & Drink",
|
||||
6014: "Games",
|
||||
7001: "Action",
|
||||
7002: "Adventure",
|
||||
7004: "Board",
|
||||
7005: "Card",
|
||||
7006: "Casino",
|
||||
7003: "Casual",
|
||||
7007: "Dice",
|
||||
7008: "Educational",
|
||||
7009: "Family",
|
||||
7011: "Music",
|
||||
7012: "Puzzle",
|
||||
7013: "Racing",
|
||||
7014: "Role Playing",
|
||||
7015: "Simulation",
|
||||
7016: "Sports",
|
||||
7017: "Strategy",
|
||||
7018: "Trivia",
|
||||
7019: "Word",
|
||||
6027: "Graphics & Design",
|
||||
6013: "Health & Fitness",
|
||||
6012: "Lifestyle",
|
||||
6021: "Magazines & Newspapers",
|
||||
13007: "Arts & Photography",
|
||||
13006: "Automotive",
|
||||
13008: "Brides & Weddings",
|
||||
13009: "Business & Investing",
|
||||
13010: "Children's Magazines",
|
||||
13011: "Computers & Internet",
|
||||
13012: "Cooking, Food & Drink",
|
||||
13013: "Crafts & Hobbies",
|
||||
13014: "Electronics & Audio",
|
||||
13015: "Entertainment",
|
||||
13002: "Fashion & Style",
|
||||
13017: "Health, Mind & Body",
|
||||
13018: "History",
|
||||
13003: "Home & Garden",
|
||||
13019: "Literary Magazines & Journals",
|
||||
13020: "Men's Interest",
|
||||
13021: "Movies & Music",
|
||||
13001: "News & Politics",
|
||||
13004: "Outdoors & Nature",
|
||||
13023: "Parenting & Family",
|
||||
13024: "Pets",
|
||||
13025: "Professional & Trade",
|
||||
13026: "Regional News",
|
||||
13027: "Science",
|
||||
13005: "Sports & Leisure",
|
||||
13028: "Teens",
|
||||
13029: "Travel & Regional",
|
||||
13030: "Women's Interest",
|
||||
6020: "Medical",
|
||||
6011: "Music",
|
||||
6010: "Navigation",
|
||||
6009: "News",
|
||||
6008: "Photo & Video",
|
||||
6007: "Productivity",
|
||||
6006: "Reference",
|
||||
6024: "Shopping",
|
||||
6005: "Social Networking",
|
||||
6004: "Sports",
|
||||
6025: "Stickers",
|
||||
16003: "Animals & Nature",
|
||||
16005: "Art",
|
||||
16006: "Celebrations",
|
||||
16007: "Celebrities",
|
||||
16008: "Comics & Cartoons",
|
||||
16009: "Eating & Drinking",
|
||||
16001: "Emoji & Expressions",
|
||||
16026: "Fashion",
|
||||
16010: "Gaming",
|
||||
16025: "Kids & Family",
|
||||
16014: "Movies & TV",
|
||||
16015: "Music",
|
||||
16017: "People",
|
||||
16019: "Places & Objects",
|
||||
16021: "Sports & Activities",
|
||||
6003: "Travel",
|
||||
6002: "Utilities",
|
||||
6001: "Weather",
|
||||
|
||||
// MARK: macOS
|
||||
12001: "Business",
|
||||
12002: "Developer Tools",
|
||||
12003: "Education",
|
||||
12004: "Entertainment",
|
||||
12005: "Finance",
|
||||
12006: "Games",
|
||||
12201: "Action",
|
||||
12202: "Adventure",
|
||||
12204: "Board",
|
||||
12205: "Card",
|
||||
12206: "Casino",
|
||||
12203: "Casual",
|
||||
12207: "Dice",
|
||||
12208: "Educational",
|
||||
12209: "Family",
|
||||
12210: "Kids",
|
||||
12211: "Music",
|
||||
12212: "Puzzle",
|
||||
12213: "Racing",
|
||||
12214: "Role Playing",
|
||||
12215: "Simulation",
|
||||
12216: "Sports",
|
||||
12217: "Strategy",
|
||||
12218: "Trivia",
|
||||
12219: "Word",
|
||||
12022: "Graphics & Design",
|
||||
12007: "Health & Fitness",
|
||||
12008: "Lifestyle",
|
||||
12010: "Medical",
|
||||
12011: "Music",
|
||||
12012: "News",
|
||||
12013: "Photography",
|
||||
12014: "Productivity",
|
||||
12015: "Reference",
|
||||
12016: "Social Networking",
|
||||
12017: "Sports",
|
||||
12018: "Travel",
|
||||
12019: "Utilities",
|
||||
12020: "Video",
|
||||
12021: "Weather",
|
||||
]
|
||||
91
src/AppIcon+Car.swift
Normal file
91
src/AppIcon+Car.swift
Normal file
@@ -0,0 +1,91 @@
|
||||
import Foundation
|
||||
import AppKit // NSImage
|
||||
import CoreUI // CUICatalog
|
||||
import os // OSLog
|
||||
|
||||
private let log = OSLog(subsystem: Bundle.main.bundleIdentifier!, category: "AppIcon+Car")
|
||||
|
||||
// this has been written from scratch but general usage on
|
||||
// including the private framework has been taken from:
|
||||
// https://github.com/showxu/cartools
|
||||
// also see:
|
||||
// https://blog.timac.org/2018/1018-reverse-engineering-the-car-file-format/
|
||||
|
||||
extension AppIcon {
|
||||
/// Use `CUICatalog` to extract an image from `Assets.car`
|
||||
func imageFromAssetsCar(_ imageName: String) -> NSImage? {
|
||||
guard let data = meta.readPayloadFile("Assets.car") else {
|
||||
return nil
|
||||
}
|
||||
let catalog: CUICatalog
|
||||
do {
|
||||
catalog = try data.withUnsafeBytes { try CUICatalog(bytes: $0.baseAddress!, length: UInt64(data.count)) }
|
||||
} catch {
|
||||
os_log(.error, log: log, "[icon-car] ERROR: could not open catalog: %{public}@", error.localizedDescription)
|
||||
return nil
|
||||
}
|
||||
|
||||
if let validName = carVerifyNameExists(imageName, in: catalog) {
|
||||
if let bestImage = carFindHighestResolutionIcon(catalog.images(withName: validName)) {
|
||||
os_log(.debug, log: log, "[icon-car] using Assets.car with key %{public}@", validName)
|
||||
return NSImage(cgImage: bestImage.image, size: bestImage.size)
|
||||
}
|
||||
}
|
||||
return nil;
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Helper: Assets.car
|
||||
|
||||
/// Helper method to check available icon names. Will return a valid name or `nil` if no image with that key is found.
|
||||
func carVerifyNameExists(_ imageName: String, in catalog: CUICatalog) -> String? {
|
||||
if let availableNames = catalog.allImageNames(), !availableNames.contains(imageName) {
|
||||
// Theoretically this should never happen. Assuming the image name is found in an image file.
|
||||
os_log(.info, log: log, "[icon-car] WARN: key '%{public}@' does not match any available key", imageName)
|
||||
|
||||
if let alternativeName = carSearchAlternativeName(imageName, inAvailable: availableNames) {
|
||||
os_log(.info, log: log, "[icon-car] falling back to '%{public}@'", alternativeName)
|
||||
return alternativeName
|
||||
}
|
||||
os_log(.debug, log: log, "[icon-car] available keys: %{public}@", catalog.allImageNames() ?? [])
|
||||
return nil
|
||||
}
|
||||
return imageName;
|
||||
}
|
||||
|
||||
/// If exact name does not exist in catalog, search for a name that shares the same prefix.
|
||||
/// E.g., "AppIcon60x60" may match "AppIcon" or "AppIcon60x60_small"
|
||||
func carSearchAlternativeName(_ originalName: String, inAvailable availableNames: [String]) -> String? {
|
||||
var bestOption: String? = nil
|
||||
var bestDiff: Int = 999
|
||||
|
||||
for option in availableNames {
|
||||
if option.hasPrefix(originalName) || originalName.hasPrefix(option) {
|
||||
let thisDiff = max(originalName.count, option.count) - min(originalName.count, option.count)
|
||||
if thisDiff < bestDiff {
|
||||
bestDiff = thisDiff
|
||||
bestOption = option
|
||||
}
|
||||
}
|
||||
}
|
||||
return bestOption
|
||||
}
|
||||
|
||||
/// Given a list of `CUINamedImage`, return the one with the highest resolution. Vector graphics are ignored.
|
||||
func carFindHighestResolutionIcon(_ availableImages: [CUINamedImage]) -> CUINamedImage? {
|
||||
var largestWidth: CGFloat = 0
|
||||
var largestImage: CUINamedImage? = nil
|
||||
// cast to NSArray is necessary as otherwise this will crash
|
||||
for img in availableImages as NSArray {
|
||||
guard let img = img as? CUINamedImage else {
|
||||
continue // ignore CUINamedMultisizeImageSet
|
||||
}
|
||||
let w = img.size.width
|
||||
if w > largestWidth {
|
||||
largestWidth = w
|
||||
largestImage = img
|
||||
}
|
||||
}
|
||||
return largestImage
|
||||
}
|
||||
}
|
||||
105
src/AppIcon.swift
Normal file
105
src/AppIcon.swift
Normal file
@@ -0,0 +1,105 @@
|
||||
import Foundation
|
||||
import AppKit // NSImage
|
||||
import os // OSLog
|
||||
|
||||
private let log = OSLog(subsystem: Bundle.main.bundleIdentifier!, category: "AppIcon")
|
||||
|
||||
|
||||
struct AppIcon {
|
||||
let meta: QuickLookInfo
|
||||
|
||||
init(_ meta: QuickLookInfo) {
|
||||
self.meta = meta
|
||||
}
|
||||
|
||||
/// Try multiple methods to extract image.
|
||||
/// This method will always return an image even if none is found, in which case it returns the default image.
|
||||
func extractImage(from appPlist: PlistDict?) -> NSImage {
|
||||
// no need to unwrap the plist, and most .ipa should include the Artwork anyway
|
||||
if meta.type == .IPA {
|
||||
if let data = meta.zipFile!.unzipFile("iTunesArtwork") {
|
||||
os_log(.debug, log: log, "[icon] using iTunesArtwork.")
|
||||
return NSImage(data: data)!
|
||||
}
|
||||
}
|
||||
|
||||
// Extract image name from app plist
|
||||
var plistImgNames = iconNamesFromPlist(appPlist)
|
||||
os_log(.debug, log: log, "[icon] icon names in plist: %{public}@", plistImgNames)
|
||||
|
||||
// If no previous filename works (or empty), try default icon names
|
||||
plistImgNames.append("Icon")
|
||||
plistImgNames.append("icon")
|
||||
|
||||
// First, try if an image file with that name exists.
|
||||
if let actualName = expandImageName(plistImgNames) {
|
||||
os_log(.debug, log: log, "[icon] using plist image file %{public}@", actualName)
|
||||
if meta.type == .IPA {
|
||||
let data = meta.zipFile!.unzipFile(actualName)!
|
||||
return NSImage(data: data)!
|
||||
}
|
||||
return NSImage(contentsOfFile: actualName)!
|
||||
}
|
||||
|
||||
// Else: try Assets.car
|
||||
if let img = imageFromAssetsCar(plistImgNames.first!) {
|
||||
return img
|
||||
}
|
||||
|
||||
// Fallback to default icon
|
||||
let iconURL = Bundle.main.url(forResource: "defaultIcon", withExtension: "png")!
|
||||
return NSImage(contentsOf: iconURL)!
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Extension: NSImage
|
||||
|
||||
// AppIcon extension
|
||||
extension NSImage {
|
||||
/// Because some (PNG) image data will return weird float values
|
||||
private func bestImageSize() -> NSSize {
|
||||
var w: Int = 0
|
||||
var h: Int = 0
|
||||
for imageRep in self.representations {
|
||||
w = max(w, imageRep.pixelsWide)
|
||||
h = max(h, imageRep.pixelsHigh)
|
||||
}
|
||||
return NSSize(width: w, height: h)
|
||||
}
|
||||
|
||||
/// Apply rounded corners to image (iOS7 style)
|
||||
func withRoundCorners() -> NSImage {
|
||||
let existingSize = bestImageSize()
|
||||
let composedImage = NSImage(size: existingSize)
|
||||
|
||||
composedImage.lockFocus()
|
||||
NSGraphicsContext.current?.imageInterpolation = .high
|
||||
|
||||
let imageFrame = NSRect(origin: .zero, size: existingSize)
|
||||
let clipPath = NSBezierPath.IOS7RoundedRect(imageFrame, cornerRadius: existingSize.width * 0.225)
|
||||
clipPath.windingRule = .evenOdd
|
||||
clipPath.addClip()
|
||||
|
||||
self.draw(in: imageFrame)
|
||||
composedImage.unlockFocus()
|
||||
return composedImage
|
||||
}
|
||||
|
||||
/// Convert image to PNG and encode with base64 to be embeded in html output.
|
||||
func asBase64() -> String {
|
||||
// appIcon = [self roundCorners:appIcon];
|
||||
let imageData = tiffRepresentation!
|
||||
let imageRep = NSBitmapImageRep(data: imageData)!
|
||||
let imageDataPNG = imageRep.representation(using: .png, properties: [:])!
|
||||
return imageDataPNG.base64EncodedString()
|
||||
}
|
||||
|
||||
/// If the image is larger than the provided maximum size, scale it down. Otherwise leave it untouched.
|
||||
// func downscale(ifLargerThan maxSize: CGSize) {
|
||||
// // TODO: if downscale, then this should respect retina resolution
|
||||
// if size.width > maxSize.width && size.height > maxSize.height {
|
||||
// self.size = maxSize
|
||||
// }
|
||||
// }
|
||||
}
|
||||
143
src/Entitlements.swift
Normal file
143
src/Entitlements.swift
Normal file
@@ -0,0 +1,143 @@
|
||||
import Foundation
|
||||
import os // OSLog
|
||||
|
||||
private let log = OSLog(subsystem: Bundle.main.bundleIdentifier!, category: "Entitlements")
|
||||
|
||||
|
||||
struct Entitlements {
|
||||
var hasError: Bool = false
|
||||
/// only set after calling `applyFallbackIfNeeded(:)`
|
||||
var html: String? = nil
|
||||
|
||||
private let binaryPath: String
|
||||
/// It is either `plist` or `codeSignErrors` not both.
|
||||
private var plist: [String: Any]? = nil
|
||||
/// It is either `plist` or `codeSignErrors` not both.
|
||||
private var codeSignError: String? = nil
|
||||
|
||||
/// Use provision plist data without running `codesign` or
|
||||
static func withoutBinary() -> Self {
|
||||
return Entitlements(forBinary: nil)
|
||||
}
|
||||
|
||||
/// First, try to extract real entitlements by running `SecCode` module in-memory.
|
||||
/// If that fails, fallback to running `codesign` via system call.
|
||||
init(forBinary path: String?) {
|
||||
guard let path else {
|
||||
self.binaryPath = ""
|
||||
return
|
||||
}
|
||||
self.binaryPath = path
|
||||
if FileManager.default.fileExists(atPath: path) {
|
||||
self.plist = getSecCodeEntitlements()
|
||||
} else {
|
||||
os_log(.error, log: log, "[entitlements] provided binary '%{public}@' does not exist (unzip error?)", path)
|
||||
self.plist = nil
|
||||
self.codeSignError = nil
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - public methods
|
||||
|
||||
/// Provided provision plist is only used if @c SecCode and @c CodeSign failed.
|
||||
mutating func applyFallbackIfNeeded(_ fallbackEntitlementsPlist: PlistDict?) {
|
||||
// checking for !error ensures that codesign gets precedence.
|
||||
// show error before falling back to provision based entitlements.
|
||||
if plist == nil && codeSignError == nil {
|
||||
if let fallbackEntitlementsPlist {
|
||||
os_log(.debug, log: log, "[entitlements] fallback to provision plist entitlements")
|
||||
self.plist = fallbackEntitlementsPlist
|
||||
}
|
||||
}
|
||||
self.html = format(plist)
|
||||
self.plist = nil // free memory
|
||||
self.codeSignError = nil
|
||||
}
|
||||
|
||||
/// Print formatted plist in a @c \<pre> tag
|
||||
func format(_ plist: [String: Any]?) -> String? {
|
||||
guard let plist else {
|
||||
return codeSignError // may be nil
|
||||
}
|
||||
var output = ""
|
||||
recursiveKeyValue(plist, &output)
|
||||
return "<pre>\(output)</pre>"
|
||||
}
|
||||
|
||||
// MARK: - SecCode in-memory reader
|
||||
|
||||
/// use in-memory `SecCode` for entitlement extraction
|
||||
func getSecCodeEntitlements() -> PlistDict? {
|
||||
let url = URL(fileURLWithPath: self.binaryPath)
|
||||
var codeRef: SecStaticCode?
|
||||
SecStaticCodeCreateWithPath(url as CFURL, [], &codeRef)
|
||||
guard let codeRef else {
|
||||
return nil
|
||||
}
|
||||
|
||||
var requirementInfo: CFDictionary?
|
||||
SecCodeCopySigningInformation(codeRef, SecCSFlags(rawValue: kSecCSRequirementInformation), &requirementInfo)
|
||||
guard let requirementInfo = requirementInfo as? PlistDict else {
|
||||
return nil
|
||||
}
|
||||
|
||||
// if 'entitlements-dict' key exists, use that one
|
||||
os_log(.debug, log: log, "[entitlements] read SecCode 'entitlements-dict' key")
|
||||
if let plist = requirementInfo[kSecCodeInfoEntitlementsDict as String] as? PlistDict {
|
||||
return plist
|
||||
}
|
||||
|
||||
// else, fallback to parse data from 'entitlements' key
|
||||
os_log(.debug, log: log, "[entitlements] read SecCode 'entitlements' key")
|
||||
guard let data = requirementInfo[kSecCodeInfoEntitlements as String] as? Data else {
|
||||
return nil
|
||||
}
|
||||
|
||||
// expect magic number header. Currently no support for other formats.
|
||||
let header = data.subdata(in: 0..<4)
|
||||
guard header == Data([0xFA, 0xDE, 0x71, 0x71]) else {
|
||||
os_log(.error, log: log, "[entitlements] unsupported embedded plist format: %{public}@", header as NSData)
|
||||
return nil // try anyway?
|
||||
}
|
||||
|
||||
// big endian, so no memcpy for us :(
|
||||
let size: UInt32 = (UInt32(data[4]) << 24) | (UInt32(data[5]) << 16) | (UInt32(data[6]) << 8) | UInt32(data[7])
|
||||
if size != data.count {
|
||||
os_log(.error, log: log, "[entitlements] unpack error for FADE7171 size %lu != %lu", data.count, size)
|
||||
// but try anyway
|
||||
}
|
||||
return data.subdata(in: 8..<data.count).asPlistOrNil()
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Plist formatter
|
||||
|
||||
/// Print recursive tree of key-value mappings.
|
||||
func recursiveKeyValue(_ value: Any, _ output: inout String, _ level: Int = -1, _ key: String? = nil) {
|
||||
let indent = level > 0 ? String(repeating: " ", count: level * 4) : ""
|
||||
let prefix = indent + (key?.appending(" = ") ?? "")
|
||||
|
||||
if let dict = value as? [String: Any] {
|
||||
if level > -1 {
|
||||
output.append(prefix + "{\n")
|
||||
}
|
||||
for (subKey, subValue) in dict.sorted(by: { $0.key < $1.key }) {
|
||||
recursiveKeyValue(subValue, &output, level + 1, subKey)
|
||||
}
|
||||
if level > -1 {
|
||||
output.append(indent + "}\n")
|
||||
}
|
||||
} else if let array = value as? [Any] {
|
||||
output.append(prefix + "(\n")
|
||||
for element in array {
|
||||
recursiveKeyValue(element, &output, level + 1, nil)
|
||||
}
|
||||
output.append(indent + ")\n")
|
||||
} else if let data = value as? Data {
|
||||
output.append(prefix + "\(data.count) bytes of data\n")
|
||||
} else {
|
||||
output.append(prefix + "\(value)\n")
|
||||
}
|
||||
}
|
||||
}
|
||||
190
src/Plist.swift
Normal file
190
src/Plist.swift
Normal file
@@ -0,0 +1,190 @@
|
||||
import Foundation
|
||||
import os // OSLog
|
||||
|
||||
private let log = OSLog(subsystem: Bundle.main.bundleIdentifier!, category: "Plist")
|
||||
|
||||
|
||||
typealias PlistDict = [String: Any] // basically an untyped Dict
|
||||
|
||||
|
||||
// MARK: -
|
||||
|
||||
extension Data {
|
||||
/// Helper for optional chaining.
|
||||
func asPlistOrNil() -> PlistDict? {
|
||||
if self.isEmpty {
|
||||
return nil
|
||||
}
|
||||
// var format: PropertyListSerialization.PropertyListFormat = .xml
|
||||
do {
|
||||
return try PropertyListSerialization.propertyList(from: self, format: nil) as? PlistDict
|
||||
} catch {
|
||||
os_log(.error, log: log, "ERROR reading plist %{public}@", error.localizedDescription)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: -
|
||||
|
||||
extension QuickLookInfo {
|
||||
/// Read app default `Info.plist`.
|
||||
func readPlistApp() -> PlistDict? {
|
||||
switch self.type {
|
||||
case .IPA, .Archive, .Extension:
|
||||
return self.readPayloadFile("Info.plist")?.asPlistOrNil()
|
||||
}
|
||||
}
|
||||
|
||||
/// Read `iTunesMetadata.plist` if available
|
||||
func readPlistItunes() -> PlistDict? {
|
||||
switch self.type {
|
||||
case .IPA:
|
||||
return self.zipFile!.unzipFile("iTunesMetadata.plist")?.asPlistOrNil()
|
||||
case .Archive, .Extension:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
/// Read `embedded.mobileprovision` file and decode with CMS decoder.
|
||||
func readPlistProvision() -> PlistDict? {
|
||||
guard let provisionData = self.readPayloadFile("embedded.mobileprovision") else {
|
||||
os_log(.info, log: log, "No embedded.mobileprovision file for %{public}@", self.url.path)
|
||||
return nil
|
||||
}
|
||||
|
||||
var decoder: CMSDecoder? = nil
|
||||
CMSDecoderCreate(&decoder)
|
||||
let data = provisionData.withUnsafeBytes { ptr in
|
||||
CMSDecoderUpdateMessage(decoder!, ptr.baseAddress!, provisionData.count)
|
||||
CMSDecoderFinalizeMessage(decoder!)
|
||||
var dataRef: CFData?
|
||||
CMSDecoderCopyContent(decoder!, &dataRef)
|
||||
return Data(referencing: dataRef!)
|
||||
}
|
||||
return data.asPlistOrNil()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: -
|
||||
|
||||
extension AppIcon {
|
||||
/// Parse app plist to find the bundle icon filename.
|
||||
/// @param appPlist If `nil`, will load plist on the fly (used for thumbnail)
|
||||
/// @return Filenames which do not necessarily exist on filesystem. This may include `@2x` and/or no file extension.
|
||||
func iconNamesFromPlist(_ appPlist: PlistDict?) -> [String] {
|
||||
let appPlist = appPlist == nil ? meta.readPlistApp()! : appPlist!
|
||||
// Check for CFBundleIcons (since 5.0)
|
||||
if let icons = unpackNameListFromPlistDict(appPlist["CFBundleIcons"]), !icons.isEmpty {
|
||||
return icons
|
||||
}
|
||||
// iPad-only apps
|
||||
if let icons = unpackNameListFromPlistDict(appPlist["CFBundleIcons~ipad"]), !icons.isEmpty {
|
||||
return icons
|
||||
}
|
||||
// Check for CFBundleIconFiles (since 3.2)
|
||||
if let icons = appPlist["CFBundleIconFiles"] as? [String], !icons.isEmpty {
|
||||
return icons
|
||||
}
|
||||
// key found on iTunesU app
|
||||
if let icons = appPlist["Icon files"] as? [String], !icons.isEmpty {
|
||||
return icons
|
||||
}
|
||||
// Check for CFBundleIconFile (legacy, before 3.2)
|
||||
if let icon = appPlist["CFBundleIconFile"] as? String { // may be nil
|
||||
return [icon]
|
||||
}
|
||||
return [] // [self sortedByResolution:icons];
|
||||
}
|
||||
|
||||
/// Given a filename, search Bundle or Filesystem for files that match. Select the filename with the highest resolution.
|
||||
func expandImageName(_ iconList: [String]) -> String? {
|
||||
var matches: [String] = []
|
||||
switch meta.type {
|
||||
case .IPA:
|
||||
guard let zipFile = meta.zipFile else {
|
||||
// in case unzip in memory is not available, fallback to pattern matching with dynamic suffix
|
||||
return "Payload/*.app/\(iconList.first!)*"
|
||||
}
|
||||
for iconPath in iconList {
|
||||
let zipPath = "Payload/*.app/\(iconPath)*"
|
||||
for zip in zipFile.filesMatching(zipPath) {
|
||||
if zip.sizeUncompressed > 0 {
|
||||
matches.append(zip.filepath)
|
||||
}
|
||||
}
|
||||
if matches.count > 0 {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
case .Archive, .Extension:
|
||||
let basePath = meta.effectiveUrl ?? meta.url
|
||||
for iconPath in iconList {
|
||||
let fileName = iconPath.components(separatedBy: "/").last!
|
||||
let parentDir = basePath.appendingPathComponent(iconPath, isDirectory: false).deletingLastPathComponent().path
|
||||
guard let files = try? FileManager.default.contentsOfDirectory(atPath: parentDir) else {
|
||||
continue
|
||||
}
|
||||
for file in files {
|
||||
if file.hasPrefix(fileName) {
|
||||
let fullPath = parentDir + "/" + file
|
||||
if let fSize = try? FileManager.default.attributesOfItem(atPath: fullPath)[FileAttributeKey.size] as? Int {
|
||||
if fSize > 0 {
|
||||
matches.append(fullPath)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if matches.count > 0 {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
return matches.isEmpty ? nil : sortedByResolution(matches).first
|
||||
}
|
||||
|
||||
/// Deep select icons from plist key `CFBundleIcons` and `CFBundleIcons~ipad`
|
||||
private func unpackNameListFromPlistDict(_ bundleDict: Any?) -> [String]? {
|
||||
if let bundleDict = bundleDict as? PlistDict {
|
||||
if let primaryDict = bundleDict["CFBundlePrimaryIcon"] as? PlistDict {
|
||||
if let icons = primaryDict["CFBundleIconFiles"] as? [String] {
|
||||
return icons
|
||||
}
|
||||
if let name = primaryDict["CFBundleIconName"] as? String { // key found on a .tipa file
|
||||
return [name]
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
/// @return lower index means higher resolution.
|
||||
private func resolutionIndex(_ iconName: String) -> Int {
|
||||
let lower = iconName.lowercased()
|
||||
// "defaultX" = launch image
|
||||
let penalty = lower.contains("small") || lower.hasPrefix("default") ? 20 : 0
|
||||
|
||||
let resolutionOrder: [String] = [
|
||||
"@3x", "180", "167", "152", "@2x", "120",
|
||||
"144", "114", "87", "80", "76", "72", "58", "57"
|
||||
]
|
||||
for (i, res) in resolutionOrder.enumerated() {
|
||||
if iconName.contains(res) {
|
||||
return i + penalty
|
||||
}
|
||||
}
|
||||
return 50 + penalty
|
||||
}
|
||||
|
||||
/// Given a list of filenames, order them highest resolution first.
|
||||
private func sortedByResolution(_ icons: [String]) -> [String] {
|
||||
return icons.sorted { (icon1, icon2) -> Bool in
|
||||
let index1 = self.resolutionIndex(icon1)
|
||||
let index2 = self.resolutionIndex(icon2)
|
||||
return index1 < index2
|
||||
}
|
||||
}
|
||||
}
|
||||
555
src/PreviewGenerator.swift
Normal file
555
src/PreviewGenerator.swift
Normal file
@@ -0,0 +1,555 @@
|
||||
import Foundation
|
||||
import os // OSLog
|
||||
|
||||
private let log = OSLog(subsystem: Bundle.main.bundleIdentifier!, category: "PreviewGenerator")
|
||||
|
||||
typealias HtmlDict = [String: String] // used for TAG replacements
|
||||
|
||||
|
||||
// MARK: - Generic data formatting & printing
|
||||
|
||||
typealias TableRow = [String]
|
||||
|
||||
/// Print html table with arbitrary number of columns
|
||||
/// @param header If set, start the table with a `tr` column row.
|
||||
func formatAsTable(_ data: [[String]], header: TableRow? = nil) -> String {
|
||||
var table = "<table>\n"
|
||||
if let header = header {
|
||||
table += "<tr><th>\(header.joined(separator: "</th><th>"))</th></tr>\n"
|
||||
}
|
||||
for row in data {
|
||||
table += "<tr><td>\(row.joined(separator: "</td><td>"))</td></tr>\n"
|
||||
}
|
||||
return table + "</table>\n"
|
||||
}
|
||||
|
||||
/// Print recursive tree of key-value mappings.
|
||||
func recursiveDict(_ dictionary: [String: Any], withReplacements replacements: [String: String] = [:], _ level: Int = 0) -> String {
|
||||
var output = ""
|
||||
for (key, value) in dictionary {
|
||||
let localizedKey = replacements[key] ?? key
|
||||
for _ in 0..<level {
|
||||
output += (level == 1) ? "- " : " "
|
||||
}
|
||||
|
||||
if let subDict = value as? [String: Any] {
|
||||
output += "\(localizedKey):<div class=\"list\">\n"
|
||||
output += recursiveDict(subDict, withReplacements: replacements, level + 1)
|
||||
output += "</div>\n"
|
||||
} else if let number = value as? NSNumber {
|
||||
output += "\(localizedKey): \(number.boolValue ? "YES" : "NO")<br />"
|
||||
} else {
|
||||
output += "\(localizedKey): \(value)<br />"
|
||||
}
|
||||
}
|
||||
return output
|
||||
}
|
||||
|
||||
/// Replace occurrences of chars `&"'<>` with html encoding.
|
||||
func escapeXML(_ stringToEscape: String) -> String {
|
||||
return stringToEscape
|
||||
.replacingOccurrences(of: "&", with: "&")
|
||||
.replacingOccurrences(of: "\"", with: """)
|
||||
.replacingOccurrences(of: "'", with: "'")
|
||||
.replacingOccurrences(of: "<", with: "<")
|
||||
.replacingOccurrences(of: ">", with: ">")
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Date processing
|
||||
|
||||
/// @return Difference between two dates as components.
|
||||
func dateDiff(_ start: Date, _ end: Date) -> DateComponents {
|
||||
return Calendar.current.dateComponents([.day, .hour, .minute], from: start, to: end)
|
||||
}
|
||||
|
||||
/// @return Print largest component. E.g., "3 days" or "14 hours"
|
||||
func relativeDateString(_ comp: DateComponents) -> String {
|
||||
let formatter = DateComponentsFormatter()
|
||||
formatter.unitsStyle = .full
|
||||
formatter.maximumUnitCount = 1
|
||||
return formatter.string(from: comp)!
|
||||
}
|
||||
|
||||
/// @return Print the date with current locale and medium length style.
|
||||
func formattedDate(_ date: Date) -> String {
|
||||
let formatter = DateFormatter()
|
||||
formatter.dateStyle = .medium
|
||||
formatter.timeStyle = .medium
|
||||
return formatter.string(from: date)
|
||||
}
|
||||
|
||||
/// Parse date from plist regardless if it has `NSDate` or `NSString` type.
|
||||
func parseDate(_ value: Any?) -> Date? {
|
||||
if let date = value as? Date {
|
||||
return date
|
||||
}
|
||||
|
||||
guard let stringValue = value as? String else {
|
||||
return nil
|
||||
}
|
||||
|
||||
// parse the date from a string
|
||||
let formatter = DateFormatter()
|
||||
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss Z"
|
||||
if let date = formatter.date(from: stringValue) {
|
||||
return date
|
||||
}
|
||||
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
|
||||
if let date = formatter.date(from: stringValue) {
|
||||
return date
|
||||
}
|
||||
os_log(.error, log: log, "ERROR formatting date: %{public}@", stringValue)
|
||||
return nil
|
||||
}
|
||||
|
||||
/// @return Relative distance to today. E.g., "Expired today"
|
||||
func relativeExpirationDateString(_ date: Date) -> String {
|
||||
let isPast = date < Date()
|
||||
let isToday = Calendar.current.isDateInToday(date)
|
||||
|
||||
if isToday {
|
||||
return isPast ? "<span>Expired today</span>" : "<span>Expires today</span>"
|
||||
}
|
||||
|
||||
if isPast {
|
||||
let comp = dateDiff(date, Date())
|
||||
return "<span>Expired \(relativeDateString(comp)) ago</span>"
|
||||
}
|
||||
|
||||
let comp = dateDiff(Date(), date)
|
||||
if comp.day! < 30 {
|
||||
return "<span>Expires in \(relativeDateString(comp))</span>"
|
||||
}
|
||||
return "Expires in \(relativeDateString(comp))"
|
||||
}
|
||||
|
||||
/// @return Relative distance to today. E.g., "DATE (Expires in 3 days)"
|
||||
func formattedExpirationDate(_ date: Date) -> String {
|
||||
return "\(formattedDate(date)) (\(relativeExpirationDateString(date)))"
|
||||
}
|
||||
|
||||
/// @return Relative distance to today. E.g., "DATE (Created 3 days ago)"
|
||||
func formattedCreationDate(_ date: Date) -> String {
|
||||
let isToday = Calendar.current.isDateInToday(date)
|
||||
let comp = dateDiff(date, Date())
|
||||
return "\(formattedDate(date)) (Created \(isToday ? "today" : "\(relativeDateString(comp)) ago"))"
|
||||
}
|
||||
|
||||
/// @return CSS class for expiration status.
|
||||
func classNameForExpirationStatus(_ date: Date?) -> String {
|
||||
switch ExpirationStatus(date) {
|
||||
case .Expired: return "expired"
|
||||
case .Expiring: return "expiring"
|
||||
case .Valid: return "valid"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - App Info
|
||||
|
||||
/// @return List of ATS flags.
|
||||
func formattedAppTransportSecurity(_ appPlist: PlistDict) -> String {
|
||||
if let value = appPlist["NSAppTransportSecurity"] as? PlistDict {
|
||||
let localizedKeys = [
|
||||
"NSAllowsArbitraryLoads": "Allows Arbitrary Loads",
|
||||
"NSAllowsArbitraryLoadsForMedia": "Allows Arbitrary Loads for Media",
|
||||
"NSAllowsArbitraryLoadsInWebContent": "Allows Arbitrary Loads in Web Content",
|
||||
"NSAllowsLocalNetworking": "Allows Local Networking",
|
||||
"NSExceptionDomains": "Exception Domains",
|
||||
|
||||
"NSIncludesSubdomains": "Includes Subdomains",
|
||||
"NSRequiresCertificateTransparency": "Requires Certificate Transparency",
|
||||
|
||||
"NSExceptionAllowsInsecureHTTPLoads": "Allows Insecure HTTP Loads",
|
||||
"NSExceptionMinimumTLSVersion": "Minimum TLS Version",
|
||||
"NSExceptionRequiresForwardSecrecy": "Requires Forward Secrecy",
|
||||
|
||||
"NSThirdPartyExceptionAllowsInsecureHTTPLoads": "Allows Insecure HTTP Loads",
|
||||
"NSThirdPartyExceptionMinimumTLSVersion": "Minimum TLS Version",
|
||||
"NSThirdPartyExceptionRequiresForwardSecrecy": "Requires Forward Secrecy",
|
||||
]
|
||||
|
||||
return "<div class=\"list\">\(recursiveDict(value, withReplacements: localizedKeys))</div>"
|
||||
}
|
||||
|
||||
let sdkName = appPlist["DTSDKName"] as? String ?? "0"
|
||||
let sdkNumber = Double(sdkName.trimmingCharacters(in: .letters)) ?? 0
|
||||
if sdkNumber < 9.0 {
|
||||
return "Not applicable before iOS 9.0"
|
||||
}
|
||||
return "No exceptions"
|
||||
}
|
||||
|
||||
/// Process info stored in `Info.plist`
|
||||
func procAppInfo(_ appPlist: PlistDict?) -> HtmlDict {
|
||||
guard let appPlist else {
|
||||
return [
|
||||
"AppInfoHidden": "hiddenDiv",
|
||||
"ProvisionTitleHidden": "",
|
||||
]
|
||||
}
|
||||
|
||||
var platforms = (appPlist["UIDeviceFamily"] as? [Int])?.compactMap({
|
||||
switch $0 {
|
||||
case 1: return "iPhone"
|
||||
case 2: return "iPad"
|
||||
case 3: return "TV"
|
||||
case 4: return "Watch"
|
||||
default: return nil
|
||||
}
|
||||
}).joined(separator: ", ")
|
||||
|
||||
let minVersion = appPlist["MinimumOSVersion"] as? String ?? ""
|
||||
if platforms?.isEmpty ?? true, minVersion.hasPrefix("1.") || minVersion.hasPrefix("2.") || minVersion.hasPrefix("3.") {
|
||||
platforms = "iPhone"
|
||||
}
|
||||
|
||||
let extensionType = (appPlist["NSExtension"] as? PlistDict)?["NSExtensionPointIdentifier"] as? String
|
||||
return [
|
||||
"AppInfoHidden": "",
|
||||
"ProvisionTitleHidden": "hiddenDiv",
|
||||
|
||||
"CFBundleName": appPlist["CFBundleDisplayName"] as? String ?? appPlist["CFBundleName"] as? String ?? "",
|
||||
"CFBundleShortVersionString": appPlist["CFBundleShortVersionString"] as? String ?? "",
|
||||
"CFBundleVersion": appPlist["CFBundleVersion"] as? String ?? "",
|
||||
"CFBundleIdentifier": appPlist["CFBundleIdentifier"] as? String ?? "",
|
||||
|
||||
"ExtensionTypeHidden": extensionType != nil ? "" : "hiddenDiv",
|
||||
"ExtensionType": extensionType ?? "",
|
||||
|
||||
"UIDeviceFamily": platforms ?? "",
|
||||
"DTSDKName": appPlist["DTSDKName"] as? String ?? "",
|
||||
"MinimumOSVersion": minVersion,
|
||||
"AppTransportSecurityFormatted": formattedAppTransportSecurity(appPlist),
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
// MARK: - iTunes Purchase Information
|
||||
|
||||
/// Concatenate all (sub)genres into a comma separated list.
|
||||
func formattedGenres(_ itunesPlist: PlistDict) -> String {
|
||||
var genres: [String] = []
|
||||
let genreId = itunesPlist["genreId"] as? Int ?? 0
|
||||
if let mainGenre = AppCategories[genreId] ?? itunesPlist["genre"] as? String {
|
||||
genres.append(mainGenre)
|
||||
}
|
||||
|
||||
for subgenre in itunesPlist["subgenres"] as? [PlistDict] ?? [] {
|
||||
let subgenreId = subgenre["genreId"] as? Int ?? 0
|
||||
if let subgenreStr = AppCategories[subgenreId] ?? subgenre["genre"] as? String {
|
||||
genres.append(subgenreStr)
|
||||
}
|
||||
}
|
||||
return genres.joined(separator: ", ")
|
||||
}
|
||||
|
||||
/// Process info stored in `iTunesMetadata.plist`
|
||||
func parseItunesMeta(_ itunesPlist: PlistDict?) -> HtmlDict {
|
||||
guard let itunesPlist else {
|
||||
return ["iTunesHidden": "hiddenDiv"]
|
||||
}
|
||||
|
||||
let downloadInfo = itunesPlist["com.apple.iTunesStore.downloadInfo"] as? PlistDict
|
||||
let accountInfo = downloadInfo?["accountInfo"] as? PlistDict ?? [:]
|
||||
|
||||
let purchaseDate = parseDate(downloadInfo?["purchaseDate"] ?? itunesPlist["purchaseDate"])
|
||||
let releaseDate = parseDate(downloadInfo?["releaseDate"] ?? itunesPlist["releaseDate"])
|
||||
// AppleId & purchaser name
|
||||
let appleId = accountInfo["AppleID"] as? String ?? itunesPlist["appleId"] as? String ?? ""
|
||||
let firstName = accountInfo["FirstName"] as? String ?? ""
|
||||
let lastName = accountInfo["LastName"] as? String ?? ""
|
||||
|
||||
let name: String
|
||||
if !firstName.isEmpty || !lastName.isEmpty {
|
||||
name = "\(firstName) \(lastName) (\(appleId))"
|
||||
} else {
|
||||
name = appleId
|
||||
}
|
||||
os_log(.error, log: log, "id: %{public}@", String(describing: itunesPlist["itemId"]))
|
||||
return [
|
||||
"iTunesHidden": "",
|
||||
"iTunesId": (itunesPlist["itemId"] as? Int)?.description ?? "", // description]
|
||||
"iTunesName": itunesPlist["itemName"] as? String ?? "",
|
||||
"iTunesGenres": formattedGenres(itunesPlist),
|
||||
"iTunesReleaseDate": releaseDate == nil ? "" : formattedDate(releaseDate!),
|
||||
|
||||
"iTunesAppleId": name,
|
||||
"iTunesPurchaseDate": purchaseDate == nil ? "" : formattedDate(purchaseDate!),
|
||||
"iTunesPrice": itunesPlist["priceDisplay"] as? String ?? "",
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Certificates
|
||||
|
||||
/// Process a single certificate. Extract invalidity / expiration date.
|
||||
/// @param subject just used for printing error logs.
|
||||
func getCertificateInvalidityDate(_ certificate: SecCertificate, subject: String) -> Date? {
|
||||
var error: Unmanaged<CFError>?
|
||||
guard let outerDict = SecCertificateCopyValues(certificate, [kSecOIDInvalidityDate] as CFArray, &error) as? PlistDict else {
|
||||
os_log(.error, log: log, "Could not get values in '%{public}@' certificate, error = %{public}@", subject, error?.takeUnretainedValue().localizedDescription ?? "unknown error")
|
||||
return nil
|
||||
}
|
||||
guard let innerDict = outerDict[kSecOIDInvalidityDate as String] as? PlistDict else {
|
||||
os_log(.error, log: log, "No invalidity values in '%{public}@' certificate, dictionary = %{public}@", subject, outerDict)
|
||||
return nil
|
||||
}
|
||||
// NOTE: the invalidity date type of kSecPropertyTypeDate is documented as a CFStringRef in the "Certificate, Key, and Trust Services Reference".
|
||||
// In reality, it's a __NSTaggedDate (presumably a tagged pointer representing an NSDate.) But to be sure, we'll check:
|
||||
guard let dateString = innerDict[kSecPropertyKeyValue as String] else {
|
||||
os_log(.error, log: log, "No invalidity date in '%{public}@' certificate, dictionary = %{public}@", subject, innerDict)
|
||||
return nil
|
||||
}
|
||||
return parseDate(dateString);
|
||||
}
|
||||
|
||||
/// Process list of all certificates. Return a two column table with subject and expiration date.
|
||||
func getCertificateList(_ provisionPlist: PlistDict) -> [TableRow] {
|
||||
guard let certs = provisionPlist["DeveloperCertificates"] as? [Data] else {
|
||||
return []
|
||||
}
|
||||
return certs.compactMap {
|
||||
guard let cert = SecCertificateCreateWithData(nil, $0 as CFData) else {
|
||||
return nil
|
||||
}
|
||||
guard let subject = SecCertificateCopySubjectSummary(cert) as? String else {
|
||||
os_log(.error, log: log, "Could not get subject from certificate")
|
||||
return nil
|
||||
}
|
||||
let expiration: String
|
||||
if let invalidityDate = getCertificateInvalidityDate(cert, subject: subject) {
|
||||
expiration = relativeExpirationDateString(invalidityDate)
|
||||
} else {
|
||||
expiration = "<span class='warning'>No invalidity date in certificate</span>"
|
||||
}
|
||||
return TableRow([subject, expiration])
|
||||
}.sorted { $0[0] < $1[0] }
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Provisioning
|
||||
|
||||
/// Returns provision type string like "Development" or "Distribution (App Store)".
|
||||
func stringForProfileType(_ provisionPlist: PlistDict, isOSX: Bool) -> String {
|
||||
let hasDevices = provisionPlist["ProvisionedDevices"] is [Any]
|
||||
if isOSX {
|
||||
return hasDevices ? "Development" : "Distribution (App Store)"
|
||||
}
|
||||
if hasDevices {
|
||||
let getTaskAllow = (provisionPlist["Entitlements"] as? PlistDict)?["get-task-allow"] as? Bool ?? false
|
||||
return getTaskAllow ? "Development" : "Distribution (Ad Hoc)"
|
||||
}
|
||||
let isEnterprise = provisionPlist["ProvisionsAllDevices"] as? Bool ?? false
|
||||
return isEnterprise ? "Enterprise" : "Distribution (App Store)"
|
||||
}
|
||||
|
||||
/// Enumerate all entries from provison plist with key `ProvisionedDevices`
|
||||
func getDeviceList(_ provisionPlist: PlistDict) -> [TableRow] {
|
||||
guard let devArr = provisionPlist["ProvisionedDevices"] as? [String] else {
|
||||
return []
|
||||
}
|
||||
var currentPrefix: String? = nil
|
||||
return devArr.sorted().map { device in
|
||||
// compute the prefix for the first column of the table
|
||||
let displayPrefix: String
|
||||
let devicePrefix = String(device.prefix(1))
|
||||
if currentPrefix != devicePrefix {
|
||||
currentPrefix = devicePrefix
|
||||
displayPrefix = "\(devicePrefix) ➞ "
|
||||
} else {
|
||||
displayPrefix = ""
|
||||
}
|
||||
return [displayPrefix, device]
|
||||
}
|
||||
}
|
||||
|
||||
/// Process info stored in `embedded.mobileprovision`
|
||||
func procProvision(_ provisionPlist: PlistDict?, isOSX: Bool) -> HtmlDict {
|
||||
guard let provisionPlist else {
|
||||
return ["ProvisionHidden": "hiddenDiv"]
|
||||
}
|
||||
|
||||
let creationDate = provisionPlist["CreationDate"] as? Date
|
||||
let expireDate = provisionPlist["ExpirationDate"] as? Date
|
||||
let devices = getDeviceList(provisionPlist)
|
||||
let certs = getCertificateList(provisionPlist)
|
||||
|
||||
return [
|
||||
"ProvisionHidden": "",
|
||||
"ProfileName": provisionPlist["Name"] as? String ?? "",
|
||||
"ProfileUUID": provisionPlist["UUID"] as? String ?? "",
|
||||
"TeamName": provisionPlist["TeamName"] as? String ?? "<em>Team name not available</em>",
|
||||
"TeamIds": (provisionPlist["TeamIdentifier"] as? [String])?.joined(separator: ", ") ?? "<em>Team ID not available</em>",
|
||||
"CreationDateFormatted": creationDate == nil ? "" : formattedCreationDate(creationDate!),
|
||||
"ExpirationDateFormatted": expireDate == nil ? "" : formattedExpirationDate(expireDate!),
|
||||
"ExpStatus": classNameForExpirationStatus(expireDate),
|
||||
|
||||
"ProfilePlatform": isOSX ? "Mac" : "iOS",
|
||||
"ProfileType": stringForProfileType(provisionPlist, isOSX: isOSX),
|
||||
|
||||
"ProvisionedDevicesCount": devices.isEmpty ? "No Devices" : "\(devices.count) Device\(devices.count == 1 ? "" : "s")",
|
||||
"ProvisionedDevicesFormatted": devices.isEmpty ? "Distribution Profile" : formatAsTable(devices, header: ["", "UDID"]),
|
||||
|
||||
"DeveloperCertificatesFormatted": certs.isEmpty ? "No Developer Certificates" : formatAsTable(certs),
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Entitlements
|
||||
|
||||
/// Search for app binary and run `codesign` on it.
|
||||
func readEntitlements(_ meta: QuickLookInfo, _ bundleExecutable: String?) -> Entitlements {
|
||||
guard let bundleExecutable else {
|
||||
return Entitlements.withoutBinary()
|
||||
}
|
||||
|
||||
switch meta.type {
|
||||
case .IPA:
|
||||
let tmpPath = NSTemporaryDirectory() + "/" + UUID().uuidString
|
||||
try! FileManager.default.createDirectory(atPath: tmpPath, withIntermediateDirectories: true)
|
||||
defer {
|
||||
try? FileManager.default.removeItem(atPath: tmpPath)
|
||||
}
|
||||
try! meta.zipFile!.unzipFile("Payload/*.app/\(bundleExecutable)", toDir: tmpPath)
|
||||
return Entitlements(forBinary: tmpPath + "/" + bundleExecutable)
|
||||
case .Archive:
|
||||
return Entitlements(forBinary: meta.effectiveUrl!.path + "/" + bundleExecutable)
|
||||
case .Extension:
|
||||
return Entitlements(forBinary: meta.url.path + "/" + bundleExecutable)
|
||||
}
|
||||
}
|
||||
|
||||
/// Process compiled binary and provision plist to extract `Entitlements`
|
||||
func procEntitlements(_ meta: QuickLookInfo, _ appPlist: PlistDict?, _ provisionPlist: PlistDict?) -> HtmlDict {
|
||||
var entitlements = readEntitlements(meta, appPlist?["CFBundleExecutable"] as? String)
|
||||
entitlements.applyFallbackIfNeeded(provisionPlist?["Entitlements"] as? PlistDict)
|
||||
|
||||
return [
|
||||
"EntitlementsWarningHidden": entitlements.hasError ? "" : "hiddenDiv",
|
||||
"EntitlementsFormatted": entitlements.html ?? "No Entitlements",
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
// MARK: - File Info
|
||||
|
||||
/// Title of the preview window
|
||||
func stringForFileType(_ meta: QuickLookInfo) -> String {
|
||||
switch meta.type {
|
||||
case .IPA: return "App info"
|
||||
case .Archive: return "Archive info"
|
||||
case .Extension: return "App extension info"
|
||||
}
|
||||
}
|
||||
|
||||
/// Calculate file / folder size.
|
||||
func getFileSize(_ path: String) -> Int64 {
|
||||
var isDir: ObjCBool = false
|
||||
FileManager.default.fileExists(atPath: path, isDirectory: &isDir)
|
||||
if !isDir.boolValue {
|
||||
return try! FileManager.default.attributesOfItem(atPath: path)[.size] as! Int64
|
||||
}
|
||||
var fileSize: Int64 = 0
|
||||
for child in try! FileManager.default.subpathsOfDirectory(atPath: path) {
|
||||
fileSize += try! FileManager.default.attributesOfItem(atPath: path + "/" + child)[.size] as! Int64
|
||||
}
|
||||
return fileSize
|
||||
}
|
||||
|
||||
/// Process meta information about the file itself. Like file size and last modification.
|
||||
func procFileInfo(_ url: URL) -> HtmlDict {
|
||||
let formattedValue : String
|
||||
if let attrs = try? FileManager.default.attributesOfItem(atPath: url.path) {
|
||||
let size = ByteCountFormatter.string(fromByteCount: getFileSize(url.path), countStyle: .file)
|
||||
formattedValue = "\(size), Modified \(formattedDate(attrs[.modificationDate] as! Date))"
|
||||
} else {
|
||||
formattedValue = ""
|
||||
}
|
||||
return [
|
||||
"FileName": escapeXML(url.lastPathComponent),
|
||||
"FileInfo": formattedValue,
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Footer Info
|
||||
|
||||
/// Process meta information about the plugin. Like version and debug flag.
|
||||
func procFooterInfo() -> HtmlDict {
|
||||
#if DEBUG
|
||||
let debugString = "(debug)"
|
||||
#else
|
||||
let debugString = ""
|
||||
#endif
|
||||
return [
|
||||
"DEBUG": debugString,
|
||||
"BundleShortVersionString": Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? "",
|
||||
"BundleVersion": Bundle.main.infoDictionary?["CFBundleVersion"] as? String ?? "",
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Main Entry
|
||||
|
||||
func applyHtmlTemplate(_ templateValues: HtmlDict) -> String {
|
||||
let templateURL = Bundle.main.url(forResource: "template", withExtension: "html")!
|
||||
let html = try! String(contentsOf: templateURL, encoding: .utf8)
|
||||
|
||||
// this is less efficient
|
||||
// for (key, value) in templateValues {
|
||||
// html = html.replacingOccurrences(of: "__\(key)__", with: value)
|
||||
// }
|
||||
|
||||
var rv = ""
|
||||
var prevLoc = html.startIndex
|
||||
let regex = try! NSRegularExpression(pattern: "__[^ _]{1,40}?__")
|
||||
regex.enumerateMatches(in: html, range: NSRange(location: 0, length: html.count), using: { match, flags, stop in
|
||||
let start = html.index(html.startIndex, offsetBy: match!.range.lowerBound)
|
||||
let key = String(html[html.index(start, offsetBy: 2) ..< html.index(start, offsetBy: match!.range.length - 2)])
|
||||
// append unrelated text up to this key
|
||||
rv.append(contentsOf: html[prevLoc ..< start])
|
||||
prevLoc = html.index(start, offsetBy: match!.range.length)
|
||||
// append key if exists (else remove template-key)
|
||||
if let value = templateValues[key] {
|
||||
rv.append(value)
|
||||
} else {
|
||||
// os_log(.debug, log: log, "unknown template key: %{public}@", key)
|
||||
}
|
||||
})
|
||||
// append remaining text
|
||||
rv.append(contentsOf: html[prevLoc ..< html.endIndex])
|
||||
return rv
|
||||
}
|
||||
|
||||
func generateHtml(at url: URL) -> String {
|
||||
let meta = QuickLookInfo(url)
|
||||
var infoLayer: HtmlDict = [
|
||||
"AppInfoTitle": stringForFileType(meta),
|
||||
]
|
||||
|
||||
// App Info
|
||||
let plistApp = meta.readPlistApp()
|
||||
infoLayer.merge(procAppInfo(plistApp)) { (_, new) in new }
|
||||
|
||||
let plistItunes = meta.readPlistItunes()
|
||||
infoLayer.merge(parseItunesMeta(plistItunes)) { (_, new) in new }
|
||||
|
||||
// Provisioning
|
||||
let plistProvision = meta.readPlistProvision()
|
||||
infoLayer.merge(procProvision(plistProvision, isOSX: meta.isOSX)) { (_, new) in new }
|
||||
|
||||
// Entitlements
|
||||
let entitlements = procEntitlements(meta, plistApp, plistProvision)
|
||||
infoLayer.merge(entitlements) { (_, new) in new }
|
||||
// File Info
|
||||
infoLayer.merge(procFileInfo(url)) { (_, new) in new }
|
||||
// Footer Info
|
||||
infoLayer.merge(procFooterInfo()) { (_, new) in new }
|
||||
// App Icon (last, because the image uses a lot of memory)
|
||||
let icon = AppIcon(meta)
|
||||
infoLayer["AppIcon"] = icon.extractImage(from: plistApp).withRoundCorners().asBase64()
|
||||
// prepare html, replace values
|
||||
return applyHtmlTemplate(infoLayer)
|
||||
}
|
||||
58
src/RoundedIcon.swift
Normal file
58
src/RoundedIcon.swift
Normal file
@@ -0,0 +1,58 @@
|
||||
import Foundation
|
||||
import AppKit // NSBezierPath
|
||||
|
||||
//
|
||||
// NSBezierPath+IOS7RoundedRect
|
||||
//
|
||||
// Created by Matej Dunik on 11/12/13.
|
||||
// Copyright (c) 2013 PixelCut. All rights reserved except as below:
|
||||
// This code is provided as-is, without warranty of any kind. You may use it in your projects as you wish.
|
||||
//
|
||||
|
||||
extension NSBezierPath {
|
||||
public class func IOS7RoundedRect(_ rect: NSRect, cornerRadius: CGFloat) -> NSBezierPath {
|
||||
let path = NSBezierPath()
|
||||
let limit = min(rect.size.width, rect.size.height) / 2 / 1.52866483
|
||||
let limitedRadius = min(cornerRadius, limit)
|
||||
|
||||
@inline(__always) func topLeft(_ x: CGFloat, _ y: CGFloat) -> NSPoint {
|
||||
return NSPoint(x: rect.origin.x + x * limitedRadius, y: rect.origin.y + y * limitedRadius)
|
||||
}
|
||||
|
||||
@inline(__always) func topRight(_ x: CGFloat, _ y: CGFloat) -> NSPoint {
|
||||
return NSPoint(x: rect.origin.x + rect.size.width - x * limitedRadius, y: rect.origin.y + y * limitedRadius)
|
||||
}
|
||||
|
||||
@inline(__always) func bottomRight(_ x: CGFloat, _ y: CGFloat) -> NSPoint {
|
||||
return NSPoint(x: rect.origin.x + rect.size.width - x * limitedRadius, y: rect.origin.y + rect.size.height - y * limitedRadius)
|
||||
}
|
||||
|
||||
@inline(__always) func bottomLeft(_ x: CGFloat, _ y: CGFloat) -> NSPoint {
|
||||
return NSPoint(x: rect.origin.x + x * limitedRadius, y: rect.origin.y + rect.size.height - y * limitedRadius)
|
||||
}
|
||||
|
||||
path.move(to: topLeft(1.52866483, 0.00000000))
|
||||
path.line(to: topRight(1.52866471, 0.00000000))
|
||||
path.curve(to: topRight(0.66993427, 0.06549600), controlPoint1: topRight(1.08849323, 0.00000000), controlPoint2: topRight(0.86840689, 0.00000000))
|
||||
path.line(to: topRight(0.63149399, 0.07491100))
|
||||
path.curve(to: topRight(0.07491176, 0.63149399), controlPoint1: topRight(0.37282392, 0.16905899), controlPoint2: topRight(0.16906013, 0.37282401))
|
||||
path.curve(to: topRight(0.00000000, 1.52866483), controlPoint1: topRight(0.00000000, 0.86840701), controlPoint2: topRight(0.00000000, 1.08849299))
|
||||
path.line(to: bottomRight(0.00000000, 1.52866471))
|
||||
path.curve(to: bottomRight(0.06549569, 0.66993493), controlPoint1: bottomRight(0.00000000, 1.08849323), controlPoint2: bottomRight(0.00000000, 0.86840689))
|
||||
path.line(to: bottomRight(0.07491111, 0.63149399))
|
||||
path.curve(to: bottomRight(0.63149399, 0.07491111), controlPoint1: bottomRight(0.16905883, 0.37282392), controlPoint2: bottomRight(0.37282392, 0.16905883))
|
||||
path.curve(to: bottomRight(1.52866471, 0.00000000), controlPoint1: bottomRight(0.86840689, 0.00000000), controlPoint2: bottomRight(1.08849323, 0.00000000))
|
||||
path.line(to: bottomLeft(1.52866483, 0.00000000))
|
||||
path.curve(to: bottomLeft(0.66993397, 0.06549569), controlPoint1: bottomLeft(1.08849299, 0.00000000), controlPoint2: bottomLeft(0.86840701, 0.00000000))
|
||||
path.line(to: bottomLeft(0.63149399, 0.07491111))
|
||||
path.curve(to: bottomLeft(0.07491100, 0.63149399), controlPoint1: bottomLeft(0.37282401, 0.16905883), controlPoint2: bottomLeft(0.16906001, 0.37282392))
|
||||
path.curve(to: bottomLeft(0.00000000, 1.52866471), controlPoint1: bottomLeft(0.00000000, 0.86840689), controlPoint2: bottomLeft(0.00000000, 1.08849323))
|
||||
path.line(to: topLeft(0.00000000, 1.52866483))
|
||||
path.curve(to: topLeft(0.06549600, 0.66993397), controlPoint1: topLeft(0.00000000, 1.08849299), controlPoint2: topLeft(0.00000000, 0.86840701))
|
||||
path.line(to: topLeft(0.07491100, 0.63149399))
|
||||
path.curve(to: topLeft(0.63149399, 0.07491100), controlPoint1: topLeft(0.16906001, 0.37282401), controlPoint2: topLeft(0.37282401, 0.16906001))
|
||||
path.curve(to: topLeft(1.52866483, 0.00000000), controlPoint1: topLeft(0.86840701, 0.00000000), controlPoint2: topLeft(1.08849299, 0.00000000))
|
||||
path.close()
|
||||
return path
|
||||
}
|
||||
}
|
||||
91
src/Shared.swift
Normal file
91
src/Shared.swift
Normal file
@@ -0,0 +1,91 @@
|
||||
import Foundation
|
||||
import os // OSLog
|
||||
|
||||
private let log = OSLog(subsystem: Bundle.main.bundleIdentifier!, category: "Shared")
|
||||
|
||||
|
||||
// Init QuickLook Type
|
||||
enum FileType {
|
||||
case IPA
|
||||
case Archive
|
||||
case Extension
|
||||
}
|
||||
|
||||
struct QuickLookInfo {
|
||||
let UTI: String
|
||||
let url: URL
|
||||
let effectiveUrl: URL? // if set, will point to the app inside of an archive
|
||||
|
||||
let type: FileType
|
||||
let zipFile: ZipFile? // only set for zipped file types
|
||||
let isOSX = false
|
||||
|
||||
/// Use file url and UTI type to generate an info object to pass around.
|
||||
init(_ url: URL) {
|
||||
self.url = url
|
||||
self.UTI = try! url.resourceValues(forKeys: [.typeIdentifierKey]).typeIdentifier ?? "Unknown"
|
||||
|
||||
var effective: URL? = nil
|
||||
var zipFile: ZipFile? = nil
|
||||
|
||||
switch self.UTI {
|
||||
case "com.apple.itunes.ipa":
|
||||
self.type = FileType.IPA;
|
||||
zipFile = ZipFile(self.url.path);
|
||||
case "com.apple.xcode.archive":
|
||||
self.type = FileType.Archive;
|
||||
effective = appPathForArchive(self.url);
|
||||
case "com.apple.application-and-system-extension":
|
||||
self.type = FileType.Extension;
|
||||
default:
|
||||
os_log(.error, log: log, "Unsupported file type: %{public}@", self.UTI)
|
||||
fatalError()
|
||||
}
|
||||
self.zipFile = zipFile
|
||||
self.effectiveUrl = effective
|
||||
}
|
||||
|
||||
/// Load a file from bundle into memory. Either by file path or via unzip.
|
||||
func readPayloadFile(_ filename: String) -> Data? {
|
||||
switch (self.type) {
|
||||
case .IPA:
|
||||
return zipFile!.unzipFile("Payload/*.app/".appending(filename))
|
||||
case .Archive:
|
||||
return try? Data(contentsOf: effectiveUrl!.appendingPathComponent(filename))
|
||||
case .Extension:
|
||||
return try? Data(contentsOf: url.appendingPathComponent(filename))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Meta data for QuickLook
|
||||
|
||||
/// Search an archive for the .app or .ipa bundle.
|
||||
func appPathForArchive(_ url: URL) -> URL? {
|
||||
let appsDir = url.appendingPathComponent("Products/Applications/")
|
||||
if FileManager.default.fileExists(atPath: appsDir.path) {
|
||||
if let x = try? FileManager.default.contentsOfDirectory(at: appsDir, includingPropertiesForKeys: nil), !x.isEmpty {
|
||||
return x.first
|
||||
}
|
||||
}
|
||||
return nil;
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Other helper
|
||||
|
||||
enum ExpirationStatus {
|
||||
case Expired
|
||||
case Expiring
|
||||
case Valid
|
||||
|
||||
/// Check time between date and now. Set Expiring if less than 30 days until expiration
|
||||
init(_ date: Date?) {
|
||||
if date == nil || date!.timeIntervalSinceNow < 0 {
|
||||
self = .Expired
|
||||
}
|
||||
let components = Calendar.current.dateComponents([.day], from: Date(), to: date!)
|
||||
self = components.day! < 30 ? .Expiring : .Valid
|
||||
}
|
||||
}
|
||||
162
src/ThumbnailGenerator.swift
Normal file
162
src/ThumbnailGenerator.swift
Normal file
@@ -0,0 +1,162 @@
|
||||
import Foundation
|
||||
|
||||
////#import "Shared.h"
|
||||
//#import "AppIcon.h"
|
||||
//
|
||||
//// makro to stop further processing
|
||||
//#define ALLOW_EXIT if (QLThumbnailRequestIsCancelled(thumbnail)) { return noErr; }
|
||||
//
|
||||
////Layout constants
|
||||
//#define BADGE_MARGIN 10.0
|
||||
//#define MIN_BADGE_WIDTH 40.0
|
||||
//#define BADGE_HEIGHT 75.0
|
||||
//#define BADGE_MARGIN_X 60.0
|
||||
//#define BADGE_MARGIN_Y 80.0
|
||||
//
|
||||
////Drawing constants
|
||||
//#define BADGE_BG_COLOR [NSColor lightGrayColor]
|
||||
//#define BADGE_VALID_COLOR [NSColor colorWithCalibratedRed:(0/255.0) green:(98/255.0) blue:(25/255.0) alpha:1]
|
||||
//#define BADGE_EXPIRING_COLOR [NSColor colorWithCalibratedRed:(146/255.0) green:(95/255.0) blue:(28/255.0) alpha:1]
|
||||
//#define BADGE_EXPIRED_COLOR [NSColor colorWithCalibratedRed:(141/255.0) green:(0/255.0) blue:(7/255.0) alpha:1]
|
||||
//#define BADGE_FONT [NSFont boldSystemFontOfSize:64]
|
||||
//
|
||||
//
|
||||
//OSStatus GenerateThumbnailForURL(void *thisInterface, QLThumbnailRequestRef thumbnail, CFURLRef url, CFStringRef contentTypeUTI, CFDictionaryRef options, CGSize maxSize);
|
||||
//void CancelThumbnailGeneration(void *thisInterface, QLThumbnailRequestRef thumbnail);
|
||||
//
|
||||
///* -----------------------------------------------------------------------------
|
||||
// Generate a thumbnail for file
|
||||
//
|
||||
// This function's job is to create thumbnail for designated file as fast as possible
|
||||
// ----------------------------------------------------------------------------- */
|
||||
//
|
||||
//// MARK: .ipa .xcarchive
|
||||
//
|
||||
//OSStatus renderAppIcon(QuickLookInfo meta, QLThumbnailRequestRef thumbnail) {
|
||||
// AppIcon *icon = [AppIcon load:meta];
|
||||
// if (!icon.canExtractImage) {
|
||||
// return noErr;
|
||||
// }
|
||||
//
|
||||
// // set magic flag to draw icon without additional markers
|
||||
// static const NSString *IconFlavor;
|
||||
// if (@available(macOS 10.15, *)) {
|
||||
// IconFlavor = @"icon";
|
||||
// } else {
|
||||
// IconFlavor = @"IconFlavor";
|
||||
// }
|
||||
// NSDictionary *propertiesDict = nil;
|
||||
// if (meta.type == FileTypeArchive) {
|
||||
// // 0: Plain transparent, 1: Shadow, 2: Book, 3: Movie, 4: Address, 5: Image,
|
||||
// // 6: Gloss, 7: Slide, 8: Square, 9: Border, 11: Calendar, 12: Pattern
|
||||
// propertiesDict = @{IconFlavor : @(12)}; // looks like "in development"
|
||||
// } else {
|
||||
// propertiesDict = @{IconFlavor : @(0)}; // no border, no anything
|
||||
// }
|
||||
//
|
||||
// NSImage *appIcon = [[icon extractImage:nil] withRoundCorners];
|
||||
// ALLOW_EXIT
|
||||
//
|
||||
// // image-only icons can be drawn efficiently by calling `SetImage` directly.
|
||||
// QLThumbnailRequestSetImageWithData(thumbnail, (__bridge CFDataRef)[appIcon TIFFRepresentation], (__bridge CFDictionaryRef)propertiesDict);
|
||||
// return noErr;
|
||||
//}
|
||||
//
|
||||
//
|
||||
//// MARK: .provisioning
|
||||
//
|
||||
//OSStatus renderProvision(QuickLookInfo meta, QLThumbnailRequestRef thumbnail, BOOL iconMode) {
|
||||
// NSDictionary *propertyList = readPlistProvision(meta);
|
||||
// ALLOW_EXIT
|
||||
//
|
||||
// NSUInteger devicesCount = arrayOrNil(propertyList[@"ProvisionedDevices"]).count;
|
||||
// NSDate *expirationDate = dateOrNil(propertyList[@"ExpirationDate"]);
|
||||
//
|
||||
// NSImage *appIcon = nil;
|
||||
// if (iconMode) {
|
||||
// NSURL *iconURL = [[NSBundle bundleWithIdentifier:kPluginBundleId] URLForResource:@"blankIcon" withExtension:@"png"];
|
||||
// appIcon = [[NSImage alloc] initWithContentsOfURL:iconURL];
|
||||
// } else {
|
||||
// appIcon = [[NSWorkspace sharedWorkspace] iconForFileType:meta.UTI];
|
||||
// [appIcon setSize:NSMakeSize(512, 512)];
|
||||
// }
|
||||
// ALLOW_EXIT
|
||||
//
|
||||
// NSRect renderRect = NSMakeRect(0.0, 0.0, appIcon.size.width, appIcon.size.height);
|
||||
//
|
||||
// // Font attributes
|
||||
// NSColor *outlineColor;
|
||||
// switch (expirationStatus(expirationDate)) {
|
||||
// case ExpirationStatusExpired: outlineColor = BADGE_EXPIRED_COLOR; break;
|
||||
// case ExpirationStatusExpiring: outlineColor = BADGE_EXPIRING_COLOR; break;
|
||||
// case ExpirationStatusValid: outlineColor = BADGE_VALID_COLOR; break;
|
||||
// }
|
||||
//
|
||||
// NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
|
||||
// paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;
|
||||
// paragraphStyle.alignment = NSTextAlignmentCenter;
|
||||
//
|
||||
// NSDictionary *fontAttrs = @{
|
||||
// NSFontAttributeName : BADGE_FONT,
|
||||
// NSForegroundColorAttributeName : outlineColor,
|
||||
// NSParagraphStyleAttributeName: paragraphStyle
|
||||
// };
|
||||
//
|
||||
// // Badge size & placement
|
||||
// int badgeX = renderRect.origin.x + BADGE_MARGIN_X;
|
||||
// int badgeY = renderRect.origin.y + renderRect.size.height - BADGE_HEIGHT - BADGE_MARGIN_Y;
|
||||
// if (!iconMode) {
|
||||
// badgeX += 75;
|
||||
// badgeY -= 10;
|
||||
// }
|
||||
// int badgeNumX = badgeX + BADGE_MARGIN;
|
||||
// NSPoint badgeTextPoint = NSMakePoint(badgeNumX, badgeY);
|
||||
//
|
||||
// NSString *badge = [NSString stringWithFormat:@"%lu",(unsigned long)devicesCount];
|
||||
// NSSize badgeNumSize = [badge sizeWithAttributes:fontAttrs];
|
||||
// int badgeWidth = badgeNumSize.width + BADGE_MARGIN * 2;
|
||||
// NSRect badgeOutlineRect = NSMakeRect(badgeX, badgeY, MAX(badgeWidth, MIN_BADGE_WIDTH), BADGE_HEIGHT);
|
||||
//
|
||||
// // Do as much work as possible before the `CreateContext`. We can try to quit early before that!
|
||||
// CGContextRef _context = QLThumbnailRequestCreateContext(thumbnail, renderRect.size, false, NULL);
|
||||
// if (_context) {
|
||||
// NSGraphicsContext *_graphicsContext = [NSGraphicsContext graphicsContextWithCGContext:(void *)_context flipped:NO];
|
||||
// [NSGraphicsContext setCurrentContext:_graphicsContext];
|
||||
// [appIcon drawInRect:renderRect];
|
||||
//
|
||||
// NSBezierPath *badgePath = [NSBezierPath bezierPathWithRoundedRect:badgeOutlineRect xRadius:10 yRadius:10];
|
||||
// [badgePath setLineWidth:8.0];
|
||||
// [BADGE_BG_COLOR set];
|
||||
// [badgePath fill];
|
||||
// [outlineColor set];
|
||||
// [badgePath stroke];
|
||||
//
|
||||
// [badge drawAtPoint:badgeTextPoint withAttributes:fontAttrs];
|
||||
//
|
||||
// QLThumbnailRequestFlushContext(thumbnail, _context);
|
||||
// CFRelease(_context);
|
||||
// }
|
||||
// return noErr;
|
||||
//}
|
||||
//
|
||||
//
|
||||
//// MARK: Main Entry
|
||||
//
|
||||
//OSStatus GenerateThumbnailForURL(void *thisInterface, QLThumbnailRequestRef thumbnail, CFURLRef url, CFStringRef contentTypeUTI, CFDictionaryRef options, CGSize maxSize) {
|
||||
// @autoreleasepool {
|
||||
// QuickLookInfo meta = initQLInfo(contentTypeUTI, url);
|
||||
//
|
||||
// if (meta.type == FileTypeProvision) {
|
||||
// NSDictionary *optionsDict = (__bridge NSDictionary *)options;
|
||||
// BOOL iconMode = ([optionsDict objectForKey:(NSString *)kQLThumbnailOptionIconModeKey]) ? YES : NO;
|
||||
// return renderProvision(meta, thumbnail, iconMode);
|
||||
// } else {
|
||||
// return renderAppIcon(meta, thumbnail);
|
||||
// }
|
||||
// }
|
||||
// return noErr;
|
||||
//}
|
||||
//
|
||||
//void CancelThumbnailGeneration(void *thisInterface, QLThumbnailRequestRef thumbnail) {
|
||||
// // Implement only if supported
|
||||
//}
|
||||
366
src/Zip.swift
Normal file
366
src/Zip.swift
Normal file
@@ -0,0 +1,366 @@
|
||||
import Foundation
|
||||
import Compression // compression_decode_buffer
|
||||
import zlib // Z_DEFLATED, crc32
|
||||
import os // OSLog
|
||||
|
||||
private let log = OSLog(subsystem: Bundle.main.bundleIdentifier!, category: "Zip")
|
||||
|
||||
|
||||
// MARK: - Helper to parse byte headers
|
||||
|
||||
private struct ByteScanner {
|
||||
private let data: Data
|
||||
private var index: Int
|
||||
private let endIndex: Int
|
||||
|
||||
init (_ data: Data, start: Int) {
|
||||
self.data = data
|
||||
self.index = start
|
||||
self.endIndex = data.endIndex
|
||||
}
|
||||
|
||||
mutating func scan<T>() -> T {
|
||||
let newIndex = index + MemoryLayout<T>.size
|
||||
if newIndex > endIndex {
|
||||
os_log(.fault, log: log, "ByteScanner out of bounds")
|
||||
fatalError()
|
||||
}
|
||||
let result = data.subdata(in: index ..< newIndex).withUnsafeBytes { $0.load(as: T.self) }
|
||||
index = newIndex
|
||||
return result
|
||||
}
|
||||
|
||||
mutating func scanString(length: Int) -> String {
|
||||
let bytes = data.subdata(in: index ..< index + length)
|
||||
index += length
|
||||
return String(data: bytes, encoding: .utf8) ?? ""
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - ZIP Headers
|
||||
|
||||
// See http://en.wikipedia.org/wiki/ZIP_(file_format)#File_headers
|
||||
|
||||
/// Local file header
|
||||
private struct ZIP_LocalFile {
|
||||
static let LENGTH: Int = 30
|
||||
|
||||
let magicNumber: UInt32 // 50 4B 03 04
|
||||
let versionNeededToExtract: UInt16
|
||||
let generalPurposeBitFlag: UInt16
|
||||
let compressionMethod: UInt16
|
||||
let fileLastModificationTime: UInt16
|
||||
let fileLastModificationDate: UInt16
|
||||
let CRC32: UInt32
|
||||
let compressedSize: UInt32
|
||||
let uncompressedSize: UInt32
|
||||
let fileNameLength: UInt16
|
||||
let extraFieldLength: UInt16
|
||||
|
||||
// let fileName: String
|
||||
// Extra field
|
||||
|
||||
init(_ data: Data, start: Data.Index = 0) {
|
||||
var scanner = ByteScanner(data, start: start)
|
||||
magicNumber = scanner.scan()
|
||||
versionNeededToExtract = scanner.scan()
|
||||
generalPurposeBitFlag = scanner.scan()
|
||||
compressionMethod = scanner.scan()
|
||||
fileLastModificationTime = scanner.scan()
|
||||
fileLastModificationDate = scanner.scan()
|
||||
CRC32 = scanner.scan()
|
||||
compressedSize = scanner.scan()
|
||||
uncompressedSize = scanner.scan()
|
||||
fileNameLength = scanner.scan()
|
||||
extraFieldLength = scanner.scan()
|
||||
// fileName = scanner.scanString(length: Int(fileNameLength))
|
||||
}
|
||||
}
|
||||
|
||||
/// Central directory file header
|
||||
private struct ZIP_CDFH {
|
||||
static let LENGTH: Int = 46
|
||||
|
||||
let magicNumber: UInt32 // 50 4B 01 02
|
||||
let versionMadeBy: UInt16
|
||||
let versionNeededToExtract: UInt16
|
||||
let generalPurposeBitFlag: UInt16
|
||||
let compressionMethod: UInt16
|
||||
let fileLastModificationTime: UInt16
|
||||
let fileLastModificationDate: UInt16
|
||||
let CRC32: UInt32
|
||||
let compressedSize: UInt32
|
||||
let uncompressedSize: UInt32
|
||||
let fileNameLength: UInt16
|
||||
let extraFieldLength: UInt16
|
||||
let fileCommentLength: UInt16
|
||||
let diskNumberWhereFileStarts: UInt16
|
||||
let internalFileAttributes: UInt16
|
||||
let externalFileAttributes: UInt32
|
||||
let relativeOffsetOfLocalFileHeader: UInt32
|
||||
|
||||
let fileName: String
|
||||
// Extra field
|
||||
// File comment
|
||||
|
||||
init(_ data: Data, start: Data.Index = 0) {
|
||||
var scanner = ByteScanner(data, start: start)
|
||||
magicNumber = scanner.scan()
|
||||
versionMadeBy = scanner.scan()
|
||||
versionNeededToExtract = scanner.scan()
|
||||
generalPurposeBitFlag = scanner.scan()
|
||||
compressionMethod = scanner.scan()
|
||||
fileLastModificationTime = scanner.scan()
|
||||
fileLastModificationDate = scanner.scan()
|
||||
CRC32 = scanner.scan()
|
||||
compressedSize = scanner.scan()
|
||||
uncompressedSize = scanner.scan()
|
||||
fileNameLength = scanner.scan()
|
||||
extraFieldLength = scanner.scan()
|
||||
fileCommentLength = scanner.scan()
|
||||
diskNumberWhereFileStarts = scanner.scan()
|
||||
internalFileAttributes = scanner.scan()
|
||||
externalFileAttributes = scanner.scan()
|
||||
relativeOffsetOfLocalFileHeader = scanner.scan()
|
||||
fileName = scanner.scanString(length: Int(fileNameLength))
|
||||
}
|
||||
}
|
||||
|
||||
/// End of central directory record
|
||||
private struct ZIP_EOCD {
|
||||
static let LENGTH: Int = 22
|
||||
|
||||
let magicNumber: UInt32 // 50 4B 05 06
|
||||
let numberOfThisDisk: UInt16
|
||||
let diskWhereCentralDirectoryStarts: UInt16
|
||||
let numberOfCentralDirectoryRecordsOnThisDisk: UInt16
|
||||
let totalNumberOfCentralDirectoryRecords: UInt16
|
||||
let sizeOfCentralDirectory: UInt32
|
||||
let offsetOfStartOfCentralDirectory: UInt32
|
||||
let commentLength: UInt16
|
||||
// Comment
|
||||
|
||||
init(_ data: Data, start: Data.Index = 0) {
|
||||
var scanner = ByteScanner(data, start: start)
|
||||
magicNumber = scanner.scan()
|
||||
numberOfThisDisk = scanner.scan()
|
||||
diskWhereCentralDirectoryStarts = scanner.scan()
|
||||
numberOfCentralDirectoryRecordsOnThisDisk = scanner.scan()
|
||||
totalNumberOfCentralDirectoryRecords = scanner.scan()
|
||||
sizeOfCentralDirectory = scanner.scan()
|
||||
offsetOfStartOfCentralDirectory = scanner.scan()
|
||||
commentLength = scanner.scan()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - CRC32 check
|
||||
|
||||
extension Data {
|
||||
func crc() -> UInt32 {
|
||||
return UInt32(self.withUnsafeBytes { crc32(0, $0.baseAddress!, UInt32($0.count)) })
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Unzip data
|
||||
|
||||
func unzipFileEntry(_ path: String, _ entry: ZipEntry) -> Data? {
|
||||
guard let fp = FileHandle(forReadingAtPath: path) else {
|
||||
return nil
|
||||
}
|
||||
defer {
|
||||
try? fp.close()
|
||||
}
|
||||
fp.seek(toFileOffset: UInt64(entry.offset))
|
||||
let file_record = ZIP_LocalFile(fp.readData(ofLength: ZIP_LocalFile.LENGTH))
|
||||
os_log(.debug, log: log, "header: %{public}@ vs %{public}@", String(describing: file_record), String(describing: entry))
|
||||
|
||||
// central directory size and local file size may differ! use local file for ground truth
|
||||
let dataOffset = Int(entry.offset) + ZIP_LocalFile.LENGTH + Int(file_record.fileNameLength) + Int(file_record.extraFieldLength)
|
||||
fp.seek(toFileOffset: UInt64(dataOffset))
|
||||
let rawData = fp.readData(ofLength: Int(entry.sizeCompressed))
|
||||
|
||||
if entry.method == Z_DEFLATED {
|
||||
let size = Int(entry.sizeUncompressed)
|
||||
let buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: size)
|
||||
defer {
|
||||
buffer.deallocate()
|
||||
}
|
||||
|
||||
let uncompressedData = rawData.withUnsafeBytes ({
|
||||
let ptr = $0.baseAddress!.bindMemory(to: UInt8.self, capacity: 1)
|
||||
let read = compression_decode_buffer(buffer, size, ptr, Int(entry.sizeCompressed), nil, COMPRESSION_ZLIB)
|
||||
return Data(bytes: buffer, count:read)
|
||||
})
|
||||
if file_record.CRC32 != 0, uncompressedData.crc() != file_record.CRC32 {
|
||||
os_log(.error, log: log, "CRC check failed (after uncompress)")
|
||||
return nil
|
||||
}
|
||||
return uncompressedData
|
||||
|
||||
} else if entry.method == 0 {
|
||||
if file_record.CRC32 != 0, rawData.crc() != file_record.CRC32 {
|
||||
os_log(.error, log: log, "CRC check failed (uncompressed data)")
|
||||
return nil
|
||||
}
|
||||
return rawData
|
||||
|
||||
} else {
|
||||
os_log(.error, log: log, "unimplemented compression method: %{public}d", entry.method)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - List files
|
||||
|
||||
private func listZip(_ path: String) -> [ZipEntry] {
|
||||
guard let fp = FileHandle(forReadingAtPath: path) else {
|
||||
return []
|
||||
}
|
||||
defer {
|
||||
try? fp.close()
|
||||
}
|
||||
|
||||
guard let endRecord = findCentralDirectory(fp), endRecord.sizeOfCentralDirectory > 0 else {
|
||||
return [];
|
||||
}
|
||||
return listDirectoryEntries(fp, endRecord);
|
||||
}
|
||||
|
||||
/// Find signature for central directory.
|
||||
private func findCentralDirectory(_ fp: FileHandle) -> ZIP_EOCD? {
|
||||
let eof = fp.seekToEndOfFile()
|
||||
fp.seek(toFileOffset: max(0, eof - 4096))
|
||||
let data = fp.readDataToEndOfFile()
|
||||
|
||||
let centralDirSignature: [UInt8] = [0x50, 0x4b, 0x05, 0x06]
|
||||
|
||||
guard let range = data.lastRange(of: centralDirSignature) else {
|
||||
os_log(.error, log: log, "no zip end-header found!")
|
||||
return nil
|
||||
}
|
||||
return ZIP_EOCD(data, start: range.lowerBound)
|
||||
}
|
||||
|
||||
/// List all files and folders of of the central directory.
|
||||
private func listDirectoryEntries(_ fp: FileHandle, _ centralDir: ZIP_EOCD) -> [ZipEntry] {
|
||||
fp.seek(toFileOffset: UInt64(centralDir.offsetOfStartOfCentralDirectory))
|
||||
let data = fp.readData(ofLength: Int(centralDir.sizeOfCentralDirectory))
|
||||
let total = data.count
|
||||
|
||||
var idx = 0
|
||||
var entries: [ZipEntry] = []
|
||||
|
||||
while idx + ZIP_CDFH.LENGTH < total {
|
||||
let record = ZIP_CDFH(data, start: idx)
|
||||
// read filename
|
||||
idx += ZIP_CDFH.LENGTH
|
||||
let filename = String(data: data.subdata(in: idx ..< idx + Int(record.fileNameLength)), encoding: .utf8)!
|
||||
entries.append(ZipEntry(filename, record))
|
||||
// update index
|
||||
idx += Int(record.fileNameLength + record.extraFieldLength + record.fileCommentLength)
|
||||
}
|
||||
return entries
|
||||
}
|
||||
|
||||
|
||||
// MARK: - ZipEntry
|
||||
|
||||
struct ZipEntry {
|
||||
let filepath: String
|
||||
let offset: UInt32
|
||||
let method: UInt16
|
||||
let sizeCompressed: UInt32
|
||||
let sizeUncompressed: UInt32
|
||||
let filenameLength: UInt16
|
||||
let extraFieldLength: UInt16
|
||||
let CRC32: UInt32
|
||||
|
||||
fileprivate init(_ filename: String, _ record: ZIP_CDFH) {
|
||||
self.filepath = filename
|
||||
self.offset = record.relativeOffsetOfLocalFileHeader
|
||||
self.method = record.compressionMethod
|
||||
self.sizeCompressed = record.compressedSize
|
||||
self.sizeUncompressed = record.uncompressedSize
|
||||
self.filenameLength = record.fileNameLength
|
||||
self.extraFieldLength = record.extraFieldLength
|
||||
self.CRC32 = record.CRC32
|
||||
}
|
||||
}
|
||||
|
||||
extension Array where Element == ZipEntry {
|
||||
/// Return entry with shortest possible path (thus ignoring deeper nested files).
|
||||
func zipEntryWithShortestPath() -> ZipEntry? {
|
||||
var shortest = 99999
|
||||
var bestMatch: ZipEntry? = nil
|
||||
|
||||
for entry in self {
|
||||
if shortest > entry.filepath.count {
|
||||
shortest = entry.filepath.count
|
||||
bestMatch = entry
|
||||
}
|
||||
}
|
||||
return bestMatch
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - ZipFile
|
||||
|
||||
struct ZipFile {
|
||||
private let pathToZipFile: String
|
||||
private let centralDirectory: [ZipEntry]
|
||||
|
||||
init(_ path: String) {
|
||||
self.pathToZipFile = path
|
||||
self.centralDirectory = listZip(path)
|
||||
}
|
||||
|
||||
// MARK: - public methods
|
||||
|
||||
func filesMatching(_ path: String) -> [ZipEntry] {
|
||||
let parts = path.split(separator: "*", omittingEmptySubsequences: false)
|
||||
return centralDirectory.filter {
|
||||
var idx = $0.filepath.startIndex
|
||||
if !$0.filepath.hasPrefix(parts.first!) || !$0.filepath.hasSuffix(parts.last!) {
|
||||
return false
|
||||
}
|
||||
for part in parts {
|
||||
guard let found = $0.filepath.range(of: part, range: idx..<$0.filepath.endIndex) else {
|
||||
return false
|
||||
}
|
||||
idx = found.upperBound
|
||||
}
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
/// Unzip file directly into memory.
|
||||
/// @param filePath File path inside zip file.
|
||||
func unzipFile(_ filePath: String) -> Data? {
|
||||
if let matchingFile = self.filesMatching(filePath).zipEntryWithShortestPath() {
|
||||
os_log(.debug, log: log, "[unzip] %{public}@", matchingFile.filepath)
|
||||
return unzipFileEntry(pathToZipFile, matchingFile)
|
||||
}
|
||||
|
||||
// There is a dir listing but no matching file.
|
||||
// This means there wont be anything to extract.
|
||||
os_log(.error, log: log, "cannot find '%{public}@' for unzip", filePath)
|
||||
return nil
|
||||
}
|
||||
|
||||
/// Unzip file to filesystem.
|
||||
/// @param filePath File path inside zip file.
|
||||
/// @param targetDir Directory in which to unzip the file.
|
||||
func unzipFile(_ filePath: String, toDir targetDir: String) throws {
|
||||
if let data = self.unzipFile(filePath) {
|
||||
let filename = filePath.components(separatedBy: "/").last!
|
||||
let outputPath = targetDir.appending("/" + filename)
|
||||
os_log(.debug, log: log, "[unzip] write to %{public}@", outputPath)
|
||||
try data.write(to: URL(fileURLWithPath: outputPath), options: .atomic)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user