Skip to content

PHPLIB-1248 Add examples on GridFS #1196

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 8, 2024
Merged

Conversation

GromNaN
Copy link
Member

@GromNaN GromNaN commented Nov 21, 2023

@GromNaN GromNaN marked this pull request as ready for review January 4, 2024 17:17
@GromNaN GromNaN requested a review from jmikola January 4, 2024 17:19
$size += strlen($data);
}

echo 'Read a total of ' . $size . ' bytes' . PHP_EOL;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can these be added to ExamplesTest? The output for both scripts looks deterministic enough to assert.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added the tests. But I had to update the upload example to use echo, the contents written in STDOUT isn't caught by ob_get_contents for the tests.

// Create an in-memory stream, this can be any stream source like STDIN or php://input for web requests
$stream = fopen('php://temp', 'w+');
fwrite($stream, 'Hello world!');
rewind($stream);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is rewind() necessary? Bucket::uploadFromStream() uses stream_copy_to_stream(), the docs for which are actually a bit ambiguous because the $offset parameter defaults to 0.

Makes a copy of up to length bytes of data from the current position (or from the offset position, if specified) in from to to. If length is null, all remaining content in from will be copied.

Looking at the implementation, it appears that $offset=0 actually has no effect. The offset is only used to seek the $from stream when the value is > 0. That suggests that we can't even explicitly pass 0 to copy from the beginning of $from.

To that end, I suppose this rewind() is necessary.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Streams are generally consumed from the current cursor location. I have to rewind to get the file contents from the beginning.

}

// Last data are flushed to the server when the stream is closed
fclose($stream);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you want to delete this stream at the end of the script as you do in gridfs-upload.php?

Copy link
Member Author

@GromNaN GromNaN Jan 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This gave me the opportunity to show the getFileIdForStream method.

@GromNaN GromNaN force-pushed the PHPLIB-1248 branch 3 times, most recently from 5ae1e00 to 6e73380 Compare January 5, 2024 10:12
@GromNaN
Copy link
Member Author

GromNaN commented Jan 5, 2024

I renamed the example files to use underscores as it was previously done for command_logger.php.

Copy link
Member

@jmikola jmikola left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One comment about line endings, but LGTM otherwise.

@GromNaN GromNaN requested a review from alcaeus January 8, 2024 08:24
require __DIR__ . '/../vendor/autoload.php';

$client = new Client(getenv('MONGODB_URI') ?: 'mongodb://127.0.0.1/');
// Disable MD5 computation for faster uploads, this feature is deprecated
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I explicitly disabled MD5, that's what developers should do in their applications.

@GromNaN GromNaN merged commit 4c6eb66 into mongodb:master Jan 8, 2024
@GromNaN GromNaN deleted the PHPLIB-1248 branch January 8, 2024 09:39
alcaeus added a commit to alcaeus/mongo-php-library that referenced this pull request Jan 15, 2024
* master:
  PHPLIB-1323 Implement `unlink` for GridFS stream wrapper (mongodb#1206)
  PHPLIB-1330: Sync tests for failCommand errorLabels reqs (mongodb#1214)
  PHPLIB-1246: Test PHP 8.3 on Evergreen (mongodb#1213)
  PHPLIB-1324 Implement `rename` for GridFS stream wrapper  (mongodb#1207)
  PHPLIB-1248 Add examples on GridFS (mongodb#1196)
  Deprecate setting GridFS disableMD5 to false explicitly (mongodb#1205)
  PHPLIB-1326: Use more permissive top-level runOnRequirements (mongodb#1210)
  PHPLIB-1206 Add bucket alises for context resolver using GridFS StreamWrapper (mongodb#1138)
  Bump actions/upload-artifact from 3 to 4 (mongodb#1208)
  PHPLIB-1275: Replace apiargs usage in docs with extracts (mongodb#1203)
  Fix title formatting in Client::removeSubscriber() docs (mongodb#1204)
  PHPLIB-1304: Pull mongohouse image from ECR repo (mongodb#1202)
  Fix evergreen failures (mongodb#1200)
  Enable workflows to run for GitHub Merge Queue (mongodb#1199)
  PHPLIB-1313 Ensure the GridFS stream is saved when the script ends (mongodb#1197)
  PHPLIB-1309 Add addSubscriber/removeSubscriber to Client class to ease configuration (mongodb#1195)
  Master is now 1.18-dev
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants