Jump to content

Hook processInputFile and processInputDeleteFile


cwsoft
 Share

Recommended Posts

Hi,

I added the following hook into site/ready.php to extract the contents of an uploaded ZIP archive to a specific folder (site/assets/files/uploads). The first steps works just fine.

// Hook into input file field to extract zip files into specific target folder.
$wire->addHookAfter('InputfieldFile::processInputFile', function (HookEvent $event) {
    // Limit unzip handler to specific file fields on specific template pages.
    $pageFile = $event->arguments('pagefile');
    $page = $pageFile?->page;
    if (!$page || $page->template->name !== 'XX' || $pageFile->field->name !== 'XX_zip') 
		return $event->return;

    // Build path to specific target folder, where ZIP files gets extracted to.
    $files = $this->wire('files');
    $extractPath = $this->wire('config')->paths->assets . "files/uploads";

    // Ensure destination folder for files to extract exists.
    if (!$files->exists($extractPath, 'dir')) {
        $files->mkdir($extractPath, $recursive = true);
    }

    // Extract ZIP file into specific folder.
    if ($files->exists($extractPath, 'writeable dir')) {
        $items = $files->unzip($pageFile->filename, $extractPath);
    }
    return $event->return;
});

Then I tried to remove the automatically extracted files from the specific folder, once the uploaded ZIP files gets deleted via the backend via the hook below (again in site/ready.php). The code below works standalone, but not in combination with the other hook above.

Issue: Once the code in the delete hook (below) was executed, the code in the processInputFile hook (above) kicks in again, causing the unpacking of the files from the uploaded ZIP archive again. The ZIP file gets deleted during the process, but the unpacked files remain inside /site/assets/files/uploads.

// Hook into file field to remove extracted files from specific folder once ZIP file gets deleted.
$wire->addHookAfter('InputfieldFile::processInputDeleteFile', function (HookEvent $event) {
    // Limit unzip handler to specific file fields on specific template pages.
    $pageFile = $event->arguments('pagefile');
    $page = $pageFile?->page;
    if (!$page || $page->template->name !== 'XX' || $pageFile->field->name !== 'XX_zip') 
		return $event->return;

    // Build path to target folder.
    $files = $this->wire('files');
    $extractPath = $this->wire('config')->paths->assets . "files/uploads";

    // Delete zip folder when ZIP file gets deleted.
    $files->rmdir($extractPath, $recursive = true);

    return $event->return;
});

Also tried to hook to processInputAddFile without success.

Is there any way I can check inside processInputFile, if the hook was triggered by the delete or upload/add action?

Thanks in advance for any hints or comments on this issue.

Edited by cwsoft
Link to comment
Share on other sites

Hi. Added a "workaround" to solve my issue by setting a session variable at the end of the processInputDelete Hook to indicate, that the ZIP file was deleted. Then I check in the processInputHook if this session variable exists and is true to skip the part to extract the content of the ZIP file again. That seems to work.

However I doubt this is the best solution to deal with this issue. Is there a more PW like way achieving the same without the ugly session variable hack? Any hints or tips on this topic are welcome.

Link to comment
Share on other sites

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...