Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unused salsify images after a pull #55

Open
mak001 opened this issue Jun 25, 2020 · 2 comments
Open

Remove unused salsify images after a pull #55

mak001 opened this issue Jun 25, 2020 · 2 comments
Labels
enhancement New feature or request
Milestone

Comments

@mak001
Copy link
Contributor

mak001 commented Jun 25, 2020

Would help with keeping the files section clean and keeping the number of assets down

@mak001 mak001 added the enhancement New feature or request label Jun 25, 2020
@mak001 mak001 added this to the 1.0.0-beta milestone Jun 25, 2020
@mak001
Copy link
Contributor Author

mak001 commented Oct 28, 2020

@mak001
Copy link
Contributor Author

mak001 commented Nov 13, 2020

could be done like the following. The only issue doing it this way is if the image is related to a non-salsify object.

    /**
     * @param $file
     * @param bool $multiple
     */
    public function onAfterMap($file, $multiple)
    {
        if ($multiple == Mapper::$SINGLE) {
            return;
        }

        // Images and Files
        $this->owner->resetAssetStream();
        $assetStream = $this->owner->getAssetStream();
        $this->archiveUnused($assetStream, File::class, 'assets', true);
    }

    /**
     * @param JsonMachine $stream
     * @param string|DataObject $class
     * @param string $typeName
     * @param bool $excludeNull If things with a null salsify ID should be archived
     */
    private function archiveUnused($stream, $class, $typeName, $excludeNull = false)
    {
        $codes = [];
        if ($excludeNull) {
            $codes[] = null;
        }

        foreach ($this->owner->yieldKeyVal($stream) as $name => $data) {
            $codes[] = $data['salsify:id'];
        }

        $invalidObjects = $class::get()->exclude([
            'SalsifyID' => $codes,
        ]);

        $count = 0;
        foreach ($this->owner->yieldSingle($invalidObjects) as $invalid) {
            /** @var File $invalidProduct */
            $invalid->doArchive();
            $count++;
        }
        ImportTask::output("Archived {$count} {$typeName}.");
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant