Skip to content

Commit 0517c84

Browse files
committed
Merge branch 'develop'
2 parents a9081f5 + 6648c25 commit 0517c84

29 files changed

+455
-198
lines changed

uPic.xcodeproj/project.pbxproj

+10-2
Original file line numberDiff line numberDiff line change
@@ -449,9 +449,8 @@
449449
16A6DC5622AA375700813706 /* uPic */ = {
450450
isa = PBXGroup;
451451
children = (
452-
162E5D4722C096FD001FAA46 /* Assets.xcassets */,
453452
16A6DC5722AA375700813706 /* AppDelegate.swift */,
454-
169F074322AF7A3D008E8525 /* StatusMenuController.swift */,
453+
162E5D4722C096FD001FAA46 /* Assets.xcassets */,
455454
163632ED22B2745A00805E7F /* Basic */,
456455
1675516522AC165600D3EB6F /* Extensions */,
457456
163F241E22B12E6700F81D03 /* General */,
@@ -461,6 +460,7 @@
461460
164C39FE22B2AA5E003ADE39 /* Notifier */,
462461
16068C7622AECB26004D39B7 /* PreferencesWindow */,
463462
16A6DC6822AA3AD900813706 /* Supporting Files */,
463+
16ADBE5E22D1D74D00E6C605 /* Views */,
464464
);
465465
path = uPic;
466466
sourceTree = "<group>";
@@ -475,6 +475,14 @@
475475
path = "Supporting Files";
476476
sourceTree = "<group>";
477477
};
478+
16ADBE5E22D1D74D00E6C605 /* Views */ = {
479+
isa = PBXGroup;
480+
children = (
481+
169F074322AF7A3D008E8525 /* StatusMenuController.swift */,
482+
);
483+
path = Views;
484+
sourceTree = "<group>";
485+
};
478486
16EA204A22AF8AB30006FB9C /* uPicHelper */ = {
479487
isa = PBXGroup;
480488
children = (

uPic/AppDelegate.swift

+131-59
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,18 @@ class AppDelegate: NSObject, NSApplicationDelegate {
1919

2020
@IBOutlet weak var statusItemMenu: NSMenu!
2121

22+
var uploding = false
23+
var needUploadFiles = [Any]()
24+
var resultUrls = [String]()
25+
2226
lazy var preferencesWindowController: PreferencesWindowController = {
2327
let storyboard = NSStoryboard(name: "Preferences", bundle: nil)
2428
return storyboard.instantiateInitialController() as? PreferencesWindowController ?? PreferencesWindowController()
2529
}()
2630

2731
func applicationDidFinishLaunching(_ aNotification: Notification) {
2832
// Insert code here to initialize your application
29-
33+
3034
self.resetNewVersionLaunchAtLogin()
3135

3236
indicator.minValue = 0.0
@@ -43,11 +47,11 @@ class AppDelegate: NSObject, NSApplicationDelegate {
4347
func applicationWillTerminate(_ notification: Notification) {
4448
NSStatusBar.system.removeStatusItem(statusItem)
4549
}
46-
50+
4751
func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
4852
return true
4953
}
50-
54+
5155
func resetNewVersionLaunchAtLogin() {
5256
guard let launchAtLogin = ConfigManager.shared.launchAtLogin else {
5357
return
@@ -110,11 +114,16 @@ extension AppDelegate {
110114
/* 选择文件 */
111115
@objc func selectFile() {
112116

117+
if self.uploding {
118+
NotificationExt.sendUplodingNotification()
119+
return
120+
}
121+
113122
NSApp.activate(ignoringOtherApps: true)
114123
let fileExtensions = BaseUploader.getFileExtensions()
115124

116125
let openPanel = NSOpenPanel()
117-
openPanel.allowsMultipleSelection = false
126+
openPanel.allowsMultipleSelection = true
118127
openPanel.canChooseDirectories = false
119128
openPanel.canCreateDirectories = false
120129
openPanel.canChooseFiles = true
@@ -126,46 +135,51 @@ extension AppDelegate {
126135
openPanel.begin { (result) -> Void in
127136
openPanel.close()
128137
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)
133139
}
134140
}
135141
}
136142

137143
@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: "文件格式不支持"))
153162
}
154-
self.uploadFile(url, data: nil)
155-
} else {
156-
self.uploadFaild(errorMsg: NSLocalizedString("file-format-is-not-supported", comment: "文件格式不支持"))
157163
}
158164
}
159165

160166
@objc func screenshotAndUpload() {
161167

168+
if self.uploding {
169+
NotificationExt.sendUplodingNotification()
170+
return
171+
}
172+
162173
let task = Process()
163174
task.launchPath = "/usr/sbin/screencapture"
164175
task.arguments = ["-i", "-c"]
165176
task.launch()
166177
task.waitUntilExit()
167178

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+
}
169183
}
170184

171185

@@ -174,12 +188,39 @@ extension AppDelegate {
174188
}
175189
}
176190

191+
// 上传多个文件
192+
func uploadFiles(_ files: [Any]) {
193+
self.needUploadFiles = files
194+
self.resultUrls.removeAll()
177195

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()
183224
}
184225
}
185226

@@ -188,8 +229,29 @@ extension AppDelegate {
188229
///
189230
func uploadCompleted(url: String) {
190231
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
193255
}
194256

195257
func copyUrl(url: String) -> String {
@@ -213,26 +275,34 @@ extension AppDelegate {
213275

214276
return outputUrl
215277
}
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 = "![pic](\(url))"
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
236306
}
237307
}
238308

@@ -241,7 +311,7 @@ extension AppDelegate: NSWindowDelegate, NSDraggingDestination {
241311
// MARK: 拖拽文件
242312

243313
func draggingEntered(_ sender: NSDraggingInfo) -> NSDragOperation {
244-
if sender.isValidFile {
314+
if sender.isValid {
245315
if let button = statusItem.button {
246316
button.image = NSImage(named: "uploadIcon")
247317
}
@@ -251,11 +321,13 @@ extension AppDelegate: NSWindowDelegate, NSDraggingDestination {
251321
}
252322

253323
func performDragOperation(_ sender: NSDraggingInfo) -> Bool {
254-
// TODO: 需要支持所有格式文件/根据图床支持的格式再进行判断
255-
if sender.isValidFile {
256-
let fileUrl = sender.draggedFileURL!.absoluteURL
324+
if sender.isValid {
257325
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)
259331
return true
260332
}
261333
return false

uPic/Extensions/NSDragingInfoExt.swift

+24-15
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,31 @@ extension NSDraggingInfo {
1717
}
1818
}
1919

20-
var draggedFileURL: NSURL? {
21-
let filenames = draggingPasteboard.propertyList(forType: NSPasteboard.PasteboardType("NSFilenamesPboardType")) as? [String]
22-
let path = filenames?.first
23-
return path.map(NSURL.init)
24-
}
25-
26-
var isValidFile: Bool {
27-
get {
28-
if fileExtensions.count == 0 {
29-
return true
30-
}
31-
32-
guard let fileExtension = draggedFileURL?.pathExtension?.lowercased() else {
33-
return false
20+
var draggedFileURLs: [NSURL] {
21+
var urls = [NSURL]()
22+
if let filenames = draggingPasteboard.propertyList(forType: NSPasteboard.PasteboardType("NSFilenamesPboardType")) as? [String] {
23+
for path in filenames {
24+
urls.append(NSURL(fileURLWithPath: path))
3425
}
35-
return fileExtensions.contains(fileExtension)
3626
}
27+
28+
if fileExtensions.count == 0 {
29+
return urls
30+
} else {
31+
// 过滤不支持的文件
32+
urls = urls.filter({url -> Bool in
33+
guard let fileExtension = url.pathExtension?.lowercased() else {
34+
return false
35+
}
36+
37+
return fileExtensions.contains(fileExtension)
38+
})
39+
}
40+
41+
return urls
42+
}
43+
44+
var isValid: Bool {
45+
return self.draggedFileURLs.count > 0
3746
}
3847
}

0 commit comments

Comments
 (0)