Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix multiple download and browse while downloading #1242

Conversation

vadasziattila
Copy link
Contributor

this change resolves #744 by adding the option to able to download multiple files and browse while downloading.
Signed-off-by: Vadászi Attila [email protected]

@prasathmani prasathmani merged commit 8d953bf into prasathmani:master Oct 14, 2024
@@ -2225,6 +2309,8 @@ class="edit-file"><i class="fa fa-pencil-square-o"></i> <?php echo lng('Advanced
<li class="list-inline-item"> <a href="#/select-all" class="btn btn-small btn-outline-primary btn-2" onclick="select_all();return false;"><i class="fa fa-check-square"></i> <?php echo lng('SelectAll') ?> </a></li>
<li class="list-inline-item"><a href="#/unselect-all" class="btn btn-small btn-outline-primary btn-2" onclick="unselect_all();return false;"><i class="fa fa-window-close"></i> <?php echo lng('UnSelectAll') ?> </a></li>
<li class="list-inline-item"><a href="#/invert-all" class="btn btn-small btn-outline-primary btn-2" onclick="invert_all();return false;"><i class="fa fa-th-list"></i> <?php echo lng('InvertSelection') ?> </a></li>
<li class="list-inline-item"><input type="submit" class="hidden" name="download" id="a-download" value="Download" onclick="return confirm('<?php echo lng('Download selected files and folders?'); ?>')">
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Download selected files and folders?
Does it work for folders though?

// Add the file to the ZIP archive
$zip->addFile($new_path, basename($new_path));
} else {
$errors++;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It clearly does not work with folders, so prompting Download selected files and folders? is misleading.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vadasziattila can you add folder as well.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think here is a good opportunity to make good use of the included FM_Zipper class.
Just a suggestion, but this would work okay:

// Mass downloading
if (isset($_POST['group'], $_POST['download'], $_POST['token']) && !FM_READONLY) {

    // Verify token to ensure it's valid
    if(!verifyToken($_POST['token'])) {
        fm_set_msg(lng("Invalid Token."), 'error');
        exit;
    }

    $path = FM_ROOT_PATH;
    if (FM_PATH != '') {
        $path .= '/' . FM_PATH;
    }

    $files = $_POST['file']; // List of selected files
    if (is_array($files) && count($files)) {
        // Create a new ZIP archive
        $zip = new FM_Zipper();
        $zip_filename = 'download_' . date('Y-m-d_H-i-s') . '.zip';
        $zip_filepath = sys_get_temp_dir() . '/' . $zip_filename;

        chdir($path);
        $sanitized_filepaths = array();
        foreach($files as $f){
            if ($f != '') {
                array_push($sanitized_filepaths, fm_clean_path($f)); // Sanitize the file path
            }
        }
        $res = $zip->create($zip_filepath, $sanitized_filepaths);

        if ($res && file_exists($zip_filepath)) {
            // Serve the ZIP file for download
            header('Content-Type: application/zip');
            header('Content-Disposition: attachment; filename="' . $zip_filename . '"');
            header('Content-Length: ' . filesize($zip_filepath));
            readfile($zip_filepath);
            // Remove the ZIP file from the temporary directory after download
            unlink($zip_filepath);
            exit;
        } else {
            fm_set_msg(lng('Error creating ZIP file'), 'error');
        }
    } else {
        fm_set_msg(lng('Nothing selected'), 'alert');
    }

    $FM_PATH = FM_PATH;
    fm_redirect(FM_SELF_URL . '?p=' . urlencode($FM_PATH));
}

@prasathmani
Copy link
Owner

PR #1244

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Can download only one file at a time / cannot browse while downloading
3 participants