Fix subfolder download bug

Fix bug i chunkdownloader chunk naming for files with same names
This commit is contained in:
tonikelope 2023-10-19 14:11:51 +02:00
parent cc0e6a3245
commit 9729a2bcfd
9 changed files with 70 additions and 56 deletions

View File

@ -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>

View File

@ -292,11 +292,11 @@ public class ChunkDownloader implements Runnable, SecureSingleThreadNotifiable {
} 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());

View File

@ -191,7 +191,7 @@ public class ChunkWriterManager implements Runnable, SecureSingleThreadNotifiabl
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) {
@ -251,7 +251,7 @@ public class ChunkWriterManager implements Runnable, SecureSingleThreadNotifiabl
}
if (_bytes_written == _file_size) {
if (_bytes_written == _file_size && MiscTools.isDirEmpty(Paths.get(getChunks_dir()))) {
delete_chunks_temp_dir();
}
}

View File

@ -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>

View File

@ -287,7 +287,7 @@ public class FileGrabberDialog extends javax.swing.JDialog {
);
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"));
@ -822,7 +822,7 @@ public class FileGrabberDialog extends javax.swing.JDialog {
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();

View File

@ -26,7 +26,6 @@ import javax.swing.JOptionPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
/**
*
@ -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);
}
@ -326,7 +323,7 @@ public class FolderLinkDialog extends javax.swing.JDialog {
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);
@ -482,11 +479,13 @@ public class FolderLinkDialog extends javax.swing.JDialog {
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);
@ -509,12 +508,20 @@ public class FolderLinkDialog extends javax.swing.JDialog {
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(() -> {
@ -522,9 +529,11 @@ public class FolderLinkDialog extends javax.swing.JDialog {
node_bar.setIndeterminate(true);
});
if (root != null) {
MiscTools.sortTree(root);
MiscTools.calculateTreeFolderSizes(root);
}
if (root == null) {
LOG.log(SEVERE, null, "MEGA FOLDER ERROR (EMPTY?)");
@ -536,8 +545,6 @@ public class FolderLinkDialog extends javax.swing.JDialog {
final MegaMutableTreeNode roott = root;
final String ssubfolder_id = subfolder_id;
MiscTools.GUIRunAndWait(() -> {
node_bar.setIndeterminate(true);
@ -549,12 +556,6 @@ public class FolderLinkDialog extends javax.swing.JDialog {
ftree.setEnabled(true);
});
if (ssubfolder_id != null) {
_subfolder_node = MiscTools.findMegaTreeNodeByID(roott, ssubfolder_id);
}
}
} catch (MegaAPIException mex) {
@ -581,8 +582,6 @@ public class FolderLinkDialog extends javax.swing.JDialog {
MiscTools.GUIRun(() -> {
working = true;
String folder_id = findFirstRegex("#F!([^!]+)", _link, 1);
_download_links.clear();
MegaMutableTreeNode root = (MegaMutableTreeNode) file_tree.getModel().getRoot();
@ -591,6 +590,15 @@ public class FolderLinkDialog extends javax.swing.JDialog {
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()) {
@ -599,6 +607,8 @@ public class FolderLinkDialog extends javax.swing.JDialog {
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();
@ -610,6 +620,8 @@ public class FolderLinkDialog extends javax.swing.JDialog {
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<>();
@ -663,28 +675,6 @@ public class FolderLinkDialog extends javax.swing.JDialog {
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()));
});
}
});
});
}

View File

@ -69,7 +69,7 @@ import javax.swing.UIManager;
*/
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;

View File

@ -491,12 +491,20 @@ public class MiscTools {
border.setTitleFont(new_title_font);
}
public static String HashString(String algo, String data) throws NoSuchAlgorithmException, UnsupportedEncodingException {
public static String HashString(String algo, String data) {
try {
MessageDigest md = MessageDigest.getInstance(algo);
byte[] thedigest = md.digest(data.getBytes("UTF-8"));
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 {
@ -787,7 +795,19 @@ public class MiscTools {
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();
@ -866,6 +886,10 @@ public class MiscTools {
}
}
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);

Binary file not shown.

Before

Width:  |  Height:  |  Size: 214 KiB

After

Width:  |  Height:  |  Size: 211 KiB