5
5
// Created by Shu Lyu on 2022-05-07.
6
6
//
7
7
8
- import Foundation
9
8
import Cocoa
9
+ import Foundation
10
10
11
11
struct URLUtils {
12
12
static let applicationSupportPath = try ! FileManager . default. url (
@@ -30,38 +30,65 @@ struct URLUtils {
30
30
create: true
31
31
)
32
32
33
- static let legacyPlanetsPath = applicationSupportPath. appendingPathComponent ( " planets " , isDirectory: true )
33
+ static let legacyPlanetsPath = applicationSupportPath. appendingPathComponent (
34
+ " planets " ,
35
+ isDirectory: true
36
+ )
34
37
35
- static let legacyTemplatesPath = applicationSupportPath. appendingPathComponent ( " templates " , isDirectory: true )
38
+ static let legacyTemplatesPath = applicationSupportPath. appendingPathComponent (
39
+ " templates " ,
40
+ isDirectory: true
41
+ )
36
42
37
- static let legacyDraftPath = applicationSupportPath. appendingPathComponent ( " drafts " , isDirectory: true )
43
+ static let legacyDraftPath = applicationSupportPath. appendingPathComponent (
44
+ " drafts " ,
45
+ isDirectory: true
46
+ )
38
47
39
48
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
+ {
41
52
let libraryURL = URL ( fileURLWithPath: libraryLocation)
42
53
let planetURL = libraryURL. appendingPathComponent ( " Planet " , isDirectory: true )
43
54
if FileManager . default. fileExists ( atPath: planetURL. path) {
44
55
do {
45
56
let bookmarkKey = libraryURL. path. md5 ( )
46
57
if let bookmarkData = UserDefaults . standard. data ( forKey: bookmarkKey) {
47
58
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
+ )
49
65
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
+ )
51
71
UserDefaults . standard. set ( updatedBookmarkData, forKey: bookmarkKey)
52
72
}
53
73
if url. startAccessingSecurityScopedResource ( ) {
54
74
return planetURL
55
- } else {
75
+ }
76
+ else {
56
77
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
+ )
58
81
}
59
82
}
60
- } catch {
83
+ }
84
+ catch {
61
85
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
+ )
63
89
}
64
- } else {
90
+ }
91
+ else {
65
92
UserDefaults . standard. removeObject ( forKey: . settingsLibraryLocation)
66
93
}
67
94
}
@@ -84,12 +111,18 @@ struct URLUtils {
84
111
extension URL {
85
112
var isHTTP : Bool {
86
113
if let scheme = scheme? . lowercased ( ) ,
87
- scheme == " http " || scheme == " https " {
114
+ scheme == " http " || scheme == " https "
115
+ {
88
116
return true
89
117
}
90
118
return false
91
119
}
92
120
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
+
93
126
var pathQueryFragment : String {
94
127
var s = path
95
128
if let query = query {
@@ -117,11 +150,17 @@ extension URL {
117
150
var asNSImage : NSImage {
118
151
let ext = pathExtension. lowercased ( )
119
152
if [ " jpg " , " jpeg " , " png " , " gif " , " tiff " , " bmp " ] . contains ( ext) ,
120
- let image = NSImage ( contentsOf: self ) {
153
+ let image = NSImage ( contentsOf: self )
154
+ {
121
155
return image
122
156
}
123
157
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
+ {
125
164
let image = NSImage ( size: rep. size)
126
165
image. addRepresentation ( rep)
127
166
return image
@@ -139,10 +178,16 @@ extension URL {
139
178
let source = CGImageSourceCreateWithData ( data as CFData , nil ) !
140
179
let count = CGImageSourceGetCount ( source)
141
180
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
+ ) !
143
187
for i in 0 ..< count {
144
188
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 ]
146
191
var newProperties = properties
147
192
newProperties. removeValue ( forKey: kCGImagePropertyGPSDictionary)
148
193
// newProperties.removeValue(forKey: kCGImagePropertyExifDictionary)
@@ -151,33 +196,40 @@ extension URL {
151
196
}
152
197
CGImageDestinationFinalize ( destination)
153
198
try mutableData. write ( to: self )
154
- } catch {
199
+ }
200
+ catch {
155
201
// Handle the error here
156
202
print ( " Remove GPS Error: \( error) " )
157
203
}
158
204
}
159
205
160
- var imageCode : String {
206
+ var htmlCode : String {
161
207
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
+ }
175
223
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
+ }
179
230
}
231
+ return " <img alt= \" \( ( name as NSString ) . deletingPathExtension) \" src= \" \( name) \" > "
180
232
}
181
- return " <img alt = \" \( ( name as NSString ) . deletingPathExtension ) \" src= \" \ ( name) \" > "
233
+ return " <a href = \" \( name) \" > \ ( name) </a >"
182
234
}
183
235
}
0 commit comments