Skip to content

Instantly share code, notes, and snippets.

@abhinav272
Created June 6, 2018 10:11
Show Gist options
  • Save abhinav272/75c8c3a7d9c173eac5a7002e48927082 to your computer and use it in GitHub Desktop.
Save abhinav272/75c8c3a7d9c173eac5a7002e48927082 to your computer and use it in GitHub Desktop.
Normal FileUtils class
public class FileUtils {
private static final String F_COLLAB_EXT = "collab";
private static final String F_ARGUMENT_EXT = "argument";
private static final String F_COMMENTS_EXT = "comment";
private static final String F_CHUNKS_EXT = "chunks";
private static final String F_COMBINED_EXT = "combined";
private static final String F_FLYER_EXT = "flyers";
private static final String F_PROCESS_VID_EXT = "video";
private static FileUtils fileUtils;
private Context context;
private FileUtils(Context context) {
this.context = context;
}
public static FileUtils getInstance(Context context) {
if (fileUtils == null) {
synchronized (FileUtils.class) {
if (fileUtils == null)
fileUtils = new FileUtils(context);
}
}
return fileUtils;
}
private File getCollabChunkDir() {
File filesDir = context.getFilesDir();
File collabDir = new File(filesDir, F_COLLAB_EXT);
File collabChunkDir = new File(collabDir, F_CHUNKS_EXT);
if (!collabChunkDir.exists()) {
collabChunkDir.mkdirs();
}
return collabChunkDir;
}
private File getCollabCombinedDir() {
File filesDir = context.getFilesDir();
File collabDir = new File(filesDir, F_COLLAB_EXT);
File collabCombinedDir = new File(collabDir, F_COMBINED_EXT);
if (!collabCombinedDir.exists()) {
collabCombinedDir.mkdirs();
}
return collabCombinedDir;
}
private File getArgumentChunkDir() {
File filesDir = context.getFilesDir();
File argumentDir = new File(filesDir, F_ARGUMENT_EXT);
File argumentChunkDir = new File(argumentDir, F_CHUNKS_EXT);
if (!argumentChunkDir.exists()) {
argumentChunkDir.mkdirs();
}
return argumentChunkDir;
}
private File getArgumentCombinedDir() {
File filesDir = context.getFilesDir();
File argumentDir = new File(filesDir, F_ARGUMENT_EXT);
File argumentCombinedDir = new File(argumentDir, F_COMBINED_EXT);
if (!argumentCombinedDir.exists()) {
argumentCombinedDir.mkdirs();
}
return argumentCombinedDir;
}
private File getCommentChunkDir() {
File filesDir = context.getFilesDir();
File commentDir = new File(filesDir, F_COMMENTS_EXT);
File commentChunkDir = new File(commentDir, F_CHUNKS_EXT);
if (!commentChunkDir.exists()) {
commentChunkDir.mkdirs();
}
return commentChunkDir;
}
private File getCommentCombinedDir() {
File filesDir = context.getFilesDir();
File commentDir = new File(filesDir, F_COMMENTS_EXT);
File commentCombinedDir = new File(commentDir, F_COMBINED_EXT);
if (!commentCombinedDir.exists()) {
commentCombinedDir.mkdirs();
}
return commentCombinedDir;
}
private File getFlyerDir() {
File filesDir = context.getFilesDir();
File flyerDir = new File(filesDir, F_FLYER_EXT);
if (!flyerDir.exists()) {
flyerDir.mkdirs();
}
return flyerDir;
}
private File getProcessedVideoDir() {
File filesDir = context.getFilesDir();
File processVidDir = new File(filesDir, F_PROCESS_VID_EXT);
if (!processVidDir.exists()) {
processVidDir.mkdirs();
}
return processVidDir;
}
public File makeTempCollabChunkFile() {
File collabChunkDir = getCollabChunkDir();
String filePrefix = "WHZCHUNK_";
String fileExt = ".mp4";
File tempChunk = new File(collabChunkDir, filePrefix + fileExt);
int fileNo = 0;
while (tempChunk.exists()) {
fileNo++;
tempChunk = new File(collabChunkDir, filePrefix + fileNo + fileExt);
}
return tempChunk;
}
public File makeCombinedCollabFile() {
File collabCombinedDir = getCollabCombinedDir();
String filePrefix = "WHZCOMBINED_";
String fileExt = ".mp4";
File combinedFile = new File(collabCombinedDir, filePrefix + fileExt);
int fileNo = 0;
while (combinedFile.exists()) {
fileNo++;
combinedFile = new File(collabCombinedDir, filePrefix + fileNo + fileExt);
}
return combinedFile;
}
public File makeTempArgumentChunkFile() {
File argumentChunkDir = getArgumentChunkDir();
String filePrefix = "WHZCHUNK_";
String fileExt = ".mp4";
File tempChunk = new File(argumentChunkDir, filePrefix + fileExt);
int fileNo = 0;
while (tempChunk.exists()) {
fileNo++;
tempChunk = new File(argumentChunkDir, filePrefix + fileNo + fileExt);
}
return tempChunk;
}
public File makeCombinedArgumentFile() {
File argumentCombinedDir = getArgumentCombinedDir();
String filePrefix = "WHZCOMBINED_";
String fileExt = ".mp4";
File combinedFile = new File(argumentCombinedDir, filePrefix + fileExt);
int fileNo = 0;
while (combinedFile.exists()) {
fileNo++;
combinedFile = new File(argumentCombinedDir, filePrefix + fileNo + fileExt);
}
return combinedFile;
}
public File makeTempCommentChunkFile() {
File commentChunkDir = getCommentChunkDir();
String filePrefix = "WHZCHUNK_";
String fileExt = ".mp4";
File tempChunk = new File(commentChunkDir, filePrefix + fileExt);
int fileNo = 0;
while (tempChunk.exists()) {
fileNo++;
tempChunk = new File(commentChunkDir, filePrefix + fileNo + fileExt);
}
return tempChunk;
}
public File makeCombinedCommentFile() {
File commentCombinedDir = getCommentCombinedDir();
String filePrefix = "WHZCOMBINED_";
String fileExt = ".mp4";
File combinedFile = new File(commentCombinedDir, filePrefix + fileExt);
int fileNo = 0;
while (combinedFile.exists()) {
fileNo++;
combinedFile = new File(commentCombinedDir, filePrefix + fileNo + fileExt);
}
return combinedFile;
}
public File makeFlyerFile() {
File flyerDir = getFlyerDir();
String filePrefix = "WHZFLYER";
String fileExt = ".jpg";
return new File(flyerDir, filePrefix + fileExt);
}
public File makeProcessVidFile() {
File processVidDir = getProcessedVideoDir();
String filePrefix = "WHZPROCESSVID";
String fileExt = ".mp4";
File processVid = new File(processVidDir, filePrefix + fileExt);
// int fileNo = 0;
// while (processVid.exists()) {
// fileNo++;
// processVid = new File(processVidDir, filePrefix + fileNo + fileExt);
// }
return processVid;
}
public void clearArgumentFiles() {
File argumentChunkDir = getArgumentChunkDir();
File argumentCombinedDir = getArgumentCombinedDir();
try {
deleteRecursive(argumentChunkDir);
deleteRecursive(argumentCombinedDir);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public void clearCollabFiles() {
File collabChunkDir = getCollabChunkDir();
File collabCombinedDir = getCollabCombinedDir();
try {
deleteRecursive(collabChunkDir);
deleteRecursive(collabCombinedDir);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public void clearCommentFiles() {
File commentChunkDir = getCommentChunkDir();
File commentCombinedDir = getCommentCombinedDir();
try {
deleteRecursive(commentChunkDir);
deleteRecursive(commentCombinedDir);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
private boolean deleteRecursive(File path) throws FileNotFoundException {
if (!path.exists()) throw new FileNotFoundException(path.getAbsolutePath());
boolean ret = true;
if (path.isDirectory()) {
for (File f : path.listFiles()) {
ret = ret && deleteRecursive(f);
}
}
return ret && path.delete();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment