5454use OC \Files \Filesystem ;
5555use OC \Files \Storage \Common ;
5656use OCA \Files_External \Lib \Notify \SMBNotifyHandler ;
57+ use OCP \Constants ;
5758use OCP \Files \Notify \IChange ;
5859use OCP \Files \Notify \IRenameChange ;
5960use OCP \Files \Storage \INotifyStorage ;
@@ -96,7 +97,7 @@ public function __construct($params) {
9697 if (isset ($ params ['auth ' ])) {
9798 $ auth = $ params ['auth ' ];
9899 } else if (isset ($ params ['user ' ]) && isset ($ params ['password ' ]) && isset ($ params ['share ' ])) {
99- list ( $ workgroup , $ user) = $ this ->splitUser ($ params ['user ' ]);
100+ [ $ workgroup , $ user] = $ this ->splitUser ($ params ['user ' ]);
100101 $ auth = new BasicAuth ($ user , $ workgroup , $ params ['password ' ]);
101102 } else {
102103 throw new \Exception ('Invalid configuration, no credentials provided ' );
@@ -205,30 +206,31 @@ protected function throwUnavailable(\Exception $e) {
205206 * @return \Icewind\SMB\IFileInfo[]
206207 * @throws StorageNotAvailableException
207208 */
208- protected function getFolderContents ($ path ) {
209+ protected function getFolderContents ($ path ): iterable {
209210 try {
210211 $ path = ltrim ($ this ->buildPath ($ path ), '/ ' );
211212 $ files = $ this ->share ->dir ($ path );
212213 foreach ($ files as $ file ) {
213214 $ this ->statCache [$ path . '/ ' . $ file ->getName ()] = $ file ;
214215 }
215- return array_filter ($ files , function (IFileInfo $ file ) {
216+
217+ foreach ($ files as $ file ) {
216218 try {
217219 // the isHidden check is done before checking the config boolean to ensure that the metadata is always fetch
218220 // so we trigger the below exceptions where applicable
219221 $ hide = $ file ->isHidden () && !$ this ->showHidden ;
220222 if ($ hide ) {
221223 $ this ->logger ->debug ('hiding hidden file ' . $ file ->getName ());
222224 }
223- return !$ hide ;
225+ if (!$ hide ) {
226+ yield $ file ;
227+ }
224228 } catch (ForbiddenException $ e ) {
225229 $ this ->logger ->logException ($ e , ['level ' => ILogger::DEBUG , 'message ' => 'Hiding forbidden entry ' . $ file ->getName ()]);
226- return false ;
227230 } catch (NotFoundException $ e ) {
228231 $ this ->logger ->logException ($ e , ['level ' => ILogger::DEBUG , 'message ' => 'Hiding not found entry ' . $ file ->getName ()]);
229- return false ;
230232 }
231- });
233+ }
232234 } catch (ConnectException $ e ) {
233235 $ this ->logger ->logException ($ e , ['message ' => 'Error while getting folder content ' ]);
234236 throw new StorageNotAvailableException ($ e ->getMessage (), $ e ->getCode (), $ e );
@@ -507,6 +509,46 @@ public function touch($path, $time = null) {
507509 }
508510 }
509511
512+ public function getMetaData ($ path ) {
513+ $ fileInfo = $ this ->getFileInfo ($ path );
514+ if (!$ fileInfo ) {
515+ return null ;
516+ }
517+
518+ return $ this ->getMetaDataFromFileInfo ($ fileInfo );
519+ }
520+
521+ private function getMetaDataFromFileInfo (IFileInfo $ fileInfo ) {
522+ $ permissions = Constants::PERMISSION_READ + Constants::PERMISSION_SHARE ;
523+
524+ if (!$ fileInfo ->isReadOnly ()) {
525+ $ permissions += Constants::PERMISSION_DELETE ;
526+ $ permissions += Constants::PERMISSION_UPDATE ;
527+ if ($ fileInfo ->isDirectory ()) {
528+ $ permissions += Constants::PERMISSION_CREATE ;
529+ }
530+ }
531+
532+ $ data = [];
533+ if ($ fileInfo ->isDirectory ()) {
534+ $ data ['mimetype ' ] = 'httpd/unix-directory ' ;
535+ } else {
536+ $ data ['mimetype ' ] = \OC ::$ server ->getMimeTypeDetector ()->detectPath ($ fileInfo ->getPath ());
537+ }
538+ $ data ['mtime ' ] = $ fileInfo ->getMTime ();
539+ if ($ fileInfo ->isDirectory ()) {
540+ $ data ['size ' ] = -1 ; //unknown
541+ } else {
542+ $ data ['size ' ] = $ fileInfo ->getSize ();
543+ }
544+ $ data ['etag ' ] = $ this ->getETag ($ fileInfo ->getPath ());
545+ $ data ['storage_mtime ' ] = $ data ['mtime ' ];
546+ $ data ['permissions ' ] = $ permissions ;
547+ $ data ['name ' ] = $ fileInfo ->getName ();
548+
549+ return $ data ;
550+ }
551+
510552 public function opendir ($ path ) {
511553 try {
512554 $ files = $ this ->getFolderContents ($ path );
@@ -518,10 +560,17 @@ public function opendir($path) {
518560 $ names = array_map (function ($ info ) {
519561 /** @var \Icewind\SMB\IFileInfo $info */
520562 return $ info ->getName ();
521- }, $ files );
563+ }, iterator_to_array ( $ files) );
522564 return IteratorDirectory::wrap ($ names );
523565 }
524566
567+ public function getDirectoryContent ($ directory ): iterable {
568+ $ files = $ this ->getFolderContents ($ directory );
569+ foreach ($ files as $ file ) {
570+ yield $ this ->getMetaDataFromFileInfo ($ file );
571+ }
572+ }
573+
525574 public function filetype ($ path ) {
526575 try {
527576 return $ this ->getFileInfo ($ path )->isDirectory () ? 'dir ' : 'file ' ;
0 commit comments