From ab9eed42b5058ddaedb1b23c777070cddaf09ced Mon Sep 17 00:00:00 2001 From: Sam Strachan Date: Sat, 17 Mar 2018 23:51:17 +0000 Subject: [PATCH] Styling / installation script --- classes_php/Config.php | 7 +- classes_php/FileInfo.php | 15 +++-- classes_php/ScanRequest.php | 6 +- classes_php/Scanimage.php | 28 +++++--- classes_php/ScannerOptions.php | 22 ++++--- install.md | 114 ++++++++++++++++++--------------- install.sh | 28 ++++++++ 7 files changed, 141 insertions(+), 79 deletions(-) create mode 100644 install.sh diff --git a/classes_php/Config.php b/classes_php/Config.php index 1f9362d..fd4db24 100644 --- a/classes_php/Config.php +++ b/classes_php/Config.php @@ -12,15 +12,16 @@ class Config { // Use an empty filter by default. The spirit of the default implementation // is to create non-lossy scans with no post processing. Should you wish to - // override this behaviour then change the filter which will have the scanimage - // output piped to it. + // override this behaviour then change the filter which will have the + // scanimage output piped to it. //const OutputFilter = "/opt/bin/convert 2>/dev/null - -normalize -sharpen 0x1 "; const OutputFilter = self::Convert . " - "; // As with the output filter, the default implementation prefers non-lossy // output. Should you wish you override this then you can change the output // type below - // TIFF is supported by most scanners by default and it is a good option if no filters are used + // TIFF is supported by most scanners by default and it is a good option if + // no filters are used const OutputExtension = Format::TIFF; // Only useful for development debugging diff --git a/classes_php/FileInfo.php b/classes_php/FileInfo.php index e33aa1f..ebc787f 100644 --- a/classes_php/FileInfo.php +++ b/classes_php/FileInfo.php @@ -11,10 +11,15 @@ function FileInfo($fullname) { $this->fullname = $fullname; $bytes = filesize($this->fullname); - if ($bytes >= 1000000000) { $bytes = number_format($bytes / 1000000000, 1) . ' GB'; } - elseif ($bytes >= 1000000) { $bytes = number_format($bytes / 1000000, 1) . ' MB'; } - elseif ($bytes >= 1000) { $bytes = number_format($bytes / 1000, 1) . ' KB'; } - else { $bytes = $bytes . ' B'; } + if ($bytes >= 1000000000) { + $bytes = number_format($bytes / 1000000000, 1) . ' GB'; + } elseif ($bytes >= 1000000) { + $bytes = number_format($bytes / 1000000, 1) . ' MB'; + } elseif ($bytes >= 1000) { + $bytes = number_format($bytes / 1000, 1) . ' KB'; + } else { + $bytes = $bytes . ' B'; + } $info = pathinfo($this->fullname); $this->name = $info['basename']; @@ -22,7 +27,7 @@ function FileInfo($fullname) { $this->extension = $info['extension']; $this->size = $bytes; $this->lastModified = date("Y-m-d @ H:i:s",filemtime($this->fullname)); - } + } public function Delete() { if(is_readable($this->fullname)) { diff --git a/classes_php/ScanRequest.php b/classes_php/ScanRequest.php index 3cf89b8..33e9b29 100644 --- a/classes_php/ScanRequest.php +++ b/classes_php/ScanRequest.php @@ -22,8 +22,10 @@ public function Validate() { $scannerOptions = $scanner["options"]; foreach ($this->options as $key => $value) { - // If option is not present in device interface or its default value marked "inactive", then remove it - if (!array_key_exists($key, $scannerOptions) || $scannerOptions[$key]->defaultValue === "inactive") { + // If option is not present in device interface or its default + // value marked "inactive", then remove it + if (!array_key_exists($key, $scannerOptions) || + $scannerOptions[$key]->defaultValue === "inactive") { unset($this->options[$key]); // Otherwise, validate the selected value } else if (!$scannerOptions[$key]->isValidValue($value)) { diff --git a/classes_php/Scanimage.php b/classes_php/Scanimage.php index 87ed6c8..dc8daf0 100644 --- a/classes_php/Scanimage.php +++ b/classes_php/Scanimage.php @@ -8,31 +8,42 @@ private function CommandLine($scanRequest) { $cmd .= " --format='" . Config::OutputExtension . "'"; // Set device - if (isset($scanRequest->device)) $cmd .= " --device-name='" . $scanRequest->device . "'"; + if (isset($scanRequest->device)) { + $cmd .= " --device-name='" . $scanRequest->device . "'"; + } + // Set device-specific options $scanner = ScannerOptions::get($scanRequest->device); $scannerOptions = $scanner["options"]; foreach ($scanRequest->options as $key => $value) { // Apparently, on some HP All-In-One (OfficeJet?), as long as --source flag is set, // the quality will automatically drop to ADF (lower), even though flatbed is selected - if (strtolower($key) == "source" && strtolower($value) == "flatbed" && - strstr(strtolower($scanner["name"]),"hp") && strstr(strtolower($scanner["description"]),"officejet")) - continue; + if (strtolower($key) == "source" && + strtolower($value) == "flatbed" && + strstr(strtolower($scanner["name"]),"hp") && + strstr(strtolower($scanner["description"]),"officejet")) { + continue; + } $cmd .= " " . $scannerOptions[$key]->name . " '" . $value . "'"; } // Make PDF a bit lighter $cmd2 = Config::OutputFilter; - if ($scanRequest->format == Format::PDF) $cmd2 .= " -compress JPEG -quality 50 "; - + if ($scanRequest->format == Format::PDF) { + $cmd2 .= " -compress JPEG -quality 50 "; + } + // No output filter or default output format which is handled by scanimage directly - if (empty(Config::OutputFilter) || $scanRequest->format == Config::OutputExtension) + if (empty(Config::OutputFilter) || $scanRequest->format == Config::OutputExtension) { $cmd = $cmd . ' > "' . $scanRequest->outputFilepath . '"'; - else + } else { $cmd = $cmd . ' | ' . $cmd2 . ' "' . $scanRequest->outputFilepath . '"'; + } + return $cmd; } + public function Execute($scanRequest) { $scanResponse = new ScanResponse(); $scanResponse->errors = $scanRequest->Validate(); @@ -41,6 +52,7 @@ public function Execute($scanRequest) { System::Execute($scanResponse->cmdline, $scanResponse->output, $scanResponse->returnCode); $scanResponse->image = $scanRequest->outputFilepath; } + return $scanResponse; } } diff --git a/classes_php/ScannerOptions.php b/classes_php/ScannerOptions.php index f6e1cc9..bd94c2a 100644 --- a/classes_php/ScannerOptions.php +++ b/classes_php/ScannerOptions.php @@ -22,8 +22,9 @@ final class ScannerOptions { private static $options = NULL; public static function getAll() { - if (self::$options == NULL) - self::$options = self::parseOptions(); + if (self::$options == NULL) { + self::$options = self::parseOptions(); + } return self::$options; } @@ -89,25 +90,30 @@ private static function parseOptions($device = "") { $option->name = $matched[1]; $option->defaultValue = preg_replace("/(\[|\])/","",$matched[3]); - if (strstr($matched[2],"|")) { // Fixed set of enumerated values, separated with "|" + if (strstr($matched[2],"|")) { + // Fixed set of enumerated values, separated with "|" $option->values = explode("|",$matched[2]); $option->isRange = false; - } else if (strstr($matched[2],"..")) { // Values ranging from low to high, i.e., lo ".." hi + } else if (strstr($matched[2],"..")) { + // Values ranging from low to high, i.e., lo ".." hi $option->values = explode("..",$matched[2]); $option->isRange = true; - } else { // Single element, manually create array + } else { + // Single element, manually create array $option->values = array($matched[2]); $option->isRange = false; } - // Floor all numerical option values... no reason found to keep them as floats + // Floor all numerical option values... no reason found to keep + // them as floats if (is_numeric($option->defaultValue)) { $option->defaultValue = floor($option->defaultValue); for ($n = 0; $n < count($option->values); ++$n) $option->values[$n] = floor($option->values[$n]); } - // Check whether interface accepts a range for resolution and enumerate instead - // This is to avoid having to make a slider for resolution in frontend + // Check whether interface accepts a range for resolution and + // enumerate instead. This is to avoid having to make a slider + // for resolution in frontend if ($option->isRange && $key == "resolution") { $option->isRange = false; $increasing = array(); diff --git a/install.md b/install.md index 2e2c515..54fdaf7 100644 --- a/install.md +++ b/install.md @@ -1,52 +1,21 @@ # installation Make sure your scanner is [working with SANE](install-sane.md). -## QNAP -``` -cd ~ -wget --no-check-certificate https://github.com/sbs20/scanserv/archive/master.zip -cd /share/Qweb -sudo unzip ~/master.zip -sudo mv scanserv-master/ scanserv -``` - -Set variables correctly - -``` -/opt/bin/nano /share/Qweb/classes_php/Config.php -``` - -Then set the Scanimage and Convert lines - mine were as follows - -``` -\n"; - const Scanimage = "/opt/bin/scanimage"; - const Convert = "/usr/local/sbin/convert"; - const BypassSystemExecute = false; - const OutputDirectory = "./output/"; - const PreviewDirectory = "./preview/"; - const MaximumScanWidthInMm = 215; - const MaximumScanHeightInMm = 297; -} -?> -``` +## Debian / Raspbian / Ubuntu -### Test - * You may need to set the permissions of your new directory: `chmod 775 /share/Qweb/scanserv` - * Ensure your QNAP web server is running - * Open your browser and navigate to http://YOUR_QNAP:PORT/scanserv/ - -## Raspberry Pi / Debian +### Prerequisites ``` sudo apt-get update sudo apt-get install apache2 apache2-utils libapache2-mod-php5 php5 sane-utils imagemagick ``` +On newer debians you will need +``` +sudo apt-get install apache2 apache2-utils libapache2-mod-php sane-utils imagemagick +``` -Check apache can use SANE. If you know about saned and permissions then you may not need to worry about this. +Check apache can use SANE. If you know about saned and permissions then you may +not need to worry about this. ``` sudo su -m www-data -c 'scanimage --test' @@ -58,17 +27,25 @@ if not then try sudo gpasswd -a www-data scanner ``` -### Download and configure -Download and install scanserv (note, this will download a file called master.zip to the current user's home -directory). +### Install +Here's a one liner to install +``` +wget -O ~/install.sh https://raw.githubusercontent.com/sbs20/scanserv/master/install.sh && chmod +x ~/install.sh && sudo ~/install.sh +``` + +### Manual install +Download and install scanserv (note, this will download a file called master.zip +to the current user's home directory). ``` cd ~ sudo wget https://github.com/sbs20/scanserv/archive/master.zip ``` -Note, older versions of raspbian install web pages in /var/www, we are assuming /var/www/html as that is -what newer versions use. We are going to install scanserv so that you can access it with the url -http://my.pi.example.com/scanserv +Note, older versions of raspbian install web pages in `/var/www`, we are +assuming `/var/www/html` as that is what newer versions use. We are going to +install scanserv so that you can access it with the url + +http://my.example.com/scanserv ``` cd /var/www/html @@ -90,19 +67,50 @@ sudo chmod 775 /var/www/html/scanserv/output/ sudo chmod 775 /var/www/html/scanserv/preview/ ``` -Now configure scanserv to point at the binaries +If you want to change any configuration then look in +`/var/www/html/scanserv/classes_php/Config.php` + +## References + * http://forum.qnap.com/viewtopic.php?f=182&t=8351 + * http://sourceforge.net/p/phpsane/wiki/FreeBSD/ +## QNAP ``` -sudo nano /var/www/html/scanserv/classes_php/Config.php +cd ~ +wget --no-check-certificate https://github.com/sbs20/scanserv/archive/master.zip +cd /share/Qweb +sudo unzip ~/master.zip +sudo mv scanserv-master/ scanserv ``` - * Change /opt/bin/scanimage to /usr/bin/scanimage - * Change /opt/bin/convert to /usr/bin/convert -Edit anything else you think is interesting, though the other defaults should be okay. +Set variables correctly -## References - * http://forum.qnap.com/viewtopic.php?f=182&t=8351 - * http://sourceforge.net/p/phpsane/wiki/FreeBSD/ +``` +/opt/bin/nano /share/Qweb/classes_php/Config.php +``` + +Then set the Scanimage and Convert lines - mine were as follows + +``` +\n"; + const Scanimage = "/opt/bin/scanimage"; + const Convert = "/usr/local/sbin/convert"; + const BypassSystemExecute = false; + const OutputDirectory = "./output/"; + const PreviewDirectory = "./preview/"; + const MaximumScanWidthInMm = 215; + const MaximumScanHeightInMm = 297; +} +?> +``` + +### Test + * You may need to set the permissions of your new directory: `chmod 775 /share/Qweb/scanserv` + * Ensure your QNAP web server is running + * Open your browser and navigate to http://YOUR_QNAP:PORT/scanserv/ ## QNAP NAS install OLD (Pre QTS version 4.0?) * [Install IPKG](http://wiki.qnap.com/wiki/Install_Optware_IPKG) diff --git a/install.sh b/install.sh new file mode 100644 index 0000000..d84c311 --- /dev/null +++ b/install.sh @@ -0,0 +1,28 @@ +#!/bin/sh + +ROOTUID="0" +if [ "$(id -u)" -ne "$ROOTUID" ] ; then + echo "Error: This script must be executed with root privileges. Try sudo." + exit 1 +fi + +if [ -e ~/scanserv.zip ] ; then + rm ~/scanserv.zip +fi + +wget -O ~/scanserv.zip "https://github.com/sbs20/scanserv/archive/master.zip" + +if ! cd /var/www/html; then + echo "Is apache installed? Maybe:" + echo " sudo apt install apache2 apache2-utils libapache2-mod-php" + echo + echo "You may also want: sudo apt install sane-utils imagemagick" + exit 1 +fi + +sudo unzip ~/scanserv.zip +sudo mv scanserv-master scanserv +sudo chown -R root:www-data scanserv/output/ +sudo chown -R root:www-data scanserv/preview/ +sudo chmod 775 scanserv/output/ +sudo chmod 775 scanserv/preview/ \ No newline at end of file