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