Some checks failed
Build & Test / Analyze (javascript, affine) (push) Failing after 4s
Build & Test / Analyze (typescript, affine) (push) Failing after 3s
Build & Test / Rust test filter (push) Failing after 4s
Build & Test / Check yarn binary (push) Failing after 3s
Build & Test / E2E BlockSuite Test (3) (push) Failing after 3s
Build & Test / Analyze (javascript, blocksuite) (push) Failing after 3s
Build & Test / Analyze (typescript, blocksuite) (push) Failing after 3s
Build & Test / Mobile native build filter (push) Failing after 3s
Build & Test / E2E BlockSuite Test (1) (push) Failing after 4s
Build & Test / E2E BlockSuite Test (2) (push) Failing after 4s
Build & Test / E2E Mobile Test (1) (push) Failing after 3s
Build & Test / Unit Test (1) (push) Has been skipped
Build & Test / Unit Test (2) (push) Has been skipped
Build & Test / Unit Test (3) (push) Has been skipped
Build & Test / Unit Test (4) (push) Has been skipped
Build & Test / Unit Test (5) (push) Has been skipped
Build & Test / Native Unit Test (push) Has been skipped
Build & Test / E2E BlockSuite Test (4) (push) Failing after 3s
Build & Test / E2E BlockSuite Test (5) (push) Failing after 3s
Build & Test / E2E BlockSuite Cross Browser Test (push) Failing after 3s
Build & Test / E2E Mobile Test (2) (push) Failing after 3s
Build & Test / Build AFFiNE native (x86_64-unknown-linux-gnu) (push) Failing after 3s
Build & Test / Build Server native (push) Failing after 3s
Build & Test / Check Git Status (push) Has been skipped
Build & Test / Server Test (0, 4) (push) Has been skipped
Build & Test / Server Test (1, 4) (push) Has been skipped
Build & Test / Server Test (2, 4) (push) Has been skipped
Build & Test / Server Test (3, 4) (push) Has been skipped
Build & Test / Server Test with Elasticsearch (push) Has been skipped
Build & Test / Build @affine/electron renderer (push) Failing after 3s
Build & Test / Copilot test filter (push) Failing after 4s
Build & Test / Lint Rust (push) Has been skipped
Build & Test / Server Test with Embedded Search (push) Has been skipped
Build & Test / Server Test with Manticore Search (push) Has been skipped
Build & Test / Server E2E Test (push) Has been skipped
Build & Test / Cloud Desktop E2E Test (push) Has been skipped
Build & Test / Cloud E2E Test 1/2 (push) Has been skipped
Build & Test / Cloud E2E Test 2/2 (push) Has been skipped
Build & Test / Build Android app (push) Has been skipped
Build & Test / Test Android app (push) Has been skipped
Build & Test / Run native tests (push) Has been skipped
Build & Test / Server Copilot Api Test (push) Has been skipped
Build & Test / Frontend Copilot E2E Test (1, 5) (push) Has been skipped
Build & Test / Frontend Copilot E2E Test (2, 5) (push) Has been skipped
Build & Test / Frontend Copilot E2E Test (3, 5) (push) Has been skipped
Build & Test / Frontend Copilot E2E Test (4, 5) (push) Has been skipped
Build & Test / Frontend Copilot E2E Test (5, 5) (push) Has been skipped
Release / Canary Gate (push) Has been skipped
Build & Test / Build AFFiNE native (aarch64-apple-darwin) (push) Has been cancelled
Build & Test / Lint (push) Has been cancelled
Build & Test / Typecheck (push) Has been cancelled
Build & Test / Build AFFiNE native (x86_64-apple-darwin) (push) Has been cancelled
Build & Test / Desktop Test (ubuntu-latest, linux, x64, x86_64-unknown-linux-gnu, true) (push) Has been cancelled
Build & Test / Desktop Test (windows-latest, windows, x64, x86_64-pc-windows-msvc, true) (push) Has been cancelled
Build & Test / E2E Test (1) (push) Has been cancelled
Build & Test / E2E Test (2) (push) Has been cancelled
Build & Test / E2E Test (3) (push) Has been cancelled
Build & Test / E2E Test (4) (push) Has been cancelled
Build & Test / E2E Test (5) (push) Has been cancelled
Build & Test / Build AFFiNE native (aarch64-pc-windows-msvc) (push) Has been cancelled
Build & Test / Build AFFiNE native (x86_64-pc-windows-msvc) (push) Has been cancelled
Build & Test / Desktop Test (macos-latest, macos, arm64, aarch64-apple-darwin, true) (push) Has been cancelled
Build & Test / Desktop Test (macos-latest, macos, x64, x86_64-apple-darwin, false) (push) Has been cancelled
Build & Test / 3, 2, 1 Launch (push) Has been cancelled
Build & Test / Build iOS app (push) Has been cancelled
Copilot Test Automatically / Setup Test (push) Failing after 1s
Release / Prepare (push) Failing after 2s
Release / Release Cloud (push) Has been skipped
Release / Release Desktop (push) Has been skipped
Release / Release Docker Image (push) Has been skipped
Release / Release Mobile (push) Has been skipped
243 lines
8.4 KiB
Swift
243 lines
8.4 KiB
Swift
import Foundation
|
|
import ImageIO
|
|
import UIKit
|
|
|
|
struct ShareLinkPreview: Decodable, Equatable {
|
|
struct Author: Decodable, Equatable {
|
|
var name: String
|
|
var handle: String?
|
|
var avatar: String?
|
|
}
|
|
|
|
struct Transcript: Decodable, Equatable {
|
|
struct Segment: Decodable, Equatable {
|
|
var text: String
|
|
var startSeconds: Double?
|
|
var durationSeconds: Double?
|
|
var speaker: String?
|
|
}
|
|
|
|
struct Chapter: Decodable, Equatable {
|
|
var title: String
|
|
var startSeconds: Double
|
|
}
|
|
|
|
var language: String?
|
|
var segments: [Segment]
|
|
var chapters: [Chapter]?
|
|
var truncated: Bool?
|
|
}
|
|
|
|
var url: String
|
|
var title: String?
|
|
var siteName: String?
|
|
var description: String?
|
|
var images: [String]?
|
|
var favicons: [String]?
|
|
var mediaType: String?
|
|
var provider: String?
|
|
var author: Author?
|
|
var publishedAt: String?
|
|
var durationSeconds: Double?
|
|
var transcript: Transcript?
|
|
|
|
private enum CodingKeys: String, CodingKey {
|
|
case url, title, siteName, description, images, favicons, mediaType, provider
|
|
case author, publishedAt, durationSeconds, transcript
|
|
}
|
|
|
|
init(from decoder: Decoder) throws {
|
|
let values = try decoder.container(keyedBy: CodingKeys.self)
|
|
let rawURL = try values.decode(String.self, forKey: .url)
|
|
guard let normalized = ShareInboxSafety.normalizedWebURL(rawURL) else {
|
|
throw DecodingError.dataCorruptedError(forKey: .url, in: values, debugDescription: "Invalid preview URL")
|
|
}
|
|
url = normalized
|
|
func text(_ key: CodingKeys, limit: Int) -> String? {
|
|
guard let value = try? values.decode(String.self, forKey: key),
|
|
let text = SharePayloadBuilder.nonEmpty(value) else { return nil }
|
|
return String(text.prefix(limit))
|
|
}
|
|
title = text(.title, limit: 120)
|
|
siteName = text(.siteName, limit: 80)
|
|
description = text(.description, limit: 500)
|
|
mediaType = text(.mediaType, limit: 80)
|
|
provider = text(.provider, limit: 80)
|
|
publishedAt = text(.publishedAt, limit: 80)
|
|
images = (try? values.decode([String].self, forKey: .images))?
|
|
.compactMap(ShareInboxSafety.normalizedWebURL).prefix(1).map { $0 }
|
|
favicons = (try? values.decode([String].self, forKey: .favicons))?
|
|
.compactMap(ShareInboxSafety.normalizedWebURL).prefix(1).map { $0 }
|
|
author = try? values.decode(Author.self, forKey: .author)
|
|
if let name = author?.name { author?.name = String(name.prefix(80)) }
|
|
durationSeconds = try? values.decode(Double.self, forKey: .durationSeconds)
|
|
if let duration = durationSeconds, !duration.isFinite || duration < 0 { durationSeconds = nil }
|
|
if let source = try? values.decode(Transcript.self, forKey: .transcript), let excerpt = source.previewText {
|
|
transcript = Transcript(language: source.language, segments: [Transcript.Segment(text: excerpt)], chapters: nil, truncated: nil)
|
|
}
|
|
}
|
|
}
|
|
|
|
extension ShareLinkPreview.Transcript {
|
|
var previewText: String? {
|
|
var text = ""
|
|
for segment in segments {
|
|
let part = String(segment.text.prefix(241)).split(whereSeparator: \.isWhitespace).joined(separator: " ")
|
|
if part.isEmpty { continue }
|
|
if !text.isEmpty { text += " " }
|
|
text += part
|
|
if text.count > 240 { return String(text.prefix(240)) + "…" }
|
|
}
|
|
return text.isEmpty ? nil : text
|
|
}
|
|
}
|
|
|
|
enum ShareLinkPreviewState: Equatable {
|
|
case idle
|
|
case loading
|
|
case loaded(ShareLinkPreview)
|
|
case failed
|
|
}
|
|
|
|
struct ShareLinkPreviewClient {
|
|
private let session: URLSession
|
|
private let appVersion: String
|
|
|
|
init(session: URLSession? = nil, appVersion: String? = nil) {
|
|
self.appVersion = appVersion ?? Self.bundledAppVersion
|
|
if let session {
|
|
self.session = session
|
|
} else {
|
|
let configuration = URLSessionConfiguration.ephemeral
|
|
configuration.timeoutIntervalForRequest = 4
|
|
configuration.timeoutIntervalForResource = 6
|
|
configuration.requestCachePolicy = .reloadIgnoringLocalCacheData
|
|
configuration.urlCache = nil
|
|
self.session = URLSession(configuration: configuration)
|
|
}
|
|
}
|
|
|
|
func fetch(url: String) async throws -> ShareLinkPreview {
|
|
guard let normalized = ShareInboxSafety.normalizedWebURL(url) else {
|
|
throw URLError(.badURL)
|
|
}
|
|
var request = URLRequest(url: ShareInboxConstants.officialLinkPreviewURL)
|
|
request.httpMethod = "POST"
|
|
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
|
|
addClientHeaders(to: &request)
|
|
request.httpBody = try JSONEncoder().encode(
|
|
Request(url: normalized, include: ["transcript"])
|
|
)
|
|
let (data, response) = try await read(request, maxBytes: 1024 * 1024)
|
|
guard let response = response as? HTTPURLResponse, response.statusCode == 200 else {
|
|
throw URLError(.badServerResponse)
|
|
}
|
|
return try JSONDecoder().decode(ShareLinkPreview.self, from: data)
|
|
}
|
|
|
|
func fetchImage(url value: String) async throws -> UIImage {
|
|
guard let candidate = URL(
|
|
string: value,
|
|
relativeTo: ShareInboxConstants.officialLinkPreviewURL
|
|
) else {
|
|
throw URLError(.badURL)
|
|
}
|
|
let resolved = candidate.absoluteURL
|
|
guard
|
|
let normalized = ShareInboxSafety.normalizedWebURL(resolved.absoluteString),
|
|
let url = URL(string: normalized)
|
|
else {
|
|
throw URLError(.badURL)
|
|
}
|
|
guard url.scheme == "https",
|
|
url.host == ShareInboxConstants.officialLinkPreviewURL.host,
|
|
url.port == ShareInboxConstants.officialLinkPreviewURL.port,
|
|
url.path == "/api/worker/image-proxy"
|
|
else { throw URLError(.badURL) }
|
|
var request = URLRequest(
|
|
url: url,
|
|
cachePolicy: .reloadIgnoringLocalCacheData,
|
|
timeoutInterval: 3
|
|
)
|
|
addClientHeaders(to: &request)
|
|
let (data, response) = try await read(request, maxBytes: 2 * 1024 * 1024)
|
|
guard let response = response as? HTTPURLResponse, response.statusCode == 200 else {
|
|
throw URLError(.badServerResponse)
|
|
}
|
|
guard let source = CGImageSourceCreateWithData(data as CFData, nil),
|
|
let thumbnail = CGImageSourceCreateThumbnailAtIndex(source, 0, [
|
|
kCGImageSourceCreateThumbnailFromImageAlways: true,
|
|
kCGImageSourceCreateThumbnailWithTransform: true,
|
|
kCGImageSourceThumbnailMaxPixelSize: 480,
|
|
] as CFDictionary)
|
|
else {
|
|
throw URLError(.cannotDecodeContentData)
|
|
}
|
|
return UIImage(cgImage: thumbnail)
|
|
}
|
|
|
|
func fetchImageIfPresent(url: String?) async -> UIImage? {
|
|
guard let url else { return nil }
|
|
return try? await fetchImage(url: url)
|
|
}
|
|
|
|
private final class NoRedirect: NSObject, URLSessionTaskDelegate {
|
|
func urlSession(_: URLSession, task _: URLSessionTask,
|
|
willPerformHTTPRedirection _: HTTPURLResponse, newRequest _: URLRequest,
|
|
completionHandler: @escaping (URLRequest?) -> Void)
|
|
{
|
|
completionHandler(nil)
|
|
}
|
|
}
|
|
|
|
private func read(_ request: URLRequest, maxBytes: Int) async throws -> (Data, URLResponse) {
|
|
let (bytes, response) = try await session.bytes(for: request, delegate: NoRedirect())
|
|
defer { bytes.task.cancel() }
|
|
guard response.expectedContentLength <= maxBytes else { throw URLError(.dataLengthExceedsMaximum) }
|
|
var data = Data()
|
|
var buffer = [UInt8]()
|
|
let chunkSize = 16 * 1024
|
|
buffer.reserveCapacity(chunkSize)
|
|
for try await byte in bytes {
|
|
guard data.count + buffer.count < maxBytes else { throw URLError(.dataLengthExceedsMaximum) }
|
|
buffer.append(byte)
|
|
if buffer.count == chunkSize {
|
|
data.append(contentsOf: buffer)
|
|
buffer.removeAll(keepingCapacity: true)
|
|
}
|
|
}
|
|
data.append(contentsOf: buffer)
|
|
return (data, response)
|
|
}
|
|
|
|
private func addClientHeaders(to request: inout URLRequest) {
|
|
request.setValue("AFFiNE/\(appVersion)", forHTTPHeaderField: "User-Agent")
|
|
request.setValue(appVersion, forHTTPHeaderField: "x-affine-version")
|
|
}
|
|
|
|
private struct Request: Encodable {
|
|
var url: String
|
|
var include: [String]
|
|
}
|
|
|
|
private struct AppConfig: Decodable {
|
|
var affineVersion: String
|
|
}
|
|
|
|
private static var bundledAppVersion: String {
|
|
if let url = Bundle.main.url(forResource: "capacitor.config", withExtension: "json"),
|
|
let data = try? Data(contentsOf: url),
|
|
let version = try? JSONDecoder().decode(AppConfig.self, from: data).affineVersion,
|
|
!version.isEmpty
|
|
{
|
|
return version
|
|
}
|
|
if let version = Bundle.main.object(
|
|
forInfoDictionaryKey: "CFBundleShortVersionString"
|
|
) as? String, !version.isEmpty {
|
|
return version
|
|
}
|
|
return "0.2"
|
|
}
|
|
}
|