Passed
Push — master ( 8113df...5a27e5 )
by Morris
27:51 queued 12:24
created
lib/private/Files/ObjectStore/StorageObjectStore.php 1 patch
Indentation   +56 added lines, -56 removed lines patch added patch discarded remove patch
@@ -30,66 +30,66 @@
 block discarded – undo
30 30
  * Object store that wraps a storage backend, mostly for testing purposes
31 31
  */
32 32
 class StorageObjectStore implements IObjectStore {
33
-	/** @var IStorage */
34
-	private $storage;
33
+    /** @var IStorage */
34
+    private $storage;
35 35
 
36
-	/**
37
-	 * @param IStorage $storage
38
-	 */
39
-	public function __construct(IStorage $storage) {
40
-		$this->storage = $storage;
41
-	}
36
+    /**
37
+     * @param IStorage $storage
38
+     */
39
+    public function __construct(IStorage $storage) {
40
+        $this->storage = $storage;
41
+    }
42 42
 
43
-	/**
44
-	 * @return string the container or bucket name where objects are stored
45
-	 * @since 7.0.0
46
-	 */
47
-	function getStorageId() {
48
-		$this->storage->getId();
49
-	}
43
+    /**
44
+     * @return string the container or bucket name where objects are stored
45
+     * @since 7.0.0
46
+     */
47
+    function getStorageId() {
48
+        $this->storage->getId();
49
+    }
50 50
 
51
-	/**
52
-	 * @param string $urn the unified resource name used to identify the object
53
-	 * @return resource stream with the read data
54
-	 * @throws \Exception when something goes wrong, message will be logged
55
-	 * @since 7.0.0
56
-	 */
57
-	function readObject($urn) {
58
-		$handle = $this->storage->fopen($urn, 'r');
59
-		if ($handle) {
60
-			return $handle;
61
-		} else {
62
-			throw new \Exception();
63
-		}
64
-	}
51
+    /**
52
+     * @param string $urn the unified resource name used to identify the object
53
+     * @return resource stream with the read data
54
+     * @throws \Exception when something goes wrong, message will be logged
55
+     * @since 7.0.0
56
+     */
57
+    function readObject($urn) {
58
+        $handle = $this->storage->fopen($urn, 'r');
59
+        if ($handle) {
60
+            return $handle;
61
+        } else {
62
+            throw new \Exception();
63
+        }
64
+    }
65 65
 
66
-	/**
67
-	 * @param string $urn the unified resource name used to identify the object
68
-	 * @param resource $stream stream with the data to write
69
-	 * @throws \Exception when something goes wrong, message will be logged
70
-	 * @since 7.0.0
71
-	 */
72
-	function writeObject($urn, $stream) {
73
-		$handle = $this->storage->fopen($urn, 'w');
74
-		if ($handle) {
75
-			stream_copy_to_stream($stream, $handle);
76
-			fclose($handle);
77
-		} else {
78
-			throw new \Exception();
79
-		}
80
-	}
66
+    /**
67
+     * @param string $urn the unified resource name used to identify the object
68
+     * @param resource $stream stream with the data to write
69
+     * @throws \Exception when something goes wrong, message will be logged
70
+     * @since 7.0.0
71
+     */
72
+    function writeObject($urn, $stream) {
73
+        $handle = $this->storage->fopen($urn, 'w');
74
+        if ($handle) {
75
+            stream_copy_to_stream($stream, $handle);
76
+            fclose($handle);
77
+        } else {
78
+            throw new \Exception();
79
+        }
80
+    }
81 81
 
82
-	/**
83
-	 * @param string $urn the unified resource name used to identify the object
84
-	 * @return void
85
-	 * @throws \Exception when something goes wrong, message will be logged
86
-	 * @since 7.0.0
87
-	 */
88
-	function deleteObject($urn) {
89
-		$this->storage->unlink($urn);
90
-	}
82
+    /**
83
+     * @param string $urn the unified resource name used to identify the object
84
+     * @return void
85
+     * @throws \Exception when something goes wrong, message will be logged
86
+     * @since 7.0.0
87
+     */
88
+    function deleteObject($urn) {
89
+        $this->storage->unlink($urn);
90
+    }
91 91
 
92
-	public function objectExists($urn) {
93
-		return $this->storage->file_exists($urn);
94
-	}
92
+    public function objectExists($urn) {
93
+        return $this->storage->file_exists($urn);
94
+    }
95 95
 }
Please login to merge, or discard this patch.
lib/private/Files/ObjectStore/Swift.php 1 patch
Indentation   +98 added lines, -98 removed lines patch added patch discarded remove patch
@@ -33,102 +33,102 @@
 block discarded – undo
33 33
 use OpenStack\Common\Error\BadResponseError;
34 34
 
35 35
 class Swift implements IObjectStore {
36
-	/**
37
-	 * @var array
38
-	 */
39
-	private $params;
40
-
41
-	/** @var SwiftFactory */
42
-	private $swiftFactory;
43
-
44
-	public function __construct($params, SwiftFactory $connectionFactory = null) {
45
-		$this->swiftFactory = $connectionFactory ?: new SwiftFactory(
46
-			\OC::$server->getMemCacheFactory()->createDistributed('swift::'),
47
-			$params,
48
-			\OC::$server->getLogger()
49
-		);
50
-		$this->params = $params;
51
-	}
52
-
53
-	/**
54
-	 * @return \OpenStack\ObjectStore\v1\Models\Container
55
-	 * @throws StorageAuthException
56
-	 * @throws \OCP\Files\StorageNotAvailableException
57
-	 */
58
-	private function getContainer() {
59
-		return $this->swiftFactory->getContainer();
60
-	}
61
-
62
-	/**
63
-	 * @return string the container name where objects are stored
64
-	 */
65
-	public function getStorageId() {
66
-		if (isset($this->params['bucket'])) {
67
-			return $this->params['bucket'];
68
-		}
69
-
70
-		return $this->params['container'];
71
-	}
72
-
73
-	/**
74
-	 * @param string $urn the unified resource name used to identify the object
75
-	 * @param resource $stream stream with the data to write
76
-	 * @throws \Exception from openstack lib when something goes wrong
77
-	 */
78
-	public function writeObject($urn, $stream) {
79
-		$this->getContainer()->createObject([
80
-			'name' => $urn,
81
-			'stream' => stream_for($stream)
82
-		]);
83
-	}
84
-
85
-	/**
86
-	 * @param string $urn the unified resource name used to identify the object
87
-	 * @return resource stream with the read data
88
-	 * @throws \Exception from openstack lib when something goes wrong
89
-	 * @throws NotFoundException if file does not exist
90
-	 */
91
-	public function readObject($urn) {
92
-		try {
93
-			$object = $this->getContainer()->getObject($urn);
94
-
95
-			// we need to keep a reference to objectContent or
96
-			// the stream will be closed before we can do anything with it
97
-			$objectContent = $object->download();
98
-		} catch (BadResponseError $e) {
99
-			if ($e->getResponse()->getStatusCode() === 404) {
100
-				throw new NotFoundException("object $urn not found in object store");
101
-			} else {
102
-				throw $e;
103
-			}
104
-		}
105
-		$objectContent->rewind();
106
-
107
-		$stream = $objectContent->detach();
108
-		// save the object content in the context of the stream to prevent it being gc'd until the stream is closed
109
-		stream_context_set_option($stream, 'swift', 'content', $objectContent);
110
-
111
-		return RetryWrapper::wrap($stream);
112
-	}
113
-
114
-	/**
115
-	 * @param string $urn Unified Resource Name
116
-	 * @return void
117
-	 * @throws \Exception from openstack lib when something goes wrong
118
-	 */
119
-	public function deleteObject($urn) {
120
-		$this->getContainer()->getObject($urn)->delete();
121
-	}
122
-
123
-	/**
124
-	 * @return void
125
-	 * @throws \Exception from openstack lib when something goes wrong
126
-	 */
127
-	public function deleteContainer() {
128
-		$this->getContainer()->delete();
129
-	}
130
-
131
-	public function objectExists($urn) {
132
-		return $this->getContainer()->objectExists($urn);
133
-	}
36
+    /**
37
+     * @var array
38
+     */
39
+    private $params;
40
+
41
+    /** @var SwiftFactory */
42
+    private $swiftFactory;
43
+
44
+    public function __construct($params, SwiftFactory $connectionFactory = null) {
45
+        $this->swiftFactory = $connectionFactory ?: new SwiftFactory(
46
+            \OC::$server->getMemCacheFactory()->createDistributed('swift::'),
47
+            $params,
48
+            \OC::$server->getLogger()
49
+        );
50
+        $this->params = $params;
51
+    }
52
+
53
+    /**
54
+     * @return \OpenStack\ObjectStore\v1\Models\Container
55
+     * @throws StorageAuthException
56
+     * @throws \OCP\Files\StorageNotAvailableException
57
+     */
58
+    private function getContainer() {
59
+        return $this->swiftFactory->getContainer();
60
+    }
61
+
62
+    /**
63
+     * @return string the container name where objects are stored
64
+     */
65
+    public function getStorageId() {
66
+        if (isset($this->params['bucket'])) {
67
+            return $this->params['bucket'];
68
+        }
69
+
70
+        return $this->params['container'];
71
+    }
72
+
73
+    /**
74
+     * @param string $urn the unified resource name used to identify the object
75
+     * @param resource $stream stream with the data to write
76
+     * @throws \Exception from openstack lib when something goes wrong
77
+     */
78
+    public function writeObject($urn, $stream) {
79
+        $this->getContainer()->createObject([
80
+            'name' => $urn,
81
+            'stream' => stream_for($stream)
82
+        ]);
83
+    }
84
+
85
+    /**
86
+     * @param string $urn the unified resource name used to identify the object
87
+     * @return resource stream with the read data
88
+     * @throws \Exception from openstack lib when something goes wrong
89
+     * @throws NotFoundException if file does not exist
90
+     */
91
+    public function readObject($urn) {
92
+        try {
93
+            $object = $this->getContainer()->getObject($urn);
94
+
95
+            // we need to keep a reference to objectContent or
96
+            // the stream will be closed before we can do anything with it
97
+            $objectContent = $object->download();
98
+        } catch (BadResponseError $e) {
99
+            if ($e->getResponse()->getStatusCode() === 404) {
100
+                throw new NotFoundException("object $urn not found in object store");
101
+            } else {
102
+                throw $e;
103
+            }
104
+        }
105
+        $objectContent->rewind();
106
+
107
+        $stream = $objectContent->detach();
108
+        // save the object content in the context of the stream to prevent it being gc'd until the stream is closed
109
+        stream_context_set_option($stream, 'swift', 'content', $objectContent);
110
+
111
+        return RetryWrapper::wrap($stream);
112
+    }
113
+
114
+    /**
115
+     * @param string $urn Unified Resource Name
116
+     * @return void
117
+     * @throws \Exception from openstack lib when something goes wrong
118
+     */
119
+    public function deleteObject($urn) {
120
+        $this->getContainer()->getObject($urn)->delete();
121
+    }
122
+
123
+    /**
124
+     * @return void
125
+     * @throws \Exception from openstack lib when something goes wrong
126
+     */
127
+    public function deleteContainer() {
128
+        $this->getContainer()->delete();
129
+    }
130
+
131
+    public function objectExists($urn) {
132
+        return $this->getContainer()->objectExists($urn);
133
+    }
134 134
 }
Please login to merge, or discard this patch.
lib/private/Files/ObjectStore/S3ObjectTrait.php 1 patch
Indentation   +61 added lines, -61 removed lines patch added patch discarded remove patch
@@ -28,70 +28,70 @@
 block discarded – undo
28 28
 const S3_UPLOAD_PART_SIZE = 524288000; // 500MB
29 29
 
30 30
 trait S3ObjectTrait {
31
-	/**
32
-	 * Returns the connection
33
-	 *
34
-	 * @return S3Client connected client
35
-	 * @throws \Exception if connection could not be made
36
-	 */
37
-	abstract protected function getConnection();
31
+    /**
32
+     * Returns the connection
33
+     *
34
+     * @return S3Client connected client
35
+     * @throws \Exception if connection could not be made
36
+     */
37
+    abstract protected function getConnection();
38 38
 
39
-	/**
40
-	 * @param string $urn the unified resource name used to identify the object
41
-	 * @return resource stream with the read data
42
-	 * @throws \Exception when something goes wrong, message will be logged
43
-	 * @since 7.0.0
44
-	 */
45
-	function readObject($urn) {
46
-		$client = $this->getConnection();
47
-		$command = $client->getCommand('GetObject', [
48
-			'Bucket' => $this->bucket,
49
-			'Key' => $urn
50
-		]);
51
-		$request = \Aws\serialize($command);
52
-		$headers = [];
53
-		foreach ($request->getHeaders() as $key => $values) {
54
-			foreach ($values as $value) {
55
-				$headers[] = "$key: $value";
56
-			}
57
-		}
58
-		$opts = [
59
-			'http' => [
60
-				'header' => $headers
61
-			]
62
-		];
39
+    /**
40
+     * @param string $urn the unified resource name used to identify the object
41
+     * @return resource stream with the read data
42
+     * @throws \Exception when something goes wrong, message will be logged
43
+     * @since 7.0.0
44
+     */
45
+    function readObject($urn) {
46
+        $client = $this->getConnection();
47
+        $command = $client->getCommand('GetObject', [
48
+            'Bucket' => $this->bucket,
49
+            'Key' => $urn
50
+        ]);
51
+        $request = \Aws\serialize($command);
52
+        $headers = [];
53
+        foreach ($request->getHeaders() as $key => $values) {
54
+            foreach ($values as $value) {
55
+                $headers[] = "$key: $value";
56
+            }
57
+        }
58
+        $opts = [
59
+            'http' => [
60
+                'header' => $headers
61
+            ]
62
+        ];
63 63
 
64
-		$context = stream_context_create($opts);
65
-		return fopen($request->getUri(), 'r', false, $context);
66
-	}
64
+        $context = stream_context_create($opts);
65
+        return fopen($request->getUri(), 'r', false, $context);
66
+    }
67 67
 
68
-	/**
69
-	 * @param string $urn the unified resource name used to identify the object
70
-	 * @param resource $stream stream with the data to write
71
-	 * @throws \Exception when something goes wrong, message will be logged
72
-	 * @since 7.0.0
73
-	 */
74
-	function writeObject($urn, $stream) {
75
-		$this->getConnection()->upload($this->bucket, $urn, $stream, 'private', [
76
-			'mup_threshold' => S3_UPLOAD_PART_SIZE,
77
-			'part_size' => S3_UPLOAD_PART_SIZE
78
-		]);
79
-	}
68
+    /**
69
+     * @param string $urn the unified resource name used to identify the object
70
+     * @param resource $stream stream with the data to write
71
+     * @throws \Exception when something goes wrong, message will be logged
72
+     * @since 7.0.0
73
+     */
74
+    function writeObject($urn, $stream) {
75
+        $this->getConnection()->upload($this->bucket, $urn, $stream, 'private', [
76
+            'mup_threshold' => S3_UPLOAD_PART_SIZE,
77
+            'part_size' => S3_UPLOAD_PART_SIZE
78
+        ]);
79
+    }
80 80
 
81
-	/**
82
-	 * @param string $urn the unified resource name used to identify the object
83
-	 * @return void
84
-	 * @throws \Exception when something goes wrong, message will be logged
85
-	 * @since 7.0.0
86
-	 */
87
-	function deleteObject($urn) {
88
-		$this->getConnection()->deleteObject([
89
-			'Bucket' => $this->bucket,
90
-			'Key' => $urn
91
-		]);
92
-	}
81
+    /**
82
+     * @param string $urn the unified resource name used to identify the object
83
+     * @return void
84
+     * @throws \Exception when something goes wrong, message will be logged
85
+     * @since 7.0.0
86
+     */
87
+    function deleteObject($urn) {
88
+        $this->getConnection()->deleteObject([
89
+            'Bucket' => $this->bucket,
90
+            'Key' => $urn
91
+        ]);
92
+    }
93 93
 
94
-	public function objectExists($urn) {
95
-		return $this->getConnection()->doesObjectExist($this->bucket, $urn);
96
-	}
94
+    public function objectExists($urn) {
95
+        return $this->getConnection()->doesObjectExist($this->bucket, $urn);
96
+    }
97 97
 }
Please login to merge, or discard this patch.
lib/private/Files/ObjectStore/Azure.php 1 patch
Indentation   +94 added lines, -94 removed lines patch added patch discarded remove patch
@@ -26,106 +26,106 @@
 block discarded – undo
26 26
 use OCP\Files\ObjectStore\IObjectStore;
27 27
 
28 28
 class Azure implements IObjectStore {
29
-	/** @var string */
30
-	private $containerName;
31
-	/** @var string */
32
-	private $accountName;
33
-	/** @var string */
34
-	private $accountKey;
35
-	/** @var BlobRestProxy|null */
36
-	private $blobClient = null;
37
-	/** @var string|null */
38
-	private $endpoint = null;
39
-	/** @var bool  */
40
-	private $autoCreate = false;
29
+    /** @var string */
30
+    private $containerName;
31
+    /** @var string */
32
+    private $accountName;
33
+    /** @var string */
34
+    private $accountKey;
35
+    /** @var BlobRestProxy|null */
36
+    private $blobClient = null;
37
+    /** @var string|null */
38
+    private $endpoint = null;
39
+    /** @var bool  */
40
+    private $autoCreate = false;
41 41
 
42
-	/**
43
-	 * @param array $parameters
44
-	 */
45
-	public function __construct($parameters) {
46
-		$this->containerName = $parameters['container'];
47
-		$this->accountName = $parameters['account_name'];
48
-		$this->accountKey = $parameters['account_key'];
49
-		if (isset($parameters['endpoint'])) {
50
-			$this->endpoint = $parameters['endpoint'];
51
-		}
52
-		if (isset($parameters['autocreate'])) {
53
-			$this->autoCreate = $parameters['autocreate'];
54
-		}
55
-	}
42
+    /**
43
+     * @param array $parameters
44
+     */
45
+    public function __construct($parameters) {
46
+        $this->containerName = $parameters['container'];
47
+        $this->accountName = $parameters['account_name'];
48
+        $this->accountKey = $parameters['account_key'];
49
+        if (isset($parameters['endpoint'])) {
50
+            $this->endpoint = $parameters['endpoint'];
51
+        }
52
+        if (isset($parameters['autocreate'])) {
53
+            $this->autoCreate = $parameters['autocreate'];
54
+        }
55
+    }
56 56
 
57
-	/**
58
-	 * @return BlobRestProxy
59
-	 */
60
-	private function getBlobClient() {
61
-		if (!$this->blobClient) {
62
-			$protocol = $this->endpoint ? substr($this->endpoint, 0, strpos($this->endpoint, ':')) : 'https';
63
-			$connectionString = "DefaultEndpointsProtocol=" . $protocol . ";AccountName=" . $this->accountName . ";AccountKey=" . $this->accountKey;
64
-			if ($this->endpoint) {
65
-				$connectionString .= ';BlobEndpoint=' . $this->endpoint;
66
-			}
67
-			$this->blobClient = BlobRestProxy::createBlobService($connectionString);
57
+    /**
58
+     * @return BlobRestProxy
59
+     */
60
+    private function getBlobClient() {
61
+        if (!$this->blobClient) {
62
+            $protocol = $this->endpoint ? substr($this->endpoint, 0, strpos($this->endpoint, ':')) : 'https';
63
+            $connectionString = "DefaultEndpointsProtocol=" . $protocol . ";AccountName=" . $this->accountName . ";AccountKey=" . $this->accountKey;
64
+            if ($this->endpoint) {
65
+                $connectionString .= ';BlobEndpoint=' . $this->endpoint;
66
+            }
67
+            $this->blobClient = BlobRestProxy::createBlobService($connectionString);
68 68
 
69
-			if ($this->autoCreate) {
70
-				try {
71
-					$this->blobClient->createContainer($this->containerName);
72
-				} catch (ServiceException $e) {
73
-					if ($e->getCode() === 409) {
74
-						// already exists
75
-					} else {
76
-						throw $e;
77
-					}
78
-				}
79
-			}
80
-		}
81
-		return $this->blobClient;
82
-	}
69
+            if ($this->autoCreate) {
70
+                try {
71
+                    $this->blobClient->createContainer($this->containerName);
72
+                } catch (ServiceException $e) {
73
+                    if ($e->getCode() === 409) {
74
+                        // already exists
75
+                    } else {
76
+                        throw $e;
77
+                    }
78
+                }
79
+            }
80
+        }
81
+        return $this->blobClient;
82
+    }
83 83
 
84
-	/**
85
-	 * @return string the container or bucket name where objects are stored
86
-	 */
87
-	public function getStorageId() {
88
-		return 'azure::blob::' . $this->containerName;
89
-	}
84
+    /**
85
+     * @return string the container or bucket name where objects are stored
86
+     */
87
+    public function getStorageId() {
88
+        return 'azure::blob::' . $this->containerName;
89
+    }
90 90
 
91
-	/**
92
-	 * @param string $urn the unified resource name used to identify the object
93
-	 * @return resource stream with the read data
94
-	 * @throws \Exception when something goes wrong, message will be logged
95
-	 */
96
-	public function readObject($urn) {
97
-		$blob = $this->getBlobClient()->getBlob($this->containerName, $urn);
98
-		return $blob->getContentStream();
99
-	}
91
+    /**
92
+     * @param string $urn the unified resource name used to identify the object
93
+     * @return resource stream with the read data
94
+     * @throws \Exception when something goes wrong, message will be logged
95
+     */
96
+    public function readObject($urn) {
97
+        $blob = $this->getBlobClient()->getBlob($this->containerName, $urn);
98
+        return $blob->getContentStream();
99
+    }
100 100
 
101
-	/**
102
-	 * @param string $urn the unified resource name used to identify the object
103
-	 * @param resource $stream stream with the data to write
104
-	 * @throws \Exception when something goes wrong, message will be logged
105
-	 */
106
-	public function writeObject($urn, $stream) {
107
-		$this->getBlobClient()->createBlockBlob($this->containerName, $urn, $stream);
108
-	}
101
+    /**
102
+     * @param string $urn the unified resource name used to identify the object
103
+     * @param resource $stream stream with the data to write
104
+     * @throws \Exception when something goes wrong, message will be logged
105
+     */
106
+    public function writeObject($urn, $stream) {
107
+        $this->getBlobClient()->createBlockBlob($this->containerName, $urn, $stream);
108
+    }
109 109
 
110
-	/**
111
-	 * @param string $urn the unified resource name used to identify the object
112
-	 * @return void
113
-	 * @throws \Exception when something goes wrong, message will be logged
114
-	 */
115
-	public function deleteObject($urn) {
116
-		$this->getBlobClient()->deleteBlob($this->containerName, $urn);
117
-	}
110
+    /**
111
+     * @param string $urn the unified resource name used to identify the object
112
+     * @return void
113
+     * @throws \Exception when something goes wrong, message will be logged
114
+     */
115
+    public function deleteObject($urn) {
116
+        $this->getBlobClient()->deleteBlob($this->containerName, $urn);
117
+    }
118 118
 
119
-	public function objectExists($urn) {
120
-		try {
121
-			$this->getBlobClient()->getBlobMetadata($this->containerName, $urn);
122
-			return true;
123
-		} catch (ServiceException $e) {
124
-			if ($e->getCode() === 404) {
125
-				return false;
126
-			} else {
127
-				throw $e;
128
-			}
129
-		}
130
-	}
119
+    public function objectExists($urn) {
120
+        try {
121
+            $this->getBlobClient()->getBlobMetadata($this->containerName, $urn);
122
+            return true;
123
+        } catch (ServiceException $e) {
124
+            if ($e->getCode() === 404) {
125
+                return false;
126
+            } else {
127
+                throw $e;
128
+            }
129
+        }
130
+    }
131 131
 }
Please login to merge, or discard this patch.
lib/private/Files/ObjectStore/ObjectStoreStorage.php 2 patches
Indentation   +443 added lines, -443 removed lines patch added patch discarded remove patch
@@ -33,447 +33,447 @@
 block discarded – undo
33 33
 use OCP\Files\ObjectStore\IObjectStore;
34 34
 
35 35
 class ObjectStoreStorage extends \OC\Files\Storage\Common {
36
-	/**
37
-	 * @var \OCP\Files\ObjectStore\IObjectStore $objectStore
38
-	 */
39
-	protected $objectStore;
40
-	/**
41
-	 * @var string $id
42
-	 */
43
-	protected $id;
44
-	/**
45
-	 * @var \OC\User\User $user
46
-	 */
47
-	protected $user;
48
-
49
-	private $objectPrefix = 'urn:oid:';
50
-
51
-	private $logger;
52
-
53
-	public function __construct($params) {
54
-		if (isset($params['objectstore']) && $params['objectstore'] instanceof IObjectStore) {
55
-			$this->objectStore = $params['objectstore'];
56
-		} else {
57
-			throw new \Exception('missing IObjectStore instance');
58
-		}
59
-		if (isset($params['storageid'])) {
60
-			$this->id = 'object::store:' . $params['storageid'];
61
-		} else {
62
-			$this->id = 'object::store:' . $this->objectStore->getStorageId();
63
-		}
64
-		if (isset($params['objectPrefix'])) {
65
-			$this->objectPrefix = $params['objectPrefix'];
66
-		}
67
-		//initialize cache with root directory in cache
68
-		if (!$this->is_dir('/')) {
69
-			$this->mkdir('/');
70
-		}
71
-
72
-		$this->logger = \OC::$server->getLogger();
73
-	}
74
-
75
-	public function mkdir($path) {
76
-		$path = $this->normalizePath($path);
77
-
78
-		if ($this->file_exists($path)) {
79
-			return false;
80
-		}
81
-
82
-		$mTime = time();
83
-		$data = [
84
-			'mimetype' => 'httpd/unix-directory',
85
-			'size' => 0,
86
-			'mtime' => $mTime,
87
-			'storage_mtime' => $mTime,
88
-			'permissions' => \OCP\Constants::PERMISSION_ALL,
89
-		];
90
-		if ($path === '') {
91
-			//create root on the fly
92
-			$data['etag'] = $this->getETag('');
93
-			$this->getCache()->put('', $data);
94
-			return true;
95
-		} else {
96
-			// if parent does not exist, create it
97
-			$parent = $this->normalizePath(dirname($path));
98
-			$parentType = $this->filetype($parent);
99
-			if ($parentType === false) {
100
-				if (!$this->mkdir($parent)) {
101
-					// something went wrong
102
-					return false;
103
-				}
104
-			} else if ($parentType === 'file') {
105
-				// parent is a file
106
-				return false;
107
-			}
108
-			// finally create the new dir
109
-			$mTime = time(); // update mtime
110
-			$data['mtime'] = $mTime;
111
-			$data['storage_mtime'] = $mTime;
112
-			$data['etag'] = $this->getETag($path);
113
-			$this->getCache()->put($path, $data);
114
-			return true;
115
-		}
116
-	}
117
-
118
-	/**
119
-	 * @param string $path
120
-	 * @return string
121
-	 */
122
-	private function normalizePath($path) {
123
-		$path = trim($path, '/');
124
-		//FIXME why do we sometimes get a path like 'files//username'?
125
-		$path = str_replace('//', '/', $path);
126
-
127
-		// dirname('/folder') returns '.' but internally (in the cache) we store the root as ''
128
-		if (!$path || $path === '.') {
129
-			$path = '';
130
-		}
131
-
132
-		return $path;
133
-	}
134
-
135
-	/**
136
-	 * Object Stores use a NoopScanner because metadata is directly stored in
137
-	 * the file cache and cannot really scan the filesystem. The storage passed in is not used anywhere.
138
-	 *
139
-	 * @param string $path
140
-	 * @param \OC\Files\Storage\Storage (optional) the storage to pass to the scanner
141
-	 * @return \OC\Files\ObjectStore\NoopScanner
142
-	 */
143
-	public function getScanner($path = '', $storage = null) {
144
-		if (!$storage) {
145
-			$storage = $this;
146
-		}
147
-		if (!isset($this->scanner)) {
148
-			$this->scanner = new NoopScanner($storage);
149
-		}
150
-		return $this->scanner;
151
-	}
152
-
153
-	public function getId() {
154
-		return $this->id;
155
-	}
156
-
157
-	public function rmdir($path) {
158
-		$path = $this->normalizePath($path);
159
-
160
-		if (!$this->is_dir($path)) {
161
-			return false;
162
-		}
163
-
164
-		$this->rmObjects($path);
165
-
166
-		$this->getCache()->remove($path);
167
-
168
-		return true;
169
-	}
170
-
171
-	private function rmObjects($path) {
172
-		$children = $this->getCache()->getFolderContents($path);
173
-		foreach ($children as $child) {
174
-			if ($child['mimetype'] === 'httpd/unix-directory') {
175
-				$this->rmObjects($child['path']);
176
-			} else {
177
-				$this->unlink($child['path']);
178
-			}
179
-		}
180
-	}
181
-
182
-	public function unlink($path) {
183
-		$path = $this->normalizePath($path);
184
-		$stat = $this->stat($path);
185
-
186
-		if ($stat && isset($stat['fileid'])) {
187
-			if ($stat['mimetype'] === 'httpd/unix-directory') {
188
-				return $this->rmdir($path);
189
-			}
190
-			try {
191
-				$this->objectStore->deleteObject($this->getURN($stat['fileid']));
192
-			} catch (\Exception $ex) {
193
-				if ($ex->getCode() !== 404) {
194
-					$this->logger->logException($ex, [
195
-						'app' => 'objectstore',
196
-						'message' => 'Could not delete object ' . $this->getURN($stat['fileid']) . ' for ' . $path,
197
-					]);
198
-					return false;
199
-				}
200
-				//removing from cache is ok as it does not exist in the objectstore anyway
201
-			}
202
-			$this->getCache()->remove($path);
203
-			return true;
204
-		}
205
-		return false;
206
-	}
207
-
208
-	public function stat($path) {
209
-		$path = $this->normalizePath($path);
210
-		$cacheEntry = $this->getCache()->get($path);
211
-		if ($cacheEntry instanceof CacheEntry) {
212
-			return $cacheEntry->getData();
213
-		} else {
214
-			return false;
215
-		}
216
-	}
217
-
218
-	/**
219
-	 * Override this method if you need a different unique resource identifier for your object storage implementation.
220
-	 * The default implementations just appends the fileId to 'urn:oid:'. Make sure the URN is unique over all users.
221
-	 * You may need a mapping table to store your URN if it cannot be generated from the fileid.
222
-	 *
223
-	 * @param int $fileId the fileid
224
-	 * @return null|string the unified resource name used to identify the object
225
-	 */
226
-	protected function getURN($fileId) {
227
-		if (is_numeric($fileId)) {
228
-			return $this->objectPrefix . $fileId;
229
-		}
230
-		return null;
231
-	}
232
-
233
-	public function opendir($path) {
234
-		$path = $this->normalizePath($path);
235
-
236
-		try {
237
-			$files = array();
238
-			$folderContents = $this->getCache()->getFolderContents($path);
239
-			foreach ($folderContents as $file) {
240
-				$files[] = $file['name'];
241
-			}
242
-
243
-			return IteratorDirectory::wrap($files);
244
-		} catch (\Exception $e) {
245
-			$this->logger->logException($e);
246
-			return false;
247
-		}
248
-	}
249
-
250
-	public function filetype($path) {
251
-		$path = $this->normalizePath($path);
252
-		$stat = $this->stat($path);
253
-		if ($stat) {
254
-			if ($stat['mimetype'] === 'httpd/unix-directory') {
255
-				return 'dir';
256
-			}
257
-			return 'file';
258
-		} else {
259
-			return false;
260
-		}
261
-	}
262
-
263
-	public function fopen($path, $mode) {
264
-		$path = $this->normalizePath($path);
265
-
266
-		if (strrpos($path, '.') !== false) {
267
-			$ext = substr($path, strrpos($path, '.'));
268
-		} else {
269
-			$ext = '';
270
-		}
271
-
272
-		switch ($mode) {
273
-			case 'r':
274
-			case 'rb':
275
-				$stat = $this->stat($path);
276
-				if (is_array($stat)) {
277
-					try {
278
-						return $this->objectStore->readObject($this->getURN($stat['fileid']));
279
-					} catch (NotFoundException $e) {
280
-						$this->logger->logException($e, [
281
-							'app' => 'objectstore',
282
-							'message' => 'Could not get object ' . $this->getURN($stat['fileid']) . ' for file ' . $path,
283
-						]);
284
-						throw $e;
285
-					} catch (\Exception $ex) {
286
-						$this->logger->logException($ex, [
287
-							'app' => 'objectstore',
288
-							'message' => 'Could not get object ' . $this->getURN($stat['fileid']) . ' for file ' . $path,
289
-						]);
290
-						return false;
291
-					}
292
-				} else {
293
-					return false;
294
-				}
295
-			case 'w':
296
-			case 'wb':
297
-			case 'w+':
298
-			case 'wb+':
299
-				$tmpFile = \OC::$server->getTempManager()->getTemporaryFile($ext);
300
-				$handle = fopen($tmpFile, $mode);
301
-				return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) {
302
-					$this->writeBack($tmpFile, $path);
303
-				});
304
-			case 'a':
305
-			case 'ab':
306
-			case 'r+':
307
-			case 'a+':
308
-			case 'x':
309
-			case 'x+':
310
-			case 'c':
311
-			case 'c+':
312
-				$tmpFile = \OC::$server->getTempManager()->getTemporaryFile($ext);
313
-				if ($this->file_exists($path)) {
314
-					$source = $this->fopen($path, 'r');
315
-					file_put_contents($tmpFile, $source);
316
-				}
317
-				$handle = fopen($tmpFile, $mode);
318
-				return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) {
319
-					$this->writeBack($tmpFile, $path);
320
-				});
321
-		}
322
-		return false;
323
-	}
324
-
325
-	public function file_exists($path) {
326
-		$path = $this->normalizePath($path);
327
-		return (bool)$this->stat($path);
328
-	}
329
-
330
-	public function rename($source, $target) {
331
-		$source = $this->normalizePath($source);
332
-		$target = $this->normalizePath($target);
333
-		$this->remove($target);
334
-		$this->getCache()->move($source, $target);
335
-		$this->touch(dirname($target));
336
-		return true;
337
-	}
338
-
339
-	public function getMimeType($path) {
340
-		$path = $this->normalizePath($path);
341
-		$stat = $this->stat($path);
342
-		if (is_array($stat)) {
343
-			return $stat['mimetype'];
344
-		} else {
345
-			return false;
346
-		}
347
-	}
348
-
349
-	public function touch($path, $mtime = null) {
350
-		if (is_null($mtime)) {
351
-			$mtime = time();
352
-		}
353
-
354
-		$path = $this->normalizePath($path);
355
-		$dirName = dirname($path);
356
-		$parentExists = $this->is_dir($dirName);
357
-		if (!$parentExists) {
358
-			return false;
359
-		}
360
-
361
-		$stat = $this->stat($path);
362
-		if (is_array($stat)) {
363
-			// update existing mtime in db
364
-			$stat['mtime'] = $mtime;
365
-			$this->getCache()->update($stat['fileid'], $stat);
366
-		} else {
367
-			try {
368
-				//create a empty file, need to have at least on char to make it
369
-				// work with all object storage implementations
370
-				$this->file_put_contents($path, ' ');
371
-				$mimeType = \OC::$server->getMimeTypeDetector()->detectPath($path);
372
-				$stat = array(
373
-					'etag' => $this->getETag($path),
374
-					'mimetype' => $mimeType,
375
-					'size' => 0,
376
-					'mtime' => $mtime,
377
-					'storage_mtime' => $mtime,
378
-					'permissions' => \OCP\Constants::PERMISSION_ALL - \OCP\Constants::PERMISSION_CREATE,
379
-				);
380
-				$this->getCache()->put($path, $stat);
381
-			} catch (\Exception $ex) {
382
-				$this->logger->logException($ex, [
383
-					'app' => 'objectstore',
384
-					'message' => 'Could not create object for ' . $path,
385
-				]);
386
-				throw $ex;
387
-			}
388
-		}
389
-		return true;
390
-	}
391
-
392
-	public function writeBack($tmpFile, $path) {
393
-		$size = filesize($tmpFile);
394
-		$this->writeStream($path, fopen($tmpFile, 'r'), $size);
395
-	}
396
-
397
-	/**
398
-	 * external changes are not supported, exclusive access to the object storage is assumed
399
-	 *
400
-	 * @param string $path
401
-	 * @param int $time
402
-	 * @return false
403
-	 */
404
-	public function hasUpdated($path, $time) {
405
-		return false;
406
-	}
407
-
408
-	public function needsPartFile() {
409
-		return false;
410
-	}
411
-
412
-	public function file_put_contents($path, $data) {
413
-		$stream = fopen('php://temp', 'r+');
414
-		fwrite($stream, $data);
415
-		rewind($stream);
416
-		return $this->writeStream($path, $stream, strlen($data)) > 0;
417
-	}
418
-
419
-	public function writeStream(string $path, $stream, int $size = null): int {
420
-		$stat = $this->stat($path);
421
-		if (empty($stat)) {
422
-			// create new file
423
-			$stat = [
424
-				'permissions' => \OCP\Constants::PERMISSION_ALL - \OCP\Constants::PERMISSION_CREATE,
425
-			];
426
-		}
427
-		// update stat with new data
428
-		$mTime = time();
429
-		$stat['size'] = (int)$size;
430
-		$stat['mtime'] = $mTime;
431
-		$stat['storage_mtime'] = $mTime;
432
-
433
-		$mimetypeDetector = \OC::$server->getMimeTypeDetector();
434
-		$mimetype = $mimetypeDetector->detectPath($path);
435
-
436
-		$stat['mimetype'] = $mimetype;
437
-		$stat['etag'] = $this->getETag($path);
438
-
439
-		$exists = $this->getCache()->inCache($path);
440
-		$uploadPath = $exists ? $path : $path . '.part';
441
-		$fileId = $this->getCache()->put($uploadPath, $stat);
442
-		$urn = $this->getURN($fileId);
443
-		try {
444
-			//upload to object storage
445
-			if ($size === null) {
446
-				$countStream = CountReadStream::wrap($stream, function ($writtenSize) use ($fileId, &$size) {
447
-					$this->getCache()->update($fileId, [
448
-						'size' => $writtenSize
449
-					]);
450
-					$size = $writtenSize;
451
-				});
452
-				$this->objectStore->writeObject($urn, $countStream);
453
-				if (is_resource($countStream)) {
454
-					fclose($countStream);
455
-				}
456
-			} else {
457
-				$this->objectStore->writeObject($urn, $stream);
458
-			}
459
-		} catch (\Exception $ex) {
460
-			$this->getCache()->remove($uploadPath);
461
-			$this->logger->logException($ex, [
462
-				'app' => 'objectstore',
463
-				'message' => 'Could not create object ' . $urn . ' for ' . $path,
464
-			]);
465
-			throw $ex; // make this bubble up
466
-		}
467
-
468
-		if (!$exists) {
469
-			if ($this->objectStore->objectExists($urn)) {
470
-				$this->getCache()->move($uploadPath, $path);
471
-			} else {
472
-				$this->getCache()->remove($uploadPath);
473
-				throw new \Exception("Object not found after writing (urn: $urn, path: $path)", 404);
474
-			}
475
-		}
476
-
477
-		return $size;
478
-	}
36
+    /**
37
+     * @var \OCP\Files\ObjectStore\IObjectStore $objectStore
38
+     */
39
+    protected $objectStore;
40
+    /**
41
+     * @var string $id
42
+     */
43
+    protected $id;
44
+    /**
45
+     * @var \OC\User\User $user
46
+     */
47
+    protected $user;
48
+
49
+    private $objectPrefix = 'urn:oid:';
50
+
51
+    private $logger;
52
+
53
+    public function __construct($params) {
54
+        if (isset($params['objectstore']) && $params['objectstore'] instanceof IObjectStore) {
55
+            $this->objectStore = $params['objectstore'];
56
+        } else {
57
+            throw new \Exception('missing IObjectStore instance');
58
+        }
59
+        if (isset($params['storageid'])) {
60
+            $this->id = 'object::store:' . $params['storageid'];
61
+        } else {
62
+            $this->id = 'object::store:' . $this->objectStore->getStorageId();
63
+        }
64
+        if (isset($params['objectPrefix'])) {
65
+            $this->objectPrefix = $params['objectPrefix'];
66
+        }
67
+        //initialize cache with root directory in cache
68
+        if (!$this->is_dir('/')) {
69
+            $this->mkdir('/');
70
+        }
71
+
72
+        $this->logger = \OC::$server->getLogger();
73
+    }
74
+
75
+    public function mkdir($path) {
76
+        $path = $this->normalizePath($path);
77
+
78
+        if ($this->file_exists($path)) {
79
+            return false;
80
+        }
81
+
82
+        $mTime = time();
83
+        $data = [
84
+            'mimetype' => 'httpd/unix-directory',
85
+            'size' => 0,
86
+            'mtime' => $mTime,
87
+            'storage_mtime' => $mTime,
88
+            'permissions' => \OCP\Constants::PERMISSION_ALL,
89
+        ];
90
+        if ($path === '') {
91
+            //create root on the fly
92
+            $data['etag'] = $this->getETag('');
93
+            $this->getCache()->put('', $data);
94
+            return true;
95
+        } else {
96
+            // if parent does not exist, create it
97
+            $parent = $this->normalizePath(dirname($path));
98
+            $parentType = $this->filetype($parent);
99
+            if ($parentType === false) {
100
+                if (!$this->mkdir($parent)) {
101
+                    // something went wrong
102
+                    return false;
103
+                }
104
+            } else if ($parentType === 'file') {
105
+                // parent is a file
106
+                return false;
107
+            }
108
+            // finally create the new dir
109
+            $mTime = time(); // update mtime
110
+            $data['mtime'] = $mTime;
111
+            $data['storage_mtime'] = $mTime;
112
+            $data['etag'] = $this->getETag($path);
113
+            $this->getCache()->put($path, $data);
114
+            return true;
115
+        }
116
+    }
117
+
118
+    /**
119
+     * @param string $path
120
+     * @return string
121
+     */
122
+    private function normalizePath($path) {
123
+        $path = trim($path, '/');
124
+        //FIXME why do we sometimes get a path like 'files//username'?
125
+        $path = str_replace('//', '/', $path);
126
+
127
+        // dirname('/folder') returns '.' but internally (in the cache) we store the root as ''
128
+        if (!$path || $path === '.') {
129
+            $path = '';
130
+        }
131
+
132
+        return $path;
133
+    }
134
+
135
+    /**
136
+     * Object Stores use a NoopScanner because metadata is directly stored in
137
+     * the file cache and cannot really scan the filesystem. The storage passed in is not used anywhere.
138
+     *
139
+     * @param string $path
140
+     * @param \OC\Files\Storage\Storage (optional) the storage to pass to the scanner
141
+     * @return \OC\Files\ObjectStore\NoopScanner
142
+     */
143
+    public function getScanner($path = '', $storage = null) {
144
+        if (!$storage) {
145
+            $storage = $this;
146
+        }
147
+        if (!isset($this->scanner)) {
148
+            $this->scanner = new NoopScanner($storage);
149
+        }
150
+        return $this->scanner;
151
+    }
152
+
153
+    public function getId() {
154
+        return $this->id;
155
+    }
156
+
157
+    public function rmdir($path) {
158
+        $path = $this->normalizePath($path);
159
+
160
+        if (!$this->is_dir($path)) {
161
+            return false;
162
+        }
163
+
164
+        $this->rmObjects($path);
165
+
166
+        $this->getCache()->remove($path);
167
+
168
+        return true;
169
+    }
170
+
171
+    private function rmObjects($path) {
172
+        $children = $this->getCache()->getFolderContents($path);
173
+        foreach ($children as $child) {
174
+            if ($child['mimetype'] === 'httpd/unix-directory') {
175
+                $this->rmObjects($child['path']);
176
+            } else {
177
+                $this->unlink($child['path']);
178
+            }
179
+        }
180
+    }
181
+
182
+    public function unlink($path) {
183
+        $path = $this->normalizePath($path);
184
+        $stat = $this->stat($path);
185
+
186
+        if ($stat && isset($stat['fileid'])) {
187
+            if ($stat['mimetype'] === 'httpd/unix-directory') {
188
+                return $this->rmdir($path);
189
+            }
190
+            try {
191
+                $this->objectStore->deleteObject($this->getURN($stat['fileid']));
192
+            } catch (\Exception $ex) {
193
+                if ($ex->getCode() !== 404) {
194
+                    $this->logger->logException($ex, [
195
+                        'app' => 'objectstore',
196
+                        'message' => 'Could not delete object ' . $this->getURN($stat['fileid']) . ' for ' . $path,
197
+                    ]);
198
+                    return false;
199
+                }
200
+                //removing from cache is ok as it does not exist in the objectstore anyway
201
+            }
202
+            $this->getCache()->remove($path);
203
+            return true;
204
+        }
205
+        return false;
206
+    }
207
+
208
+    public function stat($path) {
209
+        $path = $this->normalizePath($path);
210
+        $cacheEntry = $this->getCache()->get($path);
211
+        if ($cacheEntry instanceof CacheEntry) {
212
+            return $cacheEntry->getData();
213
+        } else {
214
+            return false;
215
+        }
216
+    }
217
+
218
+    /**
219
+     * Override this method if you need a different unique resource identifier for your object storage implementation.
220
+     * The default implementations just appends the fileId to 'urn:oid:'. Make sure the URN is unique over all users.
221
+     * You may need a mapping table to store your URN if it cannot be generated from the fileid.
222
+     *
223
+     * @param int $fileId the fileid
224
+     * @return null|string the unified resource name used to identify the object
225
+     */
226
+    protected function getURN($fileId) {
227
+        if (is_numeric($fileId)) {
228
+            return $this->objectPrefix . $fileId;
229
+        }
230
+        return null;
231
+    }
232
+
233
+    public function opendir($path) {
234
+        $path = $this->normalizePath($path);
235
+
236
+        try {
237
+            $files = array();
238
+            $folderContents = $this->getCache()->getFolderContents($path);
239
+            foreach ($folderContents as $file) {
240
+                $files[] = $file['name'];
241
+            }
242
+
243
+            return IteratorDirectory::wrap($files);
244
+        } catch (\Exception $e) {
245
+            $this->logger->logException($e);
246
+            return false;
247
+        }
248
+    }
249
+
250
+    public function filetype($path) {
251
+        $path = $this->normalizePath($path);
252
+        $stat = $this->stat($path);
253
+        if ($stat) {
254
+            if ($stat['mimetype'] === 'httpd/unix-directory') {
255
+                return 'dir';
256
+            }
257
+            return 'file';
258
+        } else {
259
+            return false;
260
+        }
261
+    }
262
+
263
+    public function fopen($path, $mode) {
264
+        $path = $this->normalizePath($path);
265
+
266
+        if (strrpos($path, '.') !== false) {
267
+            $ext = substr($path, strrpos($path, '.'));
268
+        } else {
269
+            $ext = '';
270
+        }
271
+
272
+        switch ($mode) {
273
+            case 'r':
274
+            case 'rb':
275
+                $stat = $this->stat($path);
276
+                if (is_array($stat)) {
277
+                    try {
278
+                        return $this->objectStore->readObject($this->getURN($stat['fileid']));
279
+                    } catch (NotFoundException $e) {
280
+                        $this->logger->logException($e, [
281
+                            'app' => 'objectstore',
282
+                            'message' => 'Could not get object ' . $this->getURN($stat['fileid']) . ' for file ' . $path,
283
+                        ]);
284
+                        throw $e;
285
+                    } catch (\Exception $ex) {
286
+                        $this->logger->logException($ex, [
287
+                            'app' => 'objectstore',
288
+                            'message' => 'Could not get object ' . $this->getURN($stat['fileid']) . ' for file ' . $path,
289
+                        ]);
290
+                        return false;
291
+                    }
292
+                } else {
293
+                    return false;
294
+                }
295
+            case 'w':
296
+            case 'wb':
297
+            case 'w+':
298
+            case 'wb+':
299
+                $tmpFile = \OC::$server->getTempManager()->getTemporaryFile($ext);
300
+                $handle = fopen($tmpFile, $mode);
301
+                return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) {
302
+                    $this->writeBack($tmpFile, $path);
303
+                });
304
+            case 'a':
305
+            case 'ab':
306
+            case 'r+':
307
+            case 'a+':
308
+            case 'x':
309
+            case 'x+':
310
+            case 'c':
311
+            case 'c+':
312
+                $tmpFile = \OC::$server->getTempManager()->getTemporaryFile($ext);
313
+                if ($this->file_exists($path)) {
314
+                    $source = $this->fopen($path, 'r');
315
+                    file_put_contents($tmpFile, $source);
316
+                }
317
+                $handle = fopen($tmpFile, $mode);
318
+                return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) {
319
+                    $this->writeBack($tmpFile, $path);
320
+                });
321
+        }
322
+        return false;
323
+    }
324
+
325
+    public function file_exists($path) {
326
+        $path = $this->normalizePath($path);
327
+        return (bool)$this->stat($path);
328
+    }
329
+
330
+    public function rename($source, $target) {
331
+        $source = $this->normalizePath($source);
332
+        $target = $this->normalizePath($target);
333
+        $this->remove($target);
334
+        $this->getCache()->move($source, $target);
335
+        $this->touch(dirname($target));
336
+        return true;
337
+    }
338
+
339
+    public function getMimeType($path) {
340
+        $path = $this->normalizePath($path);
341
+        $stat = $this->stat($path);
342
+        if (is_array($stat)) {
343
+            return $stat['mimetype'];
344
+        } else {
345
+            return false;
346
+        }
347
+    }
348
+
349
+    public function touch($path, $mtime = null) {
350
+        if (is_null($mtime)) {
351
+            $mtime = time();
352
+        }
353
+
354
+        $path = $this->normalizePath($path);
355
+        $dirName = dirname($path);
356
+        $parentExists = $this->is_dir($dirName);
357
+        if (!$parentExists) {
358
+            return false;
359
+        }
360
+
361
+        $stat = $this->stat($path);
362
+        if (is_array($stat)) {
363
+            // update existing mtime in db
364
+            $stat['mtime'] = $mtime;
365
+            $this->getCache()->update($stat['fileid'], $stat);
366
+        } else {
367
+            try {
368
+                //create a empty file, need to have at least on char to make it
369
+                // work with all object storage implementations
370
+                $this->file_put_contents($path, ' ');
371
+                $mimeType = \OC::$server->getMimeTypeDetector()->detectPath($path);
372
+                $stat = array(
373
+                    'etag' => $this->getETag($path),
374
+                    'mimetype' => $mimeType,
375
+                    'size' => 0,
376
+                    'mtime' => $mtime,
377
+                    'storage_mtime' => $mtime,
378
+                    'permissions' => \OCP\Constants::PERMISSION_ALL - \OCP\Constants::PERMISSION_CREATE,
379
+                );
380
+                $this->getCache()->put($path, $stat);
381
+            } catch (\Exception $ex) {
382
+                $this->logger->logException($ex, [
383
+                    'app' => 'objectstore',
384
+                    'message' => 'Could not create object for ' . $path,
385
+                ]);
386
+                throw $ex;
387
+            }
388
+        }
389
+        return true;
390
+    }
391
+
392
+    public function writeBack($tmpFile, $path) {
393
+        $size = filesize($tmpFile);
394
+        $this->writeStream($path, fopen($tmpFile, 'r'), $size);
395
+    }
396
+
397
+    /**
398
+     * external changes are not supported, exclusive access to the object storage is assumed
399
+     *
400
+     * @param string $path
401
+     * @param int $time
402
+     * @return false
403
+     */
404
+    public function hasUpdated($path, $time) {
405
+        return false;
406
+    }
407
+
408
+    public function needsPartFile() {
409
+        return false;
410
+    }
411
+
412
+    public function file_put_contents($path, $data) {
413
+        $stream = fopen('php://temp', 'r+');
414
+        fwrite($stream, $data);
415
+        rewind($stream);
416
+        return $this->writeStream($path, $stream, strlen($data)) > 0;
417
+    }
418
+
419
+    public function writeStream(string $path, $stream, int $size = null): int {
420
+        $stat = $this->stat($path);
421
+        if (empty($stat)) {
422
+            // create new file
423
+            $stat = [
424
+                'permissions' => \OCP\Constants::PERMISSION_ALL - \OCP\Constants::PERMISSION_CREATE,
425
+            ];
426
+        }
427
+        // update stat with new data
428
+        $mTime = time();
429
+        $stat['size'] = (int)$size;
430
+        $stat['mtime'] = $mTime;
431
+        $stat['storage_mtime'] = $mTime;
432
+
433
+        $mimetypeDetector = \OC::$server->getMimeTypeDetector();
434
+        $mimetype = $mimetypeDetector->detectPath($path);
435
+
436
+        $stat['mimetype'] = $mimetype;
437
+        $stat['etag'] = $this->getETag($path);
438
+
439
+        $exists = $this->getCache()->inCache($path);
440
+        $uploadPath = $exists ? $path : $path . '.part';
441
+        $fileId = $this->getCache()->put($uploadPath, $stat);
442
+        $urn = $this->getURN($fileId);
443
+        try {
444
+            //upload to object storage
445
+            if ($size === null) {
446
+                $countStream = CountReadStream::wrap($stream, function ($writtenSize) use ($fileId, &$size) {
447
+                    $this->getCache()->update($fileId, [
448
+                        'size' => $writtenSize
449
+                    ]);
450
+                    $size = $writtenSize;
451
+                });
452
+                $this->objectStore->writeObject($urn, $countStream);
453
+                if (is_resource($countStream)) {
454
+                    fclose($countStream);
455
+                }
456
+            } else {
457
+                $this->objectStore->writeObject($urn, $stream);
458
+            }
459
+        } catch (\Exception $ex) {
460
+            $this->getCache()->remove($uploadPath);
461
+            $this->logger->logException($ex, [
462
+                'app' => 'objectstore',
463
+                'message' => 'Could not create object ' . $urn . ' for ' . $path,
464
+            ]);
465
+            throw $ex; // make this bubble up
466
+        }
467
+
468
+        if (!$exists) {
469
+            if ($this->objectStore->objectExists($urn)) {
470
+                $this->getCache()->move($uploadPath, $path);
471
+            } else {
472
+                $this->getCache()->remove($uploadPath);
473
+                throw new \Exception("Object not found after writing (urn: $urn, path: $path)", 404);
474
+            }
475
+        }
476
+
477
+        return $size;
478
+    }
479 479
 }
Please login to merge, or discard this patch.
Spacing   +14 added lines, -14 removed lines patch added patch discarded remove patch
@@ -57,9 +57,9 @@  discard block
 block discarded – undo
57 57
 			throw new \Exception('missing IObjectStore instance');
58 58
 		}
59 59
 		if (isset($params['storageid'])) {
60
-			$this->id = 'object::store:' . $params['storageid'];
60
+			$this->id = 'object::store:'.$params['storageid'];
61 61
 		} else {
62
-			$this->id = 'object::store:' . $this->objectStore->getStorageId();
62
+			$this->id = 'object::store:'.$this->objectStore->getStorageId();
63 63
 		}
64 64
 		if (isset($params['objectPrefix'])) {
65 65
 			$this->objectPrefix = $params['objectPrefix'];
@@ -193,7 +193,7 @@  discard block
 block discarded – undo
193 193
 				if ($ex->getCode() !== 404) {
194 194
 					$this->logger->logException($ex, [
195 195
 						'app' => 'objectstore',
196
-						'message' => 'Could not delete object ' . $this->getURN($stat['fileid']) . ' for ' . $path,
196
+						'message' => 'Could not delete object '.$this->getURN($stat['fileid']).' for '.$path,
197 197
 					]);
198 198
 					return false;
199 199
 				}
@@ -225,7 +225,7 @@  discard block
 block discarded – undo
225 225
 	 */
226 226
 	protected function getURN($fileId) {
227 227
 		if (is_numeric($fileId)) {
228
-			return $this->objectPrefix . $fileId;
228
+			return $this->objectPrefix.$fileId;
229 229
 		}
230 230
 		return null;
231 231
 	}
@@ -279,13 +279,13 @@  discard block
 block discarded – undo
279 279
 					} catch (NotFoundException $e) {
280 280
 						$this->logger->logException($e, [
281 281
 							'app' => 'objectstore',
282
-							'message' => 'Could not get object ' . $this->getURN($stat['fileid']) . ' for file ' . $path,
282
+							'message' => 'Could not get object '.$this->getURN($stat['fileid']).' for file '.$path,
283 283
 						]);
284 284
 						throw $e;
285 285
 					} catch (\Exception $ex) {
286 286
 						$this->logger->logException($ex, [
287 287
 							'app' => 'objectstore',
288
-							'message' => 'Could not get object ' . $this->getURN($stat['fileid']) . ' for file ' . $path,
288
+							'message' => 'Could not get object '.$this->getURN($stat['fileid']).' for file '.$path,
289 289
 						]);
290 290
 						return false;
291 291
 					}
@@ -298,7 +298,7 @@  discard block
 block discarded – undo
298 298
 			case 'wb+':
299 299
 				$tmpFile = \OC::$server->getTempManager()->getTemporaryFile($ext);
300 300
 				$handle = fopen($tmpFile, $mode);
301
-				return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) {
301
+				return CallbackWrapper::wrap($handle, null, null, function() use ($path, $tmpFile) {
302 302
 					$this->writeBack($tmpFile, $path);
303 303
 				});
304 304
 			case 'a':
@@ -315,7 +315,7 @@  discard block
 block discarded – undo
315 315
 					file_put_contents($tmpFile, $source);
316 316
 				}
317 317
 				$handle = fopen($tmpFile, $mode);
318
-				return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) {
318
+				return CallbackWrapper::wrap($handle, null, null, function() use ($path, $tmpFile) {
319 319
 					$this->writeBack($tmpFile, $path);
320 320
 				});
321 321
 		}
@@ -324,7 +324,7 @@  discard block
 block discarded – undo
324 324
 
325 325
 	public function file_exists($path) {
326 326
 		$path = $this->normalizePath($path);
327
-		return (bool)$this->stat($path);
327
+		return (bool) $this->stat($path);
328 328
 	}
329 329
 
330 330
 	public function rename($source, $target) {
@@ -381,7 +381,7 @@  discard block
 block discarded – undo
381 381
 			} catch (\Exception $ex) {
382 382
 				$this->logger->logException($ex, [
383 383
 					'app' => 'objectstore',
384
-					'message' => 'Could not create object for ' . $path,
384
+					'message' => 'Could not create object for '.$path,
385 385
 				]);
386 386
 				throw $ex;
387 387
 			}
@@ -426,7 +426,7 @@  discard block
 block discarded – undo
426 426
 		}
427 427
 		// update stat with new data
428 428
 		$mTime = time();
429
-		$stat['size'] = (int)$size;
429
+		$stat['size'] = (int) $size;
430 430
 		$stat['mtime'] = $mTime;
431 431
 		$stat['storage_mtime'] = $mTime;
432 432
 
@@ -437,13 +437,13 @@  discard block
 block discarded – undo
437 437
 		$stat['etag'] = $this->getETag($path);
438 438
 
439 439
 		$exists = $this->getCache()->inCache($path);
440
-		$uploadPath = $exists ? $path : $path . '.part';
440
+		$uploadPath = $exists ? $path : $path.'.part';
441 441
 		$fileId = $this->getCache()->put($uploadPath, $stat);
442 442
 		$urn = $this->getURN($fileId);
443 443
 		try {
444 444
 			//upload to object storage
445 445
 			if ($size === null) {
446
-				$countStream = CountReadStream::wrap($stream, function ($writtenSize) use ($fileId, &$size) {
446
+				$countStream = CountReadStream::wrap($stream, function($writtenSize) use ($fileId, &$size) {
447 447
 					$this->getCache()->update($fileId, [
448 448
 						'size' => $writtenSize
449 449
 					]);
@@ -460,7 +460,7 @@  discard block
 block discarded – undo
460 460
 			$this->getCache()->remove($uploadPath);
461 461
 			$this->logger->logException($ex, [
462 462
 				'app' => 'objectstore',
463
-				'message' => 'Could not create object ' . $urn . ' for ' . $path,
463
+				'message' => 'Could not create object '.$urn.' for '.$path,
464 464
 			]);
465 465
 			throw $ex; // make this bubble up
466 466
 		}
Please login to merge, or discard this patch.
lib/public/Files/ObjectStore/IObjectStore.php 1 patch
Indentation   +35 added lines, -35 removed lines patch added patch discarded remove patch
@@ -33,43 +33,43 @@
 block discarded – undo
33 33
  */
34 34
 interface IObjectStore {
35 35
 
36
-	/**
37
-	 * @return string the container or bucket name where objects are stored
38
-	 * @since 7.0.0
39
-	 */
40
-	public function getStorageId();
36
+    /**
37
+     * @return string the container or bucket name where objects are stored
38
+     * @since 7.0.0
39
+     */
40
+    public function getStorageId();
41 41
 
42
-	/**
43
-	 * @param string $urn the unified resource name used to identify the object
44
-	 * @return resource stream with the read data
45
-	 * @throws \Exception when something goes wrong, message will be logged
46
-	 * @throws NotFoundException if file does not exist
47
-	 * @since 7.0.0
48
-	 */
49
-	public function readObject($urn);
42
+    /**
43
+     * @param string $urn the unified resource name used to identify the object
44
+     * @return resource stream with the read data
45
+     * @throws \Exception when something goes wrong, message will be logged
46
+     * @throws NotFoundException if file does not exist
47
+     * @since 7.0.0
48
+     */
49
+    public function readObject($urn);
50 50
 
51
-	/**
52
-	 * @param string $urn the unified resource name used to identify the object
53
-	 * @param resource $stream stream with the data to write
54
-	 * @throws \Exception when something goes wrong, message will be logged
55
-	 * @since 7.0.0
56
-	 */
57
-	public function writeObject($urn, $stream);
51
+    /**
52
+     * @param string $urn the unified resource name used to identify the object
53
+     * @param resource $stream stream with the data to write
54
+     * @throws \Exception when something goes wrong, message will be logged
55
+     * @since 7.0.0
56
+     */
57
+    public function writeObject($urn, $stream);
58 58
 
59
-	/**
60
-	 * @param string $urn the unified resource name used to identify the object
61
-	 * @return void
62
-	 * @throws \Exception when something goes wrong, message will be logged
63
-	 * @since 7.0.0
64
-	 */
65
-	public function deleteObject($urn);
59
+    /**
60
+     * @param string $urn the unified resource name used to identify the object
61
+     * @return void
62
+     * @throws \Exception when something goes wrong, message will be logged
63
+     * @since 7.0.0
64
+     */
65
+    public function deleteObject($urn);
66 66
 
67
-	/**
68
-	 * Check if an object exists in the object store
69
-	 *
70
-	 * @param string $urn
71
-	 * @return bool
72
-	 * @since 16.0.0
73
-	 */
74
-	public function objectExists($urn);
67
+    /**
68
+     * Check if an object exists in the object store
69
+     *
70
+     * @param string $urn
71
+     * @return bool
72
+     * @since 16.0.0
73
+     */
74
+    public function objectExists($urn);
75 75
 }
Please login to merge, or discard this patch.