use full file name for all new files (Daniel Gultsch)

This commit is contained in:
12aw 2022-03-19 16:15:13 +01:00
parent f982dcf0f8
commit 40afd41237
6 changed files with 72 additions and 50 deletions

View file

@ -9,6 +9,7 @@ import org.openintents.openpgp.util.OpenPgpApi;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
@ -195,9 +196,9 @@ public class PgpDecryptionService {
String originalExtension = originalFilename == null ? null : MimeUtils.extractRelevantExtension(originalFilename);
if (originalExtension != null && MimeUtils.extractRelevantExtension(outputFile.getName()) == null) {
Log.d(Config.LOGTAG, "detected original filename during pgp decryption");
String mime = MimeUtils.guessMimeTypeFromExtension(originalExtension);
String path = outputFile.getName() + "." + originalExtension;
DownloadableFile fixedFile = mXmppConnectionService.getFileBackend().getFileForPath(path, mime);
final String mime = MimeUtils.guessMimeTypeFromExtension(originalExtension);
final String filename = outputFile.getName() + "." + originalExtension;
final File fixedFile = mXmppConnectionService.getFileBackend().getStorageLocation(filename, mime);
if (fixedFile.getParentFile().mkdirs()) {
Log.d(Config.LOGTAG, "created parent directories for " + fixedFile.getAbsolutePath());
}
@ -206,7 +207,7 @@ public class PgpDecryptionService {
}
if (outputFile.renameTo(fixedFile)) {
Log.d(Config.LOGTAG, "renamed " + outputFile.getAbsolutePath() + " to " + fixedFile.getAbsolutePath());
message.setRelativeFilePath(path);
message.setRelativeFilePath(fixedFile.getAbsolutePath());
}
}
final String url = message.getFileParams().url;

View file

@ -15,7 +15,6 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import javax.net.ssl.SSLHandshakeException;
@ -101,22 +100,12 @@ public class HttpDownloadConnection implements Transferable {
}
final String ext = extension.getExtension();
if (ext != null) {
if (message.getStatus() == Message.STATUS_RECEIVED) {
message.setRelativeFilePath(String.format("%s.%s", fileDateFormat.format(new Date(message.getTimeSent())) + "_" + message.getUuid().substring(0, 4), ext));
} else {
message.setRelativeFilePath("Sent/" + String.format("%s.%s", fileDateFormat.format(new Date(message.getTimeSent())) + "_" + message.getUuid().substring(0, 4), ext));
}
message.setRelativeFilePath(String.format("%s.%s", message.getUuid(), ext));
} else if (Strings.isNullOrEmpty(message.getRelativeFilePath())) {
if (message.getStatus() == Message.STATUS_RECEIVED) {
message.setRelativeFilePath(fileDateFormat.format(new Date(message.getTimeSent())) + "_" + message.getUuid().substring(0, 4));
} else {
message.setRelativeFilePath("Sent/" + fileDateFormat.format(new Date(message.getTimeSent())) + "_" + message.getUuid().substring(0, 4));
}
message.setRelativeFilePath(message.getUuid());
}
setupFile();
if ((this.message.getEncryption() == Message.ENCRYPTION_OTR
|| this.message.getEncryption() == Message.ENCRYPTION_AXOLOTL)
&& this.file.getKey() == null) {
if (this.message.getEncryption() == Message.ENCRYPTION_AXOLOTL && this.file.getKey() == null) {
this.message.setEncryption(Message.ENCRYPTION_NONE);
}
//TODO add auth tag size to knownFileSize
@ -345,7 +334,7 @@ public class HttpDownloadConnection implements Transferable {
if (Strings.isNullOrEmpty(extension.getExtension()) && contentType != null) {
final String fileExtension = MimeUtils.guessExtensionFromMimeType(contentType);
if (fileExtension != null) {
message.setRelativeFilePath(String.format("%s.%s", message.getUuid(), fileExtension));
mXmppConnectionService.getFileBackend().setupRelativeFilePath(message, String.format("%s.%s", message.getUuid(), fileExtension), contentType);
Log.d(Config.LOGTAG, "rewriting name after not finding extension in url but in content type");
setupFile();
}

View file

@ -46,6 +46,7 @@ import androidx.annotation.StringRes;
import androidx.core.content.FileProvider;
import androidx.exifinterface.media.ExifInterface;
import com.google.common.base.Strings;
import com.google.common.io.ByteStreams;
import java.io.ByteArrayOutputStream;
@ -104,7 +105,7 @@ public class FileBackend {
private static final SimpleDateFormat fileDateFormat = new SimpleDateFormat("yyyyMMdd_HHmmssSSS", Locale.US);
private static final String FILE_PROVIDER = ".files";
public static final String APP_DIRECTORY = "monocles chat";
public static final String APP_DIRECTORY = "blabber.im";
public static final String FILES = "Files";
public static final String SENT_FILES = "Files" + File.separator + "Sent";
public static final String AUDIOS = "Audios";
@ -405,7 +406,7 @@ public class FileBackend {
return getFileForPath(path, MimeUtils.guessMimeTypeFromExtension(MimeUtils.extractRelevantExtension(path)));
}
public DownloadableFile getFileForPath(String path, String mime) {
private DownloadableFile getFileForPath(String path, String mime) {
DownloadableFile file = null;
if (path.startsWith(File.separator)) {
file = new DownloadableFile(path);
@ -700,7 +701,7 @@ public class FileBackend {
extension = "oga";
}
String filename = "Sent" + File.separator + fileDateFormat.format(new Date(message.getTimeSent())) + "_" + message.getUuid().substring(0, 4);
message.setRelativeFilePath(filename + "." + extension);
setupRelativeFilePath(message, String.format("%s.%s", filename, extension));
copyFileToPrivateStorage(mXmppConnectionService.getFileBackend().getFile(message), uri);
}
@ -859,22 +860,53 @@ public class FileBackend {
}
public void copyImageToPrivateStorage(Message message, Uri image) throws FileCopyException, ImageCompressionException {
String filename = "Sent" + File.separator + fileDateFormat.format(new Date(message.getTimeSent())) + "_" + message.getUuid().substring(0, 4);
String filename;
String file = "Sent" + File.separator + fileDateFormat.format(new Date(message.getTimeSent())) + "_" + message.getUuid().substring(0, 4);
switch (Config.IMAGE_FORMAT) {
case JPEG:
message.setRelativeFilePath(filename + ".jpg");
filename = String.format("%s.%s", file, ".jpg");
break;
case PNG:
message.setRelativeFilePath(filename + ".png");
filename = String.format("%s.%s", file, ".png");
break;
case WEBP:
message.setRelativeFilePath(filename + ".webp");
filename = String.format("%s.%s", file, ".webp");
break;
default:
throw new IllegalStateException("Unknown image format");
}
setupRelativeFilePath(message, filename);
copyImageToPrivateStorage(getFile(message), image);
updateFileParams(message);
}
public void setupRelativeFilePath(final Message message, final String filename) {
final String extension = MimeUtils.extractRelevantExtension(filename);
final String mime = MimeUtils.guessMimeTypeFromExtension(extension);
setupRelativeFilePath(message, filename, mime);
}
public File getStorageLocation(final String filename, final String mime) {
final File parentDirectory;
if (Strings.isNullOrEmpty(mime)) {
parentDirectory = getConversationsDirectory(mXmppConnectionService, FILES);
} else if (mime.startsWith("image/")) {
parentDirectory = getConversationsDirectory(mXmppConnectionService, IMAGES);
} else if (mime.startsWith("video/")) {
parentDirectory = getConversationsDirectory(mXmppConnectionService, VIDEOS);
} else if (mime.startsWith("audio/")) {
parentDirectory = getConversationsDirectory(mXmppConnectionService, AUDIOS);
} else {
parentDirectory = getConversationsDirectory(mXmppConnectionService, FILES);
}
return new File(parentDirectory, filename);
}
public void setupRelativeFilePath(final Message message, final String filename, final String mime) {
final File file = getStorageLocation(filename, mime);
message.setRelativeFilePath(file.getAbsolutePath());
}
public void copyFile(File sourceFile, File destFile) throws IOException {
if (!destFile.getParentFile().exists()) {
destFile.getParentFile().mkdirs();

View file

@ -108,7 +108,8 @@ public class AttachFileToConversationRunnable implements Runnable, TranscoderLis
mXmppConnectionService.startForcingForegroundNotification();
isCompressingVideo = conversation.getUuid();
final SimpleDateFormat fileDateFormat = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US);
message.setRelativeFilePath("Sent/" + fileDateFormat.format(new Date(message.getTimeSent())) + "_" + message.getUuid().substring(0, 4) + "_komp.mp4");
final String filename = "Sent/" + fileDateFormat.format(new Date(message.getTimeSent())) + "_" + message.getUuid().substring(0, 4) + "_komp";
mXmppConnectionService.getFileBackend().setupRelativeFilePath(message, String.format("%s.%s", filename, "mp4"));
final DownloadableFile file = mXmppConnectionService.getFileBackend().getFile(message);
if (Objects.requireNonNull(file.getParentFile()).mkdirs()) {
Log.d(Config.LOGTAG, "created parent directory for video file");

View file

@ -3,13 +3,10 @@ package eu.siacs.conversations.ui.util;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.util.Log;
import java.io.File;
import java.util.List;
import eu.siacs.conversations.Config;
import eu.siacs.conversations.R;
@ -39,7 +36,8 @@ public class ViewUtil {
}
public static void view(Context context, File file, String mime) {
Uri uri;
Log.d(Config.LOGTAG, "viewing " + file.getAbsolutePath() + " " + mime);
final Uri uri;
try {
uri = FileBackend.getUriForFile(context, file);
} catch (SecurityException e) {
@ -49,35 +47,28 @@ public class ViewUtil {
}
// use internal viewer for images and videos
if (mime.startsWith("image/")) {
Intent intent = new Intent(context, MediaViewerActivity.class);
final Intent intent = new Intent(context, MediaViewerActivity.class);
intent.putExtra("image", Uri.fromFile(file));
try {
context.startActivity(intent);
return;
} catch (ActivityNotFoundException e) {
//ignored
}
} else if (mime.startsWith("video/")) {
Intent intent = new Intent(context, MediaViewerActivity.class);
final Intent intent = new Intent(context, MediaViewerActivity.class);
intent.putExtra("video", Uri.fromFile(file));
try {
context.startActivity(intent);
return;
} catch (ActivityNotFoundException e) {
//ignored
}
} else {
Intent openIntent = new Intent(Intent.ACTION_VIEW);
final Intent openIntent = new Intent(Intent.ACTION_VIEW);
openIntent.setDataAndType(uri, mime);
openIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
PackageManager manager = context.getPackageManager();
List<ResolveInfo> info = manager.queryIntentActivities(openIntent, 0);
if (info.size() == 0) {
openIntent.setDataAndType(uri, "*/*");
}
try {
context.startActivity(openIntent);
} catch (ActivityNotFoundException e) {
} catch (final ActivityNotFoundException e) {
ToastCompat.makeText(context, R.string.no_application_found_to_open_file, ToastCompat.LENGTH_SHORT).show();
}
}

View file

@ -492,26 +492,32 @@ public class JingleFileTransferConnection extends AbstractJingleConnection imple
AbstractConnectionManager.Extension extension = AbstractConnectionManager.Extension.of(path);
if (VALID_IMAGE_EXTENSIONS.contains(extension.main)) {
message.setType(Message.TYPE_IMAGE);
final String filename;
if (message.getStatus() == Message.STATUS_RECEIVED) {
message.setRelativeFilePath(fileDateFormat.format(new Date(message.getTimeSent())) + "_" + message.getUuid().substring(0, 4) + "." + extension.main);
filename = fileDateFormat.format(new Date(message.getTimeSent())) + "_" + message.getUuid().substring(0, 4) + "." + extension.main;
} else {
message.setRelativeFilePath("Sent/" + fileDateFormat.format(new Date(message.getTimeSent())) + "_" + message.getUuid().substring(0, 4) + "." + extension.main);
filename = "Sent/" + fileDateFormat.format(new Date(message.getTimeSent())) + "_" + message.getUuid().substring(0, 4) + "." + extension.main;
}
xmppConnectionService.getFileBackend().setupRelativeFilePath(message, filename);
} else if (VALID_CRYPTO_EXTENSIONS.contains(extension.main)) {
if (VALID_IMAGE_EXTENSIONS.contains(extension.secondary)) {
message.setType(Message.TYPE_IMAGE);
final String filename;
if (message.getStatus() == Message.STATUS_RECEIVED) {
message.setRelativeFilePath(fileDateFormat.format(new Date(message.getTimeSent())) + "_" + message.getUuid().substring(0, 4) + "." + extension.secondary);
filename = fileDateFormat.format(new Date(message.getTimeSent())) + "_" + message.getUuid().substring(0, 4) + "." + extension.secondary;
} else {
message.setRelativeFilePath("Sent/" + fileDateFormat.format(new Date(message.getTimeSent())) + "_" + message.getUuid().substring(0, 4) + "." + extension.secondary);
filename = "Sent/" + fileDateFormat.format(new Date(message.getTimeSent())) + "_" + message.getUuid().substring(0, 4) + "." + extension.secondary;
}
xmppConnectionService.getFileBackend().setupRelativeFilePath(message, filename);
} else {
message.setType(Message.TYPE_FILE);
final String filename;
if (message.getStatus() == Message.STATUS_RECEIVED) {
message.setRelativeFilePath(fileDateFormat.format(new Date(message.getTimeSent())) + "_" + message.getUuid().substring(0, 4) + (extension.secondary != null ? ("." + extension.secondary) : ""));
filename = fileDateFormat.format(new Date(message.getTimeSent())) + "_" + message.getUuid().substring(0, 4) + (extension.secondary != null ? ("." + extension.secondary) : "");
} else {
message.setRelativeFilePath("Sent/" + fileDateFormat.format(new Date(message.getTimeSent())) + "_" + message.getUuid().substring(0, 4) + (extension.secondary != null ? ("." + extension.secondary) : ""));
filename = "Sent/" + fileDateFormat.format(new Date(message.getTimeSent())) + "_" + message.getUuid().substring(0, 4) + (extension.secondary != null ? ("." + extension.secondary) : "");
}
xmppConnectionService.getFileBackend().setupRelativeFilePath(message, filename);
}
// only for OTR compatibility
if (extension.main.equals("otr")) {
@ -521,11 +527,13 @@ public class JingleFileTransferConnection extends AbstractJingleConnection imple
}
} else {
message.setType(Message.TYPE_FILE);
final String filename;
if (message.getStatus() == Message.STATUS_RECEIVED) {
message.setRelativeFilePath(fileDateFormat.format(new Date(message.getTimeSent())) + "_" + message.getUuid().substring(0, 4) + (extension.main != null ? ("." + extension.main) : ""));
filename = fileDateFormat.format(new Date(message.getTimeSent())) + "_" + message.getUuid().substring(0, 4) + (extension.main != null ? ("." + extension.main) : "");
} else {
message.setRelativeFilePath("Sent/" + fileDateFormat.format(new Date(message.getTimeSent())) + "_" + message.getUuid().substring(0, 4) + (extension.main != null ? ("." + extension.main) : ""));
filename = "Sent/" + fileDateFormat.format(new Date(message.getTimeSent())) + "_" + message.getUuid().substring(0, 4) + (extension.main != null ? ("." + extension.main) : "");
}
xmppConnectionService.getFileBackend().setupRelativeFilePath(message, filename);
}
long size = parseLong(fileSize, 0);
message.setBody(Long.toString(size));