| 
 | 1 | +<?php  | 
 | 2 | + | 
 | 3 | +namespace MongoDB\GridFS;  | 
 | 4 | + | 
 | 5 | +use MongoDB\Driver\Server;  | 
 | 6 | +use MongoDB\Exception\InvalidArgumentException;  | 
 | 7 | +use MongoDB\Exception\InvalidArgumentTypeException;  | 
 | 8 | +use MongoDB\Exception\RuntimeException;  | 
 | 9 | +use MongoDB\Exception\UnexpectedValueException;  | 
 | 10 | + | 
 | 11 | +/**  | 
 | 12 | + * Stream wrapper for reading and writing a GridFS file.  | 
 | 13 | + *  | 
 | 14 | + * @internal  | 
 | 15 | + * @see MongoDB\GridFS\Bucket::openUploadStream()  | 
 | 16 | + */  | 
 | 17 | +class StreamWrapper  | 
 | 18 | +{  | 
 | 19 | +    public $context;  | 
 | 20 | + | 
 | 21 | +    private $bucket;  | 
 | 22 | +    private $filename;  | 
 | 23 | +    private $options;  | 
 | 24 | + | 
 | 25 | +    private $protocol = 'gridfs';  | 
 | 26 | + | 
 | 27 | +    private $at;  | 
 | 28 | +    private $hashContext;  | 
 | 29 | + | 
 | 30 | +    /**  | 
 | 31 | +     * Constructs a writable upload stream.  | 
 | 32 | +     *  | 
 | 33 | +     * Supported options:  | 
 | 34 | +     *  | 
 | 35 | +     *  * chunkSizeBytes (integer): The number of bytes per chunk of this file.  | 
 | 36 | +     *    Defaults to the chunkSizeBytes of the Bucket.  | 
 | 37 | +     *  | 
 | 38 | +     *  * metadata (document): User data for the "metadata" field of the files  | 
 | 39 | +     *    collection document.  | 
 | 40 | +     *  | 
 | 41 | +     * The following options are deprecated:  | 
 | 42 | +     *  | 
 | 43 | +     *  * aliases (string[]): An array of aliases (i.e. filenames). Applications  | 
 | 44 | +     *    wishing to store aliases should add an aliases field to the metadata  | 
 | 45 | +     *    document instead.  | 
 | 46 | +     *  | 
 | 47 | +     *  * contentType (string): A valid MIME type. Applications wishing to store  | 
 | 48 | +     *    a contentType should add a contentType field to the metadata document  | 
 | 49 | +     *    instead.  | 
 | 50 | +     *  | 
 | 51 | +     * @param Bucket $bucket   Database name  | 
 | 52 | +     * @param string $filename Filename  | 
 | 53 | +     * @param array  $options  Upload options  | 
 | 54 | +     * @throws InvalidArgumentException  | 
 | 55 | +     */  | 
 | 56 | +    public function __construct(Bucket $bucket, $filename, array $options = [])  | 
 | 57 | +    {  | 
 | 58 | +        $options += [  | 
 | 59 | +            'chunkSizeBytes' => $bucket->getChunkSizeBytes(),  | 
 | 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['metadata']) && ! is_array($options['metadata']) && ! is_object($options['metadata'])) {  | 
 | 67 | +            throw new InvalidArgumentTypeException('"metadata" option', $options['metadata'], 'array or object');  | 
 | 68 | +        }  | 
 | 69 | + | 
 | 70 | +        if (isset($options['aliases'])) {  | 
 | 71 | +            if ( ! is_array($options['aliases'])) {  | 
 | 72 | +                throw new InvalidArgumentTypeException('"aliases" option', $options['aliases'], 'array or object');  | 
 | 73 | +            }  | 
 | 74 | + | 
 | 75 | +            $expectedIndex = 0;  | 
 | 76 | + | 
 | 77 | +            foreach ($options['aliases'] as $i => $alias) {  | 
 | 78 | +                if ($i !== $expectedIndex) {  | 
 | 79 | +                    throw new InvalidArgumentException(sprintf('"aliases" option is not a list (unexpected index: "%s")', $i));  | 
 | 80 | +                }  | 
 | 81 | + | 
 | 82 | +                if ( ! is_string($alias)) {  | 
 | 83 | +                    throw new InvalidArgumentTypeException(sprintf('$options["aliases"][%d]', $i), $alias, 'string');  | 
 | 84 | +                }  | 
 | 85 | + | 
 | 86 | +                $expectedIndex += 1;  | 
 | 87 | +            }  | 
 | 88 | +        }  | 
 | 89 | + | 
 | 90 | +        if (isset($options['contentType']) && ! is_string($options['contentType'])) {  | 
 | 91 | +            throw new InvalidArgumentTypeException('"contentType" option', $options['contentType'], 'string');  | 
 | 92 | +        }  | 
 | 93 | + | 
 | 94 | +        $this->bucket = $bucket;  | 
 | 95 | +        $this->filename = (string) $filename;  | 
 | 96 | +        $this->options = $options;  | 
 | 97 | +        $this->hashContext = hash_init('md5');  | 
 | 98 | +    }  | 
 | 99 | + | 
 | 100 | +    public function stream_write($data)  | 
 | 101 | +    {  | 
 | 102 | +        hash_update($this->hashContext, $data);  | 
 | 103 | + | 
 | 104 | +        //fopen('php://memory', )  | 
 | 105 | +    }  | 
 | 106 | + | 
 | 107 | +    /**  | 
 | 108 | +     * Register the GridFS stream wrapper.  | 
 | 109 | +     *  | 
 | 110 | +     * @param Manager $manager  Manager instance from the driver  | 
 | 111 | +     * @param string  $protocol Protocol to register  | 
 | 112 | +     */  | 
 | 113 | +    public static function register(Manager $manager, $protocol = 'gridfs')  | 
 | 114 | +    {  | 
 | 115 | +        if (in_array($protocol, stream_get_wrappers())) {  | 
 | 116 | +            stream_wrapper_unregister($protocol);  | 
 | 117 | +        }  | 
 | 118 | + | 
 | 119 | +        // Set the client passed in as the default stream context client  | 
 | 120 | +        stream_wrapper_register($protocol, get_called_class(), STREAM_IS_URL);  | 
 | 121 | +        $default = stream_context_get_options(stream_context_get_default());  | 
 | 122 | +        $default[$protocol]['manager'] = $manager;  | 
 | 123 | +        stream_context_set_default($default);  | 
 | 124 | +    }  | 
 | 125 | + | 
 | 126 | +    public function stream_open($path, $mode, $options, &$openedPath)  | 
 | 127 | +    {  | 
 | 128 | +        $this->initProtocol($path);  | 
 | 129 | +        $this->params = $this->getDatabase($path);  | 
 | 130 | +        $this->mode = rtrim($mode, 'bt');  | 
 | 131 | + | 
 | 132 | +        if ($errors = $this->validate($path, $this->mode)) {  | 
 | 133 | +            return $this->triggerError($errors);  | 
 | 134 | +        }  | 
 | 135 | + | 
 | 136 | +        return $this->boolCall(function() use ($path) {  | 
 | 137 | +            switch ($this->mode) {  | 
 | 138 | +                case 'r': return $this->openReadStream($path);  | 
 | 139 | +                case 'a': return $this->openAppendStream($path);  | 
 | 140 | +                default: return $this->openWriteStream($path);  | 
 | 141 | +            }  | 
 | 142 | +        });  | 
 | 143 | +    }  | 
 | 144 | + | 
 | 145 | +    private function validate($path, $mode)  | 
 | 146 | +    {  | 
 | 147 | +        $errors = [];  | 
 | 148 | + | 
 | 149 | +        if (!in_array($mode, ['r', 'w', 'a', 'x'])) {  | 
 | 150 | +            $errors[] = "Mode not supported: {$mode}. "  | 
 | 151 | +                . "Use one 'r', 'w', 'a', or 'x'.";  | 
 | 152 | +        }  | 
 | 153 | + | 
 | 154 | +        return $errors;  | 
 | 155 | +    }  | 
 | 156 | + | 
 | 157 | +    /**  | 
 | 158 | +     * Trigger one or more errors  | 
 | 159 | +     *  | 
 | 160 | +     * @param string|array $errors Errors to trigger  | 
 | 161 | +     * @param mixed        $flags  If set to STREAM_URL_STAT_QUIET, then no  | 
 | 162 | +     *                             error or exception occurs  | 
 | 163 | +     *  | 
 | 164 | +     * @return bool Returns false  | 
 | 165 | +     * @throws \RuntimeException if throw_errors is true  | 
 | 166 | +     */  | 
 | 167 | +    private function triggerError($errors, $flags = null)  | 
 | 168 | +    {  | 
 | 169 | +        // This is triggered with things like file_exists()  | 
 | 170 | +        if ($flags & STREAM_URL_STAT_QUIET) {  | 
 | 171 | +            return $flags & STREAM_URL_STAT_LINK  | 
 | 172 | +                // This is triggered for things like is_link()  | 
 | 173 | +                ? $this->formatUrlStat(false)  | 
 | 174 | +                : false;  | 
 | 175 | +        }  | 
 | 176 | +        // This is triggered when doing things like lstat() or stat()  | 
 | 177 | +        trigger_error(implode("\n", (array) $errors), E_USER_WARNING);  | 
 | 178 | +        return false;  | 
 | 179 | +    }  | 
 | 180 | +}  | 
0 commit comments