mirror of
https://github.com/oxen-io/session-android.git
synced 2025-10-24 01:21:09 +00:00
streaming media
// FREEBIE
This commit is contained in:
@@ -70,8 +70,7 @@ public class AudioSlide extends Slide {
|
||||
public static PduPart constructPartFromUri(Context context, Uri uri) throws IOException, MediaTooLargeException {
|
||||
PduPart part = new PduPart();
|
||||
|
||||
if (getMediaSize(context, uri) > MAX_MESSAGE_SIZE)
|
||||
throw new MediaTooLargeException("Audio track larger than size maximum.");
|
||||
assertMediaSize(context, uri);
|
||||
|
||||
Cursor cursor = null;
|
||||
|
||||
|
||||
45
src/org/thoughtcrime/securesms/mms/PartAuthority.java
Normal file
45
src/org/thoughtcrime/securesms/mms/PartAuthority.java
Normal file
@@ -0,0 +1,45 @@
|
||||
package org.thoughtcrime.securesms.mms;
|
||||
|
||||
import android.content.ContentUris;
|
||||
import android.content.Context;
|
||||
import android.content.UriMatcher;
|
||||
import android.net.Uri;
|
||||
|
||||
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
||||
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
||||
import org.thoughtcrime.securesms.database.PartDatabase;
|
||||
import org.thoughtcrime.securesms.providers.PartProvider;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.InputStream;
|
||||
|
||||
public class PartAuthority {
|
||||
|
||||
private static final String PART_URI_STRING = "content://org.thoughtcrime.securesms/part";
|
||||
public static final Uri PART_CONTENT_URI = Uri.parse(PART_URI_STRING);
|
||||
|
||||
private static final int PART_ROW = 1;
|
||||
|
||||
private static final UriMatcher uriMatcher;
|
||||
|
||||
static {
|
||||
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
|
||||
uriMatcher.addURI("org.thoughtcrime.securesms", "part/#", PART_ROW);
|
||||
}
|
||||
|
||||
public static InputStream getPartStream(Context context, MasterSecret masterSecret, Uri uri)
|
||||
throws FileNotFoundException
|
||||
{
|
||||
PartDatabase partDatabase = DatabaseFactory.getPartDatabase(context);
|
||||
int match = uriMatcher.match(uri);
|
||||
|
||||
switch (match) {
|
||||
case PART_ROW: return partDatabase.getPartStream(masterSecret, ContentUris.parseId(uri));
|
||||
default: return context.getContentResolver().openInputStream(uri);
|
||||
}
|
||||
}
|
||||
|
||||
public static Uri getPublicPartUri(Uri uri) {
|
||||
return ContentUris.withAppendedId(PartProvider.CONTENT_URI, ContentUris.parseId(uri));
|
||||
}
|
||||
}
|
||||
@@ -20,6 +20,7 @@ import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import org.thoughtcrime.securesms.util.Util;
|
||||
import org.w3c.dom.smil.SMILDocument;
|
||||
import org.w3c.dom.smil.SMILMediaElement;
|
||||
import org.w3c.dom.smil.SMILRegionElement;
|
||||
@@ -56,34 +57,19 @@ public abstract class Slide {
|
||||
}
|
||||
|
||||
public InputStream getPartDataInputStream() throws FileNotFoundException {
|
||||
Uri partUri = part.getDataUri();
|
||||
|
||||
Log.w("Slide", "Loading Part URI: " + partUri);
|
||||
|
||||
if (PartProvider.isAuthority(partUri))
|
||||
return DatabaseFactory.getEncryptingPartDatabase(context, masterSecret).getPartStream(ContentUris.parseId(partUri));
|
||||
else
|
||||
return context.getContentResolver().openInputStream(partUri);
|
||||
}
|
||||
|
||||
protected static long getMediaSize(Context context, Uri uri) throws IOException {
|
||||
InputStream in = context.getContentResolver().openInputStream(uri);
|
||||
long size = 0;
|
||||
byte[] buffer = new byte[512];
|
||||
int read;
|
||||
|
||||
while ((read = in.read(buffer)) != -1)
|
||||
size += read;
|
||||
|
||||
return size;
|
||||
return PartAuthority.getPartStream(context, masterSecret, part.getDataUri());
|
||||
}
|
||||
|
||||
protected byte[] getPartData() {
|
||||
if (part.getData() != null)
|
||||
return part.getData();
|
||||
try {
|
||||
if (part.getData() != null)
|
||||
return part.getData();
|
||||
|
||||
long partId = ContentUris.parseId(part.getDataUri());
|
||||
return DatabaseFactory.getEncryptingPartDatabase(context, masterSecret).getPart(partId, true).getData();
|
||||
return Util.readFully(PartAuthority.getPartStream(context, masterSecret, part.getDataUri()));
|
||||
} catch (IOException e) {
|
||||
Log.w("Slide", e);
|
||||
return new byte[0];
|
||||
}
|
||||
}
|
||||
|
||||
public String getContentType() {
|
||||
@@ -133,4 +119,18 @@ public abstract class Slide {
|
||||
public abstract SMILRegionElement getSmilRegion(SMILDocument document);
|
||||
|
||||
public abstract SMILMediaElement getMediaElement(SMILDocument document);
|
||||
|
||||
protected static void assertMediaSize(Context context, Uri uri)
|
||||
throws MediaTooLargeException, IOException
|
||||
{
|
||||
InputStream in = context.getContentResolver().openInputStream(uri);
|
||||
long size = 0;
|
||||
byte[] buffer = new byte[512];
|
||||
int read;
|
||||
|
||||
while ((read = in.read(buffer)) != -1) {
|
||||
size += read;
|
||||
if (size > MAX_MESSAGE_SIZE) throw new MediaTooLargeException("Media exceeds maximum message size.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -91,9 +91,7 @@ public class VideoSlide extends Slide {
|
||||
cursor.close();
|
||||
}
|
||||
|
||||
if (getMediaSize(context, uri) > MAX_MESSAGE_SIZE)
|
||||
throw new MediaTooLargeException("Video exceeds maximum message size.");
|
||||
|
||||
assertMediaSize(context, uri);
|
||||
part.setDataUri(uri);
|
||||
part.setContentId((System.currentTimeMillis()+"").getBytes());
|
||||
part.setName(("Video" + System.currentTimeMillis()).getBytes());
|
||||
|
||||
Reference in New Issue
Block a user