Trying to make share extension, have to recreate project

This commit is contained in:
2025-06-04 13:54:13 -04:00 Unverified
parent 36949981b4
commit b27b223a5e
14 changed files with 781 additions and 12 deletions
@@ -11,6 +11,12 @@ class GIFFileManager {
// MARK: - File Storage
private var documentsDirectory: URL {
// First try to get the App Group container
if let containerURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.gifcollector") {
return containerURL
}
// Fall back to the app's documents directory if App Group is not available
let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
return paths[0]
}
@@ -32,6 +38,11 @@ class GIFFileManager {
}
func storeGIF(data: Data, fromURL urlString: String) -> String? {
// Check if this is a shared GIF from the share extension
if let sharedGIFPath = checkForSharedGIF(withURL: urlString), FileManager.default.fileExists(atPath: sharedGIFPath) {
return sharedGIFPath
}
// Create a unique filename based on the URL hash and timestamp
let urlHash = urlString.hashValue
let timestamp = Int(Date().timeIntervalSince1970)
@@ -129,4 +140,38 @@ class GIFFileManager {
print("Error cleaning up old GIFs: \(error)")
}
}
// Check if we already have this GIF in the shared container
private func checkForSharedGIF(withURL urlString: String) -> String? {
guard let containerURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.gifcollector") else {
return nil
}
let sharedGIFsFolder = containerURL.appendingPathComponent("GIFs", isDirectory: true)
guard FileManager.default.fileExists(atPath: sharedGIFsFolder.path) else {
return nil
}
do {
let fileURLs = try FileManager.default.contentsOfDirectory(at: sharedGIFsFolder, includingPropertiesForKeys: nil)
// Find any shared GIF that matches our URL (usually won't find any, but helps avoid duplicates)
let userDefaults = UserDefaults(suiteName: "group.gifcollector")
if let pendingGIFs = userDefaults?.array(forKey: "pendingGIFs") as? [[String: Any]] {
for gifInfo in pendingGIFs {
if let originURL = gifInfo["originalURL"] as? String,
let path = gifInfo["localFilePath"] as? String,
originURL == urlString {
return path
}
}
}
return nil
} catch {
print("Error checking for shared GIFs: \(error)")
return nil
}
}
}