Skip to content
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

Fixes alerts when uploading big files and suggests users subscribe to nostr.build #1321

Merged
merged 30 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
b17ed98
non-functional update of layout for upload alerts
rabble Jul 20, 2024
fde083b
fixed one problem with localization strings, still stuck on calling t…
rabble Jul 23, 2024
4564fbf
fixed one problem with localization strings, still stuck on calling t…
rabble Jul 23, 2024
e66b0b7
added fix to help users with suggestion to pay for nostr.build to upl…
rabble Jul 23, 2024
f477447
Merge branch 'main' into large_file_upload_error
rabble Jul 23, 2024
c9f73fb
fixes error message when file size is too big by adding error to File…
rabble Jul 24, 2024
b74027b
fixes error message when file size is too big by adding error to File…
rabble Jul 24, 2024
877edd5
Update Nos/Assets/Localization/ImagePicker.xcstrings
rabble Jul 25, 2024
6f61dea
Update Nos/Assets/Localization/ImagePicker.xcstrings
rabble Jul 25, 2024
37d18a3
Update Nos/Assets/Localization/ImagePicker.xcstrings
rabble Jul 25, 2024
25cc94b
Update Nos/Assets/Localization/ImagePicker.xcstrings
rabble Jul 25, 2024
dc1dd5b
reverting package.resolved file
rabble Jul 25, 2024
862c384
adding changelog for file upload error screen
rabble Jul 30, 2024
b3f88fd
Update Nos/Views/New Note/ComposerActionBar.swift
rabble Jul 30, 2024
b4c64f1
Update Nos/Views/New Note/ComposerActionBar.swift
rabble Jul 30, 2024
e6cd12e
Merge branch 'main' into large_file_upload_error
mplorentz Jul 31, 2024
c74b82a
Fix swiftlint errors
mplorentz Jul 31, 2024
ac9c72f
Merge branch 'main' into large_file_upload_error
mplorentz Jul 31, 2024
e1eb233
Merge remote-tracking branch 'origin/main' into large_file_upload_error
pelumy Sep 11, 2024
19ede8b
differentiate buttons to show on alert for large files from other errors
pelumy Sep 11, 2024
781ab37
fix minor swiftlint warnings
pelumy Sep 11, 2024
cd298aa
make function body length shorter to fix swiftlint error
pelumy Sep 11, 2024
a3129c1
fix more swiftlint warnings
pelumy Sep 11, 2024
8276f18
Merge branch 'main' into large_file_upload_error
pelumy Sep 11, 2024
a24d659
Merge branch 'main' into large_file_upload_error
pelumy Sep 12, 2024
4be1b8c
check for error code 413
pelumy Sep 12, 2024
0b5eef9
Merge branch 'main' into large_file_upload_error
pelumy Sep 12, 2024
d9d357a
refactored regular expression for file size limit
joshuatbrown Sep 13, 2024
2aeab85
refactor createAlert function
pelumy Sep 13, 2024
d15cdf4
Update premium nostr url string
pelumy Sep 16, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactored regular expression for file size limit
  • Loading branch information
joshuatbrown committed Sep 13, 2024
commit d9d357ade5065c9d33d305d2ba83ed988ac854ca
38 changes: 19 additions & 19 deletions Nos/Service/FileStorage/FileStorageAPIClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,27 +80,16 @@ class NostrBuildAPIClient: FileStorageAPIClient {
let decodedResponse = try decoder.decode(FileStorageUploadResponseJSON.self, from: responseData)

guard let urlString = decodedResponse.nip94Event?.urlString else {
/// Assign an empty string if the response message is nil.
// Assign an empty string if the response message is nil.
let message = decodedResponse.message ?? ""

/// Checks if the response contains a status code of `413 Payload Too Large`.
// Checks if the response contains a status code of `413 Payload Too Large`.
if let errorCode = response?.statusCode, errorCode == 413 {
/// Verify if the error message indicates the file size exceeds the limit.
if let regex = try? NSRegularExpression(
pattern: "File size exceeds the limit of (\\d*\\.\\d* [MKGT]B)",
options: []
),
let match = regex.firstMatch(
in: message,
options: [],
range: NSRange(location: 0, length: message.utf16.count)
),
let range = Range(match.range(at: 1), in: message) {
let fileSizeLimit = String(message[range])
throw FileStorageAPIClientError.fileTooBig(fileSizeLimit)
}
// Verify if the error message indicates the file size exceeds the limit.
let fileSizeLimit = fileSizeLimit(from: message)
throw FileStorageAPIClientError.fileTooBig(fileSizeLimit)
}
/// Throw an error indicating the upload failed with the provided message.
// Throw an error indicating the upload failed with the provided message.
throw FileStorageAPIClientError.uploadFailed(message)
}

Expand All @@ -110,8 +99,6 @@ class NostrBuildAPIClient: FileStorageAPIClient {
return url
}

// MARK: - Internal

/// Fetches server info from the file storage API.
/// - Returns: the decoded JSON containing server info for the file storage API.
func fetchServerInfo() async throws -> FileStorageServerInfoResponseJSON {
Expand All @@ -127,6 +114,19 @@ class NostrBuildAPIClient: FileStorageAPIClient {
throw FileStorageAPIClientError.decodingError
}
}

/// Gets the file size limit from the error message.
/// - Parameter message: The error message from nostr.build.
/// - Returns: The file size limit from the error message.
func fileSizeLimit(from message: String) -> String? {
let pattern = /File size exceeds the limit of (\d*\.\d* [MKGT]B)/

guard let match = message.firstMatch(of: pattern) else {
return nil
}

return String(match.1)
}

/// Creates a URLRequest and Data from a file URL to be uploaded to the file storage API.
func uploadRequest(fileAt fileURL: URL, isProfilePhoto: Bool, apiURL: URL) throws -> (URLRequest, Data) {
Expand Down
36 changes: 36 additions & 0 deletions NosTests/Service/FileStorage/NostrBuildAPIClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,42 @@ class NostrBuildAPIClientTests: XCTestCase {
}
}

func test_fileSizeLimit_returns_limit() {
// Arrange
let subject = NostrBuildAPIClient()
let nostrBuildErrorMessage = "File size exceeds the limit of 25.00 MB"

// Act
let result = subject.fileSizeLimit(from: nostrBuildErrorMessage)

// Assert
XCTAssertEqual(result, "25.00 MB")
}

func test_fileSizeLimit_returns_limit_15() {
// Arrange
let subject = NostrBuildAPIClient()
let nostrBuildErrorMessage = "File size exceeds the limit of 15.00 MB"

// Act
let result = subject.fileSizeLimit(from: nostrBuildErrorMessage)

// Assert
XCTAssertEqual(result, "15.00 MB")
}

func test_fileSizeLimit_returns_nil_when_message_does_not_match() {
// Arrange
let subject = NostrBuildAPIClient()
let nostrBuildErrorMessage = "File size limit is 25.00 MB"

// Act
let result = subject.fileSizeLimit(from: nostrBuildErrorMessage)

// Assert
XCTAssertNil(result)
}

func test_upload_throws_error_when_serverInfo_has_invalid_apiUrl() async throws {
// Arrange
let subject = NostrBuildAPIClient()
Expand Down
Loading