Skip to content

Commit

Permalink
Add oggerStore.ThumbnailOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
kean committed Sep 7, 2024
1 parent 6929bb6 commit f0a6180
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
10 changes: 10 additions & 0 deletions Sources/Pulse/LoggerStore/LoggerStore+Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ extension LoggerStore {
/// If `true`, the images added to the store as saved as small thumbnails.
public var isStoringOnlyImageThumbnails = true

/// Defines how to generate the thumbnails for the images from network responses.
public var imageThumbnailOptions = ThumbnailOptions()

/// Limit the maximum response size stored by the logger. The default
/// value is `8 MB`. The same limit applies to requests.
public var responseBodySizeLimit: Int = 8 * 1048576
Expand Down Expand Up @@ -104,4 +107,11 @@ extension LoggerStore {
self.sizeLimit = sizeLimit
}
}

/// The configuration options for storing thumbnails for network responses
/// containing media.
public struct ThumbnailOptions {
public var maximumPixelSize: CGFloat = 512
public var compressionQuality: CGFloat = 0.5
}
}
7 changes: 4 additions & 3 deletions Sources/Pulse/LoggerStore/LoggerStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -491,14 +491,15 @@ extension LoggerStore {
}

private func preprocessData(_ data: Data, contentType: NetworkLogger.ContentType?) -> Data {
guard data.count > 20_000 else { // 20 KB is ok
guard data.count > 15_000 else { // 15 KB is ok
return data
}
guard configuration.isStoringOnlyImageThumbnails && (contentType?.isImage ?? false) else {
return data
}
guard let thumbnail = Graphics.makeThumbnail(from: data, targetSize: 512),
let data = Graphics.encode(thumbnail, compressionQuality: 0.7) else {
let options = configuration.imageThumbnailOptions
guard let thumbnail = Graphics.makeThumbnail(from: data, targetSize: options.maximumPixelSize),
let data = Graphics.encode(thumbnail, compressionQuality: options.compressionQuality) else {
return data
}
return data
Expand Down

0 comments on commit f0a6180

Please sign in to comment.