Skip to content

Commit

Permalink
fix avatar uploads on signup
Browse files Browse the repository at this point in the history
  • Loading branch information
or-else committed Sep 1, 2022
1 parent e8a1294 commit 140a046
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 10 deletions.
9 changes: 5 additions & 4 deletions app/src/main/java/co/tinode/tindroid/AttachmentHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ private ListenableWorker.Result uploadMessageAttachment(final Context context, f
// Upload then send message with a link. This is a long-running blocking call.
mUploader = Cache.getTinode().getFileUploader();
ServerMessage msg = mUploader.upload(is, fname, uploadDetails.mimeType, uploadDetails.fileSize,
(progress, size) -> setProgressAsync(new Data.Builder()
topicName, (progress, size) -> setProgressAsync(new Data.Builder()
.putAll(result.build())
.putLong(ARG_PROGRESS, progress)
.putLong(ARG_FILE_SIZE, size)
Expand Down Expand Up @@ -615,7 +615,8 @@ private ListenableWorker.Result uploadMessageAttachment(final Context context, f
* @param bmp new avatar; no action is taken if avatar is null.
* @return result of the operation.
*/
static PromisedReply<ServerMessage> uploadAvatar(@NotNull final VxCard pub, @Nullable Bitmap bmp) {
static PromisedReply<ServerMessage> uploadAvatar(@NotNull final VxCard pub, @Nullable Bitmap bmp,
@Nullable String topicName) {
if (bmp == null) {
// No action needed.
return new PromisedReply<>((ServerMessage) null);
Expand Down Expand Up @@ -652,8 +653,8 @@ static PromisedReply<ServerMessage> uploadAvatar(@NotNull final VxCard pub, @Nul
pub.photo.data = UiUtils.bitmapToBytes(UiUtils.scaleSquareBitmap(bmp, UiUtils.AVATAR_THUMBNAIL_DIM), mimeType);
// Upload then return result with a link. This is a long-running blocking call.
LargeFileHelper uploader = Cache.getTinode().getFileUploader();
result = uploader.uploadFuture(is, System.currentTimeMillis() + ".png", mimeType, fileSize, null)
.thenApply(new PromisedReply.SuccessListener<ServerMessage>() {
result = uploader.uploadFuture(is, System.currentTimeMillis() + ".png", mimeType, fileSize,
topicName, null).thenApply(new PromisedReply.SuccessListener<ServerMessage>() {
@Override
public PromisedReply<ServerMessage> onSuccess(ServerMessage msg) {
if (msg != null && msg.ctrl != null && msg.ctrl.code == 200) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ private void createTopic(final Activity activity, final String title, final Bitm
topic.setComment(subtitle);
topic.setTags(tags);
topic.setPub(new VxCard(title));
AttachmentHandler.uploadAvatar(topic.getPub(), avatar)
AttachmentHandler.uploadAvatar(topic.getPub(), avatar, null)
.thenApply(new PromisedReply.SuccessListener<ServerMessage>() {
@Override
public PromisedReply<ServerMessage> onSuccess(ServerMessage result) {
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/co/tinode/tindroid/SignUpFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public void onClick(View v) {
.thenApply(new PromisedReply.SuccessListener<ServerMessage>() {
@Override
public PromisedReply<ServerMessage> onSuccess(ServerMessage ignored_msg) {
return AttachmentHandler.uploadAvatar(theCard, bmp);
return AttachmentHandler.uploadAvatar(theCard, bmp, "newacc");
}
})
.thenApply(new PromisedReply.SuccessListener<ServerMessage>() {
Expand Down
31 changes: 27 additions & 4 deletions tinodesdk/src/main/java/co/tinode/tinodesdk/LargeFileHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public class LargeFileHelper {

private boolean mCanceled = false;

private int mReqId = 1;

public LargeFileHelper(URL urlUpload, String apikey, String authToken, String userAgent) {
mUrlUpload = urlUpload;
mHost = mUrlUpload.getHost();
Expand All @@ -48,7 +50,7 @@ public LargeFileHelper(URL urlUpload, String apikey, String authToken, String us

// Upload file out of band. This should not be called on the UI thread.
public ServerMessage upload(@NotNull InputStream in, @NotNull String filename, @NotNull String mimetype, long size,
@Nullable FileHelperProgress progress) throws IOException, CancellationException {
@Nullable String topic, @Nullable FileHelperProgress progress) throws IOException, CancellationException {
mCanceled = false;
HttpURLConnection conn = null;
ServerMessage msg;
Expand All @@ -60,10 +62,28 @@ public ServerMessage upload(@NotNull InputStream in, @NotNull String filename, @
conn.setRequestProperty("User-Agent", mUserAgent);
conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);
conn.setRequestProperty("X-Tinode-APIKey", mApiKey);
conn.setRequestProperty("X-Tinode-Auth", "Token " + mAuthToken);
if (mAuthToken != null) {
// mAuthToken could be null when uploading avatar on sign up.
conn.setRequestProperty("X-Tinode-Auth", "Token " + mAuthToken);
}
conn.setChunkedStreamingMode(0);

DataOutputStream out = new DataOutputStream(new BufferedOutputStream(conn.getOutputStream()));
// Write req ID.
out.writeBytes(TWO_HYPHENS + BOUNDARY + LINE_END);
out.writeBytes("Content-Disposition: form-data; name=\"id\"" + LINE_END);
out.writeBytes(LINE_END);
out.writeBytes(++mReqId + LINE_END);

// Write topic.
if (topic != null) {
out.writeBytes(TWO_HYPHENS + BOUNDARY + LINE_END);
out.writeBytes("Content-Disposition: form-data; name=\"topic\"" + LINE_END);
out.writeBytes(LINE_END);
out.writeBytes(topic + LINE_END);
}

// File section.
out.writeBytes(TWO_HYPHENS + BOUNDARY + LINE_END);
// Content-Disposition: form-data; name="file"; filename="1519014549699.pdf"
out.writeBytes("Content-Disposition: form-data; name=\"file\"; filename=\"" + filename + "\"" + LINE_END);
Expand All @@ -73,9 +93,11 @@ public ServerMessage upload(@NotNull InputStream in, @NotNull String filename, @
out.writeBytes("Content-Transfer-Encoding: binary" + LINE_END);
out.writeBytes(LINE_END);

// File bytes.
copyStream(in, out, size, progress);

out.writeBytes(LINE_END);

// End of form boundary.
out.writeBytes(TWO_HYPHENS + BOUNDARY + TWO_HYPHENS + LINE_END);
out.flush();
out.close();
Expand All @@ -101,11 +123,12 @@ public PromisedReply<ServerMessage> uploadFuture(final InputStream in,
final String filename,
final String mimetype,
final long size,
final String topic,
final FileHelperProgress progress) {
final PromisedReply<ServerMessage> result = new PromisedReply<>();
new Thread(() -> {
try {
ServerMessage msg = upload(in, filename, mimetype, size, progress);
ServerMessage msg = upload(in, filename, mimetype, size, topic, progress);
if (mCanceled) {
throw new CancellationException("Cancelled");
}
Expand Down

0 comments on commit 140a046

Please sign in to comment.