@@ -19,14 +19,18 @@ class AppDelegate: NSObject, NSApplicationDelegate {
19
19
20
20
@IBOutlet weak var statusItemMenu : NSMenu !
21
21
22
+ var uploding = false
23
+ var needUploadFiles = [ Any] ( )
24
+ var resultUrls = [ String] ( )
25
+
22
26
lazy var preferencesWindowController : PreferencesWindowController = {
23
27
let storyboard = NSStoryboard ( name: " Preferences " , bundle: nil )
24
28
return storyboard. instantiateInitialController ( ) as? PreferencesWindowController ?? PreferencesWindowController ( )
25
29
} ( )
26
30
27
31
func applicationDidFinishLaunching( _ aNotification: Notification ) {
28
32
// Insert code here to initialize your application
29
-
33
+
30
34
self . resetNewVersionLaunchAtLogin ( )
31
35
32
36
indicator. minValue = 0.0
@@ -43,11 +47,11 @@ class AppDelegate: NSObject, NSApplicationDelegate {
43
47
func applicationWillTerminate( _ notification: Notification ) {
44
48
NSStatusBar . system. removeStatusItem ( statusItem)
45
49
}
46
-
50
+
47
51
func applicationShouldHandleReopen( _ sender: NSApplication , hasVisibleWindows flag: Bool ) -> Bool {
48
52
return true
49
53
}
50
-
54
+
51
55
func resetNewVersionLaunchAtLogin( ) {
52
56
guard let launchAtLogin = ConfigManager . shared. launchAtLogin else {
53
57
return
@@ -110,11 +114,16 @@ extension AppDelegate {
110
114
/* 选择文件 */
111
115
@objc func selectFile( ) {
112
116
117
+ if self . uploding {
118
+ NotificationExt . sendUplodingNotification ( )
119
+ return
120
+ }
121
+
113
122
NSApp . activate ( ignoringOtherApps: true )
114
123
let fileExtensions = BaseUploader . getFileExtensions ( )
115
124
116
125
let openPanel = NSOpenPanel ( )
117
- openPanel. allowsMultipleSelection = false
126
+ openPanel. allowsMultipleSelection = true
118
127
openPanel. canChooseDirectories = false
119
128
openPanel. canCreateDirectories = false
120
129
openPanel. canChooseFiles = true
@@ -126,46 +135,51 @@ extension AppDelegate {
126
135
openPanel. begin { ( result) -> Void in
127
136
openPanel. close ( )
128
137
if result. rawValue == NSApplication . ModalResponse. OK. rawValue {
129
- let selectedPath = openPanel. url!. path
130
- let url : URL = URL ( fileURLWithPath: selectedPath)
131
-
132
- self . uploadFile ( url, data: nil )
138
+ self . uploadFiles ( openPanel. urls)
133
139
}
134
140
}
135
141
}
136
142
137
143
@objc func uploadByPasteboard( ) {
138
- let pasteboardType = NSPasteboard . general. types? . first
139
-
140
- if ( pasteboardType == NSPasteboard . PasteboardType. png) {
141
- let imgData = NSPasteboard . general. data ( forType: NSPasteboard . PasteboardType. png)
142
- self . uploadFile ( nil , data: imgData!)
143
- } else if ( pasteboardType == NSPasteboard . PasteboardType. backwardsCompatibleFileURL) {
144
-
145
- let filePath = NSPasteboard . general. string ( forType: NSPasteboard . PasteboardType. backwardsCompatibleFileURL) !
146
- let url = URL ( string: filePath) !
147
-
148
- let fileManager = FileManager . default
149
- if ( !url. isFileURL || !fileManager. fileExists ( atPath: url. path) ) {
150
- debugPrint ( " 复制的文件不存在或已被删除! " )
151
- NotificationExt . sendFileDoesNotExistNotification ( )
152
- return
144
+ if self . uploding {
145
+ NotificationExt . sendUplodingNotification ( )
146
+ return
147
+ }
148
+
149
+ if let filenames = NSPasteboard . general. propertyList ( forType: NSPasteboard . PasteboardType ( " NSFilenamesPboardType " ) ) as? [ String ] {
150
+ let fileExtensions = BaseUploader . getFileExtensions ( )
151
+ var urls = [ URL] ( )
152
+
153
+ for path in filenames {
154
+ if fileExtensions. contains ( path. pathExtension. lowercased ( ) ) {
155
+ urls. append ( URL ( fileURLWithPath: path) )
156
+ }
157
+ }
158
+ if urls. count > 0 {
159
+ self . uploadFiles ( urls)
160
+ } else {
161
+ NotificationExt . sendUploadErrorNotification ( body: NSLocalizedString ( " file-format-is-not-supported " , comment: " 文件格式不支持 " ) )
153
162
}
154
- self . uploadFile ( url, data: nil )
155
- } else {
156
- self . uploadFaild ( errorMsg: NSLocalizedString ( " file-format-is-not-supported " , comment: " 文件格式不支持 " ) )
157
163
}
158
164
}
159
165
160
166
@objc func screenshotAndUpload( ) {
161
167
168
+ if self . uploding {
169
+ NotificationExt . sendUplodingNotification ( )
170
+ return
171
+ }
172
+
162
173
let task = Process ( )
163
174
task. launchPath = " /usr/sbin/screencapture "
164
175
task. arguments = [ " -i " , " -c " ]
165
176
task. launch ( )
166
177
task. waitUntilExit ( )
167
178
168
- self . uploadByPasteboard ( )
179
+ if ( NSPasteboard . general. types? . first == NSPasteboard . PasteboardType. png) {
180
+ let imgData = NSPasteboard . general. data ( forType: NSPasteboard . PasteboardType. png)
181
+ self . uploadFiles ( [ imgData!] )
182
+ }
169
183
}
170
184
171
185
@@ -174,12 +188,39 @@ extension AppDelegate {
174
188
}
175
189
}
176
190
191
+ // 上传多个文件
192
+ func uploadFiles( _ files: [ Any ] ) {
193
+ self . needUploadFiles = files
194
+ self . resultUrls. removeAll ( )
177
195
178
- func uploadFile( _ url: URL ? , data: Data ? ) {
179
- if url != nil {
180
- BaseUploader . upload ( url: url!)
181
- } else if ( data != nil ) {
182
- BaseUploader . upload ( data: data!)
196
+ if self . needUploadFiles. count == 0 {
197
+ return
198
+ }
199
+
200
+ NotificationExt . sendStartUploadNotification ( )
201
+ self . uploding = true
202
+ self . tickFileToUpload ( )
203
+ }
204
+
205
+ // 开始上传文件队列中的第一个文件,如果所有文件上传完成则表示当前上传任务结束
206
+ func tickFileToUpload( ) {
207
+ if self . needUploadFiles. count == 0 {
208
+ // done
209
+ self . uploding = false
210
+ if self . resultUrls. count > 0 {
211
+ let outputStr = self . copyUrls ( urls: self . resultUrls)
212
+ NotificationExt . sendUploadSuccessfulNotification ( body: outputStr)
213
+ self . resultUrls. removeAll ( )
214
+ }
215
+ } else {
216
+ // next file
217
+ let firstFile = self . needUploadFiles. first
218
+ if firstFile is URL {
219
+ BaseUploader . upload ( url: firstFile as! URL )
220
+ } else if firstFile is Data {
221
+ BaseUploader . upload ( data: firstFile as! Data )
222
+ }
223
+ self . needUploadFiles. removeFirst ( )
183
224
}
184
225
}
185
226
@@ -188,8 +229,29 @@ extension AppDelegate {
188
229
///
189
230
func uploadCompleted( url: String ) {
190
231
self . setStatusBarIcon ( isIndicator: false )
191
- let outputUrl = self . copyUrl ( url: url)
192
- NotificationExt . sendUploadSuccessfulNotification ( body: outputUrl)
232
+ self . resultUrls. append ( url)
233
+ self . tickFileToUpload ( )
234
+ }
235
+
236
+ ///
237
+ /// 上传失败时被调用
238
+ ///
239
+ func uploadFaild( errorMsg: String ? = " " ) {
240
+ self . setStatusBarIcon ( isIndicator: false )
241
+ NotificationExt . sendUploadErrorNotification ( body: errorMsg)
242
+ self . tickFileToUpload ( )
243
+ }
244
+
245
+ ///
246
+ /// 上传进度更新时调用
247
+ ///
248
+ func uploadProgress( percent: Double ) {
249
+ self . indicator. doubleValue = percent
250
+ }
251
+
252
+ func uploadStart( ) {
253
+ self . setStatusBarIcon ( isIndicator: true )
254
+ self . indicator. doubleValue = 0.0
193
255
}
194
256
195
257
func copyUrl( url: String ) -> String {
@@ -213,26 +275,34 @@ extension AppDelegate {
213
275
214
276
return outputUrl
215
277
}
216
-
217
- ///
218
- /// 上传失败时被调用
219
- ///
220
- func uploadFaild( errorMsg: String ? = " " ) {
221
- self . setStatusBarIcon ( isIndicator: false )
222
- NotificationExt . sendUploadErrorNotification ( body: errorMsg)
223
- }
224
-
225
- ///
226
- /// 上传进度更新时调用
227
- ///
228
- func uploadProgress( percent: Double ) {
229
- self . indicator. doubleValue = percent
230
- }
231
-
232
- func uploadStart( ) {
233
- self . setStatusBarIcon ( isIndicator: true )
234
- self . indicator. doubleValue = 0.0
235
- NotificationExt . sendStartUploadNotification ( )
278
+
279
+ func copyUrls( urls: [ String ] ) -> String {
280
+ var outputUrls = [ String] ( )
281
+ let outputFormat = Defaults [ . ouputFormat]
282
+
283
+ for url in urls {
284
+ var outputUrl = " "
285
+ switch outputFormat {
286
+ case 1 :
287
+ outputUrl = " <img src=' \( url) '/> "
288
+ break
289
+ case 2 :
290
+ outputUrl = "  ) "
291
+ break
292
+ default :
293
+ outputUrl = url
294
+
295
+ }
296
+ outputUrls. append ( outputUrl)
297
+ }
298
+
299
+ let outputStr = outputUrls. joined ( separator: " \n " )
300
+
301
+ NSPasteboard . general. clearContents ( )
302
+ NSPasteboard . general. declareTypes ( [ . string] , owner: nil )
303
+ NSPasteboard . general. setString ( outputStr, forType: . string)
304
+
305
+ return outputStr
236
306
}
237
307
}
238
308
@@ -241,7 +311,7 @@ extension AppDelegate: NSWindowDelegate, NSDraggingDestination {
241
311
// MARK: 拖拽文件
242
312
243
313
func draggingEntered( _ sender: NSDraggingInfo ) -> NSDragOperation {
244
- if sender. isValidFile {
314
+ if sender. isValid {
245
315
if let button = statusItem. button {
246
316
button. image = NSImage ( named: " uploadIcon " )
247
317
}
@@ -251,11 +321,13 @@ extension AppDelegate: NSWindowDelegate, NSDraggingDestination {
251
321
}
252
322
253
323
func performDragOperation( _ sender: NSDraggingInfo ) -> Bool {
254
- // TODO: 需要支持所有格式文件/根据图床支持的格式再进行判断
255
- if sender. isValidFile {
256
- let fileUrl = sender. draggedFileURL!. absoluteURL
324
+ if sender. isValid {
257
325
self . setStatusBarIcon ( )
258
- self . uploadFile ( fileUrl, data: nil )
326
+ var urls = [ URL] ( )
327
+ for url in sender. draggedFileURLs {
328
+ urls. append ( url. absoluteURL!)
329
+ }
330
+ self . uploadFiles ( urls)
259
331
return true
260
332
}
261
333
return false
0 commit comments