|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace FileNamingResolver\NamingStrategy; |
|
4
|
|
|
|
|
5
|
|
|
use FileNamingResolver\FileInfo; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* @author Victor Bocharsky <[email protected]> |
|
9
|
|
|
*/ |
|
10
|
|
|
abstract class AbstractHashNamingStrategy extends AbstractNamingStrategy |
|
11
|
|
|
{ |
|
12
|
|
|
const ALGORITHM_MD5 = 'md5'; |
|
13
|
|
|
const ALGORITHM_SHA1 = 'sha1'; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var string Hash algorithm |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $algorithm; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var int Count of parts |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $partCount; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var int Length of each part |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $partLength; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var bool |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $shouldFullFilenameBeKept; |
|
34
|
|
|
|
|
35
|
10 |
|
/** |
|
36
|
|
|
* @param string $algorithm |
|
37
|
10 |
|
* @param int $partCount |
|
38
|
10 |
|
* @param int $partLength |
|
39
|
10 |
|
* @param bool $shouldFullFilenameBeKept |
|
40
|
10 |
|
*/ |
|
41
|
|
|
public function __construct($algorithm = self::ALGORITHM_MD5, $partCount = 2, $partLength = 2, $shouldFullFilenameBeKept = false) |
|
42
|
|
|
{ |
|
43
|
|
|
$this->algorithm = (string)$algorithm; |
|
44
|
|
|
$this->partCount = (int)$partCount; |
|
45
|
|
|
$this->partLength = (int)$partLength; |
|
46
|
|
|
$this->shouldFullFilenameBeKept = (bool)$shouldFullFilenameBeKept; |
|
47
|
|
|
} |
|
48
|
2 |
|
|
|
49
|
|
|
/** |
|
50
|
2 |
|
* @param FileInfo $srcFileInfo |
|
51
|
2 |
|
* @param string $hash |
|
52
|
2 |
|
* |
|
53
|
2 |
|
* @return FileInfo |
|
54
|
2 |
|
*/ |
|
55
|
2 |
|
public function provideNameByHash(FileInfo $srcFileInfo, $hash) |
|
56
|
|
|
{ |
|
57
|
2 |
|
$pathSuffixParts = array(); |
|
58
|
2 |
|
for ($i = 0; $i < $this->partCount; $i++) { |
|
59
|
2 |
|
$pathSuffixParts[] = substr($hash, $i * $this->partLength, $this->partLength); |
|
60
|
|
|
} |
|
61
|
2 |
|
if ($this->shouldFullFilenameBeKept) { |
|
62
|
|
|
$name = $hash; |
|
63
|
|
|
} else { |
|
64
|
|
|
$name = substr($hash, $i * $this->partLength); |
|
65
|
|
|
} |
|
66
|
|
|
$pathSuffix = implode(FileInfo::SEPARATOR_DIRECTORY, $pathSuffixParts); |
|
67
|
2 |
|
$dstFileInfo = $srcFileInfo |
|
68
|
|
|
->changeBasename($name) |
|
69
|
2 |
|
->changePath($srcFileInfo->getPath().FileInfo::SEPARATOR_DIRECTORY.$pathSuffix) |
|
70
|
|
|
; |
|
71
|
|
|
|
|
72
|
|
|
return $dstFileInfo; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
2 |
|
/** |
|
76
|
|
|
* @return string |
|
77
|
2 |
|
*/ |
|
78
|
|
|
public function getAlgorithm() |
|
79
|
|
|
{ |
|
80
|
|
|
return $this->algorithm; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
2 |
|
/** |
|
84
|
|
|
* @return int |
|
85
|
2 |
|
*/ |
|
86
|
|
|
public function getPartCount() |
|
87
|
|
|
{ |
|
88
|
|
|
return $this->partCount; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @return int |
|
93
|
|
|
*/ |
|
94
|
|
|
public function getPartLength() |
|
95
|
|
|
{ |
|
96
|
|
|
return $this->partLength; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @return boolean |
|
101
|
|
|
*/ |
|
102
|
|
|
public function shouldFullFilenameBeKept() |
|
103
|
|
|
{ |
|
104
|
|
|
return $this->shouldFullFilenameBeKept; |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|