package saveReddit.main;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.util.LinkedList;
import java.util.regex.Pattern;
import saveReddit.ext.imageExt;
import saveReddit.gui.MainWindow;
public class fileIO {
/**
* @author Pascal Romahn
* @Version Version 0.1
*/
private MainWindow mw;
private MainApplication main;
private imageExt imageExt;
private String settingsFolderPath;
private String extFolderPath;
private String usersPath;
private String lastSavePath;
private String extFilePath;
private File extFile;
private File userFile;
private File lastSaveFile;
private boolean overwrite;
public fileIO(MainWindow pMW, MainApplication pMain) {
mw = pMW;
main = pMain;
settingsFolderPath = "settings";
extFolderPath = settingsFolderPath + "/Extensions";
usersPath = settingsFolderPath + "/users.txt";
lastSavePath = settingsFolderPath + "/lastSave.txt";
extFilePath = extFolderPath + "/imageExt.txt";
extFile = new File(extFilePath);
userFile = new File(usersPath);
lastSaveFile = new File(lastSavePath);
try {
this.initFileSystem();
} catch (IOException e1) {
mw.mainOutputAddLine("Init file system failed!");
e1.printStackTrace();
}
try {
imageExt = new imageExt(mw, this);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void initFileSystem() throws IOException {
//Check for settingsFolder
if(this.checkFile(settingsFolderPath) == true) {
//Check for extFolder
if(this.checkFile(extFolderPath) == true) {
//Check for imageExt
if(this.checkFile(extFilePath) == true) {
} else {
//Create extFile
if(extFile.createNewFile() == true) {
mw.mainOutputAddLine(extFilePath + " created");
this.initFileSystem();
} else {
mw.mainOutputAddLine("Creating " + extFilePath + " failed!");
}
}
} else {
//Create extFolder
if(this.booleanCreateDir(extFolderPath) == true) {
mw.mainOutputAddLine("Created dir " + extFolderPath);
this.initFileSystem();
} else {
mw.mainOutputAddLine("Creating " + extFolderPath + "failed!");
}
}
//Check for userFile
if(this.checkFile(usersPath) == true) {
} else {
//Create userFile
if(userFile.createNewFile() == true) {
mw.mainOutputAddLine(usersPath + " created");
} else {
mw.mainOutputAddLine("Creating " + usersPath + " failed!");
}
}
//Check for lastSaveFile
if(this.checkFile(lastSavePath) == true) {
} else {
//Create lastSaveFile
if(lastSaveFile.createNewFile() == true) {
mw.mainOutputAddLine(lastSavePath + " created");
} else {
mw.mainOutputAddLine("Creating " + lastSavePath + " failed!");
}
}
} else {
//Create settingsFolderPath
if(this.booleanCreateDir(settingsFolderPath) == true) {
mw.mainOutputAddLine("Created dir " + settingsFolderPath);
this.initFileSystem();
} else {
mw.mainOutputAddLine("Creating " + settingsFolderPath + "failed!");
}
}
}
public synchronized void saveImageFromURL(String pPath, String imageURL) throws IOException {
String path = pPath;
URL url = new URL(imageURL);
InputStream is = url.openStream();
File file = new File(path);
if(file.exists() == false) {
file.createNewFile();
} else {
if(overwrite == false) {
int i = 1;
do {
file = new File(path.substring(0, path.lastIndexOf('.')) + "(" + i + ")" + path.substring(path.lastIndexOf('.')));
i++;
} while (file.exists() == true);
} else {
file.createNewFile();
}
}
OutputStream os = new FileOutputStream(file);
byte[] b = new byte[2048];
int length;
while ((length = is.read(b)) != -1) {
os.write(b, 0, length);
}
is.close();
os.close();
}
public synchronized boolean checkURLForImage(String pURL) {
String URL = pURL;
boolean match = false;
LinkedList<String> extensions = imageExt.getExtensionCopy();
while(extensions.peekFirst() != null) {
String extension = extensions.poll();
if(URL.substring(URL.lastIndexOf('.'), URL.length()).equals(extension)) {
match = true;
}
}
return match;
}
public synchronized String correctURLEnding(String pURL) {
String URL = pURL;
String start = URL.substring(0, URL.lastIndexOf('.'));
String end = URL.substring(URL.lastIndexOf('.')+1);
StringBuilder sb = new StringBuilder(end);
Pattern re = Pattern.compile("[a-zA-Z0-9]*");
for(int i = 0; i<sb.length(); i++) {
Character cha = sb.charAt(i);
CharSequence c = cha.toString();
if(re.matcher(c).matches() == false) {
for(int x = sb.length()-1; x>=i; x--) {
sb.deleteCharAt(x);
}
break;
}
}
return start + "." + sb.toString();
}
public synchronized boolean checkFile(String path) {
File file = new File(path);
return file.exists();
}
public synchronized boolean booleanCreateDir(String path) {
File dir = new File(path);
if(dir.exists() == false) {
return dir.mkdir();
} else {
return false;
}
}
public synchronized void createDir(String path) {
File dir = new File(path);
if(dir.exists() == false) {
dir.mkdir();
}
}
public synchronized boolean saveImage(String pDirSubredditPath, String pDirYearPath, String pDirImagePath, String pImagePath, String pURL) throws IOException {
String dirSubredditPath = pDirSubredditPath;
String dirYearPath = pDirYearPath;
String dirImagePath = pDirImagePath;
String imagePath = pImagePath;
String url = pURL;
String currentUser = main.getCurrentUser();
//Check for userDir
if(this.checkFile(currentUser) == true) {
//Check for subreddit-dir
if(this.checkFile(dirSubredditPath) == true) {
//Check for YearDir
if(this.checkFile(dirYearPath)) {
//Check for ImageDir
if(this.checkFile(dirImagePath)) {
//Save file
this.saveImageFromURL(imagePath, url);
return true;
} else {
//Create ImageDir
if(this.booleanCreateDir(dirImagePath) == true) {
//Save file
mw.mainOutputAddLine("Created dir " + dirImagePath);
this.saveImageFromURL(imagePath, url);
return true;
} else {
//ImageDir creation failed
mw.mainOutputAddLine("Creating dir " + dirImagePath + " failed");
return false;
}
}
} else {
//Create Year-Dir
if(this.booleanCreateDir(dirYearPath) == true) {
mw.mainOutputAddLine("Created dir " + dirYearPath);
//Create ImageDir subfolder
if(this.booleanCreateDir(dirImagePath) == true) {
//Save file
mw.mainOutputAddLine("Created dir " + dirImagePath);
this.saveImageFromURL(imagePath, url);
return true;
} else {
//ImageDir creation failed
mw.mainOutputAddLine("Creating dir " + dirImagePath + " failed");
return false;
}
} else {
//Year-Dir creation failed
mw.mainOutputAddLine("Creating dir " + dirYearPath + " failed");
return false;
}
}
} else {
//Create Subreddit-Dir
if(this.booleanCreateDir(dirSubredditPath) == true) {
mw.mainOutputAddLine("Created dir " + dirSubredditPath);
//Create Year-Dir and ImageDir subfolders
//Create Year-Dir
if(this.booleanCreateDir(dirYearPath) == true) {
mw.mainOutputAddLine("Created dir " + dirYearPath);
//Create ImageDir subfolder
if(this.booleanCreateDir(dirImagePath) == true) {
//Save file
mw.mainOutputAddLine("Created dir " + dirImagePath);
this.saveImageFromURL(imagePath, url);
return true;
} else {
//ImageDir creation failed
mw.mainOutputAddLine("Creating dir " + dirImagePath + " failed");
return false;
}
} else {
//Year-Dir creation failed
mw.mainOutputAddLine("Creating dir " + dirYearPath + " failed");
return false;
}
} else {
mw.mainOutputAddLine("Creating dir " + dirSubredditPath + " failed");
return false;
}
}
} else {
//Create userDir
if(this.booleanCreateDir(currentUser) == true) {
mw.mainOutputAddLine("Created dir " + currentUser);
//Create Subreddit-Dir
if(this.booleanCreateDir(dirSubredditPath) == true) {
mw.mainOutputAddLine("Created dir " + dirSubredditPath);
//Create Year-Dir and ImageDir subfolders
//Create Year-Dir
if(this.booleanCreateDir(dirYearPath) == true) {
mw.mainOutputAddLine("Created dir " + dirYearPath);
//Create ImageDir subfolder
if(this.booleanCreateDir(dirImagePath) == true) {
//Save file
mw.mainOutputAddLine("Created dir " + dirImagePath);
this.saveImageFromURL(imagePath, url);
return true;
} else {
//ImageDir creation failed
mw.mainOutputAddLine("Creating dir " + dirImagePath + " failed");
return false;
}
} else {
//Year-Dir creation failed
mw.mainOutputAddLine("Creating dir " + dirYearPath + " failed");
return false;
}
} else {
mw.mainOutputAddLine("Creating dir " + dirSubredditPath + " failed");
return false;
}
} else {
//userDir creation failed
mw.mainOutputAddLine("Creating dir " + currentUser + " failed");
return false;
}
}
}
public synchronized String getSettingsFolderPath() {
return settingsFolderPath;
}
public synchronized String getExtFolderPath() {
return extFolderPath;
}
public synchronized String getUsersPath() {
return usersPath;
}
public synchronized String getLastSavePath() {
return lastSavePath;
}
public synchronized String getExtFilePath() {
return extFilePath;
}
public synchronized void setOverwrite(boolean value) {
overwrite = value;
}
}