Skip to content

Commit cb607b5

Browse files
authored
Merge pull request nextcloud#40233 from nextcloud/fix/fix-video-seeking-public-link
[stable25] Detect aborted connection in OC\Files\View and stop writing data to the output buffer
2 parents 1db32fe + 6b2580d commit cb607b5

File tree

5 files changed

+48
-6
lines changed

5 files changed

+48
-6
lines changed

lib/composer/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@
274274
'OCP\\Files\\Config\\IMountProviderCollection' => $baseDir . '/lib/public/Files/Config/IMountProviderCollection.php',
275275
'OCP\\Files\\Config\\IRootMountProvider' => $baseDir . '/lib/public/Files/Config/IRootMountProvider.php',
276276
'OCP\\Files\\Config\\IUserMountCache' => $baseDir . '/lib/public/Files/Config/IUserMountCache.php',
277+
'OCP\\Files\\ConnectionLostException' => $baseDir . '/lib/public/Files/ConnectionLostException.php',
277278
'OCP\\Files\\DavUtil' => $baseDir . '/lib/public/Files/DavUtil.php',
278279
'OCP\\Files\\EmptyFileNameException' => $baseDir . '/lib/public/Files/EmptyFileNameException.php',
279280
'OCP\\Files\\EntityTooLargeException' => $baseDir . '/lib/public/Files/EntityTooLargeException.php',

lib/composer/composer/autoload_static.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
307307
'OCP\\Files\\Config\\IMountProviderCollection' => __DIR__ . '/../../..' . '/lib/public/Files/Config/IMountProviderCollection.php',
308308
'OCP\\Files\\Config\\IRootMountProvider' => __DIR__ . '/../../..' . '/lib/public/Files/Config/IRootMountProvider.php',
309309
'OCP\\Files\\Config\\IUserMountCache' => __DIR__ . '/../../..' . '/lib/public/Files/Config/IUserMountCache.php',
310+
'OCP\\Files\\ConnectionLostException' => __DIR__ . '/../../..' . '/lib/public/Files/ConnectionLostException.php',
310311
'OCP\\Files\\DavUtil' => __DIR__ . '/../../..' . '/lib/public/Files/DavUtil.php',
311312
'OCP\\Files\\EmptyFileNameException' => __DIR__ . '/../../..' . '/lib/public/Files/EmptyFileNameException.php',
312313
'OCP\\Files\\EntityTooLargeException' => __DIR__ . '/../../..' . '/lib/public/Files/EntityTooLargeException.php',

lib/private/Files/View.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
use OCA\Files_Sharing\SharedMount;
5454
use OCP\Constants;
5555
use OCP\Files\Cache\ICacheEntry;
56+
use OCP\Files\ConnectionLostException;
5657
use OCP\Files\EmptyFileNameException;
5758
use OCP\Files\FileNameTooLongException;
5859
use OCP\Files\InvalidCharacterInPathException;
@@ -425,10 +426,11 @@ public function readfile($path) {
425426
}
426427
$handle = $this->fopen($path, 'rb');
427428
if ($handle) {
428-
$chunkSize = 524288; // 512 kB chunks
429+
$chunkSize = 524288; // 512 kiB chunks
429430
while (!feof($handle)) {
430431
echo fread($handle, $chunkSize);
431432
flush();
433+
$this->checkConnectionStatus();
432434
}
433435
fclose($handle);
434436
return $this->filesize($path);
@@ -481,6 +483,7 @@ public function readfilePart($path, $from, $to) {
481483
}
482484
echo fread($handle, $len);
483485
flush();
486+
$this->checkConnectionStatus();
484487
}
485488
return ftell($handle) - $from;
486489
}
@@ -490,6 +493,14 @@ public function readfilePart($path, $from, $to) {
490493
return false;
491494
}
492495

496+
497+
private function checkConnectionStatus(): void {
498+
$connectionStatus = \connection_status();
499+
if ($connectionStatus !== CONNECTION_NORMAL) {
500+
throw new ConnectionLostException("Connection lost. Status: $connectionStatus");
501+
}
502+
}
503+
493504
/**
494505
* @param string $path
495506
* @return mixed
@@ -1053,7 +1064,6 @@ public function toTmpFile($path) {
10531064
public function fromTmpFile($tmpFile, $path) {
10541065
$this->assertPathLength($path);
10551066
if (Filesystem::isValidPath($path)) {
1056-
10571067
// Get directory that the file is going into
10581068
$filePath = dirname($path);
10591069

@@ -1809,7 +1819,6 @@ private function assertPathLength($path) {
18091819
* @return boolean
18101820
*/
18111821
private function targetIsNotShared(IStorage $targetStorage, string $targetInternalPath) {
1812-
18131822
// note: cannot use the view because the target is already locked
18141823
$fileId = (int)$targetStorage->getCache()->getId($targetInternalPath);
18151824
if ($fileId === -1) {

lib/private/legacy/OC_Files.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -233,14 +233,15 @@ public static function get($dir, $files, $params = null) {
233233
OC::$server->getLogger()->logException($ex);
234234
$l = \OC::$server->getL10N('lib');
235235
\OC_Template::printErrorPage($l->t('Cannot download file'), $ex->getMessage(), 200);
236+
} catch (\OCP\Files\ConnectionLostException $ex) {
237+
self::unlockAllTheFiles($dir, $files, $getType, $view, $filename);
238+
OC::$server->getLogger()->logException($ex, ['level' => \OCP\ILogger::DEBUG]);
239+
\OC_Template::printErrorPage('Connection lost', $ex->getMessage(), 200);
236240
} catch (\Exception $ex) {
237241
self::unlockAllTheFiles($dir, $files, $getType, $view, $filename);
238242
OC::$server->getLogger()->logException($ex);
239243
$l = \OC::$server->getL10N('lib');
240244
$hint = method_exists($ex, 'getHint') ? $ex->getHint() : '';
241-
if ($event && $event->getErrorMessage() !== null) {
242-
$hint .= ' ' . $event->getErrorMessage();
243-
}
244245
\OC_Template::printErrorPage($l->t('Cannot download file'), $hint, 200);
245246
}
246247
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
/**
3+
* @copyright Copyright (c) 2016, ownCloud, Inc.
4+
*
5+
* @author Côme Chilliet <come.chilliet@nextcloud.com>
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 OCP\Files;
24+
25+
/**
26+
* Exception for lost connection with the
27+
* @since 25.0.11
28+
*/
29+
class ConnectionLostException extends \RuntimeException {
30+
}

0 commit comments

Comments
 (0)