2020-02-26 11:22:25 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
namespace App\Controllers;
|
|
|
|
|
2021-08-13 07:17:24 +00:00
|
|
|
use App\Database\Repositories\UserRepository;
|
2020-02-26 11:22:25 +00:00
|
|
|
use League\Flysystem\FileNotFoundException;
|
|
|
|
use Psr\Http\Message\ResponseInterface as Response;
|
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request;
|
|
|
|
use ZipStream\Option\Archive;
|
|
|
|
use ZipStream\ZipStream;
|
|
|
|
|
|
|
|
class ExportController extends Controller
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @param Request $request
|
|
|
|
* @param Response $response
|
|
|
|
* @param int|null $id
|
|
|
|
* @return Response
|
|
|
|
* @throws \ZipStream\Exception\OverflowException
|
|
|
|
*/
|
|
|
|
public function downloadData(Request $request, Response $response, int $id): Response
|
|
|
|
{
|
2021-08-13 07:17:24 +00:00
|
|
|
$user = make(UserRepository::class)->get($request, $id, true);
|
2020-02-26 11:22:25 +00:00
|
|
|
|
|
|
|
$medias = $this->database->query('SELECT `uploads`.`filename`, `uploads`.`storage_path` FROM `uploads` WHERE `user_id` = ?', $user->id);
|
|
|
|
|
2022-11-27 18:37:37 +00:00
|
|
|
$this->logger->info("User $user->id, $user->username, exporting data...");
|
2020-04-03 13:59:49 +00:00
|
|
|
|
2020-02-26 11:22:25 +00:00
|
|
|
set_time_limit(0);
|
|
|
|
ob_end_clean();
|
|
|
|
|
|
|
|
$options = new Archive();
|
|
|
|
$options->setSendHttpHeaders(true);
|
|
|
|
|
|
|
|
$zip = new ZipStream($user->username.'-'.time().'-export.zip', $options);
|
|
|
|
|
|
|
|
$filesystem = $this->storage;
|
|
|
|
foreach ($medias as $media) {
|
|
|
|
try {
|
|
|
|
$zip->addFileFromStream($media->filename, $filesystem->readStream($media->storage_path));
|
|
|
|
} catch (FileNotFoundException $e) {
|
|
|
|
$this->logger->error('Cannot export file', ['exception' => $e]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$zip->finish();
|
|
|
|
exit(0);
|
|
|
|
}
|
2020-02-26 11:22:40 +00:00
|
|
|
}
|