eof() ) { $buf = $stream->read( self::COPY_BUFFER_SIZE ); if ( fwrite( $file, $buf ) !== strlen( $buf ) ) { throw new ShellboxError( "Error copying stream to file " . basename( $path ) ); } } if ( !fclose( $file ) ) { throw new ShellboxError( "Error copying stream to file " . basename( $path ) ); } } /** * Make a directory with group/other permission bits masked out * * @param string $path * @throws ShellboxError */ public static function mkdir( $path ) { if ( !mkdir( $path, 0700 ) ) { throw new ShellboxError( "Error creating directory " . basename( $path ) ); } } /** * Get content hash headers for MediaWiki from a stream * * @param StreamInterface $stream * @return array * @throws ShellboxError */ public static function getMwHashes( StreamInterface $stream ) { $srcSize = $stream->getSize(); $md5Context = hash_init( 'md5' ); $sha1Context = hash_init( 'sha1' ); $hashDigestSize = 0; while ( !$stream->eof() ) { $buffer = $stream->read( 131_072 ); // 128 KiB hash_update( $md5Context, $buffer ); hash_update( $sha1Context, $buffer ); $hashDigestSize += strlen( $buffer ); } if ( $hashDigestSize !== $srcSize ) { throw new ShellboxError( "Stream truncated while hashing" ); } return [ 'etag' => hash_final( $md5Context ), 'x-object-meta-sha1base36' => \Wikimedia\base_convert( hash_final( $sha1Context ), 16, 36, 31 ) ]; } }