forked from UB-Mannheim/PalMA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileHandler.class.php
190 lines (158 loc) · 5.04 KB
/
FileHandler.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php
// Copyright (C) 2014 Universitätsbibliothek Mannheim
// See file LICENSE for license details.
// Authors: Alexander Wagner, Stefan Weil
// Test whether the script was called directly (used for unit test).
if (!isset($unittest)) {
$unittest = array();
}
$unittest[__FILE__] = (sizeof(get_included_files()) == 1);
class FehHandler extends FileHandler
{
public function getControls()
{
return FileHandler::CURSOR | FileHandler::ZOOM;
}
public function show($path)
{
}
}
class LibreOfficeHandler extends FileHandler
{
public function getControls()
{
return FileHandler::CURSOR | FileHandler::ZOOM;
}
public function show($path)
{
}
}
class MidoriHandler extends FileHandler
{
public function getControls()
{
return FileHandler::CURSOR | FileHandler::ZOOM;
}
public function show($path)
{
}
}
class VlcHandler extends FileHandler
{
public function getControls()
{
return FileHandler::CURSOR | FileHandler::ZOOM;
}
public function show($path)
{
}
}
class ZathuraHandler extends FileHandler
{
public function getControls()
{
return FileHandler::CURSOR | FileHandler::ZOOM |
FileHandler::HOME | FileHandler::END |
FileHandler::PRIOR | FileHandler::NEXT |
FileHandler::DOWNLOAD;
}
public function show($path)
{
}
}
abstract class FileHandler
{
// Constants for allowed controls.
const UP = 1;
const DOWN = 2;
const LEFT = 4;
const RIGHT = 8;
const ZOOMIN = 16;
const ZOOMOUT = 32;
const HOME = 64;
const END = 128;
const PRIOR = 256;
const NEXT = 512;
const DOWNLOAD = 1024;
const COUNTERCLOCKWISE = 2048;
const CLOCKWISE = 4096;
// Shortcuts for combinations of controls.
const CURSOR = 15; // UP | DOWN | LEFT | RIGHT
const ZOOM = 48; // ZOOMIN | ZOOMOUT
const ALL = 2047;
// up down left right zoomin zoomout home end prior next download
// protected $FILES = array();
// protected $UPLOAD_PATH;
abstract protected function getControls();
abstract protected function show($path);
public static function getFileHandler($file)
{
// Get get directory, name and file extension
$pathParts = pathinfo($file);
$ftype = strtolower($pathParts['extension']);
$fdir = $pathParts['dirname'];
$fname = $pathParts['filename'];
$fhandler = "";
// Define filehandlers
$pdfHandler = '/usr/bin/zathura';
$imageHandler = '/usr/bin/feh --scale-down';
$webHandler = '/usr/bin/midori -p';
$avHandler = '/usr/bin/cvlc --no-audio';
$officeApp = "";
// $params;
// echo $ftype;
if ($ftype === 'pdf') {
$fhandler=$pdfHandler;
} elseif ($ftype === 'gif' || $ftype === 'jpg' || $ftype === 'png') {
$fhandler=$imageHandler;
} elseif ($ftype === 'html' || $ftype === 'url') {
$fhandler=$webHandler;
} elseif ($ftype === 'mpg' || $ftype === 'mpeg' || $ftype === 'avi' ||
$ftype === 'mp3' || $ftype === 'mp4') {
$fhandler=$avHandler;
} else {
if ($ftype === 'doc' || $ftype === 'docx' || $ftype === 'odt' || $ftype === 'txt') {
$officeApp = "writer";
} elseif ($ftype === 'ppt' || $ftype === 'pptx' || $ftype === 'pps' || $ftype === 'ppsx' || $ftype === 'odp') {
$officeApp = "impress";
} elseif ($ftype === 'xls' || $ftype === 'xlsx' || $ftype === 'ods') {
$officeApp = "calc";
}
$convertedFile = convertOffice($file, $officeApp, $fdir, $fname);
if ($convertedFile) {
$file = $convertedFile;
$fhandler = $pdfHandler;
} else {
$fhandler = "/usr/bin/libreoffice --'$officeApp' --nologo --norestore -o";
}
}
/*
alternatively with mime-types
// $ftype = mime_content_type($this->UPLOAD_PATH.$file);
// if($ftype=='application/pdf')
// if($ftype=='image/gif' || $ftype=='image/jpg' || $ftype=='image/png' )
// if($ftype=='html' || $ftype=='url' || $ftype="text/plain")
// (...)
*/
return array($fhandler, $file);
}
}
function convertOffice($inputFile, $office, $outputDir, $fileName)
{
shell_exec("/usr/bin/libreoffice --headless --convert-to pdf:'$office'_pdf_Export --outdir '$outputDir' '$inputFile' >/dev/null 2>&1");
$newFile=$outputDir . '/' . $fileName . '.pdf';
if (file_exists($newFile)) {
return $newFile;
} else {
return false;
}
}
if ($unittest[__FILE__]) {
// Run unit test.
$midoriHandler = new MidoriHandler;
$zathuraHandler = new ZathuraHandler;
echo("DOWNLOAD =" . FileHandler::DOWNLOAD . "\n");
echo("filehandler=" . FileHandler::getFileHandler("test.txt") . "\n");
$handler = ${'midori' . 'Handler'};
echo("controls =" . $handler->getControls() . "\n");
}