File deduplication, Android part (#3513)

This commit is contained in:
Hocuri 2025-01-27 18:40:44 +01:00 committed by GitHub
parent bfda7ab2c3
commit c6b89055d8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 32 additions and 25 deletions

View file

@ -1632,6 +1632,18 @@ JNIEXPORT void Java_com_b44t_messenger_DcMsg_setFile(JNIEnv *env, jobject obj, j
}
JNIEXPORT void Java_com_b44t_messenger_DcMsg_setFileAndDeduplicate(JNIEnv *env, jobject obj, jstring file, jstring name, jstring filemime)
{
CHAR_REF(file);
CHAR_REF(name);
CHAR_REF(filemime);
dc_msg_set_file_and_deduplicate(get_dc_msg(env, obj), filePtr, namePtr, filemimePtr);
CHAR_UNREF(filemime);
CHAR_UNREF(name);
CHAR_UNREF(file);
}
JNIEXPORT void Java_com_b44t_messenger_DcMsg_setDimension(JNIEnv *env, jobject obj, int width, int height)
{
dc_msg_set_dimension(get_dc_msg(env, obj), width, height);

View file

@ -157,6 +157,7 @@ public class DcMsg {
public native int getVideochatType ();
public native void setText (String text);
public native void setFile (String file, String filemime);
public native void setFileAndDeduplicate(String file, String name, String filemime);
public native void setDimension (int width, int height);
public native void setDuration (int duration);
public native void setLocation (float latitude, float longitude);

View file

@ -1039,7 +1039,7 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity
msg = new DcMsg(dcContext, DcMsg.DC_MSG_FILE);
}
String path = attachment.getRealPath(this);
msg.setFile(path, null);
msg.setFileAndDeduplicate(path, attachment.getFileName(), null);
}
}
msg.setText(body);
@ -1329,7 +1329,7 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity
if (quote.isPresent()) {
msg.setQuote(quote.get().getQuotedMsg());
}
msg.setFile(path, null);
msg.setFileAndDeduplicate(path, null, null);
dcContext.sendMsg(chatId, msg);
}

View file

@ -3,10 +3,8 @@ package org.thoughtcrime.securesms.components;
import android.content.Context;
import android.net.Uri;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.view.Window;

View file

@ -367,6 +367,13 @@ public class DcHelper {
return mimeType;
}
/**
* Return the path of a not-yet-existing file in the blobdir with roughly the given filename
* and the given extension.
* In many cases, since we're using setFileAndDeduplicate now, this wouldn't be necessary anymore
* and we could just create a file with a random filename,
* but there are a few usages that still need the current behavior (like `openMaps()`).
*/
public static String getBlobdirFile(DcContext dcContext, String filename, String ext) {
filename = FileUtils.sanitizeFilename(filename);
ext = FileUtils.sanitizeFilename(ext);

View file

@ -23,7 +23,6 @@ import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.PorterDuff;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
@ -68,7 +67,6 @@ import org.thoughtcrime.securesms.permissions.Permissions;
import org.thoughtcrime.securesms.providers.PersistentBlobProvider;
import org.thoughtcrime.securesms.scribbles.ScribbleActivity;
import org.thoughtcrime.securesms.util.MediaUtil;
import org.thoughtcrime.securesms.util.ThemeUtil;
import org.thoughtcrime.securesms.util.ViewUtil;
import org.thoughtcrime.securesms.util.guava.Optional;
import org.thoughtcrime.securesms.util.views.Stub;
@ -693,7 +691,7 @@ public class AttachmentManager {
DcMsg msg = new DcMsg(dcContext, DcMsg.DC_MSG_WEBXDC);
Attachment attachment = new UriAttachment(uri, null, MediaUtil.WEBXDC, AttachmentDatabase.TRANSFER_PROGRESS_STARTED, 0, 0, 0, fileName, null, false);
String path = attachment.getRealPath(context);
msg.setFile(path, MediaUtil.WEBXDC);
msg.setFileAndDeduplicate(path, fileName, MediaUtil.WEBXDC);
dcContext.setDraft(chatId, msg);
return new DocumentSlide(context, msg);
}

View file

@ -105,7 +105,7 @@ public class SendRelayedMessageUtil {
}
if (uri != null) {
message.setFile(getRealPathFromUri(context, uri), mimeType);
setFileFromUri(context, uri, message, mimeType);
}
if (text != null) {
message.setText(text);
@ -113,11 +113,12 @@ public class SendRelayedMessageUtil {
return message;
}
private static String getRealPathFromUri(Context context, Uri uri) throws NullPointerException {
private static void setFileFromUri(Context context, Uri uri, DcMsg message, String mimeType) {
String path;
DcContext dcContext = DcHelper.getContext(context);
String filename = "cannot-resolve.jpg"; // best guess, this still leads to most images being workable if OS does weird things
try {
String filename = "cannot-resolve.jpg"; // best guess, this still leads to most images being workable if OS does weird things
if (PartAuthority.isLocalUri(uri)) {
filename = uri.getPathSegments().get(PersistentBlobProvider.FILENAME_PATH_SEGMENT);
} else if (uri.getScheme().equals("content")) {
@ -135,14 +136,7 @@ public class SendRelayedMessageUtil {
}
}
String ext = "";
int i = filename.lastIndexOf(".");
if (i >= 0) {
ext = filename.substring(i);
filename = filename.substring(0, i);
}
String path = DcHelper.getBlobdirFile(dcContext, filename, ext);
path = DcHelper.getBlobdirFile(dcContext, filename, "temp");
// copy content to this file
if (path != null) {
@ -150,11 +144,11 @@ public class SendRelayedMessageUtil {
OutputStream outputStream = new FileOutputStream(path);
Util.copy(inputStream, outputStream);
}
return path;
} catch (Exception e) {
e.printStackTrace();
return null;
path = null;
}
message.setFileAndDeduplicate(path, filename, mimeType);
}
}

View file

@ -640,10 +640,7 @@ public class VideoRecoder {
return false;
}
if (!Util.moveFile(tempPath, inPath)) {
alert(context, String.format("Recoding failed for %s: cannot move temporary file %s", inPath, tempPath));
return false;
}
msg.setFileAndDeduplicate(tempPath, msg.getFilename(), msg.getFilemime());
Log.i(TAG, String.format("recoding for %s done", inPath));
}