Skip to content

Commit

Permalink
8.6
Browse files Browse the repository at this point in the history
Folder tree cache (optional) while MegaBasterd is running in order to speed up very (VEEEERY) big file folders.
  • Loading branch information
tonikelope committed Oct 25, 2023
1 parent acc52fd commit af6ee90
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 7 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.tonikelope</groupId>
<artifactId>MegaBasterd</artifactId>
<version>8.5</version>
<version>8.6</version>
<packaging>jar</packaging>
<repositories>
<repository>
Expand Down
15 changes: 14 additions & 1 deletion src/main/java/com/tonikelope/megabasterd/FolderLinkDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,19 @@ private int _loadMegaDirTree() {

String folder_id = findFirstRegex("#F!([^!]+)", _link, 1);

int r = -1;

if (ma.existsCachedFolderNodes(folder_id)) {
r = JOptionPane.showConfirmDialog(this, "Do you want to use FOLDER CACHED VERSION?\n\n(It could speed up the loading of very large folders)", "FOLDER CACHE", JOptionPane.YES_NO_OPTION);

}

if (r == 0) {
MiscTools.GUIRun(() -> {
folder_link_label.setText(_link + " (CACHED VERSION)");
});
}

String subfolder_id = null;

if (folder_id.contains("@")) {
Expand All @@ -443,7 +456,7 @@ private int _loadMegaDirTree() {

String folder_key = findFirstRegex("#F![^!]+!(.+)", _link, 1);

folder_nodes = ma.getFolderNodes(folder_id, folder_key, node_bar);
folder_nodes = ma.getFolderNodes(folder_id, folder_key, node_bar, (r == 0));

MegaMutableTreeNode root = null;

Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/tonikelope/megabasterd/MainPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
*/
public final class MainPanel {

public static final String VERSION = "8.5";
public static final String VERSION = "8.6";
public static final boolean FORCE_SMART_PROXY = false; //TRUE FOR DEBUGING SMART PROXY
public static final int THROTTLE_SLICE_SIZE = 16 * 1024;
public static final int DEFAULT_BYTE_BUFFER_SIZE = 16 * 1024;
Expand Down Expand Up @@ -924,6 +924,8 @@ public boolean checkByeBye() {

public void byebyenow(boolean restart) {

MiscTools.purgeFolderCache();

synchronized (DBTools.class) {

try {
Expand Down
56 changes: 52 additions & 4 deletions src/main/java/com/tonikelope/megabasterd/MegaAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.net.Proxy;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
Expand Down Expand Up @@ -1049,15 +1050,62 @@ public String cryptoHandleauth(String h) {
return ch;
}

public HashMap<String, Object> getFolderNodes(String folder_id, String folder_key, JProgressBar bar) throws Exception {
public boolean existsCachedFolderNodes(String folder_id) {
return Files.exists(Path.of(System.getProperty("java.io.tmpdir") + File.separator + "megabasterd_folder_cache_" + folder_id));
}

private String getCachedFolderNodes(String folder_id) {

String file_path = System.getProperty("java.io.tmpdir") + File.separator + "megabasterd_folder_cache_" + folder_id;

if (Files.exists(Path.of(file_path))) {

LOG.log(Level.INFO, "MEGA FOLDER {0} USING CACHED JSON FILE TREE", new Object[]{folder_id});

try {
return Files.readString(Path.of(file_path));
} catch (IOException ex) {
Logger.getLogger(MegaAPI.class.getName()).log(Level.SEVERE, null, ex);
}
}

return null;
}

private void writeCachedFolderNodes(String folder_id, String res) {
String file_path = System.getProperty("java.io.tmpdir") + File.separator + "megabasterd_folder_cache_" + folder_id;

try {
Files.writeString(Path.of(file_path), res);
} catch (IOException ex) {
Logger.getLogger(MegaAPI.class.getName()).log(Level.SEVERE, null, ex);
}
}

public HashMap<String, Object> getFolderNodes(String folder_id, String folder_key, JProgressBar bar, boolean cache) throws Exception {

HashMap<String, Object> folder_nodes = null;

String request = "[{\"a\":\"f\", \"c\":\"1\", \"r\":\"1\", \"ca\":\"1\"}]";
String res = null;

URL url_api = new URL(API_URL + "/cs?id=" + String.valueOf(_seqno) + "&n=" + folder_id);
if (cache) {
res = getCachedFolderNodes(folder_id);
}

String res = RAW_REQUEST(request, url_api);
if (res == null) {

String request = "[{\"a\":\"f\", \"c\":\"1\", \"r\":\"1\", \"ca\":\"1\"}]";

URL url_api = new URL(API_URL + "/cs?id=" + String.valueOf(_seqno) + "&n=" + folder_id);

res = RAW_REQUEST(request, url_api);

if (res != null) {
writeCachedFolderNodes(folder_id, res);
}
}

LOG.log(Level.INFO, "MEGA FOLDER {0} JSON FILE TREE SIZE -> {1}", new Object[]{folder_id, MiscTools.formatBytes((long) res.length())});

if (res != null) {

Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/tonikelope/megabasterd/MiscTools.java
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,17 @@ public static String computeFileSHA1(File file) throws IOException {
return null;
}

public static void purgeFolderCache() {
File directory = new File(System.getProperty("java.io.tmpdir"));

for (File f : directory.listFiles()) {
if (f.isFile() && f.getName().startsWith("megabasterd_folder_cache_")) {
f.delete();
Logger.getLogger(MiscTools.class.getName()).log(Level.INFO, "REMOVING FOLDER CACHE FILE {0}", f.getAbsolutePath());
}
}
}

public static void containerSetEnabled(Container panel, boolean enabled) {

for (Component cp : panel.getComponents()) {
Expand Down
Binary file modified src/main/resources/images/mbasterd_screen.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit af6ee90

Please sign in to comment.