Ensure exists directory

This commit is contained in:
M66B 2022-10-06 14:43:44 +02:00
parent bdfb324441
commit ff0f2e9ec0
12 changed files with 39 additions and 62 deletions

View File

@ -511,10 +511,7 @@ abstract class ActivityBase extends AppCompatActivity implements SharedPreferenc
if (TextUtils.isEmpty(fname))
return uri;
File dir = new File(getFilesDir(), "shared");
if (!dir.exists())
dir.mkdir();
File dir = Helper.ensureExists(new File(getFilesDir(), "shared"));
File file = new File(dir, fname);
Log.i("Copying shared file to " + file);

View File

@ -182,10 +182,7 @@ public class ActivityEML extends ActivityBase {
if (uri == null)
throw new FileNotFoundException();
File dir = new File(context.getFilesDir(), "shared");
if (!dir.exists())
dir.mkdir();
File dir = Helper.ensureExists(new File(context.getFilesDir(), "shared"));
File file = new File(dir, "email.eml");
Helper.copy(context, uri, file);

View File

@ -132,10 +132,7 @@ public class AdapterCertificate extends RecyclerView.Adapter<AdapterCertificate.
if (certificate == null)
return null;
File dir = new File(context.getFilesDir(), "shared");
if (!dir.exists())
dir.mkdir();
File dir = Helper.ensureExists(new File(context.getFilesDir(), "shared"));
String name = Helper.sanitizeFilename(certificate.email);
File file = new File(dir, name + ".pem");
Helper.writeText(file, certificate.getPem());

View File

@ -3757,9 +3757,7 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
response.setMethod(Method.REPLY);
response.addEvent(ev);
File dir = new File(context.getFilesDir(), "calendar");
if (!dir.exists())
dir.mkdir();
File dir = Helper.ensureExists(new File(context.getFilesDir(), "calendar"));
File ics = new File(dir, message.id + ".ics");
response.write(ics);
@ -6756,10 +6754,7 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
element.attr("x-computed", computed);
}
File dir = new File(context.getFilesDir(), "shared");
if (!dir.exists())
dir.mkdir();
File dir = Helper.ensureExists(new File(context.getFilesDir(), "shared"));
File share = new File(dir, message.id + ".txt");
Helper.writeText(share, d.html());

View File

@ -340,9 +340,7 @@ public class ContactInfo {
final String domain = d.toLowerCase(Locale.ROOT);
final String email = info.email.toLowerCase(Locale.ROOT);
File dir = new File(context.getFilesDir(), "favicons");
if (!dir.exists())
dir.mkdir();
File dir = Helper.ensureExists(new File(context.getFilesDir(), "favicons"));
try {
// check cache
@ -511,10 +509,7 @@ public class ContactInfo {
// Generated
boolean identicon = false;
if (info.bitmap == null && generated && !TextUtils.isEmpty(info.email)) {
File dir = new File(context.getFilesDir(), "generated");
if (!dir.exists())
dir.mkdir();
File dir = Helper.ensureExists(new File(context.getFilesDir(), "generated"));
File[] files = dir.listFiles(new FilenameFilter() {
@Override
public boolean accept(File file, String name) {

View File

@ -150,9 +150,7 @@ public class EntityAttachment {
}
static File getFile(Context context, long id, String name) {
File dir = new File(getRoot(context), "attachments");
if (!dir.exists())
dir.mkdir();
File dir = Helper.ensureExists(new File(getRoot(context), "attachments"));
String filename = Long.toString(id);
if (!TextUtils.isEmpty(name))
filename += "." + Helper.sanitizeFilename(name);

View File

@ -577,9 +577,7 @@ public class EntityMessage implements Serializable {
}
static File getFile(Context context, Long id) {
File dir = new File(context.getFilesDir(), "messages");
if (!dir.exists())
dir.mkdir();
File dir = Helper.ensureExists(new File(context.getFilesDir(), "messages"));
return new File(dir, id.toString());
}
@ -588,16 +586,12 @@ public class EntityMessage implements Serializable {
}
File getFile(Context context, int revision) {
File dir = new File(context.getFilesDir(), "revision");
if (!dir.exists())
dir.mkdir();
File dir = Helper.ensureExists(new File(context.getFilesDir(), "revision"));
return new File(dir, id + "." + revision);
}
File getRefFile(Context context) {
File dir = new File(context.getFilesDir(), "references");
if (!dir.exists())
dir.mkdir();
File dir = Helper.ensureExists(new File(context.getFilesDir(), "references"));
return new File(dir, id.toString());
}
@ -606,9 +600,7 @@ public class EntityMessage implements Serializable {
}
static File getRawFile(Context context, Long id) {
File dir = new File(context.getFilesDir(), "raw");
if (!dir.exists())
dir.mkdir();
File dir = Helper.ensureExists(new File(context.getFilesDir(), "raw"));
return new File(dir, id + ".eml");
}

View File

@ -2412,7 +2412,7 @@ public class FragmentCompose extends FragmentBase {
if (s > 1 && s <= edit.length() &&
edit.charAt(s - 1) == '\n' &&
edit.charAt(s - 2) != '\n' &&
(s == edit.length() || edit.charAt(s) == '\n') )
(s == edit.length() || edit.charAt(s) == '\n'))
etBody.setSelection(s - 1, s - 1);
Pair<Integer, Integer> paragraph = StyleHelper.getParagraph(etBody);
@ -3112,9 +3112,7 @@ public class FragmentCompose extends FragmentBase {
});
snackbar.show();
} else {
File dir = new File(context.getFilesDir(), "photo");
if (!dir.exists())
dir.mkdir();
File dir = Helper.ensureExists(new File(context.getFilesDir(), "photo"));
File file = new File(dir, working + ".jpg");
try {
photoURI = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID, file);
@ -3410,9 +3408,7 @@ public class FragmentCompose extends FragmentBase {
throw new IllegalArgumentException(context.getString(R.string.title_from_missing));
// Create files
File tmp = new File(context.getFilesDir(), "encryption");
if (!tmp.exists())
tmp.mkdir();
File tmp = Helper.ensureExists(new File(context.getFilesDir(), "encryption"));
File input = new File(tmp, draft.id + "_" + session + ".pgp_input");
File output = new File(tmp, draft.id + "_" + session + ".pgp_output");
@ -3759,9 +3755,7 @@ public class FragmentCompose extends FragmentBase {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean check_certificate = prefs.getBoolean("check_certificate", true);
File tmp = new File(context.getFilesDir(), "encryption");
if (!tmp.exists())
tmp.mkdir();
File tmp = Helper.ensureExists(new File(context.getFilesDir(), "encryption"));
DB db = DB.getInstance(context);

View File

@ -8113,9 +8113,7 @@ public class FragmentMessages extends FragmentBase
OutputStream out = null;
boolean inline = false;
File tmp = new File(context.getFilesDir(), "encryption");
if (!tmp.exists())
tmp.mkdir();
File tmp = Helper.ensureExists(new File(context.getFilesDir(), "encryption"));
File plain = new File(tmp, message.id + ".pgp_out");
// Find encrypted data

View File

@ -1108,11 +1108,8 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc
? context.getExternalFilesDir(null)
: context.getFilesDir());
source = new File(source, "attachments");
target = new File(target, "attachments");
source.mkdirs();
target.mkdirs();
source = Helper.ensureExists(new File(source, "attachments"));
target = Helper.ensureExists(new File(target, "attachments"));
File[] attachments = source.listFiles();
if (attachments != null)

View File

@ -149,10 +149,13 @@ import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
@ -2224,6 +2227,22 @@ public class Helper {
public static native void sync();
private static final Map<File, Boolean> exists = new HashMap<>();
static File ensureExists(File dir) {
synchronized (exists) {
if (exists.containsKey(dir))
return dir;
exists.put(dir, true);
}
if (!dir.exists())
if (!dir.mkdirs())
Log.e("Cannot create directory=" + dir);
return dir;
}
static String sanitizeFilename(String name) {
if (name == null)
return null;

View File

@ -804,9 +804,7 @@ class ImageHelper {
@NonNull
static File getCacheFile(Context context, long id, String source, String extension) {
File dir = new File(context.getFilesDir(), "images");
if (!dir.exists())
dir.mkdir();
File dir = Helper.ensureExists(new File(context.getFilesDir(), "images"));
return new File(dir, id + "_" + Math.abs(source.hashCode()) + extension);
}