Skip to content

Commit

Permalink
7.92
Browse files Browse the repository at this point in the history
Fix subfolder download bug

Fix bug i chunkdownloader chunk naming for files with same names
  • Loading branch information
tonikelope committed Oct 19, 2023
1 parent cc0e6a3 commit 9729a2b
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 56 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>7.91</version>
<version>7.92</version>
<packaging>jar</packaging>
<repositories>
<repository>
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/tonikelope/megabasterd/ChunkDownloader.java
Original file line number Diff line number Diff line change
Expand Up @@ -292,11 +292,11 @@ public void run() {

} else {

chunk_file = new File(_download.getChunkmanager().getChunks_dir() + "/" + new File(_download.getFile_name()).getName() + ".chunk" + chunk_id);
chunk_file = new File(_download.getChunkmanager().getChunks_dir() + "/" + MiscTools.HashString("sha1", _download.getUrl()) + ".chunk" + chunk_id);

if (!chunk_file.exists() || chunk_file.length() != chunk_size) {

tmp_chunk_file = new File(_download.getChunkmanager().getChunks_dir() + "/" + new File(_download.getFile_name()).getName() + ".chunk" + chunk_id + ".tmp");
tmp_chunk_file = new File(_download.getChunkmanager().getChunks_dir() + "/" + MiscTools.HashString("sha1", _download.getUrl()) + ".chunk" + chunk_id + ".tmp");

_chunk_inputstream = new ThrottledInputStream(con.getInputStream(), _download.getMain_panel().getStream_supervisor());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public void run() {

try {

File chunk_file = new File(getChunks_dir() + "/" + new File(_download.getFile_name()).getName() + ".chunk" + String.valueOf(_last_chunk_id_written + 1));
File chunk_file = new File(getChunks_dir() + "/" + MiscTools.HashString("sha1", _download.getUrl()) + ".chunk" + String.valueOf(_last_chunk_id_written + 1));

while (chunk_file.exists() && chunk_file.canRead() && chunk_file.canWrite() && chunk_file.length() > 0) {

Expand Down Expand Up @@ -251,7 +251,7 @@ public void run() {

}

if (_bytes_written == _file_size) {
if (_bytes_written == _file_size && MiscTools.isDirEmpty(Paths.get(getChunks_dir()))) {
delete_chunks_temp_dir();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
</DimensionLayout>
<DimensionLayout dim="1">
<Group type="103" groupAlignment="0" attributes="0">
<Component id="file_tree_scrollpane" alignment="0" pref="310" max="32767" attributes="0"/>
<Component id="file_tree_scrollpane" alignment="0" pref="176" max="32767" attributes="0"/>
</Group>
</DimensionLayout>
</Layout>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ private void initComponents() {
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(file_tree_scrollpane, javax.swing.GroupLayout.DEFAULT_SIZE, 310, Short.MAX_VALUE)
.addComponent(file_tree_scrollpane, javax.swing.GroupLayout.DEFAULT_SIZE, 176, Short.MAX_VALUE)
);

jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Upload info"));
Expand Down Expand Up @@ -822,7 +822,7 @@ private void account_comboboxItemStateChanged(java.awt.event.ItemEvent evt) {//G

private void skip_rest_buttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_skip_rest_buttonActionPerformed

if (deleteAllExceptSelectedTreeItems(file_tree)) {
if (deleteAllExceptSelectedTreeItems(file_tree, null)) {

_genFileList();

Expand Down
74 changes: 32 additions & 42 deletions src/main/java/com/tonikelope/megabasterd/FolderLinkDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import javax.swing.JTree;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;

/**
*
Expand All @@ -50,8 +49,6 @@ public class FolderLinkDialog extends javax.swing.JDialog {

private volatile MegaMutableTreeNode _subfolder_node = null;

private volatile boolean _subfolder_finish = false;

public List<HashMap> getDownload_links() {
return Collections.unmodifiableList(_download_links);
}
Expand Down Expand Up @@ -326,7 +323,7 @@ private void dance_buttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-

private void skip_rest_buttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_skip_rest_buttonActionPerformed

if (deleteAllExceptSelectedTreeItems(file_tree)) {
if (deleteAllExceptSelectedTreeItems(file_tree, _subfolder_node)) {
file_tree.setEnabled(false);
node_bar.setVisible(true);
skip_rest_button.setEnabled(false);
Expand Down Expand Up @@ -482,11 +479,13 @@ private int _loadMegaDirTree() {

String parent_id = (String) current_hashmap_node.get("parent");

root = null;
String current_id = (String) current_hashmap_node.get("h");

boolean ignore_node = false;

do {

if (folder_nodes.get(parent_id) != null) {
if ((subfolder_id == null && folder_nodes.get(parent_id) != null) || (subfolder_id != null && !subfolder_id.equals(current_id) && folder_nodes.get(parent_id) != null)) {

HashMap<String, Object> parent_hashmap_node = (HashMap) folder_nodes.get(parent_id);

Expand All @@ -509,22 +508,32 @@ private int _loadMegaDirTree() {

current_node = parent_node;

} else {
} else if (subfolder_id != null && subfolder_id.equals(current_id)) {

root = current_node;

} else if (subfolder_id != null && folder_nodes.get(parent_id) == null) {

ignore_node = true;

} else if (subfolder_id == null && folder_nodes.get(parent_id) == null) {

root = current_node;
}

} while (current_node != root);
} while (current_node != root && !ignore_node);
}

MiscTools.GUIRun(() -> {

node_bar.setIndeterminate(true);
});

MiscTools.sortTree(root);
if (root != null) {
MiscTools.sortTree(root);

MiscTools.calculateTreeFolderSizes(root);
MiscTools.calculateTreeFolderSizes(root);
}

if (root == null) {
LOG.log(SEVERE, null, "MEGA FOLDER ERROR (EMPTY?)");
Expand All @@ -536,8 +545,6 @@ private int _loadMegaDirTree() {

final MegaMutableTreeNode roott = root;

final String ssubfolder_id = subfolder_id;

MiscTools.GUIRunAndWait(() -> {

node_bar.setIndeterminate(true);
Expand All @@ -549,12 +556,6 @@ private int _loadMegaDirTree() {
ftree.setEnabled(true);
});

if (ssubfolder_id != null) {

_subfolder_node = MiscTools.findMegaTreeNodeByID(roott, ssubfolder_id);

}

}

} catch (MegaAPIException mex) {
Expand All @@ -581,8 +582,6 @@ private void _genDownloadLiks() {
MiscTools.GUIRun(() -> {
working = true;

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

_download_links.clear();

MegaMutableTreeNode root = (MegaMutableTreeNode) file_tree.getModel().getRoot();
Expand All @@ -591,6 +590,15 @@ private void _genDownloadLiks() {

THREAD_POOL.execute(() -> {

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

if (folder_id.contains("@")) {

String[] fids = folder_id.split("@");

folder_id = fids[0];
}

_total_space = 0L;

while (files_tree.hasMoreElements()) {
Expand All @@ -599,6 +607,8 @@ private void _genDownloadLiks() {

if (node.isLeaf() && node != root && ((HashMap<String, Object>) node.getUserObject()).get("size") != null) {

System.out.println(((HashMap<String, Object>) node.getUserObject()).get("name"));

String path = "";

Object[] object_path = node.getUserObjectPath();
Expand All @@ -610,6 +620,8 @@ private void _genDownloadLiks() {

path = path.replaceAll("^/+", "").replaceAll("^\\+", "").trim();

System.out.println(path);

String url = "https://mega.nz/#N!" + ((Map<String, Object>) node.getUserObject()).get("h") + "!" + ((Map<String, Object>) node.getUserObject()).get("key") + "###n=" + folder_id;

HashMap<String, Object> download_link = new HashMap<>();
Expand Down Expand Up @@ -663,28 +675,6 @@ private void _genDownloadLiks() {
working = false;
});

if (_subfolder_node != null && !_subfolder_finish) {

MiscTools.GUIRunAndWait(() -> {

file_tree.setSelectionPath(new TreePath(_subfolder_node.getPath()));

});

_subfolder_finish = true;

MiscTools.GUIRun(() -> {
skip_rest_button.setEnabled(true);
skip_rest_button.doClick();
});

} else if (_subfolder_node != null && _subfolder_finish) {
MiscTools.GUIRunAndWait(() -> {

file_tree.setSelectionPath(new TreePath(_subfolder_node.getPath()));

});
}
});
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/tonikelope/megabasterd/MainPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
*/
public final class MainPanel {

public static final String VERSION = "7.91";
public static final String VERSION = "7.92";
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
34 changes: 29 additions & 5 deletions src/main/java/com/tonikelope/megabasterd/MiscTools.java
Original file line number Diff line number Diff line change
Expand Up @@ -491,12 +491,20 @@ public static void updateTitledBorderFont(final TitledBorder border, final Font
border.setTitleFont(new_title_font);
}

public static String HashString(String algo, String data) throws NoSuchAlgorithmException, UnsupportedEncodingException {
MessageDigest md = MessageDigest.getInstance(algo);
public static String HashString(String algo, String data) {
try {
MessageDigest md = MessageDigest.getInstance(algo);

byte[] thedigest = md.digest(data.getBytes("UTF-8"));
byte[] thedigest = md.digest(data.getBytes("UTF-8"));

return bin2hex(thedigest);
return bin2hex(thedigest);
} catch (NoSuchAlgorithmException ex) {
Logger.getLogger(MiscTools.class.getName()).log(Level.SEVERE, null, ex);
} catch (UnsupportedEncodingException ex) {
Logger.getLogger(MiscTools.class.getName()).log(Level.SEVERE, null, ex);
}

return null;
}

public static String HashString(String algo, byte[] data) throws NoSuchAlgorithmException {
Expand Down Expand Up @@ -787,7 +795,19 @@ public static boolean deleteSelectedTreeItems(JTree tree) {
return false;
}

public static boolean deleteAllExceptSelectedTreeItems(JTree tree) {
public static boolean isDirEmpty(Path path) {
if (Files.isDirectory(path)) {
try (DirectoryStream<Path> directory = Files.newDirectoryStream(path)) {
return !directory.iterator().hasNext();
} catch (IOException ex) {
Logger.getLogger(MiscTools.class.getName()).log(Level.SEVERE, null, ex);
}
}

return false;
}

public static boolean deleteAllExceptSelectedTreeItems(JTree tree, DefaultMutableTreeNode custom_root) {

TreePath[] paths = tree.getSelectionPaths();

Expand Down Expand Up @@ -866,6 +886,10 @@ public static boolean deleteAllExceptSelectedTreeItems(JTree tree) {
}
}

if (custom_root != null) {
new_root = custom_root;
}

tree.setModel(new DefaultTreeModel(sortTree((DefaultMutableTreeNode) new_root)));

tree.setRootVisible(new_root != null ? ((TreeNode) new_root).getChildCount() > 0 : false);
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 9729a2b

Please sign in to comment.