Adding files to file manager

Permalink 1 user found helpful
Okay, so I've created a theme package with some custom icons and for the life of me, I CANNOT get the files to load into the file manager. The rest of the package loads fine??

I used the code from the documentation in my controller file, because that's where I figured that it goes:
Loader::library("file/importer");
$fi = new FileImporter();
$newFile = $fi->import('/themes/playful/images/social-media-icons/facebook.png','facebook.png');


Please help, I've been at this for two days and have yet to see any one example.

justrj
 
jasteele12 replied on at Permalink Reply
jasteele12
My guess is that you don't actually have a /themes directory, but in either case you should check the return value:

Loader::library('file/importer');
$fi = new FileImporter();
$newFile = $fi->import('/themes/playful/images/social-media-icons/facebook.png','facebook.png');
if(!is_object($newFile)) {
  echo $fi->getErrorMessage($newFile);
}


Untested, but should get you on the right path...
justrj replied on at Permalink Reply
justrj
Thanks jasteele123!
My packages directory is: packages/theme_playful(controller.php sits here)/themes/playful/images/social-media-icons

I thought writing the path relative to the controller would work, but as you saw...no go.

I tried adding the return echo, but since the code is in the controller, the echo doesn't print anywhere that I can see. I tried adding the same code to one of the page type files, but that was a bust as well. I checked the logs table in the database. I didn't see anything to point me in the right direction there.

Any other thoughts?
jasteele12 replied on at Permalink Reply
jasteele12
You might try viewing the browser source, but you can always log it yourself:
if(!is_object($newFile)) {
  Log::addEntry($fi->getErrorMessage($newFile));
}


You could also just throw an exception.
justrj replied on at Permalink Reply
justrj
Thanks - That logged an invalid file error, so now to work from there.
RJ
jasteele12 replied on at Permalink Best Answer Reply
jasteele12
I'm guessing it should be packages/theme_playful/themes/ instead of /themes/

FYI, technically the error testing should be:

if ($newFile instanceof FileVersion) {


instead of:

if(is_object($newFile)) {


Good luck!
jasteele12 replied on at Permalink Reply
jasteele12
Hi RJ,

I think this might be what you want to do. This code is untested, but should be fairly close :)
Loader::library('file/importer');
$fi = new FileImporter();
$fh = Loader::helper('file');
  //
$dir = $pkg->packagePath();
$iconPath = $dir. '/themes/playful/images/social-media-icons';
$files = $fh->getDirectoryContents($iconPath);
foreach($files as $file) {
  $newFile = $fi->import($iconPath'. '/'. $file, $file);
  if ($newFile instanceof FileVersion) {
    continue;  // or add to sets or whatever
  }
  else {
    Log::addEntry($fi->getErrorMessage($newFile));
  }

Hope that helps!

John
justrj replied on at Permalink Reply
justrj
Thanks so much John.

The path that I used was the problem it has to be absolute from the root package directory, not relative to the controller.

For anyone's reference who may find this the end code did use John's idea of making the whole thing extensible by concatinating the path with the individual files in an array.
Loader::library('file/importer');
      $fh = Loader::helper('file');
      //load icons to file manager
         $fi = new FileImporter();
         $iconPath = 'packages/theme_playful/themes/playful/images/social-media-icons/';
         $files = $fh->getDirectoryContents($iconPath);
         foreach($files as $file) {
            $newFile = $fi->import($iconPath.$file);
         }


I adjusted the other code to:
Loader::library('file/importer');
         $fi = new FileImporter();
         $fh = Loader::helper('file');
      $iconPath = 'packages/theme_playful/themes/playful/images/social-media-icons/';
      $files = $fh->getDirectoryContents($iconPath);
      foreach ($files as $file) {
         $newFile = $fi->import($iconPath. $file);
            if ($newFile instanceof FileVersion) {
                continue;  // or add to sets or whatever
              }
            else {
                Log::addEntry($fi->getErrorMessage($newFile));
              }
      }


It tested fine and didn't throw any error, but wouldn't load any files either. When I took out the if/else - it started loading fine.

Thanks again! You have saved me hours and hours of bashing my head against a keyboard...my keyboard also thanks you. :-)