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

Download a file after custom process #118

Open
afbora opened this issue Jan 23, 2024 · 2 comments
Open

Download a file after custom process #118

afbora opened this issue Jan 23, 2024 · 2 comments
Assignees

Comments

@afbora
Copy link

afbora commented Jan 23, 2024

Hey!

I'm trying to make a process that create custom zip file and download the zip file when client click the button. I tried with janitor:download and job, couldn't do that.

  • janitor:download doesn't work because endpoint route is not a file, this is a process. After zip file created, download should be started. Returns 404 when I set the route that handle the zip file.
  • job doesn't work because I can't start download process in a job or return download file path. Zip file created but download doesn't start.

Any help please?

@bnomei bnomei self-assigned this Feb 5, 2024
@bnomei
Copy link
Owner

bnomei commented Feb 5, 2024

sorry @afbora for the late reply.

does sending the header::download and exit as described in the readme not work?
https://github.com/bnomei/kirby3-janitor?tab=readme-ov-file#create-and-download-a-backup

@afbora
Copy link
Author

afbora commented Feb 5, 2024

No problem. Thanks for the reply.

I think the janitor:download method wants a direct file path. But in my case I have to create the file first (creating zip file) and then give the zip file path.

does sending the header::download and exit as described in the readme not work?

Here my job and it doesn't work (401: Unauthenticated error). What am I doing wrong?

<?php 

class downloadOrders extends \Bnomei\JanitorJob
{
    public function job(): array
    {
        $zipFile = kirby()->root() . "/export.zip";
        $zipArchive = new ZipArchive();

        if ($zipArchive->open($zipFile, (ZipArchive::CREATE | ZipArchive::OVERWRITE)) !== true) {
            die("Failed to create archive\n");
        }

        $files = Dir::read(kirby()->root() . '/orders', null, true);
        foreach ($files as $file) {
            $zipArchive->addFile($file, basename($file));
        }

        $zipArchive->close();

        Header::download([
            'mime' => F::mime($zipFile),
            'name' => F::filename($zipFile),
        ]);
        
        readfile($zipFile);
        die();

        return [
            'status' => 200
        ];
    }
}

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

No branches or pull requests

2 participants