-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebPageMetadataServiceImpl.swift
More file actions
99 lines (81 loc) · 3.17 KB
/
Copy pathWebPageMetadataServiceImpl.swift
File metadata and controls
99 lines (81 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//
// WebPageMetadataServiceImpl.swift
// DevLogInfra
//
// Created by 최윤진 on 2/9/26.
//
import Foundation
import LinkPresentation
import UIKit
import DevLogCore
import DevLogData
final class WebPageMetadataServiceImpl: WebPageMetadataService {
private let imageStore: WebPageImageStore
private let logger = Logger(category: "WebPageMetadataServiceImpl")
init(store: WebPageImageStore) {
self.imageStore = store
}
func fetchMetadata(from urlString: String) async throws -> WebPageMetadataResponse {
logger.info("Fetching metadata for URL: \(urlString)")
guard let url = URL(string: urlString) else {
logger.error("Invalid URL: \(urlString)")
throw URLError(.badURL)
}
do {
let provider = LPMetadataProvider()
provider.timeout = 10.0
let metadata = try await provider.startFetchingMetadata(for: url)
let imageURL = try await extractImageURL(from: metadata.imageProvider, url: url)
logger.info("Successfully fetched metadata for: \(metadata.title ?? "Unknown")")
return WebPageMetadataResponse(
title: metadata.title ?? "",
displayURL: (metadata.url ?? url).absoluteString,
imageURL: imageURL?.absoluteString ?? ""
)
} catch {
logger.error("Failed to fetch metadata", error: error)
throw error
}
}
func removeCachedImage(for urlString: String) async {
guard let url = URL(string: urlString) else {
logger.error("Invalid URL for cached image removal: \(urlString)")
return
}
do {
let removed = try await imageStore.removeImage(for: url)
if removed {
logger.info("Removed cached image for URL: \(urlString)")
}
} catch {
logger.error("Failed to remove cached image", error: error)
}
}
func cachedImageURL(for urlString: String) async throws -> URL {
guard let url = URL(string: urlString) else {
throw URLError(.badURL)
}
return try await imageStore.cachedImageURL(for: url)
}
private func extractImageURL(from imageProvider: NSItemProvider?, url: URL) async throws -> URL? {
guard let imageProvider else { return nil }
guard let data = try await imageData(from: imageProvider) else { return nil }
return try await imageStore.saveImage(data, for: url)
}
private func imageData(from imageProvider: NSItemProvider) async throws -> Data? {
return try await withCheckedThrowingContinuation { continuation in
imageProvider.loadObject(ofClass: UIImage.self) { image, error in
if let error {
continuation.resume(throwing: error)
return
}
guard let image = image as? UIImage,
let data = image.jpegData(compressionQuality: 1.0) else {
continuation.resume(returning: nil)
return
}
continuation.resume(returning: data)
}
}
}
}