88 */
99namespace OCA \DAV \Connector \Sabre ;
1010
11+ use Icewind \Streams \CountWrapper ;
1112use OC \Streamer ;
1213use OCA \DAV \Connector \Sabre \Exception \Forbidden ;
1314use OCP \EventDispatcher \IEventDispatcher ;
1415use OCP \Files \Events \BeforeZipCreatedEvent ;
1516use OCP \Files \File as NcFile ;
1617use OCP \Files \Folder as NcFolder ;
1718use OCP \Files \Node as NcNode ;
19+ use OCP \Files \NotPermittedException ;
1820use OCP \IConfig ;
1921use OCP \IDateTimeZone ;
2022use OCP \IL10N ;
23+ use OCP \Lock \LockedException ;
2124use Psr \Log \LoggerInterface ;
2225use Sabre \DAV \Server ;
2326use Sabre \DAV \ServerPlugin ;
@@ -57,10 +60,6 @@ public function __construct(
5760 private IL10N $ l10n ,
5861 ) {
5962 $ this ->reportMissingFiles = $ this ->config ->getSystemValueBool ('archive_report_missing_files ' , false );
60-
61- if ($ this ->reportMissingFiles ) {
62- stream_filter_register ('count.bytes ' , ByteCounterFilter::class);
63- }
6463 }
6564
6665 /**
@@ -81,6 +80,7 @@ public function initialize(Server $server): void {
8180 /**
8281 * Adding a node to the archive streamer.
8382 * @return ?string an error message if an error occurred and reporting is enabled, null otherwise
83+ * @throws NotPermittedException|LockedException
8484 */
8585 protected function streamNode (Streamer $ streamer , NcNode $ node , string $ rootPath ): ?string {
8686 // Remove the root path from the filename to make it relative to the requested folder
@@ -94,39 +94,27 @@ protected function streamNode(Streamer $streamer, NcNode $node, string $rootPath
9494
9595 if ($ node instanceof NcFile) {
9696 $ nodeSize = $ node ->getSize ();
97- try {
98- $ stream = $ node ->fopen ('rb ' );
99- } catch (\Exception $ e ) {
100- // opening failed, log the failure as reason for the missing file
101- if ($ this ->reportMissingFiles ) {
102- $ exceptionClass = get_class ($ e );
103- return $ this ->l10n ->t ('Error while opening the file: %s ' , [$ exceptionClass ]);
104- }
97+ $ stream = $ node ->fopen ('rb ' );
10598
106- throw $ e ;
99+ if ($ stream === false ) {
100+ return $ this ->l10n ->t ('File could not be opened (fopen). Please check the server logs for more information. ' );
107101 }
108102
109- if ( $ this -> reportMissingFiles ) {
110- if ($ stream === false ) {
111- return $ this -> l10n -> t ( ' File could not be opened (fopen). Please check the server logs for more information. ' ) ;
112- }
103+ $ read = 0 ;
104+ $ stream = CountWrapper:: wrap ($ stream, function ( int $ written ) use (& $ read ) {
105+ return $ read += $ written ;
106+ });
113107
114- $ byteCounter = new StreamByteCounter ();
115- $ wrapped = stream_filter_append ($ stream , 'count.bytes ' , STREAM_FILTER_READ , ['counter ' => $ byteCounter ]);
116- if ($ wrapped === false ) {
117- return $ this ->l10n ->t ('Unable to check file for consistency check ' );
118- }
108+ if ($ stream === false ) {
109+ return $ this ->l10n ->t ('Unable to check file for consistency check ' );
119110 }
120111
121112 $ fileAddedToStream = $ streamer ->addFileFromStream ($ stream , $ filename , $ nodeSize , $ mtime );
122- if ($ this ->reportMissingFiles ) {
123- if (!$ fileAddedToStream ) {
124- return $ this ->l10n ->t ('The archive was already finalized ' );
125- }
126-
127- return $ this ->logStreamErrors ($ stream , $ filename , $ nodeSize , $ byteCounter ->bytes );
113+ if (!$ fileAddedToStream ) {
114+ return $ this ->l10n ->t ('The archive was already finalized ' );
128115 }
129116
117+ return $ this ->logStreamErrors ($ stream , $ filename , $ nodeSize , $ read );
130118 }
131119
132120 return null ;
@@ -259,7 +247,20 @@ public function handleDownload(Request $request, Response $response): ?false {
259247 continue ;
260248 }
261249
262- $ streamError = $ this ->streamNode ($ streamer , $ node , $ rootPath );
250+ try {
251+ $ streamError = $ this ->streamNode ($ streamer , $ node , $ rootPath );
252+ } catch (\Exception $ e ) {
253+ if (!$ this ->reportMissingFiles ) {
254+ throw $ e ;
255+ }
256+
257+ $ logMessage = $ this ->l10n ->t ('Error while streaming the file ' );
258+ $ this ->logger ->error ($ logMessage , ['exception ' => $ e ]);
259+ $ reason = $ this ->l10n ->t ('File could not be added to the archive. Please check the server logs for more information. ' );
260+ $ this ->missingInfo [$ filename ] = $ reason ;
261+ continue ;
262+ }
263+
263264 if ($ this ->reportMissingFiles && $ streamError !== null ) {
264265 $ this ->missingInfo [$ filename ] = $ streamError ;
265266 }
0 commit comments