about summary refs log tree commit diff
path: root/modules
diff options
context:
space:
mode:
authorHailey <me@haileyok.com>2024-09-24 01:02:58 -0700
committerGitHub <noreply@github.com>2024-09-24 01:02:58 -0700
commita1e212ab59c9bd98b0923a7911c5140c3f10ac25 (patch)
treefd7f018f10974e71e9dd2af83af4063bd3608cf2 /modules
parentb57ddd0968ebc08e9d5fc80b2e93fdb8786aef7e (diff)
downloadvoidsky-a1e212ab59c9bd98b0923a7911c5140c3f10ac25.tar.zst
[Share Extension] Update to support video (#5385)
Diffstat (limited to 'modules')
-rw-r--r--modules/Share-with-Bluesky/Info.plist4
-rw-r--r--modules/Share-with-Bluesky/ShareViewController.swift77
2 files changed, 47 insertions, 34 deletions
diff --git a/modules/Share-with-Bluesky/Info.plist b/modules/Share-with-Bluesky/Info.plist
index 421abb3c4..43f46a5e5 100644
--- a/modules/Share-with-Bluesky/Info.plist
+++ b/modules/Share-with-Bluesky/Info.plist
@@ -16,6 +16,8 @@
           <integer>1</integer>
           <key>NSExtensionActivationSupportsImageWithMaxCount</key>
           <integer>10</integer>
+          <key>NSExtensionActivationSupportsMovieWithMaxCount</key>
+          <integer>1</integer>
         </dict>
       </dict>
       <key>NSExtensionPointIdentifier</key>
@@ -38,4 +40,4 @@
     <key>CFBundleShortVersionString</key>
     <string>$(MARKETING_VERSION)</string>
   </dict>
-</plist>
\ No newline at end of file
+</plist>
diff --git a/modules/Share-with-Bluesky/ShareViewController.swift b/modules/Share-with-Bluesky/ShareViewController.swift
index c045d578f..63143277a 100644
--- a/modules/Share-with-Bluesky/ShareViewController.swift
+++ b/modules/Share-with-Bluesky/ShareViewController.swift
@@ -5,7 +5,6 @@ class ShareViewController: UIViewController {
   // scheme.
   let appScheme = Bundle.main.object(forInfoDictionaryKey: "MainAppScheme") as? String ?? "bluesky"
 
-  //
   override func viewDidAppear(_ animated: Bool) {
     super.viewDidAppear(animated)
 
@@ -24,6 +23,8 @@ class ShareViewController: UIViewController {
         await self.handleUrl(item: firstAttachment)
       } else if firstAttachment.hasItemConformingToTypeIdentifier("public.image") {
         await self.handleImages(items: attachments)
+      } else if firstAttachment.hasItemConformingToTypeIdentifier("public.video") {
+        await self.handleVideos(items: attachments)
       } else {
         self.completeRequest()
       }
@@ -31,31 +32,23 @@ class ShareViewController: UIViewController {
   }
 
   private func handleText(item: NSItemProvider) async {
-    do {
-      if let data = try await item.loadItem(forTypeIdentifier: "public.text") as? String {
-        if let encoded = data.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed),
-           let url = URL(string: "\(self.appScheme)://intent/compose?text=\(encoded)") {
-          _ = self.openURL(url)
-        }
+    if let data = try? await item.loadItem(forTypeIdentifier: "public.text") as? String {
+      if let encoded = data.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed),
+         let url = URL(string: "\(self.appScheme)://intent/compose?text=\(encoded)") {
+        _ = self.openURL(url)
       }
-      self.completeRequest()
-    } catch {
-      self.completeRequest()
     }
+    self.completeRequest()
   }
 
   private func handleUrl(item: NSItemProvider) async {
-    do {
-      if let data = try await item.loadItem(forTypeIdentifier: "public.url") as? URL {
-        if let encoded = data.absoluteString.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed),
-           let url = URL(string: "\(self.appScheme)://intent/compose?text=\(encoded)") {
-          _ = self.openURL(url)
-        }
+    if let data = try? await item.loadItem(forTypeIdentifier: "public.url") as? URL {
+      if let encoded = data.absoluteString.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed),
+         let url = URL(string: "\(self.appScheme)://intent/compose?text=\(encoded)") {
+        _ = self.openURL(url)
       }
-      self.completeRequest()
-    } catch {
-      self.completeRequest()
     }
+    self.completeRequest()
   }
 
   private func handleImages(items: [NSItemProvider]) async {
@@ -105,6 +98,25 @@ class ShareViewController: UIViewController {
     self.completeRequest()
   }
 
+  private func handleVideos(items: [NSItemProvider]) async {
+    let firstItem = items.first
+
+    if let dataUri = try? await firstItem?.loadItem(forTypeIdentifier: "public.video") as? URL {
+      let ext = String(dataUri.lastPathComponent.split(separator: ".").last ?? "mp4")
+      if let tempUrl = getTempUrl(ext: ext) {
+        let data = try? Data(contentsOf: dataUri)
+        try? data?.write(to: tempUrl)
+
+        if let encoded = dataUri.absoluteString.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed),
+           let url = URL(string: "\(self.appScheme)://intent/compose?videoUri=\(encoded)") {
+          _ = self.openURL(url)
+        }
+      }
+    }
+
+    self.completeRequest()
+  }
+
   private func saveImageWithInfo(_ image: UIImage?) -> String? {
     guard let image = image else {
       return nil
@@ -114,27 +126,26 @@ class ShareViewController: UIViewController {
       // Saving this file to the bundle group's directory lets us access it from
       // inside of the app. Otherwise, we wouldn't have access even though the
       // extension does.
-      if let dir = FileManager()
-        .containerURL(
-          forSecurityApplicationGroupIdentifier: "group.app.bsky") {
-        let filePath = "\(dir.absoluteString)\(ProcessInfo.processInfo.globallyUniqueString).jpeg"
-
-        if let newUri = URL(string: filePath),
-           let jpegData = image.jpegData(compressionQuality: 1) {
-          try jpegData.write(to: newUri)
-          return "\(newUri.absoluteString)|\(image.size.width)|\(image.size.height)"
-        }
+      if let tempUrl = getTempUrl(ext: "jpeg"),
+         let jpegData = image.jpegData(compressionQuality: 1) {
+          try jpegData.write(to: tempUrl)
+          return "\(tempUrl.absoluteString)|\(image.size.width)|\(image.size.height)"
       }
-      return nil
-    } catch {
-      return nil
-    }
+    } catch {}
+    return nil
   }
 
   private func completeRequest() {
     self.extensionContext?.completeRequest(returningItems: nil)
   }
 
+  private func getTempUrl(ext: String) -> URL? {
+    if let dir = FileManager().containerURL(forSecurityApplicationGroupIdentifier: "group.app.bsky") {
+      return URL(string: "\(dir.absoluteString)\(ProcessInfo.processInfo.globallyUniqueString).\(ext)")!
+    }
+    return nil
+  }
+
   @objc func openURL(_ url: URL) -> Bool {
     var responder: UIResponder? = self
     while responder != nil {