Skip to content

Commit 2f7ef2b

Browse files
committed
Use correct sizing and mimetype when possible
Soft require: h2non/imaginary#382 Signed-off-by: Carl Schwan <carl@carlschwan.eu>
1 parent ad75d70 commit 2f7ef2b

5 files changed

Lines changed: 54 additions & 29 deletions

File tree

lib/composer/composer/autoload_classmap.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,7 @@
413413
'OCP\\ISearch' => $baseDir . '/lib/public/ISearch.php',
414414
'OCP\\IServerContainer' => $baseDir . '/lib/public/IServerContainer.php',
415415
'OCP\\ISession' => $baseDir . '/lib/public/ISession.php',
416+
'OCP\\IStreamImage' => $baseDir . '/lib/public/IStreamImage.php',
416417
'OCP\\ITagManager' => $baseDir . '/lib/public/ITagManager.php',
417418
'OCP\\ITags' => $baseDir . '/lib/public/ITags.php',
418419
'OCP\\ITempManager' => $baseDir . '/lib/public/ITempManager.php',
@@ -1295,6 +1296,7 @@
12951296
'OC\\Preview\\HEIC' => $baseDir . '/lib/private/Preview/HEIC.php',
12961297
'OC\\Preview\\Illustrator' => $baseDir . '/lib/private/Preview/Illustrator.php',
12971298
'OC\\Preview\\Image' => $baseDir . '/lib/private/Preview/Image.php',
1299+
'OC\\Preview\\Imaginary' => $baseDir . '/lib/private/Preview/Imaginary.php',
12981300
'OC\\Preview\\JPEG' => $baseDir . '/lib/private/Preview/JPEG.php',
12991301
'OC\\Preview\\Krita' => $baseDir . '/lib/private/Preview/Krita.php',
13001302
'OC\\Preview\\MP3' => $baseDir . '/lib/private/Preview/MP3.php',

lib/composer/composer/autoload_static.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
442442
'OCP\\ISearch' => __DIR__ . '/../../..' . '/lib/public/ISearch.php',
443443
'OCP\\IServerContainer' => __DIR__ . '/../../..' . '/lib/public/IServerContainer.php',
444444
'OCP\\ISession' => __DIR__ . '/../../..' . '/lib/public/ISession.php',
445+
'OCP\\IStreamImage' => __DIR__ . '/../../..' . '/lib/public/IStreamImage.php',
445446
'OCP\\ITagManager' => __DIR__ . '/../../..' . '/lib/public/ITagManager.php',
446447
'OCP\\ITags' => __DIR__ . '/../../..' . '/lib/public/ITags.php',
447448
'OCP\\ITempManager' => __DIR__ . '/../../..' . '/lib/public/ITempManager.php',
@@ -1324,6 +1325,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
13241325
'OC\\Preview\\HEIC' => __DIR__ . '/../../..' . '/lib/private/Preview/HEIC.php',
13251326
'OC\\Preview\\Illustrator' => __DIR__ . '/../../..' . '/lib/private/Preview/Illustrator.php',
13261327
'OC\\Preview\\Image' => __DIR__ . '/../../..' . '/lib/private/Preview/Image.php',
1328+
'OC\\Preview\\Imaginary' => __DIR__ . '/../../..' . '/lib/private/Preview/Imaginary.php',
13271329
'OC\\Preview\\JPEG' => __DIR__ . '/../../..' . '/lib/private/Preview/JPEG.php',
13281330
'OC\\Preview\\Krita' => __DIR__ . '/../../..' . '/lib/private/Preview/Krita.php',
13291331
'OC\\Preview\\MP3' => __DIR__ . '/../../..' . '/lib/private/Preview/MP3.php',

lib/private/Preview/Imaginary.php

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,8 @@
2727
use OCP\Http\Client\IClientService;
2828
use OCP\IConfig;
2929
use OCP\IImage;
30-
use OCP\Image;
3130

32-
use OCP\StreamImage;
31+
use OC\StreamImage;
3332
use Psr\Log\LoggerInterface;
3433

3534
class Imaginary extends ProviderV2 {
@@ -67,27 +66,50 @@ public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
6766
return null;
6867
}
6968

70-
$baseUrl = $this->config->getSystemValueString('preview_imaginary_url', 'http://previews_hpb:8088');
71-
$baseUrl = rtrim($baseUrl, '/');
69+
$imaginaryUrl = $this->config->getSystemValueString('preview_imaginary_url', 'invalid');
70+
if ($imaginaryUrl === 'invalid') {
71+
$this->logger->error('Imaginary preview provider is enabled, but no url is configured. Please provide the url of your imaginary server to the \'preview_imaginary_url\' config variable.');
72+
return null;
73+
}
74+
$imaginaryUrl = rtrim($imaginaryUrl, '/');
7275

7376
// Object store
7477
$stream = $file->fopen('r');
7578

76-
$client = $this->service->newClient();
77-
$response = $client->post(
78-
$baseUrl . "/fit?width=$maxX&height=$maxY&stripmeta=true&type=jpeg", [
79-
'stream' => true,
80-
'content-type' => $file->getMimeType(),
81-
'body' => $stream,
82-
'nextcloud' => ['allow_local_address' => true],
79+
$httpClient = $this->service->newClient();
80+
$parameters = http_build_query([
81+
'width' => $maxX,
82+
'height' => $maxY,
83+
'stripmeta' => 'true',
84+
'type' => 'jpeg',
85+
]);
86+
87+
try {
88+
$response = $httpClient->post(
89+
$imaginaryUrl . '/fit?' . $parameters, [
90+
'stream' => true,
91+
'content-type' => $file->getMimeType(),
92+
'body' => $stream,
93+
'nextcloud' => ['allow_local_address' => true],
94+
]);
95+
} catch (\Exception $e) {
96+
$this->logger->error('Imaginary preview generation failed: ' . $e->getMessage(), [
97+
'exception' => $e,
8398
]);
99+
return null;
100+
}
84101

85102
if ($response->getStatusCode() !== 200) {
86103
$this->logger->error('Imaginary preview generation failed: ' . json_decode($response->getBody())['message']);
87104
return null;
88105
}
89106

90-
$image = new StreamImage($response->getBody(), 'image/jpeg', $maxX, $maxY);
107+
if ($response->getHeader('X-Image-Width') && $response->getHeader('X-Image-Height')) {
108+
$maxX = (int)$response->getHeader('X-Image-Width');
109+
$maxY = (int)$response->getHeader('X-Image-Height');
110+
}
111+
112+
$image = new StreamImage($response->getBody(), $response->getHeader('Content-Type'), $maxX, $maxY);
91113
return $image->valid() ? $image : null;
92114
}
93115
}
Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -71,21 +71,20 @@ public function height() {
7171
return $this->height;
7272
}
7373

74-
/** This will return an invalid result */
7574
public function widthTopLeft() {
76-
return -1;
75+
throw \BadMethodCallException('Not implemented');
7776
}
7877

7978
public function heightTopLeft() {
80-
return -1;
79+
throw \BadMethodCallException('Not implemented');
8180
}
8281

8382
public function show($mimeType = null) {
84-
return -1;
83+
throw \BadMethodCallException('Not implemented');
8584
}
8685

8786
public function save($filePath = null, $mimeType = null) {
88-
return -1;
87+
throw \BadMethodCallException('Not implemented');
8988
}
9089

9190
public function resource() {
@@ -101,49 +100,50 @@ public function data() {
101100
}
102101

103102
public function getOrientation() {
104-
return -1;
103+
throw \BadMethodCallException('Not implemented');
105104
}
106105

107106
public function fixOrientation() {
108-
return false;
107+
throw \BadMethodCallException('Not implemented');
109108
}
110109

111110
public function resize($maxSize) {
112-
return false;
111+
throw \BadMethodCallException('Not implemented');
113112
}
114113

115114
public function preciseResize(int $width, int $height): bool {
116-
return false;
115+
throw \BadMethodCallException('Not implemented');
117116
}
118117

119118
public function centerCrop($size = 0) {
119+
throw \BadMethodCallException('Not implemented');
120120
}
121121

122122
public function crop(int $x, int $y, int $w, int $h): bool {
123-
return false;
123+
throw \BadMethodCallException('Not implemented');
124124
}
125125

126126
public function fitIn($maxWidth, $maxHeight) {
127-
return false;
127+
throw \BadMethodCallException('Not implemented');
128128
}
129129

130130
public function scaleDownToFit($maxWidth, $maxHeight) {
131-
return false;
131+
throw \BadMethodCallException('Not implemented');
132132
}
133133

134134
public function copy(): IImage {
135-
return $this;
135+
throw \BadMethodCallException('Not implemented');
136136
}
137137

138138
public function cropCopy(int $x, int $y, int $w, int $h): IImage {
139-
return $this;
139+
throw \BadMethodCallException('Not implemented');
140140
}
141141

142142
public function preciseResizeCopy(int $width, int $height): IImage {
143-
return $this;
143+
throw \BadMethodCallException('Not implemented');
144144
}
145145

146146
public function resizeCopy(int $maxSize): IImage {
147-
return $this;
147+
throw \BadMethodCallException('Not implemented');
148148
}
149149
}

lib/public/IStreamImage.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,4 @@
2424
namespace OCP;
2525

2626
interface IStreamImage extends IImage {
27-
2827
}

0 commit comments

Comments
 (0)