forked from kaltura/server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
KDistributedFileManager.php
147 lines (130 loc) · 3.62 KB
/
KDistributedFileManager.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
/**
*
* @package Scheduler
*
*/
class KDistributedFileManager
{
private $fileCacheTimeout = null;
private $localRoot = null;
private $remoteRoot = null;
/**
* @param string $localRoot
* @param string $remoteRoot
* @param int $fileCacheTimeout
*/
public function __construct($localRoot, $remoteRoot, $fileCacheTimeout = null)
{
$this->localRoot = $localRoot;
$this->remoteRoot = $remoteRoot;
$this->fileCacheTimeout = $fileCacheTimeout;
}
public function getLocalPath($localPath, $remotePath, &$errDescription, &$fetched = false)
{
KalturaLog::info("Translating remote path [$remotePath] to local path [$localPath]");
if(file_exists($localPath))
{
if(!$this->fileCacheTimeout)
return true;
clearstatcache();
if(filemtime($localPath) > (time() - $this->fileCacheTimeout))
return true;
@unlink($localPath);
}
$fetched = true;
$res = $this->fetchFile($remotePath, $localPath, $errDescription);
if(!$res) {
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
KalturaLog::warning("Going to flush DNS: ");
$output = system( "ipconfig /flushdns" , $rc);
KalturaLog::warning($output);
}
}
return $res;
}
/**
* @param string $localPath
* @return string
*/
public function getRemoteUrl($localPath)
{
KalturaLog::debug("str_replace($this->localRoot, $this->remoteRoot, $localPath)");
return str_replace($this->localRoot, $this->remoteRoot, $localPath);
}
/**
* @param string $remotePath
* @param string $localPath
* @param string $errDescription
* @return boolean
*/
private function fetchFile($remotePath, $localPath, &$errDescription)
{
try
{
$folder = substr($localPath, 0, strrpos($localPath, '/'));
if(!file_exists($folder))
{
kFile::mkdir($folder, 777, true);
}
$curlWrapper = new KCurlWrapper();
$curlHeaderResponse = $curlWrapper->getHeader($remotePath, true, true);
$removeServer = isset($curlHeaderResponse->headers['X-Me']) ? $curlHeaderResponse->headers['X-Me'] : "unknown";
if(!$curlHeaderResponse || $curlWrapper->getError())
{
$errDescription = "Error: ($removeServer) " . $curlWrapper->getError();
return false;
}
if(!$curlHeaderResponse->isGoodCode())
{
$errDescription = "HTTP Error: ($removeServer) " . $curlHeaderResponse->code . " " . $curlHeaderResponse->codeName;
return false;
}
$fileSize = null;
if(isset($curlHeaderResponse->headers['content-length']))
$fileSize = $curlHeaderResponse->headers['content-length'];
$curlWrapper->close();
// overcome a 32bit issue with curl fetching >=4gb files
if (intval("9223372036854775807") == 2147483647 && $fileSize >= 4 * 1024 * 1024 * 1024)
{
unlink($localPath);
$cmd = "curl -s $remotePath -o $localPath";
KalturaLog::debug($cmd);
exec($cmd);
}
else
{
$curlWrapper = new KCurlWrapper();
$res = $curlWrapper->exec($remotePath, $localPath, null, true);
KalturaLog::debug("Curl results: $res");
if(!$res || $curlWrapper->getError())
{
$errDescription = "Error: ($removeServer) " . $curlWrapper->getError();
$curlWrapper->close();
return false;
}
$curlWrapper->close();
}
if(!file_exists($localPath))
{
$errDescription = "Error: output file doesn't exist";
return false;
}
if($fileSize)
{
clearstatcache();
if(kFile::fileSize($localPath) != $fileSize)
{
$errDescription = "Error: output file have a wrong size";
return false;
}
}
}
catch(Exception $ex)
{
$errDescription = "Error: " . $ex->getMessage();
return false;
}
return true;
}
}