Skip to content

Commit a41d51b

Browse files
authored
Merge pull request #40285 from nextcloud/backport/40233/master
Detect aborted connection in OC\Files\View and stop writing data to t…
2 parents a0c4935 + 003d0de commit a41d51b

File tree

5 files changed

+51
-2
lines changed

5 files changed

+51
-2
lines changed

lib/composer/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@
302302
'OCP\\Files\\Config\\IMountProviderCollection' => $baseDir . '/lib/public/Files/Config/IMountProviderCollection.php',
303303
'OCP\\Files\\Config\\IRootMountProvider' => $baseDir . '/lib/public/Files/Config/IRootMountProvider.php',
304304
'OCP\\Files\\Config\\IUserMountCache' => $baseDir . '/lib/public/Files/Config/IUserMountCache.php',
305+
'OCP\\Files\\ConnectionLostException' => $baseDir . '/lib/public/Files/ConnectionLostException.php',
305306
'OCP\\Files\\DavUtil' => $baseDir . '/lib/public/Files/DavUtil.php',
306307
'OCP\\Files\\EmptyFileNameException' => $baseDir . '/lib/public/Files/EmptyFileNameException.php',
307308
'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
@@ -335,6 +335,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
335335
'OCP\\Files\\Config\\IMountProviderCollection' => __DIR__ . '/../../..' . '/lib/public/Files/Config/IMountProviderCollection.php',
336336
'OCP\\Files\\Config\\IRootMountProvider' => __DIR__ . '/../../..' . '/lib/public/Files/Config/IRootMountProvider.php',
337337
'OCP\\Files\\Config\\IUserMountCache' => __DIR__ . '/../../..' . '/lib/public/Files/Config/IUserMountCache.php',
338+
'OCP\\Files\\ConnectionLostException' => __DIR__ . '/../../..' . '/lib/public/Files/ConnectionLostException.php',
338339
'OCP\\Files\\DavUtil' => __DIR__ . '/../../..' . '/lib/public/Files/DavUtil.php',
339340
'OCP\\Files\\EmptyFileNameException' => __DIR__ . '/../../..' . '/lib/public/Files/EmptyFileNameException.php',
340341
'OCP\\Files\\EntityTooLargeException' => __DIR__ . '/../../..' . '/lib/public/Files/EntityTooLargeException.php',

lib/private/Files/View.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
use OCA\Files_Sharing\SharedMount;
5757
use OCP\Constants;
5858
use OCP\Files\Cache\ICacheEntry;
59+
use OCP\Files\ConnectionLostException;
5960
use OCP\Files\EmptyFileNameException;
6061
use OCP\Files\FileNameTooLongException;
6162
use OCP\Files\InvalidCharacterInPathException;
@@ -397,10 +398,11 @@ public function readfile($path) {
397398
}
398399
$handle = $this->fopen($path, 'rb');
399400
if ($handle) {
400-
$chunkSize = 524288; // 512 kB chunks
401+
$chunkSize = 524288; // 512 kiB chunks
401402
while (!feof($handle)) {
402403
echo fread($handle, $chunkSize);
403404
flush();
405+
$this->checkConnectionStatus();
404406
}
405407
fclose($handle);
406408
return $this->filesize($path);
@@ -423,7 +425,7 @@ public function readfilePart($path, $from, $to) {
423425
}
424426
$handle = $this->fopen($path, 'rb');
425427
if ($handle) {
426-
$chunkSize = 524288; // 512 kB chunks
428+
$chunkSize = 524288; // 512 kiB chunks
427429
$startReading = true;
428430

429431
if ($from !== 0 && $from !== '0' && fseek($handle, $from) !== 0) {
@@ -453,6 +455,7 @@ public function readfilePart($path, $from, $to) {
453455
}
454456
echo fread($handle, $len);
455457
flush();
458+
$this->checkConnectionStatus();
456459
}
457460
return ftell($handle) - $from;
458461
}
@@ -462,6 +465,13 @@ public function readfilePart($path, $from, $to) {
462465
return false;
463466
}
464467

468+
private function checkConnectionStatus(): void {
469+
$connectionStatus = \connection_status();
470+
if ($connectionStatus !== CONNECTION_NORMAL) {
471+
throw new ConnectionLostException("Connection lost. Status: $connectionStatus");
472+
}
473+
}
474+
465475
/**
466476
* @param string $path
467477
* @return mixed

lib/private/legacy/OC_Files.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,10 @@ public static function get($dir, $files, $params = null) {
230230
OC::$server->getLogger()->logException($ex);
231231
$l = \OC::$server->getL10N('lib');
232232
\OC_Template::printErrorPage($l->t('Cannot download file'), $ex->getMessage(), 200);
233+
} catch (\OCP\Files\ConnectionLostException $ex) {
234+
self::unlockAllTheFiles($dir, $files, $getType, $view, $filename);
235+
OC::$server->getLogger()->logException($ex, ['level' => \OCP\ILogger::DEBUG]);
236+
\OC_Template::printErrorPage('Connection lost', $ex->getMessage(), 200);
233237
} catch (\Exception $ex) {
234238
self::unlockAllTheFiles($dir, $files, $getType, $view, $filename);
235239
OC::$server->getLogger()->logException($ex);
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* @copyright Copyright (c) 2016, ownCloud, Inc.
7+
*
8+
* @author Côme Chilliet <come.chilliet@nextcloud.com>
9+
*
10+
* @license AGPL-3.0
11+
*
12+
* This code is free software: you can redistribute it and/or modify
13+
* it under the terms of the GNU Affero General Public License, version 3,
14+
* as published by the Free Software Foundation.
15+
*
16+
* This program is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU Affero General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU Affero General Public License, version 3,
22+
* along with this program. If not, see <http://www.gnu.org/licenses/>
23+
*
24+
*/
25+
26+
namespace OCP\Files;
27+
28+
/**
29+
* Exception for lost connection with the
30+
* @since 25.0.11
31+
*/
32+
class ConnectionLostException extends \RuntimeException {
33+
}

0 commit comments

Comments
 (0)