Jump to content

Moving files from folder to file fields. [SOLVED]


elabx
 Share

Recommended Posts

I'm trying to add some files uploaded through FTP to a files field within a page.

  • File is uploaded to /assets/{order_id} through FTP
  • ProcessWire page has the order_id saved in a field to figure out the path later, and when the page is saved, trigggers the hook, it goes to the required folder and tries to pick the files to save them in the processwire image field (named 'images') or a repeater containing a file field to save  videos. (repeater named 'videos', with a field withing it called 'video_file').

The main issue I'm having is that I don't want to copy the files twice if they already exist but for some reason the findOne()  method on repeaters to find already upload files is returning false as if the file didn't exist ?

PW version  3.0.184

EDIT: The save filename saved  is obviously not the same since it's been sanitized lol ?

$wire->addHookAfter('Pages::saved', function($event) {
    $page = $event->arguments(0);

	if($page->template->name != "template_name") return;
    
    $iterator = new \DirectoryIterator("{$this->config->paths->root}assets/{$page->order_id}");
   
    foreach ($iterator as $fileinfo){

        if ($fileinfo->isFile()) {
            $path = $fileinfo->getPathname();
            $file_name = $this->sanitizer->selectorValue($fileinfo->getFilename());
            switch($fileinfo->getExtension()){
                case "mp4":
                    //video_file is a field within the repeaters
                    $selector = "video_file={$file_name}";
                    // 'videos' is a repeater field
                    $found = $page->videos->findOne($selector);
                    // This one is returning false  all the time :( 
                    if(!$found){
                        $newVideo = $page->videos->getNew();
                        $newVideo->save();
                        $newVideo->video_file->add($path);
                        $newVideo->save('video_file');
                        $page->videos->add($newVideo);
                        $page->save('videos');
                    }else{
                        bd("Found already added!");
                    }
                    break;
                case "jpg":
                    // This one is returning false  all the time :( 
                    if(!$page->images->findOne("name=$file_name")) {
                        $page->images->add($path);
                        $page->save('images');
                    }
                    break;
            }
        }
    }
});

 

Link to comment
Share on other sites

Ended up as sth like this:

$wire->addHookAfter('Pages::saved', function($event) {
    $page = $event->arguments(0);
    if($page->template->name != "collection") return;
    try{
        $iterator = new \DirectoryIterator("{$this->config->paths->root}assets/{$page->order_id}");
    } catch(\Exception $e){
        $this->warning("Directory with resources not ready");
        return;
    }

    foreach ($iterator as $fileinfo){

        if ($fileinfo->isFile()) {
            $path = $fileinfo->getPathname();
            $file_name = $this->sanitizer->filename($fileinfo->getFilename());
            switch($fileinfo->getExtension()){
                case "mp4":

                    $found = false;
                    foreach($page->videos as $video){
                        $filename = $video->video_file->cleanBasename($file_name, false, false, true);
                        if($filename == $video->video_file){
                            $found = true;
                        }
                    }
                    if(!$found){
                        $newVideo = $page->videos->getNew();
                        $newVideo->save();
                        $newVideo->video_file->add($path);
                        $newVideo->save('video_file');
                        $page->videos->add($newVideo);
                        $page->save('videos');
                    }
                    break;
                case "jpg":
                    $file_name = $page->images->cleanBasename($file_name, false, false, true);
                    if(!$page->images->findOne("name=$file_name")) {
                        $page->images->add($path);
                        $page->save('images');
                    }
                    break;
            }
        }
    }
});

 

Link to comment
Share on other sites

  • elabx changed the title to Moving files from folder to file fields. [SOLVED]

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...