I was lookin into Files/Node API but I wasn’t able to find good examples on how you could loop through users files. And I would like know more about File and Folder classes and how you could use those.
Any examples and tutorials would be greatly appreciated.
Thank you again for answering, those class definitions are great and your example helps a lot. I would like to loop through users files, read all the metadata from the file that is available and possibly move and/or tag the file. Those examples would help even more
Thank you so much. This is a great that I am possible to get help.
And I have little confusion with regarding the namespaces… There seems to be OC and OCP namespaces and the examples and the bootstrap application that is created seem to use these quite mixed.
You should stick to using OCP wherever possible. The OCP namespace is the “Public” namespace which contains all the interfaces that are available to be used by other apps. The OC namespace is generally where the specific implementations of the interfaces live, and your application should not rely on these.
As a sidenote, depending on what exactly you are trying to accomplish you may be able to do it with the files_scripts app (iterating over files and tagging them is certainly possible). This is an app I wrote which allows you to run scripts inside your nextcloud instance without the need to write a whole app. The scripts can be run with: flow, occ, or as file actions directly in the files app.
Regarding the namespace issue as mentioned by @Raudius see also my post. The OC is perfectly valid in the front end.
The documentation I have sent to you are from the public API definition, aka from the OCP namespace. So, these methods are save to use.
To give you an idea of this might be working, I can write you a small snippet. Do not get fooled, I did not test anything and it might simply be wrong.
/** @var ITags */
private $tagger;
/** @var IRootFolder */
private $rootFolder;
/** @var String */
private $userId;
public function __construct(ITag $tagger, IRootFolder $rootFolder, $user_id){
$this->$tagger = $tagger;
$this->rootFolder = $rootFolder;
$this->userId = $user_id;
}
// Method for parsing a single folder
private function parseFolder(Folder $folder): void {
// No handling of the folders was requested by the OT.
// That could/would go here.
foreach ($folder->getDirectoryListing() as $node) {
if ($node->getType() === FileInfo::TYPE_FOLDER) {
$this->parseFolder($node);
} else {
$this->parseFile($node);
}
}
}
// Extract the data of a single file
private function parseFile(File $file){
// Here we do the stuff with the files.
// This is rather costly. Maybe you can replace it with something more restricted?
$stat = $file->stat();
if (false /* ... */) {
$file->move("/the/target/path");
}
if (false /* ... */){
// Add tag to file
$fileId = $file->getId();
$tag = ...
$this->tagger->tagAs($fileId, $tag);
// This will only add new tags. If you want to remove them as well (sync them), you need to do that as well.
}
}
public function iterateFolder() {
$userFolder = $this->rootFolder->get($this->userId)->get('files');
$this->parseFolder($userFolder);
}