Use helper method for content length and last modified

Signed-off-by: Daniel Kesselberg <mail@danielkesselberg.de>
This commit is contained in:
Daniel Kesselberg 2018-10-01 20:18:36 +02:00
parent 67d5380b91
commit 5cf38254bb
No known key found for this signature in database
GPG Key ID: 36E3664E099D0614
1 changed files with 54 additions and 11 deletions

View File

@ -61,10 +61,14 @@ class AmazonS3 extends \OC\Files\Storage\Common {
/** @var CappedMemoryCache|Result[] */
private $objectCache;
/** @var CappedMemoryCache|array */
private $filesCache;
public function __construct($parameters) {
parent::__construct($parameters);
$this->parseParams($parameters);
$this->objectCache = new CappedMemoryCache();
$this->filesCache = new CappedMemoryCache();
}
/**
@ -302,10 +306,11 @@ class AmazonS3 extends \OC\Files\Storage\Common {
);
$files[] = $file;
$this->objectCache->set($file, [
// store this information for later usage
$this->filesCache[$file] = [
'ContentLength' => $object['Size'],
'LastModified' => (string)$object['LastModified'],
]);
];
}
}
}
@ -321,20 +326,14 @@ class AmazonS3 extends \OC\Files\Storage\Common {
$path = $this->normalizePath($path);
try {
$stat = array();
$stat = [];
if ($this->is_dir($path)) {
//folders don't really exist
$stat['size'] = -1; //unknown
$stat['mtime'] = time() - $this->rescanDelay * 1000;
} else {
$result = $this->headObject($path);
$stat['size'] = $result['ContentLength'] ? $result['ContentLength'] : 0;
if (isset($result['Metadata']['lastmodified'])) {
$stat['mtime'] = strtotime($result['Metadata']['lastmodified']);
} else {
$stat['mtime'] = strtotime($result['LastModified']);
}
$stat['size'] = $this->getContentLength($path);
$stat['mtime'] = strtotime($this->getLastModified($path));
}
$stat['atime'] = time();
@ -345,6 +344,50 @@ class AmazonS3 extends \OC\Files\Storage\Common {
}
}
/**
* Return content length for object
*
* When the information is already present (e.g. opendir has been called before)
* this value is return. Otherwise a headObject is emitted.
*
* @param $path
* @return int|mixed
*/
private function getContentLength($path) {
if (isset($this->filesCache[$path])) {
return $this->filesCache[$path]['ContentLength'];
}
$result = $this->headObject($path);
if (isset($result['ContentLength'])) {
return $result['ContentLength'];
}
return 0;
}
/**
* Return last modified for object
*
* When the information is already present (e.g. opendir has been called before)
* this value is return. Otherwise a headObject is emitted.
*
* @param $path
* @return mixed|string
*/
private function getLastModified($path) {
if (isset($this->filesCache[$path])) {
return $this->filesCache[$path]['LastModified'];
}
$result = $this->headObject($path);
if (isset($result['LastModified'])) {
return $result['LastModified'];
}
return 'now';
}
public function is_dir($path) {
$path = $this->normalizePath($path);
try {