Update attachment content type when updating attachment data

Fixes #4687
Closes #4688
This commit is contained in:
Geonu Kang 2015-11-23 13:18:54 +09:00 committed by Moxie Marlinspike
parent afb21203b7
commit 8269047c88
4 changed files with 53 additions and 7 deletions

View File

@ -36,6 +36,7 @@ import org.thoughtcrime.securesms.crypto.DecryptingPartInputStream;
import org.thoughtcrime.securesms.crypto.EncryptingPartOutputStream; import org.thoughtcrime.securesms.crypto.EncryptingPartOutputStream;
import org.thoughtcrime.securesms.crypto.MasterSecret; import org.thoughtcrime.securesms.crypto.MasterSecret;
import org.thoughtcrime.securesms.crypto.MasterSecretUnion; import org.thoughtcrime.securesms.crypto.MasterSecretUnion;
import org.thoughtcrime.securesms.mms.MediaStream;
import org.thoughtcrime.securesms.mms.PartAuthority; import org.thoughtcrime.securesms.mms.PartAuthority;
import org.thoughtcrime.securesms.util.MediaUtil; import org.thoughtcrime.securesms.util.MediaUtil;
import org.thoughtcrime.securesms.util.MediaUtil.ThumbnailData; import org.thoughtcrime.securesms.util.MediaUtil.ThumbnailData;
@ -289,7 +290,7 @@ public class AttachmentDatabase extends Database {
public @NonNull Attachment updateAttachmentData(@NonNull MasterSecret masterSecret, public @NonNull Attachment updateAttachmentData(@NonNull MasterSecret masterSecret,
@NonNull Attachment attachment, @NonNull Attachment attachment,
@NonNull InputStream inputStream) @NonNull MediaStream mediaStream)
throws MmsException throws MmsException
{ {
SQLiteDatabase database = databaseHelper.getWritableDatabase(); SQLiteDatabase database = databaseHelper.getWritableDatabase();
@ -300,19 +301,21 @@ public class AttachmentDatabase extends Database {
throw new MmsException("No attachment data found!"); throw new MmsException("No attachment data found!");
} }
long dataSize = setAttachmentData(masterSecret, dataFile, inputStream); long dataSize = setAttachmentData(masterSecret, dataFile, mediaStream.getStream());
ContentValues contentValues = new ContentValues(); ContentValues contentValues = new ContentValues();
contentValues.put(SIZE, dataSize); contentValues.put(SIZE, dataSize);
contentValues.put(CONTENT_TYPE, mediaStream.getMimeType());
database.update(TABLE_NAME, contentValues, PART_ID_WHERE, databaseAttachment.getAttachmentId().toStrings()); database.update(TABLE_NAME, contentValues, PART_ID_WHERE, databaseAttachment.getAttachmentId().toStrings());
return new DatabaseAttachment(databaseAttachment.getAttachmentId(), return new DatabaseAttachment(databaseAttachment.getAttachmentId(),
databaseAttachment.getMmsId(), databaseAttachment.getMmsId(),
databaseAttachment.hasData(), databaseAttachment.hasData(),
databaseAttachment.getContentType(), mediaStream.getMimeType(),
databaseAttachment.getTransferState(), databaseAttachment.getTransferState(),
dataSize, databaseAttachment.getLocation(), dataSize,
databaseAttachment.getLocation(),
databaseAttachment.getKey(), databaseAttachment.getKey(),
databaseAttachment.getRelay()); databaseAttachment.getRelay());
} }

View File

@ -10,7 +10,9 @@ import org.thoughtcrime.securesms.crypto.MasterSecret;
import org.thoughtcrime.securesms.database.DatabaseFactory; import org.thoughtcrime.securesms.database.DatabaseFactory;
import org.thoughtcrime.securesms.database.AttachmentDatabase; import org.thoughtcrime.securesms.database.AttachmentDatabase;
import org.thoughtcrime.securesms.mms.MediaConstraints; import org.thoughtcrime.securesms.mms.MediaConstraints;
import org.thoughtcrime.securesms.mms.MediaStream;
import org.thoughtcrime.securesms.transport.UndeliverableMessageException; import org.thoughtcrime.securesms.transport.UndeliverableMessageException;
import org.thoughtcrime.securesms.util.MediaUtil;
import org.thoughtcrime.securesms.util.Util; import org.thoughtcrime.securesms.util.Util;
import org.whispersystems.jobqueue.JobParameters; import org.whispersystems.jobqueue.JobParameters;
@ -19,6 +21,7 @@ import java.io.InputStream;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import ws.com.google.android.mms.ContentType;
import ws.com.google.android.mms.MmsException; import ws.com.google.android.mms.MmsException;
public abstract class SendJob extends MasterSecretJob { public abstract class SendJob extends MasterSecretJob {
@ -63,7 +66,7 @@ public abstract class SendJob extends MasterSecretJob {
if (constraints.isSatisfied(context, masterSecret, attachment)) { if (constraints.isSatisfied(context, masterSecret, attachment)) {
results.add(attachment); results.add(attachment);
} else if (constraints.canResize(attachment)) { } else if (constraints.canResize(attachment)) {
InputStream resized = constraints.getResizedMedia(context, masterSecret, attachment); MediaStream resized = constraints.getResizedMedia(context, masterSecret, attachment);
results.add(attachmentDatabase.updateAttachmentData(masterSecret, attachment, resized)); results.add(attachmentDatabase.updateAttachmentData(masterSecret, attachment, resized));
} else { } else {
throw new UndeliverableMessageException("Size constraints could not be met!"); throw new UndeliverableMessageException("Size constraints could not be met!");

View File

@ -18,6 +18,8 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import ws.com.google.android.mms.ContentType;
public abstract class MediaConstraints { public abstract class MediaConstraints {
private static final String TAG = MediaConstraints.class.getSimpleName(); private static final String TAG = MediaConstraints.class.getSimpleName();
@ -58,7 +60,7 @@ public abstract class MediaConstraints {
return attachment != null && MediaUtil.isImage(attachment) && !MediaUtil.isGif(attachment); return attachment != null && MediaUtil.isImage(attachment) && !MediaUtil.isGif(attachment);
} }
public InputStream getResizedMedia(@NonNull Context context, public MediaStream getResizedMedia(@NonNull Context context,
@NonNull MasterSecret masterSecret, @NonNull MasterSecret masterSecret,
@NonNull Attachment attachment) @NonNull Attachment attachment)
throws IOException throws IOException
@ -69,7 +71,8 @@ public abstract class MediaConstraints {
try { try {
// XXX - This is loading everything into memory! We want the send path to be stream-like. // XXX - This is loading everything into memory! We want the send path to be stream-like.
return new ByteArrayInputStream(BitmapUtil.createScaledBytes(context, new DecryptableUri(masterSecret, attachment.getDataUri()), this)); return new MediaStream(new ByteArrayInputStream(BitmapUtil.createScaledBytes(context, new DecryptableUri(masterSecret, attachment.getDataUri()), this)),
ContentType.IMAGE_JPEG);
} catch (ExecutionException ee) { } catch (ExecutionException ee) {
throw new IOException(ee); throw new IOException(ee);
} }

View File

@ -0,0 +1,37 @@
/**
* Copyright (C) 2015 Open Whisper Systems
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.thoughtcrime.securesms.mms;
import java.io.InputStream;
public class MediaStream {
private final InputStream stream;
private final String mimeType;
public MediaStream(InputStream stream, String mimeType) {
this.stream = stream;
this.mimeType = mimeType;
}
public InputStream getStream() {
return stream;
}
public String getMimeType() {
return mimeType;
}
}