Skip to content

Instantly share code, notes, and snippets.

@InsanusMokrassar
Last active April 20, 2017 05:38
Show Gist options
  • Save InsanusMokrassar/28afe4435284315b3bbee1ec53eb5b8f to your computer and use it in GitHub Desktop.
Save InsanusMokrassar/28afe4435284315b3bbee1ec53eb5b8f to your computer and use it in GitHub Desktop.
It is common and simple files server which must work in all your java projects if you have internet access and know destination url
public class FilesSaver {
protected static String absolutePath;
public static void setAbsolutePath(String absolutePath) {
FilesSaver.absolutePath = absolutePath;
}
public static String constructPath(String relationPath) {
while (relationPath.matches("^/.*$")) {
relationPath = relationPath.replaceFirst("/", "");
}
return String.format(
"%s/%s",
absolutePath,
relationPath
);
}
public static File getFile(String relationPath) {
File result = new File(constructPath(relationPath));
if (!result.exists()) {
return null;
}
return result;
}
public static void downloadFile(URL whereFrom, String relationPathToSave, Boolean overwrite) {
File result = new File(constructPath(relationPathToSave));
Boolean completeLoad = result.exists() && !overwrite;
try {
if (!completeLoad) {
completeLoad = false;
result = createFile(relationPathToSave);
InputStream is = (InputStream)whereFrom.getContent();
FileOutputStream fileOutputStream = new FileOutputStream(result);
int read = -1;
while ((read = is.read()) != -1) {
fileOutputStream.write(read);
}
is.close();
fileOutputStream.flush();
fileOutputStream.close();
completeLoad = true;
}
} catch (IOException e) {
System.err.println("E\\" + FilesSaver.class.getSimpleName() + ": Can't get content using url: " + whereFrom.toString());
e.printStackTrace();
} catch (NullPointerException e) {
System.err.println("E\\" + FilesSaver.class.getSimpleName() + ": Object must be init");
e.printStackTrace();
} finally {
if (!completeLoad && result.exists()) {
result.delete();
}
}
}
public static File getOrDownload(URL whereFrom, String relationPath, Boolean overwrite) {
File result = getFile(relationPath);
if (result == null) {
downloadFile(whereFrom, relationPath, overwrite);
result = getFile(relationPath);
}
return result;
}
public static File getOrDownload(URL whereFrom, Boolean overwrite) {
String relationPath = whereFrom.getPath();
File result = getFile(relationPath);
if (result == null) {
downloadFile(whereFrom, relationPath, overwrite);
result = getFile(relationPath);
}
return result;
}
public static Uri getUriOrDownload(String what, Boolean overwrite) {
return Uri.fromFile(getOrDownload(what, overwrite));
}
public static Uri getFileUri(String relationPath) {
return new Uri.Builder().appendPath(constructPath(relationPath)).build();
}
public static File createFile(String relationFilePath) {
String fullPath = constructPath(relationFilePath);
File result = new File(fullPath);
String fileName = result.getName();
result = new File(fullPath.replace(fileName, ""));
result.mkdirs();
result = new File(fullPath);
try {
result.createNewFile();
} catch (IOException e) {
e.printStackTrace();
return null;
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment