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

Implement download file #10

Open
mkrecek234 opened this issue Apr 9, 2020 · 3 comments
Open

Implement download file #10

mkrecek234 opened this issue Apr 9, 2020 · 3 comments
Assignees

Comments

@mkrecek234
Copy link
Contributor

Are there any activities to implement also the download part?

  • Have a simple object to serve file download stream
  • Offer an option in the existing (upload/delink) field to also re-download the file
  • Offer a view object to just download the file
@mkrecek234
Copy link
Contributor Author

mkrecek234 commented May 11, 2020

Here is a very basic simple code (Work in progress) to create a download wrapper:

<?php
namespace atk4\crm;

include '../vendor/autoload.php';
include '../src/db.php';

$dsn = 'mysql://root:root@localhost:8889/db;
$db = \atk4\data\Persistence::connect($dsn);

$fs = new \atk4\filestore\Model\File($db, 'filestore_file');

$token =  $_GET["token"];

$fs->addCondition('token', $token);
$fs->loadAny();
$meta_filename = $fs['meta_filename'];
$filename = $fs['location'];

$adapter = new \League\Flysystem\Adapter\Local(__DIR__.'/../src/localfiles');
$fsa = new \League\Flysystem\Filesystem($adapter);

if ($fsa->has($filename)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename="'.$meta_filename.'"');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . $fsa->getSize($filename));
    $stream = $fsa->readStream($filename);
    $contents = stream_get_contents($stream);
    echo $contents;
    fclose($stream);
    exit;
} else {
    echo "Fehler - Datei nicht gefunden: ".$filename;
}

@mkrecek234
Copy link
Contributor Author

mkrecek234 commented May 11, 2020

Here is a more ATK4 style way for a download wrapper thanks to the hints of @abbadon1334 - however there are still 2 issues with this: 1) the stickyGet of the token does not "survive" an atk4/login login but the token is lost. 2) I don't like the button to call the callback, I would fancy a direct download. Since we are not supposed to run app->terminate directly but via callback, how can I make the callback immediately...

<?php
namespace atk4\crm;

include '../vendor/autoload.php';
include '../src/db.php';


$app = new \atk4\crm\CRMApp();

$fs = new \atk4\filestore\Model\File($app->db);
$token = $app->stickyGet('token');

$fs->addCondition('token', $token);
$fs->tryloadAny();
$meta_filename = $fs['meta_filename'];
$filename = $fs['location'];

$adapter = new \League\Flysystem\Adapter\Local(__DIR__.'/../src/localfiles');
$fsa = new \League\Flysystem\Filesystem($adapter);

if ($app->auth->user->loaded()) {
  if ($fsa->has($filename)) {
    
    $callback = \atk4\ui\Callback::addTo($app);
    $callback->set(function() use ($app, $fsa, $filename, $meta_filename) {
        $stream = $fsa->readStream($filename);
        $contents = stream_get_contents($stream);
        fclose($stream);
        
        $app->terminate($contents,[
            'Content-Description' => 'File Transfer',
            'Content-Type' => 'application/octet-stream',
            'Cache-Control' =>  'must-revalidate',
            'Expires' => '0',
            'Content-Disposition' => 'attachment; filename="'.$meta_filename.'"',
            'Content-Length' => $fsa->getSize($filename),
            'Pragma' => 'public'
        ]);
    });

   \atk4\ui\Button::addTo($app,['Download now'])->link($callback->getURL());

} else {
    $message = new \atk4\ui\Message(['File not found', 'error']); 
    $app->add($message);
}

}

@DarkSide666
Copy link
Member

@mkrecek234 Please check demos in current develop branch. Download functionality is implemented now.
Is that enough and this issue ticket can be closed now?

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

No branches or pull requests

2 participants