Skip to content

Commit a5cafbd

Browse files
committed
Fix htmlCode extension for URL
1 parent 993278e commit a5cafbd

File tree

3 files changed

+91
-39
lines changed

3 files changed

+91
-39
lines changed

Diff for: Planet/Helper/URLUtils.swift

+88-36
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
// Created by Shu Lyu on 2022-05-07.
66
//
77

8-
import Foundation
98
import Cocoa
9+
import Foundation
1010

1111
struct URLUtils {
1212
static let applicationSupportPath = try! FileManager.default.url(
@@ -30,38 +30,65 @@ struct URLUtils {
3030
create: true
3131
)
3232

33-
static let legacyPlanetsPath = applicationSupportPath.appendingPathComponent("planets", isDirectory: true)
33+
static let legacyPlanetsPath = applicationSupportPath.appendingPathComponent(
34+
"planets",
35+
isDirectory: true
36+
)
3437

35-
static let legacyTemplatesPath = applicationSupportPath.appendingPathComponent("templates", isDirectory: true)
38+
static let legacyTemplatesPath = applicationSupportPath.appendingPathComponent(
39+
"templates",
40+
isDirectory: true
41+
)
3642

37-
static let legacyDraftPath = applicationSupportPath.appendingPathComponent("drafts", isDirectory: true)
43+
static let legacyDraftPath = applicationSupportPath.appendingPathComponent(
44+
"drafts",
45+
isDirectory: true
46+
)
3847

3948
static func repoPath() -> URL {
40-
if let libraryLocation = UserDefaults.standard.string(forKey: .settingsLibraryLocation), FileManager.default.fileExists(atPath: libraryLocation) {
49+
if let libraryLocation = UserDefaults.standard.string(forKey: .settingsLibraryLocation),
50+
FileManager.default.fileExists(atPath: libraryLocation)
51+
{
4152
let libraryURL = URL(fileURLWithPath: libraryLocation)
4253
let planetURL = libraryURL.appendingPathComponent("Planet", isDirectory: true)
4354
if FileManager.default.fileExists(atPath: planetURL.path) {
4455
do {
4556
let bookmarkKey = libraryURL.path.md5()
4657
if let bookmarkData = UserDefaults.standard.data(forKey: bookmarkKey) {
4758
var isStale = false
48-
let url = try URL(resolvingBookmarkData: bookmarkData, options: .withSecurityScope, relativeTo: nil, bookmarkDataIsStale: &isStale)
59+
let url = try URL(
60+
resolvingBookmarkData: bookmarkData,
61+
options: .withSecurityScope,
62+
relativeTo: nil,
63+
bookmarkDataIsStale: &isStale
64+
)
4965
if isStale {
50-
let updatedBookmarkData = try url.bookmarkData(options: .withSecurityScope, includingResourceValuesForKeys: nil, relativeTo: nil)
66+
let updatedBookmarkData = try url.bookmarkData(
67+
options: .withSecurityScope,
68+
includingResourceValuesForKeys: nil,
69+
relativeTo: nil
70+
)
5171
UserDefaults.standard.set(updatedBookmarkData, forKey: bookmarkKey)
5272
}
5373
if url.startAccessingSecurityScopedResource() {
5474
return planetURL
55-
} else {
75+
}
76+
else {
5677
UserDefaults.standard.removeObject(forKey: .settingsLibraryLocation)
57-
debugPrint("failed to start accessing security scoped resource, abort & restore to default.")
78+
debugPrint(
79+
"failed to start accessing security scoped resource, abort & restore to default."
80+
)
5881
}
5982
}
60-
} catch {
83+
}
84+
catch {
6185
UserDefaults.standard.removeObject(forKey: .settingsLibraryLocation)
62-
debugPrint("failed to get planet library location: \(error), restore to default.")
86+
debugPrint(
87+
"failed to get planet library location: \(error), restore to default."
88+
)
6389
}
64-
} else {
90+
}
91+
else {
6592
UserDefaults.standard.removeObject(forKey: .settingsLibraryLocation)
6693
}
6794
}
@@ -84,12 +111,18 @@ struct URLUtils {
84111
extension URL {
85112
var isHTTP: Bool {
86113
if let scheme = scheme?.lowercased(),
87-
scheme == "http" || scheme == "https" {
114+
scheme == "http" || scheme == "https"
115+
{
88116
return true
89117
}
90118
return false
91119
}
92120

121+
var isImage: Bool {
122+
let ext = pathExtension.lowercased()
123+
return ["jpg", "jpeg", "png", "gif", "webp", "heic", "heif", "avif", "svg", "tiff", "bmp"].contains(ext)
124+
}
125+
93126
var pathQueryFragment: String {
94127
var s = path
95128
if let query = query {
@@ -117,11 +150,17 @@ extension URL {
117150
var asNSImage: NSImage {
118151
let ext = pathExtension.lowercased()
119152
if ["jpg", "jpeg", "png", "gif", "tiff", "bmp"].contains(ext),
120-
let image = NSImage(contentsOf: self) {
153+
let image = NSImage(contentsOf: self)
154+
{
121155
return image
122156
}
123157
if let rep = NSWorkspace.shared.icon(forFile: self.path)
124-
.bestRepresentation(for: NSRect(x: 0, y: 0, width: 128, height: 128), context: nil, hints: nil) {
158+
.bestRepresentation(
159+
for: NSRect(x: 0, y: 0, width: 128, height: 128),
160+
context: nil,
161+
hints: nil
162+
)
163+
{
125164
let image = NSImage(size: rep.size)
126165
image.addRepresentation(rep)
127166
return image
@@ -139,10 +178,16 @@ extension URL {
139178
let source = CGImageSourceCreateWithData(data as CFData, nil)!
140179
let count = CGImageSourceGetCount(source)
141180
let mutableData = NSMutableData()
142-
let destination = CGImageDestinationCreateWithData(mutableData, CGImageSourceGetType(source)!, count, nil)!
181+
let destination = CGImageDestinationCreateWithData(
182+
mutableData,
183+
CGImageSourceGetType(source)!,
184+
count,
185+
nil
186+
)!
143187
for i in 0..<count {
144188
let image = CGImageSourceCreateImageAtIndex(source, i, nil)!
145-
let properties = CGImageSourceCopyPropertiesAtIndex(source, i, nil) as! [CFString: Any]
189+
let properties =
190+
CGImageSourceCopyPropertiesAtIndex(source, i, nil) as! [CFString: Any]
146191
var newProperties = properties
147192
newProperties.removeValue(forKey: kCGImagePropertyGPSDictionary)
148193
// newProperties.removeValue(forKey: kCGImagePropertyExifDictionary)
@@ -151,33 +196,40 @@ extension URL {
151196
}
152197
CGImageDestinationFinalize(destination)
153198
try mutableData.write(to: self)
154-
} catch {
199+
}
200+
catch {
155201
// Handle the error here
156202
print("Remove GPS Error: \(error)")
157203
}
158204
}
159205

160-
var imageCode: String {
206+
var htmlCode: String {
161207
let name = self.lastPathComponent
162-
if let im = NSImage(contentsOf: self) {
163-
let imageRep = im.representations.first as? NSBitmapImageRep
164-
let width = imageRep?.pixelsWide ?? 0
165-
let height = imageRep?.pixelsHigh ?? 0
166-
let pointSize = im.size
167-
let pointWidth = pointSize.width
168-
let pointHeight = pointSize.height
169-
var widthToUse = 0
170-
if (CGFloat(width) / pointWidth) > 1 {
171-
widthToUse = Int(pointWidth)
172-
} else {
173-
widthToUse = width
174-
}
208+
if isImage {
209+
if let im = NSImage(contentsOf: self) {
210+
let imageRep = im.representations.first as? NSBitmapImageRep
211+
let width = imageRep?.pixelsWide ?? 0
212+
let height = imageRep?.pixelsHigh ?? 0
213+
let pointSize = im.size
214+
let pointWidth = pointSize.width
215+
let pointHeight = pointSize.height
216+
var widthToUse = 0
217+
if (CGFloat(width) / pointWidth) > 1 {
218+
widthToUse = Int(pointWidth)
219+
}
220+
else {
221+
widthToUse = width
222+
}
175223
if Int(widthToUse) > 0 {
176-
return "<img width=\"\(Int(widthToUse))\" alt=\"\((name as NSString).deletingPathExtension)\" src=\"\(name)\">"
177-
} else {
178-
return "<img alt=\"\((name as NSString).deletingPathExtension)\" src=\"\(name)\">"
224+
return
225+
"<img width=\"\(Int(widthToUse))\" alt=\"\((name as NSString).deletingPathExtension)\" src=\"\(name)\">"
226+
}
227+
else {
228+
return "<img alt=\"\((name as NSString).deletingPathExtension)\" src=\"\(name)\">"
229+
}
179230
}
231+
return "<img alt=\"\((name as NSString).deletingPathExtension)\" src=\"\(name)\">"
180232
}
181-
return "<img alt=\"\((name as NSString).deletingPathExtension)\" src=\"\(name)\">"
233+
return "<a href=\"\(name)\">\(name)</a>"
182234
}
183235
}

Diff for: Planet/Labs/Quick Post/QuickPostView.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ struct QuickPostView: View {
176176
.onTapGesture {
177177
// Insert media reference at cursor position
178178
let fileName = url.lastPathComponent
179-
let mediaReference = url.imageCode
179+
let mediaReference = url.htmlCode
180180

181181
// Get current cursor position
182182
let currentContent = viewModel.content
@@ -195,7 +195,7 @@ struct QuickPostView: View {
195195
}
196196
// Also remove the media reference from the content
197197
let currentContent = viewModel.content
198-
let mediaReference = url.imageCode
198+
let mediaReference = url.htmlCode
199199
viewModel.content = currentContent.replacingOccurrences(of: mediaReference, with: "")
200200
} label: {
201201
Label("Remove", systemImage: "trash")

Diff for: Planet/versioning.xcconfig

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
CURRENT_PROJECT_VERSION = 2293
1+
CURRENT_PROJECT_VERSION = 2294

0 commit comments

Comments
 (0)