Skip to content

Commit

Permalink
Fix Ambiguous File paths (#212)
Browse files Browse the repository at this point in the history
* change to relative path for file response

* fixed file handling

* Update Sources/SmileID/Classes/BiometricKYC/OrchestratedBiometricKycViewModel.swift

Co-authored-by: JNdhlovu <[email protected]>

* Update Sources/SmileID/Classes/BiometricKYC/OrchestratedBiometricKycViewModel.swift

Co-authored-by: JNdhlovu <[email protected]>

* Update OrchestratedDocumentVerificationViewModel.swift

* Update OrchestratedBiometricKycViewModel.swift

* removed trailing whitespaces

* remove force unwrapping

---------

Co-authored-by: JNdhlovu <[email protected]>
  • Loading branch information
jumaallan and JNdhlovu authored Aug 19, 2024
1 parent 00cd511 commit 473f067
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ internal class OrchestratedBiometricKycViewModel: ObservableObject {

// MARK: - Other Properties

internal var selfieFile: URL?
internal var livenessFiles: [URL]?
private var error: Error?
private var selfieCaptureResultStore: SelfieCaptureResultStore?
private var didSubmitBiometricJob: Bool = false

// MARK: - UI Properties
Expand All @@ -45,18 +46,21 @@ internal class OrchestratedBiometricKycViewModel: ObservableObject {
}

func onRetry() {
if selfieCaptureResultStore == nil {
DispatchQueue.main.async { self.step = .selfie }
if let selfieFile {
submitJob()
} else {
submitJob(selfieCaptureResultStore: selfieCaptureResultStore!)
DispatchQueue.main.async { self.step = .selfie }
}
}

func onFinished(delegate: BiometricKycResultDelegate) {
if let selfieCaptureResultStore {
if let selfieFile = selfieFile,
let livenessFiles = livenessFiles,
let selfiePath = getRelativePath(from: selfieFile)
{
delegate.didSucceed(
selfieImage: selfieCaptureResultStore.selfie,
livenessImages: selfieCaptureResultStore.livenessImages,
selfieImage: selfiePath,
livenessImages: livenessFiles.compactMap { getRelativePath(from: $0) },
didSubmitBiometricJob: didSubmitBiometricJob
)
} else if let error {
Expand All @@ -66,21 +70,39 @@ internal class OrchestratedBiometricKycViewModel: ObservableObject {
}
}

func submitJob(selfieCaptureResultStore: SelfieCaptureResultStore) {
func submitJob() {
DispatchQueue.main.async { self.step = .processing(.inProgress) }
Task {
do {
let livenessImages = selfieCaptureResultStore.livenessImages
let selfieImage = selfieCaptureResultStore.selfie
selfieFile = try LocalStorage.getFileByType(
jobId: jobId,
fileType: FileType.selfie
)

livenessFiles = try LocalStorage.getFilesByType(
jobId: jobId,
fileType: FileType.liveness
)

guard let selfieFile else {
// Set step to .selfieCapture so that the Retry button goes back to this step
DispatchQueue.main.async { self.step = .selfie }
error = SmileIDError.unknown("Error capturing selfie")
return
}

var allFiles = [URL]()
let infoJson = try LocalStorage.createInfoJsonFile(
jobId: jobId,
idInfo: idInfo.copy(entered: true),
selfie: selfieImage,
livenessImages: livenessImages
)
let zipData = try LocalStorage.zipFiles(
at: livenessImages + [selfieImage] + [infoJson]
selfie: selfieFile,
livenessImages: livenessFiles
)
allFiles.append(contentsOf: [selfieFile, infoJson])
if let livenessFiles {
allFiles.append(contentsOf: livenessFiles)
}
let zipData = try LocalStorage.zipFiles(at: allFiles)
let authRequest = AuthenticationRequest(
jobType: .biometricKyc,
enrollment: false,
Expand Down Expand Up @@ -128,18 +150,6 @@ internal class OrchestratedBiometricKycViewModel: ObservableObject {
didSubmitBiometricJob = true
do {
try LocalStorage.moveToSubmittedJobs(jobId: self.jobId)
self.selfieCaptureResultStore = SelfieCaptureResultStore(
selfie: try LocalStorage.getFileByType(
jobId: jobId,
fileType: FileType.selfie,
submitted: true
) ?? selfieCaptureResultStore.selfie,
livenessImages: try LocalStorage.getFilesByType(
jobId: jobId,
fileType: FileType.liveness,
submitted: true
) ?? selfieCaptureResultStore.livenessImages
)
} catch {
print("Error moving job to submitted directory: \(error)")
self.error = error
Expand All @@ -149,24 +159,10 @@ internal class OrchestratedBiometricKycViewModel: ObservableObject {
DispatchQueue.main.async { self.step = .processing(.success) }
} catch let error as SmileIDError {
do {
let didMove = try LocalStorage.handleOfflineJobFailure(
_ = try LocalStorage.handleOfflineJobFailure(
jobId: self.jobId,
error: error
)
if didMove {
self.selfieCaptureResultStore = SelfieCaptureResultStore(
selfie: try LocalStorage.getFileByType(
jobId: jobId,
fileType: FileType.selfie,
submitted: true
) ?? selfieCaptureResultStore.selfie,
livenessImages: try LocalStorage.getFilesByType(
jobId: jobId,
fileType: FileType.liveness,
submitted: true
) ?? selfieCaptureResultStore.livenessImages
)
}
} catch {
print("Error moving job to submitted directory: \(error)")
self.error = error
Expand Down Expand Up @@ -199,24 +195,15 @@ internal class OrchestratedBiometricKycViewModel: ObservableObject {

extension OrchestratedBiometricKycViewModel: SmartSelfieResultDelegate {
func didSucceed(
selfieImage: URL,
livenessImages: [URL],
selfieImage _: URL,
livenessImages _: [URL],
apiResponse _: SmartSelfieResponse?
) {
selfieCaptureResultStore = SelfieCaptureResultStore(
selfie: selfieImage,
livenessImages: livenessImages
)
if let selfieCaptureResultStore {
submitJob(selfieCaptureResultStore: selfieCaptureResultStore)
} else {
error = SmileIDError.unknown("Failed to save selfie capture result")
DispatchQueue.main.async { self.step = .processing(.error) }
}
submitJob()
}

func didError(error _: Error) {
error = SmileIDError.unknown("Failed to capture selfie")
error = SmileIDError.unknown("Error capturing selfie")
DispatchQueue.main.async { self.step = .processing(.error) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,24 +107,36 @@ internal class IOrchestratedDocumentVerificationViewModel<T, U: JobResult>: Obse
}

func submitJob() {
guard let documentFrontFile else {
// Set step to .frontDocumentCapture so that the Retry button goes back to this step
step = .frontDocumentCapture
onError(error: SmileIDError.unknown("Error getting document front file"))
return
}
guard let selfieFile else {
// Set step to .selfieCapture so that the Retry button goes back to this step
step = .selfieCapture
onError(error: SmileIDError.unknown("Error getting selfie file"))
return
}
DispatchQueue.main.async {
self.step = .processing(.inProgress)
}
Task {
let zip: Data
do {
guard let documentFrontFile else {
// Set step to .frontDocumentCapture so that the Retry button goes back to this step
step = .frontDocumentCapture
onError(error: SmileIDError.unknown("Error getting document front file"))
return
}

selfieFile = try LocalStorage.getFileByType(
jobId: jobId,
fileType: FileType.selfie
)

livenessFiles = try LocalStorage.getFilesByType(
jobId: jobId,
fileType: FileType.liveness
)

guard let selfieFile else {
// Set step to .selfieCapture so that the Retry button goes back to this step
step = .selfieCapture
onError(error: SmileIDError.unknown("Error getting selfie file"))
return
}

DispatchQueue.main.async {
self.step = .processing(.inProgress)
}

var allFiles = [URL]()
let frontDocumentUrl = try LocalStorage.createDocumentFile(
jobId: jobId,
Expand Down Expand Up @@ -286,9 +298,11 @@ internal class IOrchestratedDocumentVerificationViewModel<T, U: JobResult>: Obse
}

extension IOrchestratedDocumentVerificationViewModel: SmartSelfieResultDelegate {
func didSucceed(selfieImage: URL, livenessImages: [URL], apiResponse _: SmartSelfieResponse?) {
selfieFile = selfieImage
livenessFiles = livenessImages
func didSucceed(
selfieImage _: URL,
livenessImages _: [URL],
apiResponse _: SmartSelfieResponse?
) {
submitJob()
}

Expand All @@ -302,11 +316,15 @@ internal class OrchestratedDocumentVerificationViewModel:
IOrchestratedDocumentVerificationViewModel<DocumentVerificationResultDelegate, DocumentVerificationJobResult>
{
override func onFinished(delegate: DocumentVerificationResultDelegate) {
if let savedFiles {
if let savedFiles,
let selfiePath = getRelativePath(from: selfieFile),
let documentFrontPath = getRelativePath(from: savedFiles.documentFront),
let documentBackPath = getRelativePath(from: savedFiles.documentBack)
{
delegate.didSucceed(
selfie: savedFiles.selfie,
documentFrontImage: savedFiles.documentFront,
documentBackImage: savedFiles.documentBack,
selfie: selfiePath,
documentFrontImage: documentFrontPath,
documentBackImage: documentBackPath,
didSubmitDocumentVerificationJob: didSubmitJob
)
} else if let error {
Expand All @@ -325,11 +343,15 @@ internal class OrchestratedEnhancedDocumentVerificationViewModel:
IOrchestratedDocumentVerificationViewModel<EnhancedDocumentVerificationResultDelegate, EnhancedDocumentVerificationJobResult>
{
override func onFinished(delegate: EnhancedDocumentVerificationResultDelegate) {
if let savedFiles {
if let savedFiles,
let selfiePath = getRelativePath(from: selfieFile),
let documentFrontPath = getRelativePath(from: savedFiles.documentFront),
let documentBackPath = getRelativePath(from: savedFiles.documentBack)
{
delegate.didSucceed(
selfie: savedFiles.selfie,
documentFrontImage: savedFiles.documentFront,
documentBackImage: savedFiles.documentBack,
selfie: selfiePath,
documentFrontImage: documentFrontPath,
documentBackImage: documentBackPath,
didSubmitEnhancedDocVJob: didSubmitJob
)
} else if let error {
Expand Down
16 changes: 16 additions & 0 deletions Sources/SmileID/Classes/Util.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,19 @@ func getErrorSubtitle(errorMessageRes: String?, errorMessage: String?) -> String
return SmileIDResourcesHelper.localizedString(for: "Confirmation.FailureReason")
}
}

func getRelativePath(from absoluteURL: URL?) -> URL? {
guard let absoluteURL = absoluteURL else {
return nil
}

let relativeComponents = absoluteURL.pathComponents
.drop(while: { $0 != "SmileID" })
.dropFirst()

if relativeComponents.isEmpty {
return absoluteURL
} else {
return URL(string: relativeComponents.joined(separator: "/"))
}
}

0 comments on commit 473f067

Please sign in to comment.