Commit cfd67702 by Dirk Benkert

added zip download function

added job to cleanup zip files
parent f13bb56e
......@@ -60,8 +60,9 @@ class Module {
$em = $e->getApplication()->getEventManager();
$em->attach(\Zend\Mvc\MvcEvent::EVENT_ROUTE, array($this, 'checkAcl'));
$em->attach(\Zend\Mvc\MvcEvent::EVENT_ROUTE, array($this, 'initLocale'));
$em->attach(
$shared = $em->getSharedManager();
$shared->attach(
'Application',
'dispatch',
function($e) {
$request = $e->getRequest();
......
......@@ -27,6 +27,7 @@ return array(
'files/edit',
'files/save',
'files/download',
'files/zipdownload',
'dialog/newuser',
'json/searchuser',
'json/savefolderusers',
......
......@@ -43,6 +43,7 @@ use Application\Form\FileForm;
use Administration\Controller\Plugin\PaginationFilterSort;
use Administration\Controller\Plugin\Crud;
use Zend\Stdlib\Parameters;
use Application\Service\Zipper;
/**
* CRUD controller for file entities
......@@ -157,6 +158,34 @@ class FilesController extends ControllerAbstract {
}
}
public function zipdownloadAction() {
$folderId = $this->getRequest()->getQuery('folder_id', false);
$service = new Zipper($folderId, $this->getServiceLocator());
$service->prepareArchive();
$response = new \Zend\Http\Response\Stream();
$response->setStream(fopen($service->getArchivePath(), 'r'));
$response->setStatusCode(200);
$response->setStreamName($service->getArchiveName());
$headers = new \Zend\Http\Headers();
$headers->addHeaderLine('Content-Disposition', 'attachment; filename="' . $service->getArchiveName() . '"')
->addHeaderLine('Content-Type', 'application/zip')
->addHeaderLine('Content-Length', $service->getArchiveSize());
$response->setHeaders($headers);
// disable output buffering to prevent memory exhaustion on sending larger files
$level = ob_get_level();
while ($level > 0) {
ob_end_clean();
$level--;
}
return $response;
//$this->redirectTo('files/index', $this->getRequest()->getQuery()->toArray(), 'application/default');
}
/**
* get options for CRUD plugin
*
......
<?php
/**
* Dirk Benkert (https://www.dirk-benkert.de/)
*
* @link https://gitlab.dirk-benkert.de/zf2/share for the canonical source repository
* @copyright Copyright (c) 2015 Dirk Benkert (https://www.dirk-benkert.de)
* @license Apache License Version 2.0
*
* Copyright 2015 Dirk Benkert
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Application\Service;
use Application\Service\Path;
use \ZipArchive;
/**
* generates a zip archive for all files in a shared folder
*
* @package Application_Service
* @author Dirk Benkert <mail@dirk-benkert.de>
* @copyright Copyright (c) 2015 Dirk Benkert
*/
class Zipper extends AbstractService {
protected $folderId;
protected $archivePath;
protected $archiveName;
protected $archiveSize;
public function __construct($folderId, \Zend\ServiceManager\ServiceLocatorInterface $serviceLocator) {
parent::__construct($serviceLocator);
$this->setFolderId($folderId);
}
public function prepareArchive() {
$entityManager = $this->getServiceLocator()->get('Doctrine\ORM\EntityManager');
$folder = $entityManager->find('Application\Entity\Folder', $this->getFolderId());
$this->setArchiveName($folder->label . '.zip');
$path = new Path($this->getServiceLocator());
$files = $this->getFiles($folder);
$this->setArchivePath($path->getBasePath() . DIRECTORY_SEPARATOR . $this->getArchiveName());
$zip = new ZipArchive();
$zip->open($this->getArchivePath(), ZipArchive::CREATE);
foreach ($files as $file) {
$zip->addFile($file, $folder->name . DIRECTORY_SEPARATOR . basename($file));
}
$zip->close();
$this->setArchiveSize(filesize($this->getArchivePath()));
}
protected function getFiles($folder) {
$files = array();
foreach ($folder->files as $file) {
$files[] = $file->path;
}
return $files;
}
public function getArchivePath() {
return $this->archivePath;
}
public function getArchiveName() {
return $this->archiveName;
}
public function getArchiveSize() {
return $this->archiveSize;
}
public function setArchivePath($archivePath) {
$this->archivePath = $archivePath;
}
public function setArchiveName($achiveName) {
$this->archiveName = $achiveName;
}
public function setArchiveSize($archiveSize) {
$this->archiveSize = $archiveSize;
}
public function getFolderId() {
return $this->folderId;
}
public function setFolderId($folderId) {
$this->folderId = $folderId;
}
}
......@@ -41,7 +41,21 @@
));
?>" title="back"><i class="fa fa-arrow-left fa-2x"></i></a>
<?php if ($this->identity->canuploadfiles) { ?>
<?php
if ($this->identity->caninvite) {
?>
<a class="btn btn-primary remoteDialog" data-target="<?php
echo $this->url('application/default', array(
'action' => 'newuser',
'controller' => 'dialog',
), array(
'query' => $this->params
));
?>" title="New user" data-initializemodule="UserNew"><i class="fa fa-user-plus fa-2x"></i></a>
<?php
}
if ($this->identity->canuploadfiles) {
?>
<a class="btn btn-primary remoteDialog" data-target="<?php
echo $this->url('application/default', array(
'action' => 'edit',
......@@ -49,23 +63,20 @@
), array(
'query' => $this->params
));
?>" title="Neue Datei" data-initializemodule="File">
?>" title="New file" data-initializemodule="File">
<i class="fa fa-cloud-upload fa-2x"></i>
</a>
<?php }
if ($this->identity->caninvite) {
?>
<a class="btn btn-primary remoteDialog" data-target="<?php
<?php }?>
<a class="btn btn-primary" href="<?php
echo $this->url('application/default', array(
'action' => 'newuser',
'controller' => 'dialog',
'action' => 'zipdownload',
'controller' => 'files',
), array(
'query' => $this->params
));
?>" title="New User" data-initializemodule="UserNew"><i class="fa fa-user-plus fa-2x"></i></a>
<?php
}
?>
?>" title="Download all files">
<i class="fa fa-cloud-download fa-2x"></i>
</a>
</div>
</div>
<?php
......
......@@ -75,6 +75,7 @@ class Module implements
return array(
// Describe available commands
'cleanup' => 'start the cleanup process',
'cleanup-zip' => 'delete zip archives',
'validate-setup' => 'check if setup is ok',
// Describe expected parameters
array(),
......
......@@ -33,6 +33,16 @@ return array(
),
),
),
'cleanup-zip' => array(
'options' => array(
// add [ and ] if optional)
'route' => 'cleanup-zip',
'defaults' => array(
'controller' => 'Cli\Controller\Index',
'action' => 'cleanup-zip'
),
),
),
'validate-setup' => array(
'options' => array(
// add [ and ] if optional)
......@@ -42,7 +52,7 @@ return array(
'action' => 'validate-setup'
),
),
)
),
),
),
),
......
......@@ -24,6 +24,7 @@
namespace Cli\Controller;
use Cli\Service\Cleanup;
use Cli\Service\CleanupZip;
use Cli\Service\ValidateSetup;
use Application\Entity\Job;
......@@ -51,4 +52,9 @@ class IndexController extends AbstractConsoleController {
$service = new ValidateSetup($this->getServiceLocator());
$service->validate();
}
public function cleanupZipAction() {
$service = new CleanupZip($this->getServiceLocator());
$service->run();
}
}
<?php
/**
* Dirk Benkert (https://www.dirk-benkert.de/)
*
* @link https://gitlab.dirk-benkert.de/zf2/share for the canonical source repository
* @copyright Copyright (c) 2015 Dirk Benkert (https://www.dirk-benkert.de)
* @license Apache License Version 2.0
*
* Copyright 2015 Dirk Benkert
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Cli\Job;
use Cli\Task\AbstractTask;
use Cli\Task\DelayedTaskInterface;
use Application\Entity\Job;
use Application\Service\Path;
/**
* Short descirption of class
*
* Long description of class
*
* @package PACKAGE
* @author Dirk Benkert <mail@dirk-benkert.de>
* @copyright Copyright (c) 2015 Dirk Benkert
*/
class CleanupZip extends AbstractTask implements DelayedTaskInterface {
/**
* name
*
* @var array
*/
protected $jobname = array(
'cleanupZip' => 'cleanupZip',
);
/**
* entity
*
* @var Application\Entity\Job
*/
protected $job = null;
/**
* executes the job
*
* @return void
*/
public function run() {
$job = $this->getJob();
$path = new Path($this->getServiceLocator());
$archivesToDelete = glob($path->getBasePath() . DIRECTORY_SEPARATOR . '*.zip');
foreach ($archivesToDelete as $zip) {
unlink($zip);
}
$this->addMessage('deleting zip archives.');
$this->addMessage(count($archivesToDelete) . ' files deleted.');
$em = $this->getEntityManager();
$job->setExecuted(time());
$em->persist($job);
$em->flush();
}
/**
* get timestamp of last execution
*
* @return int Timestamp
*/
public function getLastExecutionTimestamp() {
$job = $this->getJob();
if (!$job->executed) {
return $time - 18144000; // now - 1 month if no timestamp is set
}
return $job->executed->format('U');
}
/**
* getter
*
* @return Application\Entity\Job
*/
public function getJob() {
if (null === $this->job) {
$em = $this->getEntityManager();
$this->job = $em->getRepository('Application\Entity\Job')->findOneBy(array('name' => $this->jobname['cleanupZip']));
if (!$this->job) {
$this->job = new Job();
$this->job->setData(array(
'name' => $this->jobname['cleanupZip']
));
}
}
return $this->job;
}
}
<?php
/**
* Dirk Benkert (https://www.dirk-benkert.de/)
*
* @link https://gitlab.dirk-benkert.de/zf2/share for the canonical source repository
* @copyright Copyright (c) 2015 Dirk Benkert (https://www.dirk-benkert.de)
* @license Apache License Version 2.0
*
* Copyright 2015 Dirk Benkert
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Cli\Service;
use Cli\Job\CleanupZip as CleanupZipJob;
/**
* cleanup zip service
*
* @package Cli_Service
* @author Dirk Benkert <mail@dirk-benkert.de>
* @copyright Copyright (c) 2015 Dirk Benkert
*/
class CleanupZip extends AbstractConsoleService {
/**
* runs the job
*
* @return void
*/
public function run() {
$this->getConsole()->writeLine('');
$this->getConsole()->writeLine('');
$this->getConsole()->writeLine('');
//cleanup job is called via console and random http request, therefore i had to decouple job from console
$cleanup = new CleanupZipJob($this->getServiceLocator());
$cleanup->run();
$this->writeConsole($cleanup->getMessages());
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment