Skip to content

Commit

Permalink
Release version 4.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Team Mobile Schorsch committed Jul 24, 2024
1 parent f5a7ceb commit 976d935
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 9 deletions.
2 changes: 2 additions & 0 deletions .jazzy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ xcodebuild_arguments:
- GiniMobile.xcworkspace
- "-scheme"
- GiniHealthAPILibrary
- "-destination"
- platform=iOS Simulator,OS=17.2,name=iPhone 14
author: Gini GmbH
author_url: https://gini.net
module: GiniHealthAPILibrary
Expand Down
4 changes: 2 additions & 2 deletions Documentation/source/Installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ Once you have your Swift package set up, adding `GiniHealthAPILibrary` as a depe

```swift
dependencies: [
.package(url: "https://github.com/gini/health-api-library-ios.git", .exact("4.1.0"))
.package(url: "https://github.com/gini/health-api-library-ios.git", .exact("4.2.0"))
]
```

In case that you want to use the certificate pinning in the library, add `GiniHealthAPILibraryPinning`:
```swift
dependencies: [
.package(url: "https://github.com/gini/health-api-library-pinning-ios.git", .exact("4.1.0"))
.package(url: "https://github.com/gini/health-api-library-pinning-ios.git", .exact("4.2.0"))
]
```

Expand Down
14 changes: 12 additions & 2 deletions Sources/GiniHealthAPILibrary/Documents/Core/Extensions/Data.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
// Data.swift
// GiniHealthAPI
//
// Created by Enrique del Pozo Gómez on 3/21/19.
// Copyright © 2024 Gini GmbH. All rights reserved.
//

import Foundation
import UIKit

extension Data {
private static let mimeTypeSignatures: [UInt8: String] = [
Expand Down Expand Up @@ -44,4 +44,14 @@ extension Data {
return nil
}
}

func isImage() -> Bool {
return UIImage(data: self) != nil
}

func isImageSizeBiggerThan(maximumSizeInMB: Double) -> Bool {
let sizeInBytes = self.count
let sizeInMB = Double(sizeInBytes) / (1024.0 * 1024.0)
return sizeInMB > maximumSizeInMB
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//

import Foundation
import UIKit

typealias DefaultDocumentServiceProtocol = DocumentService & V2DocumentService

Expand Down Expand Up @@ -68,11 +69,44 @@ public final class DefaultDocumentService: DefaultDocumentServiceProtocol {
apiDomain: apiDomain,
httpMethod: .post,
additionalHeaders: metadata?.headers ?? [:])
sessionManager.upload(resource: resource, data: data, completion: completionResult)
guard let processedData = processDataIfNeeded(data: data) else {
completion(.failure(.parseError(message: "Couldn't process data received.", response: nil, data: nil)))
return
}
sessionManager.upload(resource: resource, data: processedData, completion: completionResult)
}

}


func processDataIfNeeded(data: Data) -> Data? {
// Check if data received is image. Otherwise, pass back the data
guard data.isImage() else {
return data
}
// Check if image should be compressed
guard data.isImageSizeBiggerThan(maximumSizeInMB: Constants.maximumImageSizeInMB) else {
return data
}
// Compress image to needed size
guard let compressedData = compressImageToMax(imageData: data, mb: Constants.maximumImageSizeInMB) else {
return data
}
return compressedData
}

private func compressImageToMax(imageData: Data, mb: Double) -> Data? {
let maxFileSize: Double = mb * 1024 * 1024 // 10 MB in bytes
let image = UIImage(data: imageData)
guard let initialData = image?.jpegData(compressionQuality: 1.0) else { return nil }
let initialSize = Double(initialData.count)
if initialSize <= maxFileSize {
return initialData // Already below given MB
}
// Calculate the required compression quality so that image size will be under given MB
let compressionQuality: CGFloat = CGFloat(maxFileSize / initialSize)
let compressedImageData = image?.jpegData(compressionQuality: compressionQuality)
return compressedImageData
}

/**
* Deletes a document
*
Expand Down Expand Up @@ -221,3 +255,9 @@ public final class DefaultDocumentService: DefaultDocumentServiceProtocol {
file(urlString: urlString, resourceHandler: sessionManager.download, completion: completion)
}
}

extension DefaultDocumentService {
enum Constants {
static let maximumImageSizeInMB = 10.0
}
}
21 changes: 20 additions & 1 deletion Sources/GiniHealthAPILibrary/Documents/ExtractionResult.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,25 @@

import Foundation

/**
Payment State types from payment state from extraction result
*/
public enum PaymentState: String {
case payable = "Payable"
case other = "Other"
}
/**
Extraction Types for extraction result
*/
public enum ExtractionType: String {
case paymentState = "payment_state"
case paymentDueDate = "payment_due_date"
case amountToPay = "amount_to_pay"
case paymentRecipient = "payment_recipient"
case iban = "iban"
case paymentPurpose = "payment_purpose"
}

/**
* Data model for a document extraction result.
*/
Expand All @@ -20,7 +39,7 @@ import Foundation

/// The line item compound extractions.
public var lineItems: [[Extraction]]?

public init(extractions: [Extraction], payment: [[Extraction]]?, lineItems: [[Extraction]]?) {
self.extractions = extractions
self.payment = payment
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
// Copyright © 2024 Gini GmbH. All rights reserved.
//

public let GiniHealthAPILibraryVersion = "4.1.0"
public let GiniHealthAPILibraryVersion = "4.2.0"
19 changes: 19 additions & 0 deletions Tests/GiniHealthAPILibraryTests/DocumentServiceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,25 @@ final class DocumentServicesTests: XCTestCase {
wait(for: [expect], timeout: 1)
}

func testPartialDocumentCreationWithImageCompression() {
// Should check size of a big image
let range = 6635000...6636000 // We need this range because on different machines, the compression is a bit bigger or smaller

guard let imageData12MB = UIImage(named: "invoice-12MB", in: Bundle.module, compatibleWith: nil)?.pngData() else { return }
let imageDataProcessed = defaultDocumentService.processDataIfNeeded(data: imageData12MB)

XCTAssertTrue(range.contains(imageDataProcessed?.count ?? 0))
}

func testDocumentCreationWithBigPDF() {
// Should check size of a big PDF
let pdfData13MB = loadFile(withName: "invoice-13MB", ofType: "pdf")
let expectedSize = pdfData13MB.count
let imageDataProcessed = defaultDocumentService.processDataIfNeeded(data: pdfData13MB)

XCTAssertEqual(imageDataProcessed?.count ?? 0, expectedSize)
}

func testCompositeDocumentCreation() {
let expect = expectation(description: "it returns a composite document")

Expand Down
Binary file not shown.

0 comments on commit 976d935

Please sign in to comment.