Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File deduplication, Android part #3513

Merged
merged 7 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions jni/dc_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/b44t/messenger/DcMsg.java
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ public JSONObject getWebxdcInfo () {
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1039,7 +1039,7 @@ protected ListenableFuture<Integer> processComposeControls(final int action, Str
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);
Expand Down Expand Up @@ -1329,7 +1329,7 @@ private void sendSticker(@NonNull Uri uri, String contentType) {
if (quote.isPresent()) {
msg.setQuote(quote.get().getQuotedMsg());
}
msg.setFile(path, null);
msg.setFileAndDeduplicate(path, null, null);
dcContext.sendMsg(chatId, msg);
}

Expand Down
adbenitez marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@

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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,13 @@ private static String checkMime(String path, String mimeType) {
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
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;
Expand Down Expand Up @@ -68,7 +67,6 @@
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;
Expand Down Expand Up @@ -693,7 +691,7 @@ public enum MediaType {
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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,19 +105,20 @@ public static DcMsg createMessage(Context context, Uri uri, String text) throws
}

if (uri != null) {
message.setFile(getRealPathFromUri(context, uri), mimeType);
setFileFromUri(context, uri, message, mimeType);
}
if (text != null) {
message.setText(text);
}
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")) {
Expand All @@ -135,26 +136,19 @@ private static String getRealPathFromUri(Context context, Uri uri) throws NullPo
}
}

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) {
InputStream inputStream = PartAuthority.getAttachmentStream(context, uri);
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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -640,10 +640,7 @@ public static boolean prepareVideo(Context context, int chatId, DcMsg msg) {
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));
}
Expand Down
Loading