Skip to content

Commit

Permalink
Fix avatar upload.
Browse files Browse the repository at this point in the history
  • Loading branch information
aforge authored and or-else committed Feb 6, 2023
1 parent 28fb0b4 commit f6f633d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
20 changes: 19 additions & 1 deletion Tinodios/LargeFileHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ public class LargeFileHelper: NSObject {
private var activeUploads: [String: Upload] = [:]
private var downloadCallbacks: [Int: ((Error?) -> Void)] = [:]
private var tinode: Tinode!
// Numeric id of upload.
private var reqId = 0

init(with tinode: Tinode, config: URLSessionConfiguration) {
super.init()
Expand Down Expand Up @@ -113,7 +115,23 @@ public class LargeFileHelper: NSObject {
LargeFileHelper.addCommonHeaders(to: &request, using: self.tinode)

var newData = Data()
let header = LargeFileHelper.kTwoHyphens + LargeFileHelper.kBoundary + LargeFileHelper.kLineEnd + "Content-Disposition: form-data; name=\"file\"; filename=\"\(filename)\"" + LargeFileHelper.kLineEnd + "Content-Type: \(mimetype)" + LargeFileHelper.kLineEnd + "Content-Transfer-Encoding: binary" + LargeFileHelper.kLineEnd + LargeFileHelper.kLineEnd
// Id section.
self.reqId += 1
var header = LargeFileHelper.kTwoHyphens + LargeFileHelper.kBoundary + LargeFileHelper.kLineEnd +
"Content-Disposition: form-data; name=\"id\"" + LargeFileHelper.kLineEnd + LargeFileHelper.kLineEnd +
"\(self.reqId)" + LargeFileHelper.kLineEnd
if !topicId.isEmpty {
// Topic.
header +=
LargeFileHelper.kTwoHyphens + LargeFileHelper.kBoundary + LargeFileHelper.kLineEnd +
"Content-Disposition: form-data; name=\"topic\"" + LargeFileHelper.kLineEnd + LargeFileHelper.kLineEnd + topicId + LargeFileHelper.kLineEnd
}
// File section.
// Content-Disposition: form-data; name="file"; filename="1519014549699.pdf"
header += LargeFileHelper.kTwoHyphens + LargeFileHelper.kBoundary + LargeFileHelper.kLineEnd +
"Content-Disposition: form-data; name=\"file\"; filename=\"\(filename)\"" + LargeFileHelper.kLineEnd
// Content type & transfer encoding.
header += "Content-Type: \(mimetype)" + LargeFileHelper.kLineEnd + "Content-Transfer-Encoding: binary" + LargeFileHelper.kLineEnd + LargeFileHelper.kLineEnd
newData.append(contentsOf: header.utf8)
newData.append(payload)
let footer = LargeFileHelper.kLineEnd + LargeFileHelper.kTwoHyphens + LargeFileHelper.kBoundary + LargeFileHelper.kTwoHyphens + LargeFileHelper.kLineEnd
Expand Down
1 change: 1 addition & 0 deletions Tinodios/SettingsPersonalViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ extension SettingsPersonalViewController: ImagePickerDelegate {
UiUtils.updateAvatar(forTopic: self.me, image: image)
.thenApply { _ in
DispatchQueue.main.async {
UiUtils.showToast(message: "Avatar successfully updated", level: .info)
self.reloadData()
}
return nil
Expand Down
2 changes: 1 addition & 1 deletion Tinodios/#ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ class #ViewController: UITableViewController {
if let imageBits = avatar?.pixelData(forMimeType: Photo.kDefaultType) {
if imageBits.count > UiUtils.kMaxInbandAvatarBytes {
// Sending image out of band.
Cache.getLargeFileHelper().startAvatarUpload(mimetype: Photo.kDefaultType, data: imageBits, topicId: Tinode.kTopicMe, completionCallback: {(srvmsg, error) in
Cache.getLargeFileHelper().startAvatarUpload(mimetype: Photo.kDefaultType, data: imageBits, topicId: "newacc", completionCallback: {(srvmsg, error) in
guard let error = error else {
let thumbnail = avatar!.resize(width: UiUtils.kAvatarPreviewDimensions, height: UiUtils.kAvatarPreviewDimensions, clip: true)
let photo = Photo(data: thumbnail?.pixelData(forMimeType: Photo.kDefaultType), ref: srvmsg?.ctrl?.getStringParam(for: "url"), width: Int(avatar!.size.width), height: Int(avatar!.size.height))
Expand Down

0 comments on commit f6f633d

Please # to comment.