4343use OC \Files \Storage \Common ;
4444use OCP \Constants ;
4545use OCP \Files \FileInfo ;
46+ use OCP \Files \IMimeTypeDetector ;
4647use phpseclib \Net \SFTP \Stream ;
4748
4849/**
@@ -61,6 +62,7 @@ class SFTP extends Common {
6162 * @var \phpseclib\Net\SFTP
6263 */
6364 protected $ client ;
65+ private IMimeTypeDetector $ mimeTypeDetector ;
6466
6567 const COPY_CHUNK_SIZE = 8 * 1024 * 1024 ;
6668
@@ -118,6 +120,7 @@ public function __construct($params) {
118120
119121 $ this ->root = '/ ' . ltrim ($ this ->root , '/ ' );
120122 $ this ->root = rtrim ($ this ->root , '/ ' ) . '/ ' ;
123+ $ this ->mimeTypeDetector = \OC ::$ server ->get (IMimeTypeDetector::class);
121124 }
122125
123126 /**
@@ -460,7 +463,7 @@ public function rename($source, $target) {
460463 }
461464
462465 /**
463- * {@inheritdoc}
466+ * @return array{mtime: int, size: int, ctime: int}|false
464467 */
465468 public function stat ($ path ) {
466469 try {
@@ -499,9 +502,12 @@ public function file_put_contents($path, $data) {
499502
500503 public function writeStream (string $ path , $ stream , int $ size = null ): int {
501504 if ($ size === null ) {
502- $ stream = CountWrapper::wrap ($ stream , function ($ writtenSize ) use (&$ size ) {
505+ $ stream = CountWrapper::wrap ($ stream , function (int $ writtenSize ) use (&$ size ) {
503506 $ size = $ writtenSize ;
504507 });
508+ if (!$ stream ) {
509+ throw new \Exception ("Failed to wrap stream " );
510+ }
505511 }
506512 /** @psalm-suppress InternalMethod */
507513 $ result = $ this ->getConnection ()->put ($ this ->absPath ($ path ), $ stream );
@@ -559,26 +565,23 @@ public function getMetaData($path) {
559565 }
560566
561567 if ($ stat ['type ' ] === NET_SFTP_TYPE_DIRECTORY ) {
562- $ permissions = Constants::PERMISSION_ALL ;
568+ $ stat [ ' permissions ' ] = Constants::PERMISSION_ALL ;
563569 } else {
564- $ permissions = Constants::PERMISSION_ALL - Constants::PERMISSION_CREATE ;
570+ $ stat [ ' permissions ' ] = Constants::PERMISSION_ALL - Constants::PERMISSION_CREATE ;
565571 }
566572
567573 if ($ stat ['type ' ] === NET_SFTP_TYPE_DIRECTORY ) {
568574 $ stat ['size ' ] = -1 ;
569575 $ stat ['mimetype ' ] = FileInfo::MIMETYPE_FOLDER ;
570576 } else {
571- $ stat ['mimetype ' ] = \ OC :: $ server -> getMimeTypeDetector () ->detectPath ($ path );
577+ $ stat ['mimetype ' ] = $ this -> mimeTypeDetector ->detectPath ($ path );
572578 }
573579
574580 $ stat ['etag ' ] = $ this ->getETag ($ path );
575581 $ stat ['storage_mtime ' ] = $ stat ['mtime ' ];
576- $ stat ['permissions ' ] = $ permissions ;
577582 $ stat ['name ' ] = basename ($ path );
578583
579584 $ keys = ['size ' , 'mtime ' , 'mimetype ' , 'etag ' , 'storage_mtime ' , 'permissions ' , 'name ' ];
580585 return array_intersect_key ($ stat , array_flip ($ keys ));
581586 }
582-
583-
584587}
0 commit comments