Passed
Push — master ( 2525f7...d93669 )
by John
15:52 queued 13s
created
lib/private/Files/ObjectStore/S3ObjectTrait.php 1 patch
Indentation   +148 added lines, -148 removed lines patch added patch discarded remove patch
@@ -35,152 +35,152 @@
 block discarded – undo
35 35
 use Psr\Http\Message\StreamInterface;
36 36
 
37 37
 trait S3ObjectTrait {
38
-	/**
39
-	 * Returns the connection
40
-	 *
41
-	 * @return S3Client connected client
42
-	 * @throws \Exception if connection could not be made
43
-	 */
44
-	abstract protected function getConnection();
45
-
46
-	abstract protected function getCertificateBundlePath(): ?string;
47
-
48
-	/**
49
-	 * @param string $urn the unified resource name used to identify the object
50
-	 * @return resource stream with the read data
51
-	 * @throws \Exception when something goes wrong, message will be logged
52
-	 * @since 7.0.0
53
-	 */
54
-	public function readObject($urn) {
55
-		return SeekableHttpStream::open(function ($range) use ($urn) {
56
-			$command = $this->getConnection()->getCommand('GetObject', [
57
-				'Bucket' => $this->bucket,
58
-				'Key' => $urn,
59
-				'Range' => 'bytes=' . $range,
60
-			]);
61
-			$request = \Aws\serialize($command);
62
-			$headers = [];
63
-			foreach ($request->getHeaders() as $key => $values) {
64
-				foreach ($values as $value) {
65
-					$headers[] = "$key: $value";
66
-				}
67
-			}
68
-			$opts = [
69
-				'http' => [
70
-					'protocol_version' => $request->getProtocolVersion(),
71
-					'header' => $headers,
72
-				]
73
-			];
74
-			$bundle = $this->getCertificateBundlePath();
75
-			if ($bundle) {
76
-				$opts['ssl'] = [
77
-					'cafile' => $bundle
78
-				];
79
-			}
80
-
81
-			if ($this->getProxy()) {
82
-				$opts['http']['proxy'] = $this->getProxy();
83
-				$opts['http']['request_fulluri'] = true;
84
-			}
85
-
86
-			$context = stream_context_create($opts);
87
-			return fopen($request->getUri(), 'r', false, $context);
88
-		});
89
-	}
90
-
91
-	/**
92
-	 * Single object put helper
93
-	 *
94
-	 * @param string $urn the unified resource name used to identify the object
95
-	 * @param StreamInterface $stream stream with the data to write
96
-	 * @param string|null $mimetype the mimetype to set for the remove object @since 22.0.0
97
-	 * @throws \Exception when something goes wrong, message will be logged
98
-	 */
99
-	protected function writeSingle(string $urn, StreamInterface $stream, string $mimetype = null): void {
100
-		$this->getConnection()->putObject([
101
-			'Bucket' => $this->bucket,
102
-			'Key' => $urn,
103
-			'Body' => $stream,
104
-			'ACL' => 'private',
105
-			'ContentType' => $mimetype,
106
-		]);
107
-	}
108
-
109
-
110
-	/**
111
-	 * Multipart upload helper that tries to avoid orphaned fragments in S3
112
-	 *
113
-	 * @param string $urn the unified resource name used to identify the object
114
-	 * @param StreamInterface $stream stream with the data to write
115
-	 * @param string|null $mimetype the mimetype to set for the remove object
116
-	 * @throws \Exception when something goes wrong, message will be logged
117
-	 */
118
-	protected function writeMultiPart(string $urn, StreamInterface $stream, string $mimetype = null): void {
119
-		$uploader = new MultipartUploader($this->getConnection(), $stream, [
120
-			'bucket' => $this->bucket,
121
-			'key' => $urn,
122
-			'part_size' => $this->uploadPartSize,
123
-			'params' => [
124
-				'ContentType' => $mimetype
125
-			],
126
-		]);
127
-
128
-		try {
129
-			$uploader->upload();
130
-		} catch (S3MultipartUploadException $e) {
131
-			// if anything goes wrong with multipart, make sure that you don´t poison and
132
-			// slow down s3 bucket with orphaned fragments
133
-			$uploadInfo = $e->getState()->getId();
134
-			if ($e->getState()->isInitiated() && (array_key_exists('UploadId', $uploadInfo))) {
135
-				$this->getConnection()->abortMultipartUpload($uploadInfo);
136
-			}
137
-			throw new \OCA\DAV\Connector\Sabre\Exception\BadGateway("Error while uploading to S3 bucket", 0, $e);
138
-		}
139
-	}
140
-
141
-
142
-	/**
143
-	 * @param string $urn the unified resource name used to identify the object
144
-	 * @param resource $stream stream with the data to write
145
-	 * @param string|null $mimetype the mimetype to set for the remove object @since 22.0.0
146
-	 * @throws \Exception when something goes wrong, message will be logged
147
-	 * @since 7.0.0
148
-	 */
149
-	public function writeObject($urn, $stream, string $mimetype = null) {
150
-		$psrStream = Utils::streamFor($stream);
151
-
152
-		// ($psrStream->isSeekable() && $psrStream->getSize() !== null) evaluates to true for a On-Seekable stream
153
-		// so the optimisation does not apply
154
-		$buffer = new Psr7\Stream(fopen("php://memory", 'rwb+'));
155
-		Utils::copyToStream($psrStream, $buffer, $this->putSizeLimit);
156
-		$buffer->seek(0);
157
-		if ($buffer->getSize() < $this->putSizeLimit) {
158
-			// buffer is fully seekable, so use it directly for the small upload
159
-			$this->writeSingle($urn, $buffer, $mimetype);
160
-		} else {
161
-			$loadStream = new Psr7\AppendStream([$buffer, $psrStream]);
162
-			$this->writeMultiPart($urn, $loadStream, $mimetype);
163
-		}
164
-	}
165
-
166
-	/**
167
-	 * @param string $urn the unified resource name used to identify the object
168
-	 * @return void
169
-	 * @throws \Exception when something goes wrong, message will be logged
170
-	 * @since 7.0.0
171
-	 */
172
-	public function deleteObject($urn) {
173
-		$this->getConnection()->deleteObject([
174
-			'Bucket' => $this->bucket,
175
-			'Key' => $urn,
176
-		]);
177
-	}
178
-
179
-	public function objectExists($urn) {
180
-		return $this->getConnection()->doesObjectExist($this->bucket, $urn);
181
-	}
182
-
183
-	public function copyObject($from, $to) {
184
-		$this->getConnection()->copy($this->getBucket(), $from, $this->getBucket(), $to);
185
-	}
38
+    /**
39
+     * Returns the connection
40
+     *
41
+     * @return S3Client connected client
42
+     * @throws \Exception if connection could not be made
43
+     */
44
+    abstract protected function getConnection();
45
+
46
+    abstract protected function getCertificateBundlePath(): ?string;
47
+
48
+    /**
49
+     * @param string $urn the unified resource name used to identify the object
50
+     * @return resource stream with the read data
51
+     * @throws \Exception when something goes wrong, message will be logged
52
+     * @since 7.0.0
53
+     */
54
+    public function readObject($urn) {
55
+        return SeekableHttpStream::open(function ($range) use ($urn) {
56
+            $command = $this->getConnection()->getCommand('GetObject', [
57
+                'Bucket' => $this->bucket,
58
+                'Key' => $urn,
59
+                'Range' => 'bytes=' . $range,
60
+            ]);
61
+            $request = \Aws\serialize($command);
62
+            $headers = [];
63
+            foreach ($request->getHeaders() as $key => $values) {
64
+                foreach ($values as $value) {
65
+                    $headers[] = "$key: $value";
66
+                }
67
+            }
68
+            $opts = [
69
+                'http' => [
70
+                    'protocol_version' => $request->getProtocolVersion(),
71
+                    'header' => $headers,
72
+                ]
73
+            ];
74
+            $bundle = $this->getCertificateBundlePath();
75
+            if ($bundle) {
76
+                $opts['ssl'] = [
77
+                    'cafile' => $bundle
78
+                ];
79
+            }
80
+
81
+            if ($this->getProxy()) {
82
+                $opts['http']['proxy'] = $this->getProxy();
83
+                $opts['http']['request_fulluri'] = true;
84
+            }
85
+
86
+            $context = stream_context_create($opts);
87
+            return fopen($request->getUri(), 'r', false, $context);
88
+        });
89
+    }
90
+
91
+    /**
92
+     * Single object put helper
93
+     *
94
+     * @param string $urn the unified resource name used to identify the object
95
+     * @param StreamInterface $stream stream with the data to write
96
+     * @param string|null $mimetype the mimetype to set for the remove object @since 22.0.0
97
+     * @throws \Exception when something goes wrong, message will be logged
98
+     */
99
+    protected function writeSingle(string $urn, StreamInterface $stream, string $mimetype = null): void {
100
+        $this->getConnection()->putObject([
101
+            'Bucket' => $this->bucket,
102
+            'Key' => $urn,
103
+            'Body' => $stream,
104
+            'ACL' => 'private',
105
+            'ContentType' => $mimetype,
106
+        ]);
107
+    }
108
+
109
+
110
+    /**
111
+     * Multipart upload helper that tries to avoid orphaned fragments in S3
112
+     *
113
+     * @param string $urn the unified resource name used to identify the object
114
+     * @param StreamInterface $stream stream with the data to write
115
+     * @param string|null $mimetype the mimetype to set for the remove object
116
+     * @throws \Exception when something goes wrong, message will be logged
117
+     */
118
+    protected function writeMultiPart(string $urn, StreamInterface $stream, string $mimetype = null): void {
119
+        $uploader = new MultipartUploader($this->getConnection(), $stream, [
120
+            'bucket' => $this->bucket,
121
+            'key' => $urn,
122
+            'part_size' => $this->uploadPartSize,
123
+            'params' => [
124
+                'ContentType' => $mimetype
125
+            ],
126
+        ]);
127
+
128
+        try {
129
+            $uploader->upload();
130
+        } catch (S3MultipartUploadException $e) {
131
+            // if anything goes wrong with multipart, make sure that you don´t poison and
132
+            // slow down s3 bucket with orphaned fragments
133
+            $uploadInfo = $e->getState()->getId();
134
+            if ($e->getState()->isInitiated() && (array_key_exists('UploadId', $uploadInfo))) {
135
+                $this->getConnection()->abortMultipartUpload($uploadInfo);
136
+            }
137
+            throw new \OCA\DAV\Connector\Sabre\Exception\BadGateway("Error while uploading to S3 bucket", 0, $e);
138
+        }
139
+    }
140
+
141
+
142
+    /**
143
+     * @param string $urn the unified resource name used to identify the object
144
+     * @param resource $stream stream with the data to write
145
+     * @param string|null $mimetype the mimetype to set for the remove object @since 22.0.0
146
+     * @throws \Exception when something goes wrong, message will be logged
147
+     * @since 7.0.0
148
+     */
149
+    public function writeObject($urn, $stream, string $mimetype = null) {
150
+        $psrStream = Utils::streamFor($stream);
151
+
152
+        // ($psrStream->isSeekable() && $psrStream->getSize() !== null) evaluates to true for a On-Seekable stream
153
+        // so the optimisation does not apply
154
+        $buffer = new Psr7\Stream(fopen("php://memory", 'rwb+'));
155
+        Utils::copyToStream($psrStream, $buffer, $this->putSizeLimit);
156
+        $buffer->seek(0);
157
+        if ($buffer->getSize() < $this->putSizeLimit) {
158
+            // buffer is fully seekable, so use it directly for the small upload
159
+            $this->writeSingle($urn, $buffer, $mimetype);
160
+        } else {
161
+            $loadStream = new Psr7\AppendStream([$buffer, $psrStream]);
162
+            $this->writeMultiPart($urn, $loadStream, $mimetype);
163
+        }
164
+    }
165
+
166
+    /**
167
+     * @param string $urn the unified resource name used to identify the object
168
+     * @return void
169
+     * @throws \Exception when something goes wrong, message will be logged
170
+     * @since 7.0.0
171
+     */
172
+    public function deleteObject($urn) {
173
+        $this->getConnection()->deleteObject([
174
+            'Bucket' => $this->bucket,
175
+            'Key' => $urn,
176
+        ]);
177
+    }
178
+
179
+    public function objectExists($urn) {
180
+        return $this->getConnection()->doesObjectExist($this->bucket, $urn);
181
+    }
182
+
183
+    public function copyObject($from, $to) {
184
+        $this->getConnection()->copy($this->getBucket(), $from, $this->getBucket(), $to);
185
+    }
186 186
 }
Please login to merge, or discard this patch.