|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace MongoDB\GridFS; |
| 4 | + |
| 5 | +use MongoDB\Collection; |
| 6 | +use MongoDB\Database; |
| 7 | +use MongoDB\BSON\ObjectId; |
| 8 | +use MongoDB\Driver\ReadPreference; |
| 9 | +use MongoDB\Driver\WriteConcern; |
| 10 | +use MongoDB\Exception\InvalidArgumentException; |
| 11 | +use MongoDB\Exception\InvalidArgumentTypeException; |
| 12 | +use MongoDB\Exception\RuntimeException; |
| 13 | +use MongoDB\Exception\UnexpectedValueException; |
| 14 | + |
| 15 | +/** |
| 16 | + * Bucket abstracts the GridFS files and chunks collections. |
| 17 | + * |
| 18 | + * @api |
| 19 | + */ |
| 20 | +class Bucket |
| 21 | +{ |
| 22 | + private $manager; |
| 23 | + private $databaseName; |
| 24 | + private $options; |
| 25 | + |
| 26 | + private $filesCollection; |
| 27 | + private $chunksCollection; |
| 28 | + |
| 29 | + private $ensuredIndexes = false; |
| 30 | + |
| 31 | + /** |
| 32 | + * Constructs a GridFS bucket. |
| 33 | + * |
| 34 | + * Supported options: |
| 35 | + * |
| 36 | + * * bucketName (string): The bucket name, which will be used as a prefix |
| 37 | + * for the files and chunks collections. Defaults to "fs". |
| 38 | + * |
| 39 | + * * chunkSizeBytes (integer): The chunk size in bytes. Defaults to |
| 40 | + * 261120 (i.e. 255 KiB). |
| 41 | + * |
| 42 | + * * readPreference (MongoDB\Driver\ReadPreference): Read preference. |
| 43 | + * |
| 44 | + * * writeConcern (MongoDB\Driver\WriteConcern): Write concern. |
| 45 | + * |
| 46 | + * @param Manager $manager Manager instance from the driver |
| 47 | + * @param string $databaseName Database name |
| 48 | + * @param array $options Bucket options |
| 49 | + * @throws InvalidArgumentException |
| 50 | + */ |
| 51 | + public function __construct(Manager $manager, $databaseName, array $options = []) |
| 52 | + { |
| 53 | + $options += [ |
| 54 | + 'bucketName' => 'fs', |
| 55 | + 'chunkSizeBytes' => 261120, |
| 56 | + ]; |
| 57 | + |
| 58 | + if (isset($options['bucketName']) && ! is_string($options['bucketName'])) { |
| 59 | + throw new InvalidArgumentTypeException('"bucketName" option', $options['bucketName'], 'string'); |
| 60 | + } |
| 61 | + |
| 62 | + if (isset($options['chunkSizeBytes']) && ! is_integer($options['chunkSizeBytes'])) { |
| 63 | + throw new InvalidArgumentTypeException('"chunkSizeBytes" option', $options['chunkSizeBytes'], 'integer'); |
| 64 | + } |
| 65 | + |
| 66 | + if (isset($options['readPreference']) && ! $options['readPreference'] instanceof ReadPreference) { |
| 67 | + throw new InvalidArgumentTypeException('"readPreference" option', $options['readPreference'], 'MongoDB\Driver\ReadPreference'); |
| 68 | + } |
| 69 | + |
| 70 | + if (isset($options['writeConcern']) && ! $options['writeConcern'] instanceof WriteConcern) { |
| 71 | + throw new InvalidArgumentTypeException('"writeConcern" option', $options['writeConcern'], 'MongoDB\Driver\WriteConcern'); |
| 72 | + } |
| 73 | + |
| 74 | + $this->manager = $manager; |
| 75 | + $this->databaseName = (string) $databaseName; |
| 76 | + $this->options = $options; |
| 77 | + |
| 78 | + $this->filesCollection = new Collection( |
| 79 | + $manager, |
| 80 | + sprintf('%s.%s.files', $this->databaseName, $options['bucketName']), |
| 81 | + isset($options['writeConcern']) ? $options['writeConcern'] : null, |
| 82 | + isset($options['readPreference']) ? $options['readPreference'] : null, |
| 83 | + ); |
| 84 | + |
| 85 | + $this->chunksCollection = new Collection( |
| 86 | + $manager, |
| 87 | + sprintf('%s.%s.chunks', $this->databaseName, $options['bucketName']), |
| 88 | + isset($options['writeConcern']) ? $options['writeConcern'] : null, |
| 89 | + isset($options['readPreference']) ? $options['readPreference'] : null, |
| 90 | + ); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Opens a Stream for reading the contents of a file specified by ID. |
| 95 | + * |
| 96 | + * @param ObjectId $id |
| 97 | + * @return Stream |
| 98 | + */ |
| 99 | + public function openDownloadStream(ObjectId $id) |
| 100 | + { |
| 101 | + |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Return the chunkSizeBytes option for this Bucket. |
| 106 | + * |
| 107 | + * @return integer |
| 108 | + */ |
| 109 | + public function getChunkSizeBytes() |
| 110 | + { |
| 111 | + return $this->options['chunkSizeBytes']; |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Opens a Stream for writing the contents of a file. |
| 116 | + * |
| 117 | + * @param ObjectId $id |
| 118 | + * @return Stream |
| 119 | + */ |
| 120 | + public function openUploadStream($filename, array $options = []) |
| 121 | + { |
| 122 | + |
| 123 | + } |
| 124 | + |
| 125 | + private function ensureIndexes() |
| 126 | + { |
| 127 | + // Indexes should only be ensured once before the first write operation |
| 128 | + if ($this->ensuredIndexes) { |
| 129 | + return; |
| 130 | + } |
| 131 | + |
| 132 | + if ( ! $this->isFilesCollectionEmpty()) { |
| 133 | + return; |
| 134 | + } |
| 135 | + |
| 136 | + $this->ensureFilesIndex(); |
| 137 | + $this->ensureChunksIndex(); |
| 138 | + |
| 139 | + $this->ensuredIndexes = true; |
| 140 | + } |
| 141 | + |
| 142 | + private function ensureChunksIndex() |
| 143 | + { |
| 144 | + foreach ($this->chunksCollection->listIndexes() as $index) { |
| 145 | + if ($index->isUnique() && $index->getKey() === ['files_id' => 1, 'n' => 1]) { |
| 146 | + return; |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + $this->chunksCollection->createIndex(['files_id' => 1, 'n' => 1], ['unique' => true]); |
| 151 | + } |
| 152 | + |
| 153 | + private function ensureFilesIndex() |
| 154 | + { |
| 155 | + foreach ($this->filesCollection->listIndexes() as $index) { |
| 156 | + if ($index->getKey() === ['filename' => 1, 'uploadDate' => 1]) { |
| 157 | + return; |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + $this->filesCollection->createIndex(['filename' => 1, 'uploadDate' => 1]); |
| 162 | + } |
| 163 | + |
| 164 | + private function isFilesCollectionEmpty() |
| 165 | + { |
| 166 | + return null === $this->filesCollection->findOne([], [ |
| 167 | + 'readPreference' => new ReadPreference(ReadPreference::RP_PRIMARY), |
| 168 | + 'projection' => ['_id' => 1], |
| 169 | + ]); |
| 170 | + } |
| 171 | +} |
0 commit comments