Skip to content

Commit

Permalink
add service to manage printer
Browse files Browse the repository at this point in the history
  • Loading branch information
simmstein committed Nov 20, 2020
1 parent 18e0c13 commit 11b873d
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 27 deletions.
46 changes: 19 additions & 27 deletions lib/Controller/PrinterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,42 +6,35 @@
use OCP\AppFramework\Http\JSONResponse;
use OCP\IRequest;
use Symfony\Component\Process\Process;
use OCA\Printer\Service\Printer;
use Symfony\Component\Process\Exception\ProcessFailedException;

class PrinterController extends Controller
{
/**
* @var OC\L10N\LazyL10N
*/
protected $language;

public function __construct($appName, IRequest $request)
/**
* @var Printer
*/
protected $printer;

public function __construct(string $appName, IRequest $request, Printer $printer)
{
parent::__construct($appName, $request);

$this->language = \OC::$server->getL10N('printer');
$this->printer = $printer;
}

/**
* callback function to get md5 hash of a file.
*
* @NoAdminRequired
*
* @param (string) $sourcefile - filename
* @param (string) $orientation - Orientation of printed file
*/
public function printfile($sourcefile, $orientation)
public function printfile(string $sourcefile, string $orientation): JSONResponse
{
$filefullpath = \OC\Files\Filesystem::getLocalFile($sourcefile);

$options = [
'landscape' => [
'lpr',
$filefullpath,
],
'portrait' => [
'lpr',
'-o',
'orientation-requested=4',
$filefullpath,
],
];
$file = \OC\Files\Filesystem::getLocalFile($sourcefile);

$success = [
'response' => 'success',
Expand All @@ -53,17 +46,16 @@ public function printfile($sourcefile, $orientation)
'msg' => $this->language->t('Print failed'),
];

if (!isset($options[$orientation])) {
if (!$this->printer->isValidOrirentation($orientation)) {
return new JSONResponse($error);
}

$process = new Process($options[$orientation]);
$process->run();
try {
$this->printer->print($file, $orientation);

if ($process->isSuccessful()) {
return new JSONResponse($success);
} catch (ProcessFailedException $exception) {
return new JSONResponse($error);
}

return new JSONResponse($error);
}
}
44 changes: 44 additions & 0 deletions lib/Service/Printer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Service;
namespace OCA\Printer\Service;

use Symfony\Component\Process\Process;

/**
* class Printer.
*
* @author Simon Vieille <[email protected]>
*/
class Printer
{
public function print(string $file, string $orientation)
{
$options = [
'landscape' => [
'lpr',
$file,
],
'portrait' => [
'lpr',
'-o',
'orientation-requested=4',
$file,
],
];

$process = new Process($options[$orientation]);
$process->mustRun();
}

/**
* Validates an orientation.
*/
public function isValidOrirentation(string $orientation): bool
{
return in_array($orientation, [
'landscape',
'portrait',
]);
}
}

0 comments on commit 11b873d

Please sign in to comment.