mirror of
https://github.com/leonboe1/QuickDrop.git
synced 2026-11-21 22:05:59 +00:00
Feat: Remove files that were already imported in photos app
This commit is contained in:
1 parent
3fa1d8621e
commit
4ddf39817c
1 file changed
+42
-29
@@ -12,6 +12,7 @@ import UniformTypeIdentifiers
|
|||||||
|
|
||||||
struct PhotoManager {
|
struct PhotoManager {
|
||||||
|
|
||||||
|
/// Returns if there are photos or videos among the given URLs.
|
||||||
static func hasPhotosOrVideos(at urls: [URL]) -> Bool {
|
static func hasPhotosOrVideos(at urls: [URL]) -> Bool {
|
||||||
urls.contains(where: { isImageFile(at: $0) || isVideoFile(at: $0) })
|
urls.contains(where: { isImageFile(at: $0) || isVideoFile(at: $0) })
|
||||||
}
|
}
|
||||||
@@ -22,10 +23,8 @@ struct PhotoManager {
|
|||||||
/// - urls: Remote or local URLs to inspect.
|
/// - urls: Remote or local URLs to inspect.
|
||||||
/// - openPhotosOnSuccess: If true, will try to open the Photos app after saving.
|
/// - openPhotosOnSuccess: If true, will try to open the Photos app after saving.
|
||||||
/// - Throws: `SaveToPhotosError` or underlying URLSession/Photos errors.
|
/// - Throws: `SaveToPhotosError` or underlying URLSession/Photos errors.
|
||||||
static func saveMediaToPhotoLibrary(
|
static func saveMediaToPhotoLibrary(from urls: [URL], openPhotosOnSuccess: Bool = true) async throws {
|
||||||
from urls: [URL],
|
|
||||||
openPhotosOnSuccess: Bool = true
|
|
||||||
) async throws {
|
|
||||||
let candidates = urls.filter { isImageFile(at: $0) || isVideoFile(at: $0) }
|
let candidates = urls.filter { isImageFile(at: $0) || isVideoFile(at: $0) }
|
||||||
guard !candidates.isEmpty else { throw SaveToPhotosError.noSavableContent }
|
guard !candidates.isEmpty else { throw SaveToPhotosError.noSavableContent }
|
||||||
|
|
||||||
@@ -34,27 +33,17 @@ struct PhotoManager {
|
|||||||
throw SaveToPhotosError.permissionDenied
|
throw SaveToPhotosError.permissionDenied
|
||||||
}
|
}
|
||||||
|
|
||||||
struct ImageItem {
|
|
||||||
let fileURL: URL
|
|
||||||
let creationDate: Date?
|
|
||||||
}
|
|
||||||
|
|
||||||
struct VideoItem {
|
|
||||||
let fileURL: URL
|
|
||||||
let creationDate: Date?
|
|
||||||
}
|
|
||||||
|
|
||||||
var imageItems: [ImageItem] = []
|
var imageItems: [ImageItem] = []
|
||||||
var videoItems: [VideoItem] = []
|
var videoItems: [VideoItem] = []
|
||||||
|
|
||||||
// 1. Download and write to temp files, extracting EXIF date for images.
|
|
||||||
|
// Extract creation date for image and video
|
||||||
for sourceURL in candidates {
|
for sourceURL in candidates {
|
||||||
|
|
||||||
if isImageFile(at: sourceURL) {
|
if isImageFile(at: sourceURL) {
|
||||||
let exifDate = EXIFUtils.exifOriginalDate(from: sourceURL)
|
let exifDate = EXIFUtils.exifOriginalDate(from: sourceURL)
|
||||||
imageItems.append(ImageItem(fileURL: sourceURL, creationDate: exifDate))
|
imageItems.append(ImageItem(fileURL: sourceURL, creationDate: exifDate))
|
||||||
} else {
|
} else {
|
||||||
// For videos you may want to parse container metadata; as a fallback use file creation date.
|
|
||||||
let videoDate = await getVideoCreationDate(from: sourceURL)
|
let videoDate = await getVideoCreationDate(from: sourceURL)
|
||||||
videoItems.append(VideoItem(fileURL: sourceURL, creationDate: videoDate))
|
videoItems.append(VideoItem(fileURL: sourceURL, creationDate: videoDate))
|
||||||
}
|
}
|
||||||
@@ -64,7 +53,8 @@ struct PhotoManager {
|
|||||||
throw SaveToPhotosError.noSavableContent
|
throw SaveToPhotosError.noSavableContent
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. Import into Photos; explicitly set creationDate so it doesn't default to "now".
|
|
||||||
|
// Import into Photos; explicitly set creationDate so it doesn't default to "now".
|
||||||
try await withCheckedThrowingContinuation { continuation in
|
try await withCheckedThrowingContinuation { continuation in
|
||||||
PHPhotoLibrary.shared().performChanges({
|
PHPhotoLibrary.shared().performChanges({
|
||||||
for item in imageItems {
|
for item in imageItems {
|
||||||
@@ -92,7 +82,19 @@ struct PhotoManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// 5. Open the Photos app
|
// Delete files in documents directory after successful import
|
||||||
|
imageItems.forEach({ item in
|
||||||
|
do { try FileManager.default.removeItem(at: item.fileURL) }
|
||||||
|
catch { log("Failed to delete image at \(item.fileURL): \(error)") }
|
||||||
|
})
|
||||||
|
|
||||||
|
videoItems.forEach({ item in
|
||||||
|
do { try FileManager.default.removeItem(at: item.fileURL) }
|
||||||
|
catch { log("Failed to delete video at \(item.fileURL): \(error)") }
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
// Open the Photos app
|
||||||
if openPhotosOnSuccess {
|
if openPhotosOnSuccess {
|
||||||
if let url = URL(string: "photos-redirect://") {
|
if let url = URL(string: "photos-redirect://") {
|
||||||
DispatchQueue.main.async {
|
DispatchQueue.main.async {
|
||||||
@@ -104,7 +106,6 @@ struct PhotoManager {
|
|||||||
|
|
||||||
|
|
||||||
private static func isImageFile(at url: URL) -> Bool {
|
private static func isImageFile(at url: URL) -> Bool {
|
||||||
// Assumes there *is* an extension, as per your requirement
|
|
||||||
let ext = url.pathExtension.lowercased()
|
let ext = url.pathExtension.lowercased()
|
||||||
guard let type = UTType(filenameExtension: ext) else {
|
guard let type = UTType(filenameExtension: ext) else {
|
||||||
return false
|
return false
|
||||||
@@ -155,20 +156,32 @@ struct PhotoManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
enum SaveToPhotosError: Error, LocalizedError {
|
private enum SaveToPhotosError: Error, LocalizedError {
|
||||||
case noSavableContent
|
case noSavableContent
|
||||||
case permissionDenied
|
case permissionDenied
|
||||||
case saveFailed
|
case saveFailed
|
||||||
|
|
||||||
var errorDescription: String? {
|
var errorDescription: String? {
|
||||||
switch self {
|
switch self {
|
||||||
case .noSavableContent:
|
case .noSavableContent:
|
||||||
return "SaveToPhotosErrorNoSavableContent".localized()
|
return "SaveToPhotosErrorNoSavableContent".localized()
|
||||||
case .permissionDenied:
|
case .permissionDenied:
|
||||||
return "SaveToPhotosErrorPermissionDenied".localized()
|
return "SaveToPhotosErrorPermissionDenied".localized()
|
||||||
case .saveFailed:
|
case .saveFailed:
|
||||||
return "SaveToPhotosErrorSaveFailed".localized()
|
return "SaveToPhotosErrorSaveFailed".localized()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private struct ImageItem {
|
||||||
|
let fileURL: URL
|
||||||
|
let creationDate: Date?
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private struct VideoItem {
|
||||||
|
let fileURL: URL
|
||||||
|
let creationDate: Date?
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user