Skip to content

Commit 516d824

Browse files
committed
Send images to imaginary docker to generate previews
Signed-off-by: Vincent Petry <[email protected]>
1 parent 426dc68 commit 516d824

File tree

4 files changed

+106
-2
lines changed

4 files changed

+106
-2
lines changed

config/config.sample.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,6 +1014,14 @@
10141014
' --headless --nologo --nofirststartwizard --invisible --norestore '.
10151015
'--convert-to png --outdir ',
10161016

1017+
/**
1018+
* Set the URL of the Imaginary service to send image previews to.
1019+
* Also requires the OC\Preview\Imaginary provider to be enabled.
1020+
*
1021+
* See https://github.com/h2non/imaginary
1022+
*/
1023+
'preview_imaginary_url' => 'http://imaginary-host:9000/'
1024+
10171025
/**
10181026
* Only register providers that have been explicitly enabled
10191027
*

lib/private/Http/Client/Client.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,8 @@ public function post(string $uri, array $options = []): IResponse {
298298
unset($options['body']);
299299
}
300300
$response = $this->client->request('post', $uri, $this->buildRequestOptions($options));
301-
return new Response($response);
301+
$isStream = isset($options['stream']) && $options['stream'];
302+
return new Response($response, $isStream);
302303
}
303304

304305
/**

lib/private/Preview/Imaginary.php

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
/**
3+
* @copyright Copyright (c) 2020, Nextcloud, GmbH.
4+
*
5+
* @author Vincent Petry <[email protected]>
6+
*
7+
* @license AGPL-3.0
8+
*
9+
* This code is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Affero General Public License, version 3,
11+
* as published by the Free Software Foundation.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU Affero General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU Affero General Public License, version 3,
19+
* along with this program. If not, see <http://www.gnu.org/licenses/>
20+
*
21+
*/
22+
23+
namespace OC\Preview;
24+
25+
use OCP\Files\File;
26+
use OCP\Http\Client;
27+
use OCP\IImage;
28+
29+
class Imaginary extends ProviderV2 {
30+
/**
31+
* {@inheritDoc}
32+
*/
33+
public function getMimeType(): string {
34+
return '/image\/.*/';
35+
}
36+
37+
/**
38+
* {@inheritDoc}
39+
*/
40+
public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
41+
\OCP\Util::writeLog(self::class, "#### Imaginary getThumbnail", \OCP\ILogger::DEBUG);
42+
$maxSizeForImages = \OC::$server->getConfig()->getSystemValue('preview_max_filesize_image', 50);
43+
$size = $file->getSize();
44+
45+
if ($maxSizeForImages !== -1 && $size > ($maxSizeForImages * 1024 * 1024)) {
46+
return null;
47+
}
48+
49+
$config = \OC::$server->getSystemConfig();
50+
$service = \OC::$server->getHTTPClientService();
51+
$baseUrl = $config->getValue('preview_imaginary_url', 'http://vvortex.local:9090');
52+
$baseUrl = rtrim($baseUrl, '/');
53+
$stream = $file->fopen('r');
54+
55+
// TODO: when dealing with local storage, could send a local file path
56+
// to retrieve the file directly, assuming that the docker has access
57+
58+
try {
59+
\OCP\Util::writeLog(self::class, "#### Imaginary sending stream", \OCP\ILogger::DEBUG);
60+
$client = $service->newClient();
61+
$response = $client->post(
62+
$baseUrl . "/fit?width=$maxX&height=$maxY&stripmeta=true", [
63+
//'headers' => [ 'Content-Type' => $file->getMimeType()],
64+
'stream' => true,
65+
'content-type' => $file->getMimeType(),
66+
'body' => $stream,
67+
'nextcloud' => ['allow_local_address' => true],
68+
]);
69+
\OCP\Util::writeLog(self::class, "#### Imaginary response status " . $response->getStatusCode(), \OCP\ILogger::DEBUG);
70+
} finally {
71+
fclose($stream);
72+
}
73+
74+
if ($response->getStatusCode() !== 200) {
75+
return null;
76+
}
77+
78+
$image = new \OC_Image();
79+
$image->loadFromFileHandle($response->getBody());
80+
if ($image->valid()) {
81+
return $image;
82+
}
83+
84+
return null;
85+
}
86+
}

lib/private/PreviewManager.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class PreviewManager implements IPreview {
5858

5959
/** @var Generator */
6060
private $generator;
61-
61+
6262
/** @var GeneratorHelper */
6363
private $helper;
6464

@@ -118,6 +118,8 @@ public function registerProvider($mimeTypeRegex, \Closure $callable) {
118118
return;
119119
}
120120

121+
\OCP\Util::writeLog(self::class, "#### registerProvider " . $mimeTypeRegex, \OCP\ILogger::DEBUG);
122+
121123
if (!isset($this->providers[$mimeTypeRegex])) {
122124
$this->providers[$mimeTypeRegex] = [];
123125
}
@@ -134,6 +136,7 @@ public function getProviders() {
134136
return [];
135137
}
136138

139+
\OCP\Util::writeLog(self::class, "#### getProviders ", \OCP\ILogger::DEBUG);
137140
$this->registerCoreProviders();
138141
if ($this->providerListDirty) {
139142
$keys = array_map('strlen', array_keys($this->providers));
@@ -352,6 +355,12 @@ protected function registerCoreProviders() {
352355
return;
353356
}
354357
$this->registeredCoreProviders = true;
358+
\OCP\Util::writeLog(self::class, "#### registerCoreProviders ", \OCP\ILogger::DEBUG);
359+
360+
$this->registerCoreProvider(Preview\Imaginary::class, '/image\/.*/');
361+
362+
// FIXME: for some reason putting that class in config.php is not enough...
363+
return
355364

356365
$this->registerCoreProvider(Preview\TXT::class, '/text\/plain/');
357366
$this->registerCoreProvider(Preview\MarkDown::class, '/text\/(x-)?markdown/');

0 commit comments

Comments
 (0)