-
Notifications
You must be signed in to change notification settings - Fork 28
Unify slim/UID-flags fetches via FetchMessageInfoOptions #167
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
Sources/SwiftMail/IMAP/IMAPServer+BodyStructureHelpers.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| import Foundation | ||
| @preconcurrency import NIOIMAP | ||
| import NIOIMAPCore | ||
|
|
||
| // MARK: - Body Structure Helpers | ||
|
|
||
| extension IMAPServer { | ||
| /** | ||
| Process a body structure recursively to fetch all parts | ||
| - Parameters: | ||
| - structure: The body structure to process | ||
| - section: The section to process | ||
| - identifier: The message identifier (SequenceNumber or UID) | ||
| - Returns: An array of message parts | ||
| - Throws: An error if the fetch operation fails | ||
| */ | ||
| func recursivelyFetchParts<T: MessageIdentifier>( | ||
| _ structure: BodyStructure, | ||
| section: Section, | ||
| identifier: T | ||
| ) async throws -> [MessagePart] { | ||
| switch structure { | ||
| case .singlepart(let part): | ||
| return [try await fetchSinglepart(part, section: section, identifier: identifier)] | ||
|
|
||
| case .multipart(let multipart): | ||
| return try await fetchMultipart(multipart, section: section, identifier: identifier) | ||
| } | ||
| } | ||
|
|
||
| /// Fetch and convert a singlepart body structure. | ||
| private func fetchSinglepart<T: MessageIdentifier>( | ||
| _ part: BodyStructure.Singlepart, | ||
| section: Section, | ||
| identifier: T | ||
| ) async throws -> MessagePart { | ||
| // Fetch the part content | ||
| let partData = try await fetchPart(section: section, of: identifier) | ||
|
|
||
| let contentType = singlepartContentType(part) | ||
| let (disposition, filename) = singlepartDispositionAndFilename(part) | ||
| let encoding: String? = part.fields.encoding?.debugDescription | ||
| let contentId = part.fields.id | ||
|
|
||
| return MessagePart( | ||
| section: section, | ||
| contentType: contentType, | ||
| disposition: disposition, | ||
| encoding: encoding, | ||
| filename: filename, | ||
| contentId: contentId, | ||
| data: partData | ||
| ) | ||
| } | ||
|
|
||
| /// Recursively fetch each part of a multipart body structure. | ||
| private func fetchMultipart<T: MessageIdentifier>( | ||
| _ multipart: BodyStructure.Multipart, | ||
| section: Section, | ||
| identifier: T | ||
| ) async throws -> [MessagePart] { | ||
| var allParts: [MessagePart] = [] | ||
|
|
||
| for (index, childPart) in multipart.parts.enumerated() { | ||
| // Create a new section by appending the current index + 1 | ||
| let childSection = Section(section.components + [index + 1]) | ||
| let childParts = try await recursivelyFetchParts( | ||
| childPart, section: childSection, identifier: identifier | ||
| ) | ||
| allParts.append(contentsOf: childParts) | ||
| } | ||
|
|
||
| return allParts | ||
| } | ||
|
|
||
| /// Build the `Content-Type` string for a singlepart structure. | ||
| private func singlepartContentType(_ part: BodyStructure.Singlepart) -> String { | ||
| var contentType: String | ||
| switch part.kind { | ||
| case .basic(let mediaType): | ||
| contentType = "\(String(mediaType.topLevel))/\(String(mediaType.sub))" | ||
| case .text(let text): | ||
| contentType = "text/\(String(text.mediaSubtype))" | ||
| case .message(let message): | ||
| contentType = "message/\(String(message.message))" | ||
| } | ||
|
|
||
| if let charset = part.fields.parameters.first(where: { $0.key.lowercased() == "charset" })?.value { | ||
| contentType += "; charset=\(charset)" | ||
| } | ||
|
|
||
| return contentType | ||
| } | ||
|
|
||
| /// Extract disposition + filename from a singlepart structure's extension data. | ||
| private func singlepartDispositionAndFilename( | ||
| _ part: BodyStructure.Singlepart | ||
| ) -> (disposition: String?, filename: String?) { | ||
| var disposition: String? | ||
| var filename: String? | ||
|
|
||
| if let ext = part.extension, let dispAndLang = ext.dispositionAndLanguage, let disp = dispAndLang.disposition { | ||
| disposition = String(describing: disp) | ||
|
|
||
| for (key, value) in disp.parameters where key.lowercased() == "filename" { | ||
| filename = value | ||
| } | ||
| } | ||
|
|
||
| return (disposition, filename) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.