-
Notifications
You must be signed in to change notification settings - Fork 119
[MBL-19512][S] OfflineMode - Bypass failures on files and images download during course sync #3810
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
Closed
suhaibabsi-inst
wants to merge
3
commits into
chore/MBL-19602-API-RateLimitExceeded-Retrial-Logic-Revised
from
bugfix/MBL-19512-Course-Sync-Issue-CanineBehaviorCollege
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
72faca2
Bypass failures on files and images download during course sync
suhaibabsi-inst bd1a5ff
Merge branch 'chore/MBL-19602-API-RateLimitExceeded-Retrial-Logic-Rev…
suhaibabsi-inst 459fc2b
Merge branch 'chore/MBL-19602-API-RateLimitExceeded-Retrial-Logic-Rev…
suhaibabsi-inst 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -65,7 +65,7 @@ public class HTMLParserLive: HTMLParser { | |
| let fileURLs = Array(Set(findRegexMatches(content, pattern: fileRegex))) | ||
| let relativeURLs = findRegexMatches(content, pattern: relativeURLRegex, groupCount: 2).filter { url in url.host == nil } | ||
| let rootURL = getRootURL(courseId: courseId, resourceId: resourceId) | ||
| let fileParser: AnyPublisher<[(URL, String)], Error> = fileURLs.publisher // Download the files to local Documents folder, return the (original link - local link) tuple | ||
| let fileParser: AnyPublisher<[(URL, String)], Never> = fileURLs.publisher // Download the files to local Documents folder, return the (original link - local link) tuple | ||
| .flatMap(maxPublishers: .max(5)) { [envResolver] url in // Replace File Links with valid access urls | ||
| if url.pathComponents.contains("files") && !url.containsQueryItem(named: "verifier") { | ||
| let fileId = url.pathComponents[(url.pathComponents.firstIndex(of: "files") ?? 0) + 1] | ||
|
|
@@ -78,27 +78,30 @@ public class HTMLParserLive: HTMLParser { | |
| .map { files in | ||
| (files.first?.url ?? url, url) | ||
| } | ||
| .replaceError(with: (url, url)) | ||
| .eraseToAnyPublisher() | ||
| } else if url.pathComponents.contains("files") { | ||
| if !url.pathComponents.contains("download") { | ||
| return Just((url.appendingPathComponent("download"), url)).setFailureType(to: Error.self).eraseToAnyPublisher() | ||
| return Just((url.appendingPathComponent("download"), url)).eraseToAnyPublisher() | ||
| } else { | ||
| return Just((url, url)).setFailureType(to: Error.self).eraseToAnyPublisher() | ||
| return Just((url, url)).eraseToAnyPublisher() | ||
| } | ||
| } else { | ||
| return Just((url, url)).setFailureType(to: Error.self).eraseToAnyPublisher() | ||
| return Just((url, url)).eraseToAnyPublisher() | ||
| } | ||
| } | ||
| .flatMap { [interactor] (fileURL, originalURL) in | ||
| return interactor.downloadFile(fileURL, courseId: courseId, resourceId: resourceId) | ||
| .map { | ||
| return (originalURL, $0) | ||
| .map { urlPath -> (URL, String)? in | ||
| return (originalURL, urlPath) | ||
| } | ||
| .replaceError(with: nil) | ||
| } | ||
| .compactMap({ $0 }) | ||
| .collect() | ||
| .eraseToAnyPublisher() | ||
|
|
||
| let imageParser: AnyPublisher<[(URL, String)], Error> = imageURLs.publisher | ||
| let imageParser: AnyPublisher<[(URL, String)], Never> = imageURLs.publisher | ||
| .flatMap(maxPublishers: .max(5)) { [envResolver] url in // Replace File Links with valid access urls | ||
| if url.pathComponents.contains("files") && !url.containsQueryItem(named: "verifier") { | ||
| let fileId = url.pathComponents[(url.pathComponents.firstIndex(of: "files") ?? 0) + 1] | ||
|
|
@@ -111,9 +114,10 @@ public class HTMLParserLive: HTMLParser { | |
| .map { files in | ||
| (files.first?.url ?? url, url) | ||
| } | ||
| .replaceError(with: (url, url)) | ||
| .eraseToAnyPublisher() | ||
| } else { | ||
| return Just((url, url)).setFailureType(to: Error.self).eraseToAnyPublisher() | ||
| return Just((url, url)).eraseToAnyPublisher() | ||
| } | ||
| } | ||
| .flatMap { [interactor] (fileURL, originalURL) in // Download images to local Documents folder, return the (original link - local link) tuple | ||
|
|
@@ -123,45 +127,43 @@ public class HTMLParserLive: HTMLParser { | |
| resourceId: resourceId, | ||
| documentsDirectory: URL.Directories.documents | ||
| ) | ||
| .map { | ||
| return (originalURL, $0) | ||
| } | ||
|
|
||
| .map { url -> (URL, String)? in | ||
| return (originalURL, url) | ||
| } | ||
| .replaceError(with: nil) | ||
| } | ||
| .compactMap({ $0 }) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. .compactMap { $0 } |
||
| .collect() // Wait for all image download to finish and handle as an array | ||
| .eraseToAnyPublisher() | ||
|
|
||
| return Publishers.Zip( | ||
| fileParser, | ||
| imageParser | ||
| ) | ||
| .map { (fileURLs, imageURLs) in | ||
| return fileURLs + imageURLs | ||
| } | ||
| .map { [content] urls in | ||
| // Replace relative path links with baseURL based absolute links. This is | ||
| // to normalize all url's for the next step that works with absolute URLs. | ||
| var newContent = content | ||
| relativeURLs.forEach { relativeURL in | ||
| if let baseURL { | ||
| let newURL = baseURL.appendingPathComponent(relativeURL.path) | ||
| newContent = newContent.replacingOccurrences(of: relativeURL.absoluteString, with: newURL.absoluteString) | ||
| return Publishers.Zip(fileParser, imageParser) | ||
| .map { (fileURLs, imageURLs) in | ||
| return fileURLs + imageURLs | ||
| } | ||
| .map { [content] urls in | ||
| // Replace relative path links with baseURL based absolute links. This is | ||
| // to normalize all url's for the next step that works with absolute URLs. | ||
| var newContent = content | ||
| relativeURLs.forEach { relativeURL in | ||
| if let baseURL { | ||
| let newURL = baseURL.appendingPathComponent(relativeURL.path) | ||
| newContent = newContent.replacingOccurrences(of: relativeURL.absoluteString, with: newURL.absoluteString) | ||
| } | ||
| } | ||
| return (newContent, urls) | ||
| } | ||
| return (newContent, urls) | ||
| } | ||
| .map { (content: String, urls: [(URL, String)]) in | ||
| // Replace all original links with the local ones, return the replaced string content | ||
| var newContent = content | ||
| urls.forEach { (originalURL, offlineURL) in | ||
| newContent = newContent.replacingOccurrences(of: originalURL.absoluteString, with: offlineURL) | ||
| .map { (content: String, urls: [(URL, String)]) in | ||
| // Replace all original links with the local ones, return the replaced string content | ||
| var newContent = content | ||
| urls.forEach { (originalURL, offlineURL) in | ||
| newContent = newContent.replacingOccurrences(of: originalURL.absoluteString, with: offlineURL) | ||
| } | ||
| return newContent | ||
| } | ||
| return newContent | ||
| } | ||
| .flatMap { [interactor, rootURL] content in // Save html parsed html string content to file. It will be loaded in offline mode) | ||
| return interactor.saveBaseContent(content: content, folderURL: rootURL) | ||
| } | ||
| .eraseToAnyPublisher() | ||
| .flatMap { [interactor, rootURL] content in // Save html parsed html string content to file. It will be loaded in offline mode) | ||
| return interactor.saveBaseContent(content: content, folderURL: rootURL) | ||
| } | ||
| .eraseToAnyPublisher() | ||
| } | ||
|
|
||
| private func findRegexMatches(_ content: String, pattern: NSRegularExpression, groupCount: Int = 1) -> [URL] { | ||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.compactMap { $0 }
For consistency, if there's no specific reason for the parenthesis