Ensure exists directory

pull/209/head
M66B 3 years ago
parent bdfb324441
commit ff0f2e9ec0

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

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

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

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

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

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

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

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

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

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

@ -149,10 +149,13 @@ import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.Date; import java.util.Date;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.concurrent.BlockingQueue; import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.LinkedBlockingQueue;
@ -2224,6 +2227,22 @@ public class Helper {
public static native void sync(); 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) { static String sanitizeFilename(String name) {
if (name == null) if (name == null)
return null; return null;

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

Loading…
Cancel
Save