mirror of https://github.com/oxen-io/session-ios
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
61 lines
2.0 KiB
Swift
61 lines
2.0 KiB
Swift
2 years ago
|
// Copyright © 2022 Rangeproof Pty Ltd. All rights reserved.
|
||
|
|
||
|
import Foundation
|
||
|
import GRDB
|
||
|
import PromiseKit
|
||
|
import SignalCoreKit
|
||
|
import SessionUtilitiesKit
|
||
|
|
||
|
public enum AttachmentUploadJob: JobExecutor {
|
||
|
public static var maxFailureCount: Int = 10
|
||
|
public static var requiresThreadId: Bool = true
|
||
|
public static let requiresInteractionId: Bool = true
|
||
|
public static func run(
|
||
|
_ job: Job,
|
||
|
success: @escaping (Job, Bool) -> (),
|
||
|
failure: @escaping (Job, Error?, Bool) -> (),
|
||
|
deferred: @escaping (Job) -> ()
|
||
|
) {
|
||
|
guard
|
||
|
let threadId: String = job.threadId,
|
||
|
let detailsData: Data = job.details,
|
||
|
let details: Details = try? JSONDecoder().decode(Details.self, from: detailsData),
|
||
|
let (attachment, openGroup): (Attachment, OpenGroup?) = GRDBStorage.shared.read({ db in
|
||
|
guard let attachment: Attachment = try Attachment.fetchOne(db, id: details.attachmentId) else {
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
return (attachment, try OpenGroup.fetchOne(db, id: threadId))
|
||
|
})
|
||
|
else {
|
||
|
failure(job, JobRunnerError.missingRequiredDetails, false)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
attachment.upload(
|
||
|
using: { data in
|
||
|
if let openGroup: OpenGroup = openGroup {
|
||
|
return OpenGroupAPIV2.upload(data, to: openGroup.room, on: openGroup.server)
|
||
|
}
|
||
|
|
||
|
return FileServerAPIV2.upload(data)
|
||
|
},
|
||
|
encrypt: (openGroup == nil),
|
||
|
success: { success(job, false) },
|
||
|
failure: { error in failure(job, error, false) }
|
||
|
)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// MARK: - AttachmentUploadJob.Details
|
||
|
|
||
|
extension AttachmentUploadJob {
|
||
|
public struct Details: Codable {
|
||
|
public let attachmentId: String
|
||
|
|
||
|
public init(attachmentId: String) {
|
||
|
self.attachmentId = attachmentId
|
||
|
}
|
||
|
}
|
||
|
}
|