-
Notifications
You must be signed in to change notification settings - Fork 45
Adds support for PostImages.org for image hosting #1218
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
JonET
wants to merge
1
commit into
main
Choose a base branch
from
postimages-hosting
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| // ImageUploadProvider.swift | ||
| // | ||
| // Copyright 2025 Awful Contributors. CC BY-NC-SA 3.0 US https://github.com/Awful/Awful.app | ||
|
|
||
| import Foundation | ||
| import Photos | ||
| import UIKit | ||
|
|
||
| /// Common protocol for image upload providers (Imgur, PostImages, etc.) | ||
| public protocol ImageUploadProvider { | ||
| /// Upload a UIImage | ||
| @discardableResult | ||
| func upload(_ image: UIImage, completion: @escaping (Result<ImageUploadResponse, Error>) -> Void) -> Progress | ||
|
|
||
| /// Upload a Photos asset | ||
| @discardableResult | ||
| func upload(_ asset: PHAsset, completion: @escaping (Result<ImageUploadResponse, Error>) -> Void) -> Progress | ||
|
|
||
| /// Upload from image picker info dictionary | ||
| @discardableResult | ||
| func upload(_ info: [UIImagePickerController.InfoKey: Any], completion: @escaping (Result<ImageUploadResponse, Error>) -> Void) -> Progress | ||
| } | ||
|
|
||
| /// Standardized response from any image upload provider | ||
| public struct ImageUploadResponse { | ||
| /// The URL of the uploaded image | ||
| public let imageURL: URL | ||
|
|
||
| /// Optional deletion URL/hash (not all providers support this) | ||
| public let deleteURL: URL? | ||
|
|
||
| /// Provider-specific metadata | ||
| public let metadata: [String: Any] | ||
|
|
||
| public init(imageURL: URL, deleteURL: URL? = nil, metadata: [String: Any] = [:]) { | ||
| self.imageURL = imageURL | ||
| self.deleteURL = deleteURL | ||
| self.metadata = metadata | ||
| } | ||
| } | ||
|
|
||
| /// Errors that can occur during image upload | ||
| public enum ImageUploadProviderError: LocalizedError { | ||
| case unsupportedImageFormat | ||
| case uploadFailed(String) | ||
| case invalidResponse | ||
| case providerUnavailable | ||
|
|
||
| public var errorDescription: String? { | ||
| switch self { | ||
| case .unsupportedImageFormat: | ||
| return "The image format is not supported" | ||
| case .uploadFailed(let message): | ||
| return "Upload failed: \(message)" | ||
| case .invalidResponse: | ||
| return "Invalid response from image hosting service" | ||
| case .providerUnavailable: | ||
| return "Image hosting service is unavailable" | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,224 @@ | ||
| // ImageUploadManager.swift | ||
| // | ||
| // Copyright 2025 Awful Contributors. CC BY-NC-SA 3.0 US https://github.com/Awful/Awful.app | ||
|
|
||
| import Foundation | ||
| import AwfulSettings | ||
| import ImgurAnonymousAPI | ||
| @_exported import PostImagesAPI | ||
| import Photos | ||
| import UIKit | ||
| import os | ||
|
|
||
| private let logger = Logger(subsystem: Bundle.main.bundleIdentifier!, category: "ImageUploadManager") | ||
|
|
||
| /// Manages image uploads using the configured provider (Imgur or PostImages) | ||
| public final class ImageUploadManager { | ||
|
|
||
| public static let shared = ImageUploadManager() | ||
|
|
||
| @FoilDefaultStorage(Settings.imageHostingProvider) private var imageHostingProvider | ||
| @FoilDefaultStorage(Settings.imgurUploadMode) private var imgurUploadMode | ||
|
|
||
| private init() {} | ||
|
|
||
| /// Get the current upload provider based on settings | ||
| private var currentProvider: ImageUploadProvider { | ||
| switch imageHostingProvider { | ||
| case .postImages: | ||
| logger.debug("Using PostImages provider") | ||
| return PostImagesUploadProviderAdapter() | ||
|
|
||
| case .imgur: | ||
| logger.debug("Using Imgur provider (mode: \(self.imgurUploadMode.rawValue))") | ||
| return ImgurUploadProviderAdapter() | ||
| } | ||
| } | ||
|
|
||
| /// Check if authentication is needed before uploading | ||
| public var needsAuthentication: Bool { | ||
| switch imageHostingProvider { | ||
| case .postImages: | ||
| return false | ||
| case .imgur: | ||
| return imgurUploadMode == .account && !ImgurAuthManager.shared.isAuthenticated | ||
| } | ||
| } | ||
|
|
||
| /// Upload a UIImage | ||
| @discardableResult | ||
| public func upload(_ image: UIImage, completion: @escaping (Result<ImageUploadResponse, Error>) -> Void) -> Progress { | ||
| if needsAuthentication { | ||
| let progress = Progress(totalUnitCount: 1) | ||
| progress.completedUnitCount = 1 | ||
| DispatchQueue.main.async { | ||
| completion(.failure(ImageUploadProviderError.providerUnavailable)) | ||
| } | ||
| return progress | ||
| } | ||
|
|
||
| return currentProvider.upload(image, completion: completion) | ||
| } | ||
|
|
||
| /// Upload a Photos asset | ||
| @discardableResult | ||
| public func upload(_ asset: PHAsset, completion: @escaping (Result<ImageUploadResponse, Error>) -> Void) -> Progress { | ||
| if needsAuthentication { | ||
| let progress = Progress(totalUnitCount: 1) | ||
| progress.completedUnitCount = 1 | ||
| DispatchQueue.main.async { | ||
| completion(.failure(ImageUploadProviderError.providerUnavailable)) | ||
| } | ||
| return progress | ||
| } | ||
|
|
||
| return currentProvider.upload(asset, completion: completion) | ||
| } | ||
|
|
||
| /// Upload from image picker info dictionary | ||
| @discardableResult | ||
| public func upload(_ info: [UIImagePickerController.InfoKey: Any], completion: @escaping (Result<ImageUploadResponse, Error>) -> Void) -> Progress { | ||
| if needsAuthentication { | ||
| let progress = Progress(totalUnitCount: 1) | ||
| progress.completedUnitCount = 1 | ||
| DispatchQueue.main.async { | ||
| completion(.failure(ImageUploadProviderError.providerUnavailable)) | ||
| } | ||
| return progress | ||
| } | ||
|
|
||
| return currentProvider.upload(info, completion: completion) | ||
| } | ||
| } | ||
|
|
||
| // MARK: - Provider Adapters | ||
|
|
||
| /// Adapter to make ImgurUploader conform to ImageUploadProvider | ||
| private class ImgurUploadProviderAdapter: ImageUploadProvider { | ||
|
|
||
| func upload(_ image: UIImage, completion: @escaping (Result<ImageUploadResponse, Error>) -> Void) -> Progress { | ||
| return ImgurUploader.shared.upload(image) { result in | ||
| switch result { | ||
| case .success(let response): | ||
| let uploadResponse = ImageUploadResponse( | ||
| imageURL: response.link, | ||
| deleteURL: nil, // Could be constructed from delete hash if needed | ||
| metadata: [ | ||
| "id": response.id, | ||
| "postLimit": response.postLimit as Any, | ||
| "rateLimit": response.rateLimit as Any | ||
| ] | ||
| ) | ||
| completion(.success(uploadResponse)) | ||
| case .failure(let error): | ||
| completion(.failure(error)) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func upload(_ asset: PHAsset, completion: @escaping (Result<ImageUploadResponse, Error>) -> Void) -> Progress { | ||
| return ImgurUploader.shared.upload(asset) { result in | ||
| switch result { | ||
| case .success(let response): | ||
| let uploadResponse = ImageUploadResponse( | ||
| imageURL: response.link, | ||
| deleteURL: nil, | ||
| metadata: [ | ||
| "id": response.id, | ||
| "postLimit": response.postLimit as Any, | ||
| "rateLimit": response.rateLimit as Any | ||
| ] | ||
| ) | ||
| completion(.success(uploadResponse)) | ||
| case .failure(let error): | ||
| completion(.failure(error)) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func upload(_ info: [UIImagePickerController.InfoKey : Any], completion: @escaping (Result<ImageUploadResponse, Error>) -> Void) -> Progress { | ||
| return ImgurUploader.shared.upload(info) { result in | ||
| switch result { | ||
| case .success(let response): | ||
| let uploadResponse = ImageUploadResponse( | ||
| imageURL: response.link, | ||
| deleteURL: nil, | ||
| metadata: [ | ||
| "id": response.id, | ||
| "postLimit": response.postLimit as Any, | ||
| "rateLimit": response.rateLimit as Any | ||
| ] | ||
| ) | ||
| completion(.success(uploadResponse)) | ||
| case .failure(let error): | ||
| completion(.failure(error)) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Adapter to make PostImagesUploader conform to ImageUploadProvider | ||
| private class PostImagesUploadProviderAdapter: ImageUploadProvider { | ||
|
|
||
| private let uploader = PostImagesUploader() | ||
|
|
||
| func upload(_ image: UIImage, completion: @escaping (Result<ImageUploadResponse, Error>) -> Void) -> Progress { | ||
| return uploader.upload(image) { result in | ||
| switch result { | ||
| case .success(let response): | ||
| let uploadResponse = ImageUploadResponse( | ||
| imageURL: response.imageURL, | ||
| deleteURL: nil, // PostImages doesn't provide deletion URLs | ||
| metadata: [ | ||
| "directLink": response.directLink as Any, | ||
| "viewerLink": response.viewerLink as Any, | ||
| "thumbnailLink": response.thumbnailLink as Any | ||
| ] | ||
| ) | ||
| completion(.success(uploadResponse)) | ||
| case .failure(let error): | ||
| completion(.failure(error)) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func upload(_ asset: PHAsset, completion: @escaping (Result<ImageUploadResponse, Error>) -> Void) -> Progress { | ||
| return uploader.upload(asset) { result in | ||
| switch result { | ||
| case .success(let response): | ||
| let uploadResponse = ImageUploadResponse( | ||
| imageURL: response.imageURL, | ||
| deleteURL: nil, | ||
| metadata: [ | ||
| "directLink": response.directLink as Any, | ||
| "viewerLink": response.viewerLink as Any, | ||
| "thumbnailLink": response.thumbnailLink as Any | ||
| ] | ||
| ) | ||
| completion(.success(uploadResponse)) | ||
| case .failure(let error): | ||
| completion(.failure(error)) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func upload(_ info: [UIImagePickerController.InfoKey : Any], completion: @escaping (Result<ImageUploadResponse, Error>) -> Void) -> Progress { | ||
| return uploader.upload(info) { result in | ||
| switch result { | ||
| case .success(let response): | ||
| let uploadResponse = ImageUploadResponse( | ||
| imageURL: response.imageURL, | ||
| deleteURL: nil, | ||
| metadata: [ | ||
| "directLink": response.directLink as Any, | ||
| "viewerLink": response.viewerLink as Any, | ||
| "thumbnailLink": response.thumbnailLink as Any | ||
| ] | ||
| ) | ||
| completion(.success(uploadResponse)) | ||
| case .failure(let error): | ||
| completion(.failure(error)) | ||
| } | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hardcoding this singleton access seems weird. At minimum can we pass it in?