mirror of
https://github.com/leonboe1/QuickDrop.git
synced 2026-11-19 04:46:08 +00:00
Add sending files, images, and clipboard to iOS app
This commit is contained in:
1 parent
045725072b
commit
c781af554f
14 files changed
+802
-231
No files matched your search
@@ -56,7 +56,7 @@ class ShareViewController: NSViewController, ShareExtensionDelegate {
|
||||
textToSend = text
|
||||
|
||||
DispatchQueue.main.async {
|
||||
self.zipFolderAndSetUpIcon()
|
||||
self.setUpIcon()
|
||||
}
|
||||
} else {
|
||||
for attachment in attachments {
|
||||
@@ -72,7 +72,7 @@ class ShareViewController: NSViewController, ShareExtensionDelegate {
|
||||
|
||||
if self.urls.count == attachments.count {
|
||||
DispatchQueue.main.async {
|
||||
self.zipFolderAndSetUpIcon()
|
||||
self.setUpIcon()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -209,42 +209,7 @@ class ShareViewController: NSViewController, ShareExtensionDelegate {
|
||||
}
|
||||
|
||||
|
||||
private func zipFolderAndSetUpIcon() {
|
||||
for url in urls {
|
||||
if url.isFileURL {
|
||||
let isDirectory = UnsafeMutablePointer<ObjCBool>.allocate(capacity: 1)
|
||||
if FileManager.default.fileExists(atPath: url.path, isDirectory: isDirectory) && isDirectory.pointee.boolValue {
|
||||
do {
|
||||
let zipUrl = try createZipAtTmp(zipFilename: url.lastPathComponent, fromDirectory: url)
|
||||
|
||||
let index = urls.firstIndex(of: url)
|
||||
urls[index!] = zipUrl
|
||||
} catch {
|
||||
log("Error creating zip file: \(error)")
|
||||
|
||||
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
|
||||
log("Canceling share request because URL \(url) is a directory")
|
||||
|
||||
AudioManager.playErrorSound()
|
||||
let alert = NSAlert()
|
||||
alert.alertStyle = .critical
|
||||
|
||||
alert.messageText = "TypeNotSupported".localized()
|
||||
alert.informativeText = "TypeNotSupportedDescription".localized()
|
||||
alert.addButton(withTitle: "TypeNotSupportedButton".localized())
|
||||
|
||||
alert.beginSheetModal(for: self.view.window!) { _ in
|
||||
let cancelError = NSError(domain: NSCocoaErrorDomain, code: NSUserCancelledError, userInfo: nil)
|
||||
self.extensionContext!.cancelRequest(withError: cancelError)
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func setUpIcon() {
|
||||
if let textToSend = textToSend {
|
||||
let maxLength = 50
|
||||
let cleanedText = textToSend.replacingOccurrences(of: "\n", with: " ").replacingOccurrences(of: "\r", with: "")
|
||||
|
||||
@@ -7,41 +7,57 @@
|
||||
|
||||
import Foundation
|
||||
|
||||
enum CreateZipError: Swift.Error {
|
||||
case urlNotADirectory(URL)
|
||||
case failedToCreateZIP(Swift.Error)
|
||||
}
|
||||
|
||||
|
||||
func createZip(zipFinalURL: URL, fromDirectory directoryURL: URL) throws -> URL {
|
||||
|
||||
guard directoryURL.isDirectory else {
|
||||
throw CreateZipError.urlNotADirectory(directoryURL)
|
||||
class Zip {
|
||||
|
||||
static func createAtTemporaryDirectory(zipFilename: String, zipExtension: String = "zip", fromDirectory directoryURL: URL) throws -> URL {
|
||||
|
||||
let finalURL = FileManager.default.temporaryDirectory
|
||||
.appendingPathComponent(zipFilename)
|
||||
.appendingPathExtension(zipExtension)
|
||||
|
||||
return try createAt(
|
||||
zipFinalURL: finalURL,
|
||||
fromDirectory: directoryURL
|
||||
)
|
||||
}
|
||||
|
||||
var fileManagerError: Swift.Error?
|
||||
var coordinatorError: NSError?
|
||||
let coordinator = NSFileCoordinator()
|
||||
|
||||
coordinator.coordinate(readingItemAt: directoryURL, options: .forUploading, error: &coordinatorError) { zipCreatedURL in
|
||||
do {
|
||||
// clean if file exists
|
||||
if FileManager.default.fileExists(atPath: zipFinalURL.path) {
|
||||
try FileManager.default.removeItem(at: zipFinalURL)
|
||||
}
|
||||
|
||||
try FileManager.default.moveItem(at: zipCreatedURL, to: zipFinalURL)
|
||||
|
||||
} catch {
|
||||
fileManagerError = error
|
||||
|
||||
static func createAt(zipFinalURL: URL, fromDirectory directoryURL: URL) throws -> URL {
|
||||
|
||||
guard directoryURL.isDirectory else {
|
||||
throw CreateZipError.urlNotADirectory(directoryURL)
|
||||
}
|
||||
|
||||
var fileManagerError: Swift.Error?
|
||||
var coordinatorError: NSError?
|
||||
let coordinator = NSFileCoordinator()
|
||||
|
||||
coordinator.coordinate(readingItemAt: directoryURL, options: .forUploading, error: &coordinatorError) { zipCreatedURL in
|
||||
do {
|
||||
// clean if file exists
|
||||
if FileManager.default.fileExists(atPath: zipFinalURL.path) {
|
||||
try FileManager.default.removeItem(at: zipFinalURL)
|
||||
}
|
||||
|
||||
try FileManager.default.moveItem(at: zipCreatedURL, to: zipFinalURL)
|
||||
|
||||
} catch {
|
||||
fileManagerError = error
|
||||
}
|
||||
}
|
||||
|
||||
if let error = coordinatorError ?? fileManagerError {
|
||||
throw CreateZipError.failedToCreateZIP(error)
|
||||
}
|
||||
|
||||
return zipFinalURL
|
||||
}
|
||||
|
||||
if let error = coordinatorError ?? fileManagerError {
|
||||
throw CreateZipError.failedToCreateZIP(error)
|
||||
}
|
||||
|
||||
return zipFinalURL
|
||||
enum CreateZipError: Swift.Error {
|
||||
case urlNotADirectory(URL)
|
||||
case failedToCreateZIP(Swift.Error)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -50,16 +66,3 @@ extension URL {
|
||||
(try? resourceValues(forKeys: [.isDirectoryKey]))?.isDirectory == true
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func createZipAtTmp(zipFilename: String, zipExtension: String = "zip", fromDirectory directoryURL: URL) throws -> URL {
|
||||
|
||||
let finalURL = FileManager.default.temporaryDirectory
|
||||
.appendingPathComponent(zipFilename)
|
||||
.appendingPathExtension(zipExtension)
|
||||
|
||||
return try createZip(
|
||||
zipFinalURL: finalURL,
|
||||
fromDirectory: directoryURL
|
||||
)
|
||||
}
|
||||
Reference in new issue
Block a user