@@ -35,163 +35,163 @@ |
||
| 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 | - abstract protected function getSSECParameters(bool $copy = false): array; |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * @param string $urn the unified resource name used to identify the object |
|
| 51 | - * |
|
| 52 | - * @return resource stream with the read data |
|
| 53 | - * @throws \Exception when something goes wrong, message will be logged |
|
| 54 | - * @since 7.0.0 |
|
| 55 | - */ |
|
| 56 | - public function readObject($urn) { |
|
| 57 | - $fh = SeekableHttpStream::open(function ($range) use ($urn) { |
|
| 58 | - $command = $this->getConnection()->getCommand('GetObject', [ |
|
| 59 | - 'Bucket' => $this->bucket, |
|
| 60 | - 'Key' => $urn, |
|
| 61 | - 'Range' => 'bytes=' . $range, |
|
| 62 | - ] + $this->getSSECParameters()); |
|
| 63 | - $request = \Aws\serialize($command); |
|
| 64 | - $headers = []; |
|
| 65 | - foreach ($request->getHeaders() as $key => $values) { |
|
| 66 | - foreach ($values as $value) { |
|
| 67 | - $headers[] = "$key: $value"; |
|
| 68 | - } |
|
| 69 | - } |
|
| 70 | - $opts = [ |
|
| 71 | - 'http' => [ |
|
| 72 | - 'protocol_version' => $request->getProtocolVersion(), |
|
| 73 | - 'header' => $headers, |
|
| 74 | - ] |
|
| 75 | - ]; |
|
| 76 | - $bundle = $this->getCertificateBundlePath(); |
|
| 77 | - if ($bundle) { |
|
| 78 | - $opts['ssl'] = [ |
|
| 79 | - 'cafile' => $bundle |
|
| 80 | - ]; |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - if ($this->getProxy()) { |
|
| 84 | - $opts['http']['proxy'] = $this->getProxy(); |
|
| 85 | - $opts['http']['request_fulluri'] = true; |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - $context = stream_context_create($opts); |
|
| 89 | - return fopen($request->getUri(), 'r', false, $context); |
|
| 90 | - }); |
|
| 91 | - if (!$fh) { |
|
| 92 | - throw new \Exception("Failed to read object $urn"); |
|
| 93 | - } |
|
| 94 | - return $fh; |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - |
|
| 98 | - /** |
|
| 99 | - * Single object put helper |
|
| 100 | - * |
|
| 101 | - * @param string $urn the unified resource name used to identify the object |
|
| 102 | - * @param StreamInterface $stream stream with the data to write |
|
| 103 | - * @param string|null $mimetype the mimetype to set for the remove object @since 22.0.0 |
|
| 104 | - * @throws \Exception when something goes wrong, message will be logged |
|
| 105 | - */ |
|
| 106 | - protected function writeSingle(string $urn, StreamInterface $stream, string $mimetype = null): void { |
|
| 107 | - $this->getConnection()->putObject([ |
|
| 108 | - 'Bucket' => $this->bucket, |
|
| 109 | - 'Key' => $urn, |
|
| 110 | - 'Body' => $stream, |
|
| 111 | - 'ACL' => 'private', |
|
| 112 | - 'ContentType' => $mimetype, |
|
| 113 | - 'StorageClass' => $this->storageClass, |
|
| 114 | - ] + $this->getSSECParameters()); |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - |
|
| 118 | - /** |
|
| 119 | - * Multipart upload helper that tries to avoid orphaned fragments in S3 |
|
| 120 | - * |
|
| 121 | - * @param string $urn the unified resource name used to identify the object |
|
| 122 | - * @param StreamInterface $stream stream with the data to write |
|
| 123 | - * @param string|null $mimetype the mimetype to set for the remove object |
|
| 124 | - * @throws \Exception when something goes wrong, message will be logged |
|
| 125 | - */ |
|
| 126 | - protected function writeMultiPart(string $urn, StreamInterface $stream, string $mimetype = null): void { |
|
| 127 | - $uploader = new MultipartUploader($this->getConnection(), $stream, [ |
|
| 128 | - 'bucket' => $this->bucket, |
|
| 129 | - 'key' => $urn, |
|
| 130 | - 'part_size' => $this->uploadPartSize, |
|
| 131 | - 'params' => [ |
|
| 132 | - 'ContentType' => $mimetype, |
|
| 133 | - 'StorageClass' => $this->storageClass, |
|
| 134 | - ] + $this->getSSECParameters(), |
|
| 135 | - ]); |
|
| 136 | - |
|
| 137 | - try { |
|
| 138 | - $uploader->upload(); |
|
| 139 | - } catch (S3MultipartUploadException $e) { |
|
| 140 | - // if anything goes wrong with multipart, make sure that you don´t poison and |
|
| 141 | - // slow down s3 bucket with orphaned fragments |
|
| 142 | - $uploadInfo = $e->getState()->getId(); |
|
| 143 | - if ($e->getState()->isInitiated() && (array_key_exists('UploadId', $uploadInfo))) { |
|
| 144 | - $this->getConnection()->abortMultipartUpload($uploadInfo); |
|
| 145 | - } |
|
| 146 | - throw new \OCA\DAV\Connector\Sabre\Exception\BadGateway("Error while uploading to S3 bucket", 0, $e); |
|
| 147 | - } |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - |
|
| 151 | - /** |
|
| 152 | - * @param string $urn the unified resource name used to identify the object |
|
| 153 | - * @param resource $stream stream with the data to write |
|
| 154 | - * @param string|null $mimetype the mimetype to set for the remove object @since 22.0.0 |
|
| 155 | - * @throws \Exception when something goes wrong, message will be logged |
|
| 156 | - * @since 7.0.0 |
|
| 157 | - */ |
|
| 158 | - public function writeObject($urn, $stream, string $mimetype = null) { |
|
| 159 | - $psrStream = Utils::streamFor($stream); |
|
| 160 | - |
|
| 161 | - // ($psrStream->isSeekable() && $psrStream->getSize() !== null) evaluates to true for a On-Seekable stream |
|
| 162 | - // so the optimisation does not apply |
|
| 163 | - $buffer = new Psr7\Stream(fopen("php://memory", 'rwb+')); |
|
| 164 | - Utils::copyToStream($psrStream, $buffer, $this->putSizeLimit); |
|
| 165 | - $buffer->seek(0); |
|
| 166 | - if ($buffer->getSize() < $this->putSizeLimit) { |
|
| 167 | - // buffer is fully seekable, so use it directly for the small upload |
|
| 168 | - $this->writeSingle($urn, $buffer, $mimetype); |
|
| 169 | - } else { |
|
| 170 | - $loadStream = new Psr7\AppendStream([$buffer, $psrStream]); |
|
| 171 | - $this->writeMultiPart($urn, $loadStream, $mimetype); |
|
| 172 | - } |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - /** |
|
| 176 | - * @param string $urn the unified resource name used to identify the object |
|
| 177 | - * @return void |
|
| 178 | - * @throws \Exception when something goes wrong, message will be logged |
|
| 179 | - * @since 7.0.0 |
|
| 180 | - */ |
|
| 181 | - public function deleteObject($urn) { |
|
| 182 | - $this->getConnection()->deleteObject([ |
|
| 183 | - 'Bucket' => $this->bucket, |
|
| 184 | - 'Key' => $urn, |
|
| 185 | - ]); |
|
| 186 | - } |
|
| 187 | - |
|
| 188 | - public function objectExists($urn) { |
|
| 189 | - return $this->getConnection()->doesObjectExist($this->bucket, $urn, $this->getSSECParameters()); |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - public function copyObject($from, $to) { |
|
| 193 | - $this->getConnection()->copy($this->getBucket(), $from, $this->getBucket(), $to, 'private', [ |
|
| 194 | - 'params' => $this->getSSECParameters() + $this->getSSECParameters(true) |
|
| 195 | - ]); |
|
| 196 | - } |
|
| 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 | + abstract protected function getSSECParameters(bool $copy = false): array; |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * @param string $urn the unified resource name used to identify the object |
|
| 51 | + * |
|
| 52 | + * @return resource stream with the read data |
|
| 53 | + * @throws \Exception when something goes wrong, message will be logged |
|
| 54 | + * @since 7.0.0 |
|
| 55 | + */ |
|
| 56 | + public function readObject($urn) { |
|
| 57 | + $fh = SeekableHttpStream::open(function ($range) use ($urn) { |
|
| 58 | + $command = $this->getConnection()->getCommand('GetObject', [ |
|
| 59 | + 'Bucket' => $this->bucket, |
|
| 60 | + 'Key' => $urn, |
|
| 61 | + 'Range' => 'bytes=' . $range, |
|
| 62 | + ] + $this->getSSECParameters()); |
|
| 63 | + $request = \Aws\serialize($command); |
|
| 64 | + $headers = []; |
|
| 65 | + foreach ($request->getHeaders() as $key => $values) { |
|
| 66 | + foreach ($values as $value) { |
|
| 67 | + $headers[] = "$key: $value"; |
|
| 68 | + } |
|
| 69 | + } |
|
| 70 | + $opts = [ |
|
| 71 | + 'http' => [ |
|
| 72 | + 'protocol_version' => $request->getProtocolVersion(), |
|
| 73 | + 'header' => $headers, |
|
| 74 | + ] |
|
| 75 | + ]; |
|
| 76 | + $bundle = $this->getCertificateBundlePath(); |
|
| 77 | + if ($bundle) { |
|
| 78 | + $opts['ssl'] = [ |
|
| 79 | + 'cafile' => $bundle |
|
| 80 | + ]; |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + if ($this->getProxy()) { |
|
| 84 | + $opts['http']['proxy'] = $this->getProxy(); |
|
| 85 | + $opts['http']['request_fulluri'] = true; |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + $context = stream_context_create($opts); |
|
| 89 | + return fopen($request->getUri(), 'r', false, $context); |
|
| 90 | + }); |
|
| 91 | + if (!$fh) { |
|
| 92 | + throw new \Exception("Failed to read object $urn"); |
|
| 93 | + } |
|
| 94 | + return $fh; |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + |
|
| 98 | + /** |
|
| 99 | + * Single object put helper |
|
| 100 | + * |
|
| 101 | + * @param string $urn the unified resource name used to identify the object |
|
| 102 | + * @param StreamInterface $stream stream with the data to write |
|
| 103 | + * @param string|null $mimetype the mimetype to set for the remove object @since 22.0.0 |
|
| 104 | + * @throws \Exception when something goes wrong, message will be logged |
|
| 105 | + */ |
|
| 106 | + protected function writeSingle(string $urn, StreamInterface $stream, string $mimetype = null): void { |
|
| 107 | + $this->getConnection()->putObject([ |
|
| 108 | + 'Bucket' => $this->bucket, |
|
| 109 | + 'Key' => $urn, |
|
| 110 | + 'Body' => $stream, |
|
| 111 | + 'ACL' => 'private', |
|
| 112 | + 'ContentType' => $mimetype, |
|
| 113 | + 'StorageClass' => $this->storageClass, |
|
| 114 | + ] + $this->getSSECParameters()); |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + |
|
| 118 | + /** |
|
| 119 | + * Multipart upload helper that tries to avoid orphaned fragments in S3 |
|
| 120 | + * |
|
| 121 | + * @param string $urn the unified resource name used to identify the object |
|
| 122 | + * @param StreamInterface $stream stream with the data to write |
|
| 123 | + * @param string|null $mimetype the mimetype to set for the remove object |
|
| 124 | + * @throws \Exception when something goes wrong, message will be logged |
|
| 125 | + */ |
|
| 126 | + protected function writeMultiPart(string $urn, StreamInterface $stream, string $mimetype = null): void { |
|
| 127 | + $uploader = new MultipartUploader($this->getConnection(), $stream, [ |
|
| 128 | + 'bucket' => $this->bucket, |
|
| 129 | + 'key' => $urn, |
|
| 130 | + 'part_size' => $this->uploadPartSize, |
|
| 131 | + 'params' => [ |
|
| 132 | + 'ContentType' => $mimetype, |
|
| 133 | + 'StorageClass' => $this->storageClass, |
|
| 134 | + ] + $this->getSSECParameters(), |
|
| 135 | + ]); |
|
| 136 | + |
|
| 137 | + try { |
|
| 138 | + $uploader->upload(); |
|
| 139 | + } catch (S3MultipartUploadException $e) { |
|
| 140 | + // if anything goes wrong with multipart, make sure that you don´t poison and |
|
| 141 | + // slow down s3 bucket with orphaned fragments |
|
| 142 | + $uploadInfo = $e->getState()->getId(); |
|
| 143 | + if ($e->getState()->isInitiated() && (array_key_exists('UploadId', $uploadInfo))) { |
|
| 144 | + $this->getConnection()->abortMultipartUpload($uploadInfo); |
|
| 145 | + } |
|
| 146 | + throw new \OCA\DAV\Connector\Sabre\Exception\BadGateway("Error while uploading to S3 bucket", 0, $e); |
|
| 147 | + } |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + |
|
| 151 | + /** |
|
| 152 | + * @param string $urn the unified resource name used to identify the object |
|
| 153 | + * @param resource $stream stream with the data to write |
|
| 154 | + * @param string|null $mimetype the mimetype to set for the remove object @since 22.0.0 |
|
| 155 | + * @throws \Exception when something goes wrong, message will be logged |
|
| 156 | + * @since 7.0.0 |
|
| 157 | + */ |
|
| 158 | + public function writeObject($urn, $stream, string $mimetype = null) { |
|
| 159 | + $psrStream = Utils::streamFor($stream); |
|
| 160 | + |
|
| 161 | + // ($psrStream->isSeekable() && $psrStream->getSize() !== null) evaluates to true for a On-Seekable stream |
|
| 162 | + // so the optimisation does not apply |
|
| 163 | + $buffer = new Psr7\Stream(fopen("php://memory", 'rwb+')); |
|
| 164 | + Utils::copyToStream($psrStream, $buffer, $this->putSizeLimit); |
|
| 165 | + $buffer->seek(0); |
|
| 166 | + if ($buffer->getSize() < $this->putSizeLimit) { |
|
| 167 | + // buffer is fully seekable, so use it directly for the small upload |
|
| 168 | + $this->writeSingle($urn, $buffer, $mimetype); |
|
| 169 | + } else { |
|
| 170 | + $loadStream = new Psr7\AppendStream([$buffer, $psrStream]); |
|
| 171 | + $this->writeMultiPart($urn, $loadStream, $mimetype); |
|
| 172 | + } |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + /** |
|
| 176 | + * @param string $urn the unified resource name used to identify the object |
|
| 177 | + * @return void |
|
| 178 | + * @throws \Exception when something goes wrong, message will be logged |
|
| 179 | + * @since 7.0.0 |
|
| 180 | + */ |
|
| 181 | + public function deleteObject($urn) { |
|
| 182 | + $this->getConnection()->deleteObject([ |
|
| 183 | + 'Bucket' => $this->bucket, |
|
| 184 | + 'Key' => $urn, |
|
| 185 | + ]); |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + public function objectExists($urn) { |
|
| 189 | + return $this->getConnection()->doesObjectExist($this->bucket, $urn, $this->getSSECParameters()); |
|
| 190 | + } |
|
| 191 | + |
|
| 192 | + public function copyObject($from, $to) { |
|
| 193 | + $this->getConnection()->copy($this->getBucket(), $from, $this->getBucket(), $to, 'private', [ |
|
| 194 | + 'params' => $this->getSSECParameters() + $this->getSSECParameters(true) |
|
| 195 | + ]); |
|
| 196 | + } |
|
| 197 | 197 | } |
@@ -33,52 +33,52 @@ |
||
| 33 | 33 | use Symfony\Component\Console\Question\ConfirmationQuestion; |
| 34 | 34 | |
| 35 | 35 | class Put extends Command { |
| 36 | - private ObjectUtil $objectUtils; |
|
| 37 | - private IMimeTypeDetector $mimeTypeDetector; |
|
| 36 | + private ObjectUtil $objectUtils; |
|
| 37 | + private IMimeTypeDetector $mimeTypeDetector; |
|
| 38 | 38 | |
| 39 | - public function __construct(ObjectUtil $objectUtils, IMimeTypeDetector $mimeTypeDetector) { |
|
| 40 | - $this->objectUtils = $objectUtils; |
|
| 41 | - $this->mimeTypeDetector = $mimeTypeDetector; |
|
| 42 | - parent::__construct(); |
|
| 43 | - } |
|
| 39 | + public function __construct(ObjectUtil $objectUtils, IMimeTypeDetector $mimeTypeDetector) { |
|
| 40 | + $this->objectUtils = $objectUtils; |
|
| 41 | + $this->mimeTypeDetector = $mimeTypeDetector; |
|
| 42 | + parent::__construct(); |
|
| 43 | + } |
|
| 44 | 44 | |
| 45 | - protected function configure(): void { |
|
| 46 | - $this |
|
| 47 | - ->setName('files:object:put') |
|
| 48 | - ->setDescription('Write a file to the object store') |
|
| 49 | - ->addArgument('input', InputArgument::REQUIRED, "Source local path, use - to read from STDIN") |
|
| 50 | - ->addArgument('object', InputArgument::REQUIRED, "Object to write") |
|
| 51 | - ->addOption('bucket', 'b', InputOption::VALUE_REQUIRED, "Bucket where to store the object, only required in cases where it can't be determined from the config");; |
|
| 52 | - } |
|
| 45 | + protected function configure(): void { |
|
| 46 | + $this |
|
| 47 | + ->setName('files:object:put') |
|
| 48 | + ->setDescription('Write a file to the object store') |
|
| 49 | + ->addArgument('input', InputArgument::REQUIRED, "Source local path, use - to read from STDIN") |
|
| 50 | + ->addArgument('object', InputArgument::REQUIRED, "Object to write") |
|
| 51 | + ->addOption('bucket', 'b', InputOption::VALUE_REQUIRED, "Bucket where to store the object, only required in cases where it can't be determined from the config");; |
|
| 52 | + } |
|
| 53 | 53 | |
| 54 | - public function execute(InputInterface $input, OutputInterface $output): int { |
|
| 55 | - $object = $input->getArgument('object'); |
|
| 56 | - $inputName = (string)$input->getArgument('input'); |
|
| 57 | - $objectStore = $this->objectUtils->getObjectStore($input->getOption("bucket"), $output); |
|
| 58 | - if (!$objectStore) { |
|
| 59 | - return -1; |
|
| 60 | - } |
|
| 54 | + public function execute(InputInterface $input, OutputInterface $output): int { |
|
| 55 | + $object = $input->getArgument('object'); |
|
| 56 | + $inputName = (string)$input->getArgument('input'); |
|
| 57 | + $objectStore = $this->objectUtils->getObjectStore($input->getOption("bucket"), $output); |
|
| 58 | + if (!$objectStore) { |
|
| 59 | + return -1; |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | - if ($fileId = $this->objectUtils->objectExistsInDb($object)) { |
|
| 63 | - $output->writeln("<error>Warning, object $object belongs to an existing file, overwriting the object contents can lead to unexpected behavior.</error>"); |
|
| 64 | - $output->writeln("You can use <info>occ files:put $inputName $fileId</info> to write to the file safely."); |
|
| 65 | - $output->writeln(""); |
|
| 62 | + if ($fileId = $this->objectUtils->objectExistsInDb($object)) { |
|
| 63 | + $output->writeln("<error>Warning, object $object belongs to an existing file, overwriting the object contents can lead to unexpected behavior.</error>"); |
|
| 64 | + $output->writeln("You can use <info>occ files:put $inputName $fileId</info> to write to the file safely."); |
|
| 65 | + $output->writeln(""); |
|
| 66 | 66 | |
| 67 | - /** @var QuestionHelper $helper */ |
|
| 68 | - $helper = $this->getHelper('question'); |
|
| 69 | - $question = new ConfirmationQuestion("Write to the object anyway? [y/N] ", false); |
|
| 70 | - if (!$helper->ask($input, $output, $question)) { |
|
| 71 | - return -1; |
|
| 72 | - } |
|
| 73 | - } |
|
| 67 | + /** @var QuestionHelper $helper */ |
|
| 68 | + $helper = $this->getHelper('question'); |
|
| 69 | + $question = new ConfirmationQuestion("Write to the object anyway? [y/N] ", false); |
|
| 70 | + if (!$helper->ask($input, $output, $question)) { |
|
| 71 | + return -1; |
|
| 72 | + } |
|
| 73 | + } |
|
| 74 | 74 | |
| 75 | - $source = $inputName === '-' ? STDIN : fopen($inputName, 'r'); |
|
| 76 | - if (!$source) { |
|
| 77 | - $output->writeln("<error>Failed to open $inputName</error>"); |
|
| 78 | - return 1; |
|
| 79 | - } |
|
| 80 | - $objectStore->writeObject($object, $source, $this->mimeTypeDetector->detectPath($inputName)); |
|
| 81 | - return 0; |
|
| 82 | - } |
|
| 75 | + $source = $inputName === '-' ? STDIN : fopen($inputName, 'r'); |
|
| 76 | + if (!$source) { |
|
| 77 | + $output->writeln("<error>Failed to open $inputName</error>"); |
|
| 78 | + return 1; |
|
| 79 | + } |
|
| 80 | + $objectStore->writeObject($object, $source, $this->mimeTypeDetector->detectPath($inputName)); |
|
| 81 | + return 0; |
|
| 82 | + } |
|
| 83 | 83 | |
| 84 | 84 | } |
@@ -30,81 +30,81 @@ |
||
| 30 | 30 | use Symfony\Component\Console\Output\OutputInterface; |
| 31 | 31 | |
| 32 | 32 | class ObjectUtil { |
| 33 | - private IConfig $config; |
|
| 34 | - private IDBConnection $connection; |
|
| 33 | + private IConfig $config; |
|
| 34 | + private IDBConnection $connection; |
|
| 35 | 35 | |
| 36 | - public function __construct(IConfig $config, IDBConnection $connection) { |
|
| 37 | - $this->config = $config; |
|
| 38 | - $this->connection = $connection; |
|
| 39 | - } |
|
| 36 | + public function __construct(IConfig $config, IDBConnection $connection) { |
|
| 37 | + $this->config = $config; |
|
| 38 | + $this->connection = $connection; |
|
| 39 | + } |
|
| 40 | 40 | |
| 41 | - private function getObjectStoreConfig(): ?array { |
|
| 42 | - $config = $this->config->getSystemValue('objectstore_multibucket'); |
|
| 43 | - if (is_array($config)) { |
|
| 44 | - $config['multibucket'] = true; |
|
| 45 | - return $config; |
|
| 46 | - } |
|
| 47 | - $config = $this->config->getSystemValue('objectstore'); |
|
| 48 | - if (is_array($config)) { |
|
| 49 | - if (!isset($config['multibucket'])) { |
|
| 50 | - $config['multibucket'] = false; |
|
| 51 | - } |
|
| 52 | - return $config; |
|
| 53 | - } else { |
|
| 54 | - return null; |
|
| 55 | - } |
|
| 56 | - } |
|
| 41 | + private function getObjectStoreConfig(): ?array { |
|
| 42 | + $config = $this->config->getSystemValue('objectstore_multibucket'); |
|
| 43 | + if (is_array($config)) { |
|
| 44 | + $config['multibucket'] = true; |
|
| 45 | + return $config; |
|
| 46 | + } |
|
| 47 | + $config = $this->config->getSystemValue('objectstore'); |
|
| 48 | + if (is_array($config)) { |
|
| 49 | + if (!isset($config['multibucket'])) { |
|
| 50 | + $config['multibucket'] = false; |
|
| 51 | + } |
|
| 52 | + return $config; |
|
| 53 | + } else { |
|
| 54 | + return null; |
|
| 55 | + } |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | - public function getObjectStore(?string $bucket, OutputInterface $output): ?IObjectStore { |
|
| 59 | - $config = $this->getObjectStoreConfig(); |
|
| 60 | - if (!$config) { |
|
| 61 | - $output->writeln("<error>Instance is not using primary object store</error>"); |
|
| 62 | - return null; |
|
| 63 | - } |
|
| 64 | - if ($config['multibucket'] && !$bucket) { |
|
| 65 | - $output->writeln("<error>--bucket option required</error> because <info>multi bucket</info> is enabled."); |
|
| 66 | - return null; |
|
| 67 | - } |
|
| 58 | + public function getObjectStore(?string $bucket, OutputInterface $output): ?IObjectStore { |
|
| 59 | + $config = $this->getObjectStoreConfig(); |
|
| 60 | + if (!$config) { |
|
| 61 | + $output->writeln("<error>Instance is not using primary object store</error>"); |
|
| 62 | + return null; |
|
| 63 | + } |
|
| 64 | + if ($config['multibucket'] && !$bucket) { |
|
| 65 | + $output->writeln("<error>--bucket option required</error> because <info>multi bucket</info> is enabled."); |
|
| 66 | + return null; |
|
| 67 | + } |
|
| 68 | 68 | |
| 69 | - if (!isset($config['arguments'])) { |
|
| 70 | - throw new \Exception("no arguments configured for object store configuration"); |
|
| 71 | - } |
|
| 72 | - if (!isset($config['class'])) { |
|
| 73 | - throw new \Exception("no class configured for object store configuration"); |
|
| 74 | - } |
|
| 69 | + if (!isset($config['arguments'])) { |
|
| 70 | + throw new \Exception("no arguments configured for object store configuration"); |
|
| 71 | + } |
|
| 72 | + if (!isset($config['class'])) { |
|
| 73 | + throw new \Exception("no class configured for object store configuration"); |
|
| 74 | + } |
|
| 75 | 75 | |
| 76 | - if ($bucket) { |
|
| 77 | - // s3, swift |
|
| 78 | - $config['arguments']['bucket'] = $bucket; |
|
| 79 | - // azure |
|
| 80 | - $config['arguments']['container'] = $bucket; |
|
| 81 | - } |
|
| 76 | + if ($bucket) { |
|
| 77 | + // s3, swift |
|
| 78 | + $config['arguments']['bucket'] = $bucket; |
|
| 79 | + // azure |
|
| 80 | + $config['arguments']['container'] = $bucket; |
|
| 81 | + } |
|
| 82 | 82 | |
| 83 | - $store = new $config['class']($config['arguments']); |
|
| 84 | - if (!$store instanceof IObjectStore) { |
|
| 85 | - throw new \Exception("configured object store class is not an object store implementation"); |
|
| 86 | - } |
|
| 87 | - return $store; |
|
| 88 | - } |
|
| 83 | + $store = new $config['class']($config['arguments']); |
|
| 84 | + if (!$store instanceof IObjectStore) { |
|
| 85 | + throw new \Exception("configured object store class is not an object store implementation"); |
|
| 86 | + } |
|
| 87 | + return $store; |
|
| 88 | + } |
|
| 89 | 89 | |
| 90 | - /** |
|
| 91 | - * Check if an object is referenced in the database |
|
| 92 | - */ |
|
| 93 | - public function objectExistsInDb(string $object): int|false { |
|
| 94 | - if (str_starts_with($object, 'urn:oid:')) { |
|
| 95 | - $fileId = (int)substr($object, strlen('urn:oid:')); |
|
| 96 | - $query = $this->connection->getQueryBuilder(); |
|
| 97 | - $query->select('fileid') |
|
| 98 | - ->from('filecache') |
|
| 99 | - ->where($query->expr()->eq('fileid', $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT))); |
|
| 100 | - $result = $query->executeQuery(); |
|
| 101 | - if ($result->fetchOne() !== false) { |
|
| 102 | - return $fileId; |
|
| 103 | - } else { |
|
| 104 | - return false; |
|
| 105 | - } |
|
| 106 | - } else { |
|
| 107 | - return false; |
|
| 108 | - } |
|
| 109 | - } |
|
| 90 | + /** |
|
| 91 | + * Check if an object is referenced in the database |
|
| 92 | + */ |
|
| 93 | + public function objectExistsInDb(string $object): int|false { |
|
| 94 | + if (str_starts_with($object, 'urn:oid:')) { |
|
| 95 | + $fileId = (int)substr($object, strlen('urn:oid:')); |
|
| 96 | + $query = $this->connection->getQueryBuilder(); |
|
| 97 | + $query->select('fileid') |
|
| 98 | + ->from('filecache') |
|
| 99 | + ->where($query->expr()->eq('fileid', $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT))); |
|
| 100 | + $result = $query->executeQuery(); |
|
| 101 | + if ($result->fetchOne() !== false) { |
|
| 102 | + return $fileId; |
|
| 103 | + } else { |
|
| 104 | + return false; |
|
| 105 | + } |
|
| 106 | + } else { |
|
| 107 | + return false; |
|
| 108 | + } |
|
| 109 | + } |
|
| 110 | 110 | } |
@@ -31,50 +31,50 @@ |
||
| 31 | 31 | use Symfony\Component\Console\Output\OutputInterface; |
| 32 | 32 | |
| 33 | 33 | class Get extends Command { |
| 34 | - private ObjectUtil $objectUtils; |
|
| 34 | + private ObjectUtil $objectUtils; |
|
| 35 | 35 | |
| 36 | - public function __construct(ObjectUtil $objectUtils) { |
|
| 37 | - $this->objectUtils = $objectUtils; |
|
| 38 | - parent::__construct(); |
|
| 39 | - } |
|
| 36 | + public function __construct(ObjectUtil $objectUtils) { |
|
| 37 | + $this->objectUtils = $objectUtils; |
|
| 38 | + parent::__construct(); |
|
| 39 | + } |
|
| 40 | 40 | |
| 41 | - protected function configure(): void { |
|
| 42 | - $this |
|
| 43 | - ->setName('files:object:get') |
|
| 44 | - ->setDescription('Get the contents of an object') |
|
| 45 | - ->addArgument('object', InputArgument::REQUIRED, "Object to get") |
|
| 46 | - ->addArgument('output', InputArgument::REQUIRED, "Target local file to output to, use - for STDOUT") |
|
| 47 | - ->addOption('bucket', 'b', InputOption::VALUE_REQUIRED, "Bucket to get the object from, only required in cases where it can't be determined from the config"); |
|
| 48 | - } |
|
| 41 | + protected function configure(): void { |
|
| 42 | + $this |
|
| 43 | + ->setName('files:object:get') |
|
| 44 | + ->setDescription('Get the contents of an object') |
|
| 45 | + ->addArgument('object', InputArgument::REQUIRED, "Object to get") |
|
| 46 | + ->addArgument('output', InputArgument::REQUIRED, "Target local file to output to, use - for STDOUT") |
|
| 47 | + ->addOption('bucket', 'b', InputOption::VALUE_REQUIRED, "Bucket to get the object from, only required in cases where it can't be determined from the config"); |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - public function execute(InputInterface $input, OutputInterface $output): int { |
|
| 51 | - $object = $input->getArgument('object'); |
|
| 52 | - $outputName = $input->getArgument('output'); |
|
| 53 | - $objectStore = $this->objectUtils->getObjectStore($input->getOption("bucket"), $output); |
|
| 54 | - if (!$objectStore) { |
|
| 55 | - return 1; |
|
| 56 | - } |
|
| 50 | + public function execute(InputInterface $input, OutputInterface $output): int { |
|
| 51 | + $object = $input->getArgument('object'); |
|
| 52 | + $outputName = $input->getArgument('output'); |
|
| 53 | + $objectStore = $this->objectUtils->getObjectStore($input->getOption("bucket"), $output); |
|
| 54 | + if (!$objectStore) { |
|
| 55 | + return 1; |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | - if (!$objectStore->objectExists($object)) { |
|
| 59 | - $output->writeln("<error>Object $object does not exist</error>"); |
|
| 60 | - return 1; |
|
| 61 | - } else { |
|
| 62 | - try { |
|
| 63 | - $source = $objectStore->readObject($object); |
|
| 64 | - } catch (\Exception $e) { |
|
| 65 | - $msg = $e->getMessage(); |
|
| 66 | - $output->writeln("<error>Failed to read $object from object store: $msg</error>"); |
|
| 67 | - return 1; |
|
| 68 | - } |
|
| 69 | - $target = $outputName === '-' ? STDOUT : fopen($outputName, 'w'); |
|
| 70 | - if (!$target) { |
|
| 71 | - $output->writeln("<error>Failed to open $outputName for writing</error>"); |
|
| 72 | - return 1; |
|
| 73 | - } |
|
| 58 | + if (!$objectStore->objectExists($object)) { |
|
| 59 | + $output->writeln("<error>Object $object does not exist</error>"); |
|
| 60 | + return 1; |
|
| 61 | + } else { |
|
| 62 | + try { |
|
| 63 | + $source = $objectStore->readObject($object); |
|
| 64 | + } catch (\Exception $e) { |
|
| 65 | + $msg = $e->getMessage(); |
|
| 66 | + $output->writeln("<error>Failed to read $object from object store: $msg</error>"); |
|
| 67 | + return 1; |
|
| 68 | + } |
|
| 69 | + $target = $outputName === '-' ? STDOUT : fopen($outputName, 'w'); |
|
| 70 | + if (!$target) { |
|
| 71 | + $output->writeln("<error>Failed to open $outputName for writing</error>"); |
|
| 72 | + return 1; |
|
| 73 | + } |
|
| 74 | 74 | |
| 75 | - stream_copy_to_stream($source, $target); |
|
| 76 | - return 0; |
|
| 77 | - } |
|
| 78 | - } |
|
| 75 | + stream_copy_to_stream($source, $target); |
|
| 76 | + return 0; |
|
| 77 | + } |
|
| 78 | + } |
|
| 79 | 79 | |
| 80 | 80 | } |
@@ -34,45 +34,45 @@ |
||
| 34 | 34 | use Symfony\Component\Console\Question\ConfirmationQuestion; |
| 35 | 35 | |
| 36 | 36 | class Delete extends Command { |
| 37 | - private ObjectUtil $objectUtils; |
|
| 37 | + private ObjectUtil $objectUtils; |
|
| 38 | 38 | |
| 39 | - public function __construct(ObjectUtil $objectUtils) { |
|
| 40 | - $this->objectUtils = $objectUtils; |
|
| 41 | - parent::__construct(); |
|
| 42 | - } |
|
| 39 | + public function __construct(ObjectUtil $objectUtils) { |
|
| 40 | + $this->objectUtils = $objectUtils; |
|
| 41 | + parent::__construct(); |
|
| 42 | + } |
|
| 43 | 43 | |
| 44 | - protected function configure(): void { |
|
| 45 | - $this |
|
| 46 | - ->setName('files:object:delete') |
|
| 47 | - ->setDescription('Delete an object from the object store') |
|
| 48 | - ->addArgument('object', InputArgument::REQUIRED, "Object to delete") |
|
| 49 | - ->addOption('bucket', 'b', InputOption::VALUE_REQUIRED, "Bucket to delete the object from, only required in cases where it can't be determined from the config"); |
|
| 50 | - } |
|
| 44 | + protected function configure(): void { |
|
| 45 | + $this |
|
| 46 | + ->setName('files:object:delete') |
|
| 47 | + ->setDescription('Delete an object from the object store') |
|
| 48 | + ->addArgument('object', InputArgument::REQUIRED, "Object to delete") |
|
| 49 | + ->addOption('bucket', 'b', InputOption::VALUE_REQUIRED, "Bucket to delete the object from, only required in cases where it can't be determined from the config"); |
|
| 50 | + } |
|
| 51 | 51 | |
| 52 | - public function execute(InputInterface $input, OutputInterface $output): int { |
|
| 53 | - $object = $input->getArgument('object'); |
|
| 54 | - $objectStore = $this->objectUtils->getObjectStore($input->getOption("bucket"), $output); |
|
| 55 | - if (!$objectStore) { |
|
| 56 | - return -1; |
|
| 57 | - } |
|
| 52 | + public function execute(InputInterface $input, OutputInterface $output): int { |
|
| 53 | + $object = $input->getArgument('object'); |
|
| 54 | + $objectStore = $this->objectUtils->getObjectStore($input->getOption("bucket"), $output); |
|
| 55 | + if (!$objectStore) { |
|
| 56 | + return -1; |
|
| 57 | + } |
|
| 58 | 58 | |
| 59 | - if ($fileId = $this->objectUtils->objectExistsInDb($object)) { |
|
| 60 | - $output->writeln("<error>Warning, object $object belongs to an existing file, deleting the object will lead to unexpected behavior if not replaced</error>"); |
|
| 61 | - $output->writeln(" Note: use <info>occ files:delete $fileId</info> to delete the file cleanly or <info>occ info:file $fileId</info> for more information about the file"); |
|
| 62 | - $output->writeln(""); |
|
| 63 | - } |
|
| 59 | + if ($fileId = $this->objectUtils->objectExistsInDb($object)) { |
|
| 60 | + $output->writeln("<error>Warning, object $object belongs to an existing file, deleting the object will lead to unexpected behavior if not replaced</error>"); |
|
| 61 | + $output->writeln(" Note: use <info>occ files:delete $fileId</info> to delete the file cleanly or <info>occ info:file $fileId</info> for more information about the file"); |
|
| 62 | + $output->writeln(""); |
|
| 63 | + } |
|
| 64 | 64 | |
| 65 | - if (!$objectStore->objectExists($object)) { |
|
| 66 | - $output->writeln("<error>Object $object does not exist</error>"); |
|
| 67 | - return -1; |
|
| 68 | - } |
|
| 65 | + if (!$objectStore->objectExists($object)) { |
|
| 66 | + $output->writeln("<error>Object $object does not exist</error>"); |
|
| 67 | + return -1; |
|
| 68 | + } |
|
| 69 | 69 | |
| 70 | - /** @var QuestionHelper $helper */ |
|
| 71 | - $helper = $this->getHelper('question'); |
|
| 72 | - $question = new ConfirmationQuestion("Delete $object? [y/N] ", false); |
|
| 73 | - if ($helper->ask($input, $output, $question)) { |
|
| 74 | - $objectStore->deleteObject($object); |
|
| 75 | - } |
|
| 76 | - return 0; |
|
| 77 | - } |
|
| 70 | + /** @var QuestionHelper $helper */ |
|
| 71 | + $helper = $this->getHelper('question'); |
|
| 72 | + $question = new ConfirmationQuestion("Delete $object? [y/N] ", false); |
|
| 73 | + if ($helper->ask($input, $output, $question)) { |
|
| 74 | + $objectStore->deleteObject($object); |
|
| 75 | + } |
|
| 76 | + return 0; |
|
| 77 | + } |
|
| 78 | 78 | } |