-
Notifications
You must be signed in to change notification settings - Fork 0
/
HubicStreamWrapperFS.php
100 lines (82 loc) · 3.32 KB
/
HubicStreamWrapperFS.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
<?php
namespace OpenStack\ObjectStore\v1\Resource;
require_once(dirname(__FILE__) .'/openstack-sdk-php/vendor/autoload.php');
class HubicStreamWrapperFS extends StreamWrapperFS
{
protected function writeRemote()
{
$contentType = $this->cxt('content_type');
if (!empty($contentType)) {
$this->obj->setContentType($contentType);
}
// Skip debug streams.
if ($this->isNeverDirty) {
return;
}
// Stream is dirty and needs a write.
if ($this->isDirty) {
$position = ftell($this->objStream);
rewind($this->objStream);
if ($contentType === null) {
$finfo = new \finfo(FILEINFO_MIME);
if ($mime = $finfo->buffer(stream_get_contents($this->objStream, 1024), FILEINFO_MIME_TYPE)) {
$this->obj->setContentType($mime);
}
rewind($this->objStream);
}
$objStream = tmpfile();
stream_copy_to_stream($this->objStream, $objStream);
$this->container->save($this->obj, $objStream);
// TODO: bad $this->objStream stream ressource after save() (Guzzle issue I presume), I had to copy
// original stream to a temporary one in order to preserve Pydio next treatments
// fclose($objStream); // can't close as the stream type is now "Undefined" instead of "stream" !!
fseek($this->objStream, $position, SEEK_SET);
}
$this->isDirty = false;
}
protected function initializeObjectStorage()
{
$token = $this->cxt('token', $_SESSION['PROP_HUBIC_account/credentials']['token']);
$endpoint = $this->cxt('swift_endpoint', $_SESSION['PROP_HUBIC_account/credentials']['endpoint']);
$client = $this->cxt('transport_client', \OpenStack\Common\Transport\Guzzle\GuzzleAdapter::create());
if (!empty($token) && !empty($endpoint)) {
$this->store = new \OpenStack\ObjectStore\v1\ObjectStorage($token, $endpoint, $client);
} else {
throw new \OpenStack\Common\Exception('Missing Token or Endpoint.');
}
return !empty($this->store);
}
public function url_stat($path, $flags)
{
$url = $this->parseUrl($path);
try {
$this->initializeObjectStorage();
$container = $this->store->container($url['host']);
$object = $container->objectsWithPrefix($url['path'], '/', 1)[0];
if ($object === null) {
return false;
}
if ($object->contentType() === 'application/directory') {
return $this->fakeStat(true, $object);
}
$stat = parent::url_stat($path, $flags);
// If the file stat setup returned anything return it.
if ($stat) {
return $stat;
}
return false;
} catch (\OpenStack\Common\Exception $e) {
return false;
}
}
protected function fakeStat($dir = false, Object $object = null)
{
$stat = parent::fakeStat($dir);
if ($object !== null) {
$stat['atime'] = $object->lastModified();
$stat['mtime'] = $object->lastModified();
$stat['ctime'] = $object->lastModified();
}
return $stat;
}
}