From 410ed9edec893469c70f13bc0991b9af8dfb0413 Mon Sep 17 00:00:00 2001 From: Fuwn Date: Tue, 25 Feb 2025 02:02:11 -0800 Subject: feat: Development commit --- Sora/Sora.entitlements | 2 + Sora/Views/Post/Details/PostDetailsImageView.swift | 51 ++++++++++++++++++++-- Sora/Views/Post/Details/PostDetailsView.swift | 2 +- 3 files changed, 51 insertions(+), 4 deletions(-) diff --git a/Sora/Sora.entitlements b/Sora/Sora.entitlements index bcc27ba..a2c464c 100644 --- a/Sora/Sora.entitlements +++ b/Sora/Sora.entitlements @@ -4,6 +4,8 @@ com.apple.security.app-sandbox + com.apple.security.assets.pictures.read-write + com.apple.security.files.user-selected.read-only com.apple.security.network.client diff --git a/Sora/Views/Post/Details/PostDetailsImageView.swift b/Sora/Views/Post/Details/PostDetailsImageView.swift index f36904f..46a8c5e 100644 --- a/Sora/Views/Post/Details/PostDetailsImageView.swift +++ b/Sora/Views/Post/Details/PostDetailsImageView.swift @@ -6,8 +6,9 @@ struct PostDetailsImageView: View { var url: URL? @Binding var loadingState: BooruPostLoadingState var finalLoadingState: BooruPostLoadingState - var postURL: URL? + private var postURL: URL? let placeholder: () -> Placeholder + let post: BooruPost? @State private var currentScale: CGFloat = 1.0 @State private var finalScale: CGFloat = 1.0 @State private var currentOffset: CGSize = .zero @@ -54,6 +55,15 @@ struct PostDetailsImageView: View { } #endif + #if os(macOS) + Button { + saveImageToPicturesFolder() + } label: { + Label("Save Image", systemImage: "square.and.arrow.down") + } + .keyboardShortcut("s", modifiers: [.command]) + #endif + #if os(iOS) if settings.enableShareShortcut { Button { @@ -92,7 +102,7 @@ struct PostDetailsImageView: View { url: URL?, loadingStage: Binding, finalLoadingState: BooruPostLoadingState = .loadingFile, - postURL: URL? = nil, + post: BooruPost? = nil, @ViewBuilder placeholder: @escaping () -> Placeholder = { GeometryReader { geometry in ProgressView() @@ -105,7 +115,42 @@ struct PostDetailsImageView: View { self.url = url _loadingState = loadingStage self.finalLoadingState = finalLoadingState - self.postURL = postURL + self.postURL = + post != nil ? URL(string: "https://yande.re/post/show/\(String(post?.id ?? "0"))")! : nil self.placeholder = placeholder + self.post = post + } + + private func saveImageToPicturesFolder() { + guard let url = self.url else { return } + + URLSession.shared.dataTask(with: url) { data, _, _ in + guard let data, let post else { return } + + let picturesURL = FileManager.default.homeDirectoryForCurrentUser + .appendingPathComponent("Pictures/Sora") + + do { + try FileManager.default.createDirectory( + at: picturesURL, + withIntermediateDirectories: true + ) + try data.write( + to: + picturesURL + .appendingPathComponent("\(post.id).\(url.pathExtension)") + ) + try post.tags.joined(separator: "\n").write( + to: picturesURL.appendingPathComponent( + "\(post.id).txt" + ), + atomically: true, + encoding: .utf8 + ) + } catch { + print("PostDetailsImageView.saveImageToPicturesFolder: \(error)") + } + } + .resume() } } diff --git a/Sora/Views/Post/Details/PostDetailsView.swift b/Sora/Views/Post/Details/PostDetailsView.swift index 802abb6..d1099ea 100644 --- a/Sora/Views/Post/Details/PostDetailsView.swift +++ b/Sora/Views/Post/Details/PostDetailsView.swift @@ -23,7 +23,7 @@ struct PostDetailsView: View { url: imageURL, loadingStage: $loadingStage, finalLoadingState: .loaded, - postURL: URL(string: "https://yande.re/post/show/\(post.id)")! + post: post ) { PostDetailsImageView( url: post.previewURL, -- cgit v1.2.3