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

[CIS-1181] Implement uploadFile API #1468

Merged
merged 2 commits into from Sep 24, 2021
Merged

[CIS-1181] Implement uploadFile API #1468

merged 2 commits into from Sep 24, 2021

Conversation

b-onc
Copy link
Contributor

@b-onc b-onc commented Sep 22, 2021

It'll allow uploading a file to CDN without having to upload an attachment to a message

@b-onc b-onc added 🙏 Feature Request A new feature request 🌐 SDK: StreamChat (LLC) Tasks related to the StreamChat LLC SDK labels Sep 22, 2021
@@ -33,6 +33,13 @@ public struct FileAttachmentPayload: AttachmentPayload {
.flatMap { try? JSONEncoder.stream.encode($0) }
.flatMap { try? JSONDecoder.stream.decode(T.self, from: $0) }
}

public init(title: String?, assetURL: URL, file: AttachmentFile, extraData: [String: RawJSON]?) {
Copy link
Contributor

@evsaev evsaev Sep 22, 2021

Choose a reason for hiding this comment

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

The public init looks safe but it actually opens the door for a miss-use. When ***AttachmentPayload is instantiated manually there's a chance it is passed to AnyAttachmentPayload.init designed for custom non-uploadable attachments:

init<Payload: AttachmentPayload>(payload: Payload) {
        self.init(
            type: Payload.type,
            payload: payload,
            localFileURL: nil
        )
    }

instead of one designed for built-in uploadable attachments:

init(
        localFileURL: URL,
        attachmentType: AttachmentType,
        extraData: Encodable? = nil
    ) { ... }

Attachment without localURL are not picked up for uploading and will be sent to the backend as is (with local URLs) 🥲

Copy link
Contributor Author

@b-onc b-onc Sep 23, 2021

Choose a reason for hiding this comment

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

IMHO that's safe, developers don't need to create built-in payload instances unless they handle the uploads themselves.

override func addAttachmentToContent(from url: URL, type: AttachmentType) throws {
        if pickingEncryptedMedia {
            pickingEncryptedMedia = false
            // Encrypt the attachment
            // Upload the attachment
            channelController?.uploadFile(type: type, localFileURL: url, completion: { result in
                do {
                    let uploadedURL = try result.get()
                    // Append already uploaded attachment
                    let attachment = AnyAttachmentPayload(
                        payload: ImageAttachmentPayload(
                            title: nil,
                            imageURL: uploadedURL,
                            extraData: nil
                        )
                    )
                    DispatchQueue.main.async {
                        self.content.attachments.append(attachment)
                    }
                } catch {
                    // Handle error
                    log.error("Failed uploading attachment: \(error)")
                }
            })
        } else {
            try super.addAttachmentToContent(from: url, type: type)
        }
    }

Copy link
Contributor

Choose a reason for hiding this comment

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

They do not need but the API allows it, that's what I was saying 🙂

That's the correct way:

let attachment = try AnyAttachmentPayload(localFileURL: url, attachmentType: .image)
content.attachments.append(attachment)

That's the 💣

let attachment = AnyAttachmentPayload(
    payload: ImageAttachmentPayload(
         title: nil, 
         imageURL: url, 
         extraData: nil
     )
)
content.attachments.append(attachment)

@codecov-commenter
Copy link

codecov-commenter commented Sep 22, 2021

Codecov Report

Merging #1468 (fefc8f6) into main (f5f7274) will increase coverage by 0.00%.
The diff coverage is 88.05%.

Impacted file tree graph

@@           Coverage Diff           @@
##             main    #1468   +/-   ##
=======================================
  Coverage   88.24%   88.25%           
=======================================
  Files         228      228           
  Lines       10364    10418   +54     
=======================================
+ Hits         9146     9194   +48     
- Misses       1218     1224    +6     
Flag Coverage Δ
llc-tests 88.25% <88.05%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
...odels/Attachments/ChatMessageAudioAttachment.swift 0.00% <0.00%> (ø)
...Chat/Models/Attachments/AnyAttachmentPayload.swift 88.52% <80.00%> (ø)
Sources/StreamChat/Workers/ChannelUpdater.swift 89.65% <93.75%> (+0.50%) ⬆️
Sources/StreamChat/APIClient/CDNClient.swift 78.84% <100.00%> (ø)
...Chat/APIClient/Endpoints/AttachmentEndpoints.swift 100.00% <100.00%> (ø)
...trollers/ChannelController/ChannelController.swift 91.89% <100.00%> (+0.27%) ⬆️
...Models/Attachments/ChatMessageFileAttachment.swift 100.00% <100.00%> (ø)
...odels/Attachments/ChatMessageImageAttachment.swift 96.87% <100.00%> (+0.57%) ⬆️
...odels/Attachments/ChatMessageVideoAttachment.swift 100.00% <100.00%> (ø)

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 f5f7274...fefc8f6. Read the comment docs.

Copy link
Member

@tbarbugli tbarbugli left a comment

Choose a reason for hiding this comment

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

Can you include code examples about this? It is not 100% obvious why this is not going via the CDN class

@b-onc
Copy link
Contributor Author

b-onc commented Sep 23, 2021

@tbarbugli It's actually going through the CDN class, just via ChannelController -> APIClient -> CDN, we don't want to expose CDN by itself

@b-onc b-onc force-pushed the sendfile-api branch 2 times, most recently from e0957bc to b522f21 Compare September 23, 2021 10:36
evsaev
evsaev previously approved these changes Sep 23, 2021
Copy link
Contributor

@evsaev evsaev left a comment

Choose a reason for hiding this comment

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

Looks good 👍

tbarbugli
tbarbugli previously approved these changes Sep 23, 2021
It'll allow uploading a file to CDN without having to upload an attachment to a message
@b-onc b-onc merged commit 6f5f89c into main Sep 24, 2021
@b-onc b-onc deleted the sendfile-api branch September 24, 2021 09:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🙏 Feature Request A new feature request 🌐 SDK: StreamChat (LLC) Tasks related to the StreamChat LLC SDK
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants