Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion resources/dist/build/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"isEntry": true
},
"resources/js/statamic-content-usage.js": {
"file": "assets/statamic-content-usage-CpmTIWDB.js",
"file": "assets/statamic-content-usage-DUPpDHbL.js",
"name": "statamic-content-usage",
"src": "resources/js/statamic-content-usage.js",
"isEntry": true
Expand Down
110 changes: 98 additions & 12 deletions src/Services/AssetUsageService.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,31 +145,117 @@ protected function extractAssetsFromDataArray(array $data): Collection
return $assets;
}

// Find assets:: prefixed references
preg_match_all('/assets::[^"\'\\s}]+/', $json, $assetMatches);
$normalizedJson = str_replace('\\/', '/', $json);

// Find assets:: prefixed references (e.g. assets::container::path)
preg_match_all('/assets::[^"\'\\s}]+/', $normalizedJson, $assetMatches);

if (! empty($assetMatches[0])) {
foreach ($assetMatches[0] as $match) {
$assets->push($match);
$this->addAssetReference($assets, $match);
}
}

// Find plain asset paths (format: "fieldname":"path/to/file.ext")
preg_match_all('/"([^"]+)"\s*:\s*"([^"]*\/[^"]*\.[a-zA-Z0-9]{2,5})"/', $json, $pathMatches);
// Find asset:: prefixed references (Link field, Bard images: asset::container::path)
preg_match_all('/(?<!s)asset::[^"\'\\s}]+/', $normalizedJson, $assetRefMatches);

if (! empty($pathMatches[2])) {
foreach ($pathMatches[2] as $path) {
// Try to resolve the path to an asset ID
$assetId = $this->resolvePathToAssetId($path);
if ($assetId !== null) {
$assets->push($assetId);
}
if (! empty($assetRefMatches[0])) {
foreach ($assetRefMatches[0] as $match) {
$this->addAssetReference($assets, $match);
}
}

// Find statamic://asset:: references (Bard asset links)
preg_match_all('~statamic://asset::([^"\'?]+)~', $normalizedJson, $statamicMatches);

if (! empty($statamicMatches[1])) {
foreach ($statamicMatches[1] as $id) {
$this->addAssetReference($assets, $id);
}
}

// Find plain asset paths in field values and arrays (e.g. "image.jpg" or "folder/image.jpg")
preg_match_all('/"([^"]+\.[a-zA-Z0-9]{2,5})"/', $normalizedJson, $pathMatches);

if (! empty($pathMatches[1])) {
foreach ($pathMatches[1] as $path) {
$this->addResolvedAssetPath($assets, $path);
}
}

// Find asset paths in URLs (e.g. https://example.com/assets/folder/file.pdf in Bard links)
preg_match_all('~/assets/([^"\'?]+)~', $normalizedJson, $urlMatches);

if (! empty($urlMatches[1])) {
foreach ($urlMatches[1] as $path) {
$this->addResolvedAssetPath($assets, $path);
}
}

return $assets->unique()->values();
}

/**
* @param Collection<int, string> $assets
*/
protected function addAssetReference(Collection $assets, string $reference): void
{
if (str_starts_with($reference, 'assets::')) {
/** @var ?Asset $asset */
$asset = AssetFacade::find($reference);

if ($asset) {
$assets->push($asset->id());
}

return;
}

if (str_starts_with($reference, 'asset::')) {
/** @var ?Asset $asset */
$asset = AssetFacade::find(substr($reference, strlen('asset::')));

if ($asset) {
$assets->push($asset->id());
}

return;
}

/** @var ?Asset $asset */
$asset = AssetFacade::find($reference);

if ($asset) {
$assets->push($asset->id());
}
}

/**
* @param Collection<int, string> $assets
*/
protected function addResolvedAssetPath(Collection $assets, string $path): void
{
if (
str_starts_with($path, 'assets::')
|| str_starts_with($path, 'asset::')
|| str_starts_with($path, 'entry::')
|| str_starts_with($path, 'http://')
|| str_starts_with($path, 'https://')
) {
if (str_starts_with($path, 'asset::')) {
$this->addAssetReference($assets, $path);
}

return;
}

$assetId = $this->resolvePathToAssetId($path);

if ($assetId !== null) {
$assets->push($assetId);
}
}

protected function resolvePathToAssetId(string $path): ?string
{
$path = trim($path, '/');
Expand Down
204 changes: 202 additions & 2 deletions tests/Services/AssetUsageServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,25 @@ public function it_skips_assets_that_cannot_be_found(): void
$this->assertEmpty($result);
}

#[Test]
public function it_skips_assets_that_cannot_be_found_during_processing(): void
{
$entryData = $this->createEntryData(['image' => 'assets::test_container::test-image.jpg']);
$collection = $this->createPageCollection();
$entry = $this->createEntry($entryData, $collection);
$asset = $this->createAsset('assets::test_container::test-image.jpg', 'test-image.jpg', 'https://example.com/test-image.jpg', 'test-image.jpg');

EntryFacade::shouldReceive('all')->andReturn(new EntryCollection([$entry]));
AssetFacade::shouldReceive('find')
->with('assets::test_container::test-image.jpg')
->andReturn($asset, null);

$service = new AssetUsageService;
$result = $service->findAssetUsage();

$this->assertEmpty($result);
}

#[Test]
public function it_deduplicates_pages_for_the_same_asset(): void
{
Expand Down Expand Up @@ -160,6 +179,187 @@ public function it_handles_entries_with_invalid_json_data(): void
$this->assertEmpty($result);
}

#[Test]
public function it_can_resolve_plain_asset_filenames_without_slashes(): void
{
$entryData = $this->createEntryData(['background_image' => 'test-not-to-be-deleted.jpg']);
$collection = $this->createPageCollection();
$entry = $this->createEntry($entryData, $collection);
$container = $this->createAssetContainer('assets');
$asset = $this->createAsset('assets::assets::test-not-to-be-deleted.jpg', 'test-not-to-be-deleted.jpg', 'https://example.com/test-not-to-be-deleted.jpg', 'test-not-to-be-deleted.jpg');

EntryFacade::shouldReceive('all')->andReturn(new EntryCollection([$entry]));
AssetContainerFacade::shouldReceive('all')->andReturn(collect([$container]));
AssetFacade::shouldReceive('find')
->with('assets::test-not-to-be-deleted.jpg')
->andReturn($asset);
AssetFacade::shouldReceive('find')
->with('assets::assets::test-not-to-be-deleted.jpg')
->andReturn($asset);

$service = new AssetUsageService;
$result = $service->findAssetUsage();

$this->assertCount(1, $result);
$first = $result->first();
$this->assertNotNull($first);
$this->assertEquals('assets::assets::test-not-to-be-deleted.jpg', $first->assetId);
}

#[Test]
public function it_can_resolve_asset_paths_in_arrays(): void
{
$entryData = $this->createEntryData(['icon' => ['logo/hart-voor-gastouderopvang.svg']]);
$collection = $this->createPageCollection();
$entry = $this->createEntry($entryData, $collection);
$container = $this->createAssetContainer('assets');
$asset = $this->createAsset('assets::assets::logo/hart-voor-gastouderopvang.svg', 'logo/hart-voor-gastouderopvang.svg', 'https://example.com/logo/hart-voor-gastouderopvang.svg', 'hart-voor-gastouderopvang.svg');

EntryFacade::shouldReceive('all')->andReturn(new EntryCollection([$entry]));
AssetContainerFacade::shouldReceive('all')->andReturn(collect([$container]));
AssetFacade::shouldReceive('find')
->with('assets::logo/hart-voor-gastouderopvang.svg')
->andReturn($asset);
AssetFacade::shouldReceive('find')
->with('assets::assets::logo/hart-voor-gastouderopvang.svg')
->andReturn($asset);

$service = new AssetUsageService;
$result = $service->findAssetUsage();

$this->assertCount(1, $result);
$first = $result->first();
$this->assertNotNull($first);
$this->assertEquals('assets::assets::logo/hart-voor-gastouderopvang.svg', $first->assetId);
}

#[Test]
public function it_can_resolve_asset_references_with_asset_prefix(): void
{
$entryData = $this->createEntryData(['button_link' => 'asset::test_container::downloads/test-document.pdf']);
$collection = $this->createPageCollection();
$entry = $this->createEntry($entryData, $collection);
$asset = $this->createAsset(
'assets::test_container::downloads/test-document.pdf',
'downloads/test-document.pdf',
'https://example.com/assets/downloads/test-document.pdf',
'test-document.pdf'
);

EntryFacade::shouldReceive('all')->andReturn(new EntryCollection([$entry]));
AssetFacade::shouldReceive('find')
->with('test_container::downloads/test-document.pdf')
->andReturn($asset);
AssetFacade::shouldReceive('find')
->with('assets::test_container::downloads/test-document.pdf')
->andReturn($asset);

$service = new AssetUsageService;
$result = $service->findAssetUsage();

$this->assertCount(1, $result);
$first = $result->first();
$this->assertNotNull($first);
$this->assertEquals('assets::test_container::downloads/test-document.pdf', $first->assetId);
}

#[Test]
public function it_can_resolve_statamic_asset_urls(): void
{
$entryData = $this->createEntryData([
'content' => [
[
'type' => 'text',
'text' => 'Download',
'marks' => [
[
'type' => 'link',
'attrs' => [
'href' => 'statamic://asset::test_container::downloads/test-document.pdf',
],
],
],
],
],
]);
$collection = $this->createPageCollection();
$entry = $this->createEntry($entryData, $collection);
$asset = $this->createAsset(
'assets::test_container::downloads/test-document.pdf',
'downloads/test-document.pdf',
'https://example.com/assets/downloads/test-document.pdf',
'test-document.pdf'
);

EntryFacade::shouldReceive('all')->andReturn(new EntryCollection([$entry]));
AssetFacade::shouldReceive('find')
->with('test_container::downloads/test-document.pdf')
->andReturn($asset);
AssetFacade::shouldReceive('find')
->with('assets::test_container::downloads/test-document.pdf')
->andReturn($asset);

$service = new AssetUsageService;
$result = $service->findAssetUsage();

$this->assertCount(1, $result);
$first = $result->first();
$this->assertNotNull($first);
$this->assertEquals('assets::test_container::downloads/test-document.pdf', $first->assetId);
}

#[Test]
public function it_can_resolve_asset_paths_in_bard_link_urls(): void
{
$entryData = $this->createEntryData([
'content' => [
[
'type' => 'paragraph',
'content' => [
[
'type' => 'text',
'marks' => [
[
'type' => 'link',
'attrs' => [
'href' => 'https://example.com/assets/downloads/test-document.pdf',
],
],
],
'text' => 'Download',
],
],
],
],
]);
$collection = $this->createPageCollection();
$entry = $this->createEntry($entryData, $collection);
$container = $this->createAssetContainer('assets');
$asset = $this->createAsset(
'assets::assets::downloads/test-document.pdf',
'downloads/test-document.pdf',
'https://example.com/assets/downloads/test-document.pdf',
'test-document.pdf'
);

EntryFacade::shouldReceive('all')->andReturn(new EntryCollection([$entry]));
AssetContainerFacade::shouldReceive('all')->andReturn(collect([$container]));
AssetFacade::shouldReceive('find')
->with('assets::downloads/test-document.pdf')
->andReturn($asset);
AssetFacade::shouldReceive('find')
->with('assets::assets::downloads/test-document.pdf')
->andReturn($asset);

$service = new AssetUsageService;
$result = $service->findAssetUsage();

$this->assertCount(1, $result);
$first = $result->first();
$this->assertNotNull($first);
$this->assertEquals('assets::assets::downloads/test-document.pdf', $first->assetId);
}

#[Test]
public function it_can_resolve_plain_asset_paths_to_asset_ids(): void
{
Expand Down Expand Up @@ -271,8 +471,8 @@ public function it_skips_assets_that_cannot_be_found_during_container_filtering(
});

EntryFacade::shouldReceive('all')->andReturn(new EntryCollection([$entry]));
AssetFacade::shouldReceive('find')->with('assets::main::test-image.jpg')->andReturn($assetMain, $assetMain);
AssetFacade::shouldReceive('find')->with('assets::images::logo.jpg')->andReturn($assetImages, null);
AssetFacade::shouldReceive('find')->with('assets::main::test-image.jpg')->andReturn($assetMain, $assetMain, $assetMain);
AssetFacade::shouldReceive('find')->with('assets::images::logo.jpg')->andReturn($assetImages, $assetImages, null);

$service = new AssetUsageService;
$result = $service->findAssetUsage(['main']);
Expand Down
Loading