Skip to content
This repository has been archived by the owner on Jun 7, 2020. It is now read-only.

[NEW] Use REST API to upload files with fallback to older servers #1058

Merged
merged 11 commits into from
Dec 18, 2017

Conversation

cardoso
Copy link
Collaborator

@cardoso cardoso commented Dec 8, 2017

@RocketChat/ios

Closes #1055

@cardoso
Copy link
Collaborator Author

cardoso commented Dec 8, 2017

This is not working yet...

I'm receiving this error from the server:

{
    "error" : "File required", 
    "success" : false
}

For reference:
API
Busboy

@@ -76,6 +76,20 @@ class UploadManager {

// swiftlint:disable function_body_length
func uploadToUFSFile(store: String, file: FileUpload, subscription: Subscription, progress: UploadProgressBlock, completion: @escaping UploadCompletionBlock) {

if let api = API.current() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't API.current() always return something valid? 🤔

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not how it's working currently. I use the Auth object from AuthManager.isAuthenticated() to build it.

@rafaelks
Copy link
Contributor

rafaelks commented Dec 8, 2017

@cardoso Take a look here in the code below, we're using a different Content-Type for the file and the order of the parameters are also different.

let boundary = String.random()
let contentType = "multipart/form-data; boundary=\(boundary)"
request.addValue(contentType, forHTTPHeaderField: "Content-Type")
var data = Data()
data.append("\r\n--\(boundary)\r\n".data(using: .utf8) ?? Data())
for postData in formData?.array ?? [] {
guard let key = postData["name"].string else { continue }
guard let value = postData["value"].string else { continue }
data.append("Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n".data(using: .utf8) ?? Data())
data.append(value.data(using: .utf8) ?? Data())
data.append("\r\n--\(boundary)\r\n".data(using: .utf8) ?? Data())
}
data.append("Content-Disposition: form-data; name=\"file\"\r\n".data(using: .utf8) ?? Data())
data.append("Content-Type: application/octet-stream\r\n\r\n".data(using: .utf8) ?? Data())
data.append(file.data)
data.append("\r\n--\(boundary)--\r\n".data(using: .utf8) ?? Data())

@codecov
Copy link

codecov bot commented Dec 8, 2017

Codecov Report

Merging #1058 into develop will increase coverage by 4.2%.
The diff coverage is 34.28%.

Impacted file tree graph

@@            Coverage Diff             @@
##           develop    #1058     +/-   ##
==========================================
+ Coverage    39.86%   44.06%   +4.2%     
==========================================
  Files          254      257      +3     
  Lines        14307    14419    +112     
  Branches       742      791     +49     
==========================================
+ Hits          5703     6354    +651     
+ Misses        8144     7599    -545     
- Partials       460      466      +6
Impacted Files Coverage Δ
...Chat/API/Requests/Message/SendMessageRequest.swift 31.25% <ø> (ø) ⬆️
Rocket.Chat/Helpers/Alert.swift 0% <0%> (ø) ⬆️
Rocket.Chat/API/Clients/UploadClient.swift 0% <0%> (ø)
...Chat/Controllers/Chat/ChatControllerUploader.swift 0% <0%> (ø) ⬆️
Rocket.Chat/API/APIRequest.swift 86.53% <100%> (+5.76%) ⬆️
...Tests/API/Requests/Message/UploadRequestSpec.swift 61.53% <61.53%> (ø)
Rocket.Chat/Extensions/DataExtension.swift 25% <85.71%> (+25%) ⬆️
...cket.Chat/API/Requests/Message/UploadRequest.swift 91.66% <91.66%> (ø)
...Controllers/Auth/ConnectServerViewController.swift 34.16% <0%> (-2.49%) ⬇️
... and 24 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update fe015ce...1196d5f. Read the comment docs.

completion(nil, false)
}, errored: { error in
if case .version = error {
// TODO: Remove Upload fallback after Rocket.Chat 1.0

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Todo Violation: TODOs should be avoided (Remove Upload fallback after R...). (todo)

@cardoso cardoso changed the title [WIP][NEW] Use REST API to upload files with fallback to older servers [NEW] Use REST API to upload files with fallback to older servers Dec 12, 2017
@cardoso
Copy link
Collaborator Author

cardoso commented Dec 12, 2017

@rafaelks ready for review 👍 thanks for the help!!

completion(response, true)
return
}
completion(nil, true)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we send the error to the completion block? I think would be interesting to show the error message here, example: file limit is 2MB.

@cardoso
Copy link
Collaborator Author

cardoso commented Dec 13, 2017

Blocked by RocketChat/Rocket.Chat#9096

completion?()
}, errored: { error in
if case .version = error {
// TODO: Remove Upload fallback after Rocket.Chat 1.0

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Todo Violation: TODOs should be avoided (Remove Upload fallback after R...). (todo)

@cardoso
Copy link
Collaborator Author

cardoso commented Dec 14, 2017

@rafaelks now it's showing error messages (unblocked by RocketChat/Rocket.Chat#9108)

})
}

func deletePushToken(token: String? = PushManager.getDeviceToken()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is deletePushToken method here? 🤔

Copy link
Collaborator Author

@cardoso cardoso Dec 15, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No good reason 😊 thanks for spotting it 😂


api.fetch(req, succeeded: { result in
if let error = result.error {
Alert(key: "alert.upload_error").withMessage(error).present()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

status code 2xx with error (I know it's API's fault)? 😂

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤷‍♀️

@rafaelks rafaelks merged commit 27d11bf into develop Dec 18, 2017
@rafaelks rafaelks deleted the feat/upload_http.1055 branch December 18, 2017 10:42
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants