move more common functions to common class.

This commit is contained in:
idk
2022-08-30 15:01:57 -04:00
parent f76fae7c1c
commit d3613d566a
4 changed files with 36 additions and 125 deletions

View File

@ -1,13 +1,6 @@
package net.i2p.i2pfirefox;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
/**
* I2PChromiumProfileBuilder.java
@ -28,11 +21,7 @@ import java.nio.file.StandardCopyOption;
* @since 0.0.1
*/
public class I2PChromiumProfileBuilder extends I2PCommonBrowser {
private static boolean strict;
private static String profileDir(String file) {
return profileDir(file, "chromium");
}
//private static boolean strict;
/**
* get the profile directory, creating it if necessary
@ -136,73 +125,6 @@ public class I2PChromiumProfileBuilder extends I2PCommonBrowser {
return false;
}
System.out.println("Copied base profile to profile directory");
return copyStrictOptions();
}
private static void copyDirectory(File sourceDirectory, File destinationDirectory) throws IOException {
destinationDirectory = new File(destinationDirectory.toString().replace("i2p.chromium.base.profile", ""));
if (!destinationDirectory.exists()) {
destinationDirectory.mkdir();
}
for (String f : sourceDirectory.list()) {
copyDirectoryCompatibityMode(new File(sourceDirectory, f), new File(destinationDirectory, f));
}
}
public static void copyDirectoryCompatibityMode(File source, File destination) throws IOException {
if (source.isDirectory()) {
copyDirectory(source, destination);
} else {
copyFile(source, destination);
}
}
private static void copyFile(File sourceFile, File destinationFile) throws IOException {
try (InputStream in = new FileInputStream(sourceFile);
OutputStream out = new FileOutputStream(destinationFile)) {
byte[] buf = new byte[1024];
int length;
while ((length = in.read(buf)) > 0) {
out.write(buf, 0, length);
}
}
}
/**
* Copy the strict options from the base profile to the profile
*
* @return true if successful, false otherwise
* @since 0.0.1
*/
public static boolean copyStrictOptions() {
if (!strict){
return true;
}
String baseProfile = baseProfileDirectory();
String profile = profileDirectory();
if (baseProfile.isEmpty() || profile.isEmpty()) {
return false;
}
File baseProfileDir = new File(baseProfile);
File profileDir = new File(profile);
if (!baseProfileDir.exists() || !profileDir.exists()) {
return false;
}
File baseOverrides = new File(baseProfile, "strict-overrides.js");
File userOverrides = new File(baseProfile, "user-overrides.js");
if (!baseOverrides.exists()) {
return false;
}
try {
Files.copy(baseOverrides.toPath(), userOverrides.toPath(), StandardCopyOption.REPLACE_EXISTING);
} catch (Exception e) {
System.out.println("Error copying base profile to profile"+e);
return false;
}
// if user-overrides.js does not exist yet, make an empty one.
//if (!touch(profileDir.toString(), "user-overrides.js")) {
//return false;
//}
return true;
}
@ -212,7 +134,7 @@ public class I2PChromiumProfileBuilder extends I2PCommonBrowser {
* @since 0.0.1
*/
public I2PChromiumProfileBuilder() {
I2PChromiumProfileBuilder.strict = false;
//I2PChromiumProfileBuilder.strict = false;
}
/**
@ -222,6 +144,6 @@ public class I2PChromiumProfileBuilder extends I2PCommonBrowser {
* @since 0.0.1
*/
public I2PChromiumProfileBuilder(boolean strict) {
I2PChromiumProfileBuilder.strict = strict;
//I2PChromiumProfileBuilder.strict = strict;
}
}

View File

@ -1,12 +1,5 @@
package net.i2p.i2pfirefox;
import java.io.File;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
/**
* I2PChromiumProfileUnpacker.java
* Copyright (C) 2022 idk <hankhill19580@gmail.com>

View File

@ -1,7 +1,11 @@
package net.i2p.i2pfirefox;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.zip.ZipEntry;
@ -138,4 +142,32 @@ public class I2PCommonBrowser {
}
return true;
}
protected static void copyDirectory(File sourceDirectory, File destinationDirectory) throws IOException {
destinationDirectory = new File(destinationDirectory.toString().replace("i2p.chromium.base.profile", ""));
if (!destinationDirectory.exists()) {
destinationDirectory.mkdir();
}
for (String f : sourceDirectory.list()) {
copyDirectoryCompatibityMode(new File(sourceDirectory, f), new File(destinationDirectory, f));
}
}
public static void copyDirectoryCompatibityMode(File source, File destination) throws IOException {
if (source.isDirectory()) {
copyDirectory(source, destination);
} else {
copyFile(source, destination);
}
}
private static void copyFile(File sourceFile, File destinationFile) throws IOException {
try (InputStream in = new FileInputStream(sourceFile);
OutputStream out = new FileOutputStream(destinationFile)) {
byte[] buf = new byte[1024];
int length;
while ((length = in.read(buf)) > 0) {
out.write(buf, 0, length);
}
}
}
}

View File

@ -1,11 +1,6 @@
package net.i2p.i2pfirefox;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
@ -30,10 +25,6 @@ import java.nio.file.StandardCopyOption;
public class I2PFirefoxProfileBuilder extends I2PCommonBrowser {
private static boolean strict;
private static String profileDir(String file) {
return profileDir(file, "firefox");
}
/**
* get the profile directory, creating it if necessary
*
@ -148,34 +139,7 @@ public class I2PFirefoxProfileBuilder extends I2PCommonBrowser {
return copyStrictOptions();
}
private static void copyDirectory(File sourceDirectory, File destinationDirectory) throws IOException {
destinationDirectory = new File(destinationDirectory.toString().replace("i2p.firefox.base.profile", ""));
if (!destinationDirectory.exists()) {
destinationDirectory.mkdir();
}
for (String f : sourceDirectory.list()) {
copyDirectoryCompatibityMode(new File(sourceDirectory, f), new File(destinationDirectory, f));
}
}
public static void copyDirectoryCompatibityMode(File source, File destination) throws IOException {
if (source.isDirectory()) {
copyDirectory(source, destination);
} else {
copyFile(source, destination);
}
}
private static void copyFile(File sourceFile, File destinationFile) throws IOException {
try (InputStream in = new FileInputStream(sourceFile);
OutputStream out = new FileOutputStream(destinationFile)) {
byte[] buf = new byte[1024];
int length;
while ((length = in.read(buf)) > 0) {
out.write(buf, 0, length);
}
}
}
/**
* Copy the strict options from the base profile to the profile