-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAssetFingerprintsPlugin.php
90 lines (72 loc) · 2.56 KB
/
AssetFingerprintsPlugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
namespace Craft;
class AssetFingerprintsPlugin extends BasePlugin
{
public function init()
{
parent::init();
craft()->on('assets.onSaveAsset', function(Event $event)
{
$asset = $event->params['asset'];
Craft::log("[assetfingerprint] Atttempting to fingerprint: ".$asset->filename, LogLevel::Info, true);
$assetStateIndicator = "existing";
$assetTimestamp = $asset->dateModified->getTimestamp();
if($event->params['isNewAsset']) // New asset
{
$assetStateIndicator = "new";
$assetTimestamp = time();
}
Craft::log("[assetfingerprint] ".$assetStateIndicator." asset: ".$asset->filename, LogLevel::Info);
if($this->filenameHasFingerprint($asset->filename))
{
Craft::log("[assetfingerprint] ".$assetStateIndicator." asset already has timestamp in filename: ".$asset->filename, LogLevel::Info, true);
}
else
{
Craft::log("[assetfingerprint] ".$assetStateIndicator." asset does NOT have timestamp in filename: ".$asset->filename, LogLevel::Info);
// Generate new filename for new asset, using current time as fingerprint
$updatedFilename = $this->newFingerprintFilename($asset->filename, $assetTimestamp);
Craft::log("[assetfingerprint] ".$assetStateIndicator." asset filename with timestamp:".$updatedFilename, LogLevel::Info);
// Set the new filename to the asset and update the files accordingly.
$asset->setAttribute('filename', $updatedFilename);
craft()->assets->renameFile($asset, $updatedFilename);
$event->performAction = false;
}
});
}
private function filenameHasFingerprint($filename)
{
// Check for timestamp in the filename.
// E.g. logo.1234567890.png vs. logo.png
return preg_match('/\.\d{10}\..{2,4}$/', $filename) != 0;
}
private function filenameDoesntHaveFingerprint($filename)
{
return $this->filenameHasFingerprint($filename) == false;
}
private function newFingerprintFilename($filename, $timestamp)
{
$assetPathInfo = pathinfo($filename);
return join(".", [$assetPathInfo['filename'], $timestamp, $assetPathInfo['extension']]);
}
function getName()
{
return Craft::t('Asset Fingerprints');
}
function getVersion()
{
return '0.4';
}
function getDeveloper()
{
return 'The Refinery, LLC';
}
function getDeveloperUrl()
{
return 'http://the-refinery.io/';
}
function getDocumentationUrl()
{
return 'https://github.com/the-refinery/asset-fingerprint-craft-plugin/blob/master/README.md';
}
}