Skip to content

Commit

Permalink
hadnle pdf content type
Browse files Browse the repository at this point in the history
  • Loading branch information
achorein committed Sep 23, 2024
1 parent 89b70a8 commit 12d70bb
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 26 deletions.
73 changes: 48 additions & 25 deletions plugin/src/ios/ShareExtensionViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class ShareViewController: UIViewController {
let textContentType = kUTTypeText as String
let urlContentType = kUTTypeURL as String
let fileURLType = kUTTypeFileURL as String
let pdfContentType = kUTTypePDF as String

override func viewDidLoad() {
super.viewDidLoad()
Expand All @@ -42,6 +43,8 @@ class ShareViewController: UIViewController {
await handleVideos(content: content, attachment: attachment, index: index)
} else if attachment.hasItemConformingToTypeIdentifier(fileURLType) {
await handleFiles(content: content, attachment: attachment, index: index)
} else if attachment.hasItemConformingToTypeIdentifier(pdfContentType) {
await handlePdf(content: content, attachment: attachment, index: index)
} else if attachment.hasItemConformingToTypeIdentifier(urlContentType) {
await handleUrl(content: content, attachment: attachment, index: index)
} else if attachment.hasItemConformingToTypeIdentifier(textContentType) {
Expand Down Expand Up @@ -235,36 +238,28 @@ class ShareViewController: UIViewController {
}
}

private func handlePdf(content: NSExtensionItem, attachment: NSItemProvider, index: Int) async {
Task.detached { [weak self] in
if let url = try? await attachment.loadItem(forTypeIdentifier: self!.pdfContentType) as? URL {
Task { @MainActor in

await self?.handleFileURL(content: content, url: url, index: index)

}
} else {
NSLog("[ERROR] Cannot load pdf content !\(String(describing: content))")
await self?.dismissWithError(
message: "Cannot load pdf content \(String(describing: content))")
}
}
}

private func handleFiles(content: NSExtensionItem, attachment: NSItemProvider, index: Int) async {
Task.detached { [weak self] in
if let url = try? await attachment.loadItem(forTypeIdentifier: self!.fileURLType) as? URL {
Task { @MainActor in

// Always copy
let fileName = self!.getFileName(from: url, type: .file)
let fileExtension = self!.getExtension(from: url, type: .file)
let fileSize = self!.getFileSize(from: url)
let mimeType = url.mimeType(ext: fileExtension)
let newName = "\(UUID().uuidString).\(fileExtension)"
let newPath = FileManager.default
.containerURL(
forSecurityApplicationGroupIdentifier: "group.\(self!.hostAppBundleIdentifier)")!
.appendingPathComponent(newName)
let copied = self!.copyFile(at: url, to: newPath)
if copied {
self!.sharedMedia.append(
SharedMediaFile(
path: newPath.absoluteString, thumbnail: nil, fileName: fileName,
fileSize: fileSize, width: nil, height: nil, duration: nil, mimeType: mimeType,
type: .file))
}

if index == (content.attachments?.count)! - 1 {
let userDefaults = UserDefaults(suiteName: "group.\(self!.hostAppBundleIdentifier)")
userDefaults?.set(self!.toData(data: self!.sharedMedia), forKey: self!.sharedKey)
userDefaults?.synchronize()
self!.redirectToHostApp(type: .file)
}
await self?.handleFileURL(content: content, url: url, index: index)

}
} else {
Expand All @@ -275,6 +270,34 @@ class ShareViewController: UIViewController {
}
}

private func handleFileURL(content: NSExtensionItem, url: URL, index: Int) async {
// Always copy
let fileName = self.getFileName(from: url, type: .file)
let fileExtension = self.getExtension(from: url, type: .file)
let fileSize = self.getFileSize(from: url)
let mimeType = url.mimeType(ext: fileExtension)
let newName = "\(UUID().uuidString).\(fileExtension)"
let newPath = FileManager.default
.containerURL(
forSecurityApplicationGroupIdentifier: "group.\(self.hostAppBundleIdentifier)")!
.appendingPathComponent(newName)
let copied = self.copyFile(at: url, to: newPath)
if copied {
self.sharedMedia.append(
SharedMediaFile(
path: newPath.absoluteString, thumbnail: nil, fileName: fileName,
fileSize: fileSize, width: nil, height: nil, duration: nil, mimeType: mimeType,
type: .file))
}

if index == (content.attachments!.count) - 1 {
let userDefaults = UserDefaults(suiteName: "group.\(self.hostAppBundleIdentifier)")
userDefaults?.set(self.toData(data: self.sharedMedia), forKey: self.sharedKey)
userDefaults?.synchronize()
self.redirectToHostApp(type: .file)
}
}

private func dismissWithError(message: String? = nil) {
DispatchQueue.main.async {
NSLog("[ERROR] Error loading application ! \(message!)")
Expand Down
2 changes: 1 addition & 1 deletion plugin/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export type CustomParameter = { [key: string]: string };

export type Parameters = {
iosActivationRules?: { [key: string]: number | boolean | string };
iosActivationRules?: { [key: string]: number | boolean | string } | string;
androidMainActivityAttributes?: CustomParameter;
androidIntentFilters?: ("text/*" | "image/*" | "video/*" | "*/*")[];
androidMultiIntentFilters?: ("image/*" | "video/*" | "*/*")[];
Expand Down

0 comments on commit 12d70bb

Please sign in to comment.