Forum

Thread tagged as: Problem, Error, Api

Imported assets being delete by clear task

Hi,

I've been working on importing a site from Wordpress to Perch using the content importer APIs. Today I've noticed that the imported assets have been removed by the resource cleanup script - but only for the resized variants. The original files are still there however the cropped versions have been deleted. For example:

  1. Import content and create new asset 'banner-image.jpg', perch adds 'banner-image.jpg' and 'banner-image-thumb@2x.jpg' to the resources folder.
  2. Assign the ID to the banner field in the template
  3. Perch creates 'banner-image-w640h480.jpg' and 'banner-image-w360h220.jpg' alongside the imported content
  4. 24 hours later 'banner-image-w640h480.jpg' and 'banner-image-w360h220.jpg' are no longer there.

Here's the script I've been using:

function extract_images($post)
{
    $images = [];

    // Fetch embedded images
    $s = '/<img[^>]*>/';
    $count = preg_match_all($s, $post->content, $matches);

    if ($count) {
        foreach ($matches as $match) {
            foreach ($match as $img) {
                $Tag = new PerchXMLTag($img);

                $name = basename($Tag->src());
                $path = parse_url($Tag->src())['path'];
                $key = md5($name);

                $images[$key] = [
                    'filename' => $name,
                    'path'     => $path,
                    'url'      => $Tag->src(),
                    'raw'      => $img,
                    'assetID'  => false,
                    'exists'   => file_exists('./media/' . $path)
                ];
            }
        }
    }

    // Process attachments
    foreach ($post->media as $media) {

        $name = basename($media->attatchment_url);
        $path = parse_url($media->attatchment_url)['path'];
        $key = md5($name);

        if (!array_key_exists($key, $images)) {
            $images[$key] = [
                'filename' => $name,
                'path'     => $path,
                'url'      => $media->attatchment_url,
                'raw'      => false,
                'assetID'  => false,
                'exists'   => file_exists('./media/' . $path)
            ];
        }
    }

    return $images;
}

foreach ($data as $post) {

    /**
     * Extract Images
     */
    $images = extract_images($post);
    $banner = false;

    /**
     * Import Assets
     */
    foreach ($images as &$image) {

        if (!$image['exists']) {
            continue;
        }

        try {

            $result = $AssetImporter->add_item([
                'type'   => 'image',
                'bucket' => 'blog',
                'path'   => './media/' . $image['path']
            ]);

            $image['assetID'] = (int) $result['id'];

            if (!$banner) {
                $banner = $image;
            }

        } catch (Exception $ex) {
            PerchUtil::debug($ex->getMessage(), 'error');
        }

    }

    /**
     * Format text in post
     */
    $post->excerpt = get_post_excerpt($post);
    $post->content = autop($post->content);

    /**
     * Replace embedded assets
     */
    foreach ($images as $image) {
        if ($image['raw']) {
            $replacement = $image['exists'] ? '[cms:asset ' . $image['assetID'] . ']' : '';
            $post->content = str_replace($image['raw'], $replacement, $post->content);
        }
    }

    /**
     * Import post
     */
    $postData = [
        'title'    => $post->title,
        'content'  => $post->content,
        'excerpt'  => $post->excerpt
    ];

    if ($banner) {
        $postData['banner'] = true;
        $postData['banner_assetID'] = $banner['assetID'];
    }

    $result = $PostImporter->add_item($postData);
}

I am aware I can turn off the resource cleanup task in the config but I'd rather not do that if there is a way around this.

James Wigger

James Wigger 0 points

  • 3 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

Do you see all the assets being logged when they're created?