use full file name for all new files

This commit is contained in:
Daniel Gultsch 2022-02-22 16:05:02 +01:00
parent 8abacd23e8
commit d6be6ddd18
6 changed files with 53 additions and 29 deletions

View file

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

View file

@ -96,11 +96,8 @@ public class HttpDownloadConnection implements Transferable {
this.message.setEncryption(Message.ENCRYPTION_NONE); this.message.setEncryption(Message.ENCRYPTION_NONE);
} }
final String ext = extension.getExtension(); final String ext = extension.getExtension();
if (ext != null) { final String filename = Strings.isNullOrEmpty(ext) ? message.getUuid() : String.format("%s.%s", message.getUuid(), ext);
message.setRelativeFilePath(String.format("%s.%s", message.getUuid(), ext)); mXmppConnectionService.getFileBackend().setupRelativeFilePath(message, filename);
} else if (Strings.isNullOrEmpty(message.getRelativeFilePath())) {
message.setRelativeFilePath(message.getUuid());
}
setupFile(); setupFile();
if (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); this.message.setEncryption(Message.ENCRYPTION_NONE);
@ -326,7 +323,7 @@ public class HttpDownloadConnection implements Transferable {
if (Strings.isNullOrEmpty(extension.getExtension()) && contentType != null) { if (Strings.isNullOrEmpty(extension.getExtension()) && contentType != null) {
final String fileExtension = MimeUtils.guessExtensionFromMimeType(contentType); final String fileExtension = MimeUtils.guessExtensionFromMimeType(contentType);
if (fileExtension != null) { 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"); Log.d(Config.LOGTAG, "rewriting name after not finding extension in url but in content type");
setupFile(); setupFile();
} }

View file

@ -533,7 +533,7 @@ public class FileBackend {
MimeUtils.guessMimeTypeFromExtension(MimeUtils.extractRelevantExtension(path))); MimeUtils.guessMimeTypeFromExtension(MimeUtils.extractRelevantExtension(path)));
} }
public DownloadableFile getFileForPath(final String path, final String mime) { private DownloadableFile getFileForPath(final String path, final String mime) {
if (path.startsWith("/")) { if (path.startsWith("/")) {
return new DownloadableFile(path); return new DownloadableFile(path);
} else { } else {
@ -719,7 +719,7 @@ public class FileBackend {
if ("ogg".equals(extension) && type != null && type.startsWith("audio/")) { if ("ogg".equals(extension) && type != null && type.startsWith("audio/")) {
extension = "oga"; extension = "oga";
} }
message.setRelativeFilePath(message.getUuid() + "." + extension); setupRelativeFilePath(message, String.format("%s.%s", message.getUuid(), extension));
copyFileToPrivateStorage(mXmppConnectionService.getFileBackend().getFile(message), uri); copyFileToPrivateStorage(mXmppConnectionService.getFileBackend().getFile(message), uri);
} }
@ -852,21 +852,51 @@ public class FileBackend {
public void copyImageToPrivateStorage(Message message, Uri image) public void copyImageToPrivateStorage(Message message, Uri image)
throws FileCopyException, ImageCompressionException { throws FileCopyException, ImageCompressionException {
final String filename;
switch (Config.IMAGE_FORMAT) { switch (Config.IMAGE_FORMAT) {
case JPEG: case JPEG:
message.setRelativeFilePath(message.getUuid() + ".jpg"); filename = String.format("%s.%s", message.getUuid(), "jpg");
break; break;
case PNG: case PNG:
message.setRelativeFilePath(message.getUuid() + ".png"); filename = String.format("%s.%s", message.getUuid(), "png");
break; break;
case WEBP: case WEBP:
message.setRelativeFilePath(message.getUuid() + ".webp"); filename = String.format("%s.%s", message.getUuid(), "webp");
break; break;
default:
throw new IllegalStateException("Unknown image format");
} }
setupRelativeFilePath(message, filename);
copyImageToPrivateStorage(getFile(message), image); copyImageToPrivateStorage(getFile(message), image);
updateFileParams(message); 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 = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
} else if (mime.startsWith("image/")) {
parentDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
} else if (mime.startsWith("video/")) {
parentDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES);
} else {
parentDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
}
final File appDirectory = new File(parentDirectory, mXmppConnectionService.getString(R.string.app_name));
return new File(appDirectory, filename);
}
public void setupRelativeFilePath(final Message message, final String filename, final String mime) {
final File file = getStorageLocation(filename, mime);
message.setRelativeFilePath(file.getAbsolutePath());
}
public boolean unusualBounds(final Uri image) { public boolean unusualBounds(final Uri image) {
try { try {
final BitmapFactory.Options options = new BitmapFactory.Options(); final BitmapFactory.Options options = new BitmapFactory.Options();

View file

@ -91,7 +91,7 @@ public class AttachFileToConversationRunnable implements Runnable, TranscoderLis
private void processAsVideo() throws FileNotFoundException { private void processAsVideo() throws FileNotFoundException {
Log.d(Config.LOGTAG, "processing file as video"); Log.d(Config.LOGTAG, "processing file as video");
mXmppConnectionService.startForcingForegroundNotification(); mXmppConnectionService.startForcingForegroundNotification();
message.setRelativeFilePath(message.getUuid() + ".mp4"); mXmppConnectionService.getFileBackend().setupRelativeFilePath(message, String.format("%s.%s", message.getUuid(), "mp4"));
final DownloadableFile file = mXmppConnectionService.getFileBackend().getFile(message); final DownloadableFile file = mXmppConnectionService.getFileBackend().getFile(message);
if (Objects.requireNonNull(file.getParentFile()).mkdirs()) { if (Objects.requireNonNull(file.getParentFile()).mkdirs()) {
Log.d(Config.LOGTAG, "created parent directory for video file"); Log.d(Config.LOGTAG, "created parent directory for video file");

View file

@ -37,9 +37,10 @@ public class ViewUtil {
view(context, file, mime); view(context, file, mime);
} }
public static void view(Context context, File file, String mime) { private static void view(Context context, File file, String mime) {
Intent openIntent = new Intent(Intent.ACTION_VIEW); Log.d(Config.LOGTAG,"viewing "+file.getAbsolutePath()+" "+mime);
Uri uri; final Intent openIntent = new Intent(Intent.ACTION_VIEW);
final Uri uri;
try { try {
uri = FileBackend.getUriForFile(context, file); uri = FileBackend.getUriForFile(context, file);
} catch (SecurityException e) { } catch (SecurityException e) {
@ -49,14 +50,9 @@ public class ViewUtil {
} }
openIntent.setDataAndType(uri, mime); openIntent.setDataAndType(uri, mime);
openIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); openIntent.addFlags(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 { try {
context.startActivity(openIntent); context.startActivity(openIntent);
} catch (ActivityNotFoundException e) { } catch (final ActivityNotFoundException e) {
Toast.makeText(context, R.string.no_application_found_to_open_file, Toast.LENGTH_SHORT).show(); Toast.makeText(context, R.string.no_application_found_to_open_file, Toast.LENGTH_SHORT).show();
} }
} }

View file

@ -492,19 +492,19 @@ public class JingleFileTransferConnection extends AbstractJingleConnection imple
AbstractConnectionManager.Extension extension = AbstractConnectionManager.Extension.of(path); AbstractConnectionManager.Extension extension = AbstractConnectionManager.Extension.of(path);
if (VALID_IMAGE_EXTENSIONS.contains(extension.main)) { if (VALID_IMAGE_EXTENSIONS.contains(extension.main)) {
message.setType(Message.TYPE_IMAGE); message.setType(Message.TYPE_IMAGE);
message.setRelativeFilePath(message.getUuid() + "." + extension.main); xmppConnectionService.getFileBackend().setupRelativeFilePath(message, message.getUuid() + "." + extension.main);
} else if (VALID_CRYPTO_EXTENSIONS.contains(extension.main)) { } else if (VALID_CRYPTO_EXTENSIONS.contains(extension.main)) {
if (VALID_IMAGE_EXTENSIONS.contains(extension.secondary)) { if (VALID_IMAGE_EXTENSIONS.contains(extension.secondary)) {
message.setType(Message.TYPE_IMAGE); message.setType(Message.TYPE_IMAGE);
message.setRelativeFilePath(message.getUuid() + "." + extension.secondary); xmppConnectionService.getFileBackend().setupRelativeFilePath(message,message.getUuid() + "." + extension.secondary);
} else { } else {
message.setType(Message.TYPE_FILE); message.setType(Message.TYPE_FILE);
message.setRelativeFilePath(message.getUuid() + (extension.secondary != null ? ("." + extension.secondary) : "")); xmppConnectionService.getFileBackend().setupRelativeFilePath(message,message.getUuid() + (extension.secondary != null ? ("." + extension.secondary) : ""));
} }
message.setEncryption(Message.ENCRYPTION_PGP); message.setEncryption(Message.ENCRYPTION_PGP);
} else { } else {
message.setType(Message.TYPE_FILE); message.setType(Message.TYPE_FILE);
message.setRelativeFilePath(message.getUuid() + (extension.main != null ? ("." + extension.main) : "")); xmppConnectionService.getFileBackend().setupRelativeFilePath(message,message.getUuid() + (extension.main != null ? ("." + extension.main) : ""));
} }
long size = parseLong(fileSize, 0); long size = parseLong(fileSize, 0);
message.setBody(Long.toString(size)); message.setBody(Long.toString(size));