Skip to content
This repository has been archived by the owner on Sep 21, 2022. It is now read-only.

feat: update gridfs to work mongodb driver v4 #150

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 0 additions & 13 deletions packages/file-collections-sa-gridfs/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions packages/file-collections-sa-gridfs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@
"dependencies": {
"@babel/runtime-corejs2": "^7.14.8",
"@reactioncommerce/file-collections-sa-base": "^0.2.2",
"debug": "^4.3.2",
"gridfs-stream": "^1.1.1"
"debug": "^4.3.2"
},
"devDependencies": {
"@babel/cli": "^7.10.5",
Expand Down
25 changes: 10 additions & 15 deletions packages/file-collections-sa-gridfs/src/GridFSStore.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import Grid from "gridfs-stream";
import StorageAdapter from "@reactioncommerce/file-collections-sa-base";
import debug from "./debug";

Expand All @@ -25,7 +24,7 @@ export default class GridFSStore extends StorageAdapter {

this.chunkSize = chunkSize;
this.collectionName = `${collectionPrefix}${name}`.trim();
this.grid = Grid(db, mongodb);
this.grid = new mongodb.GridFSBucket(db);
this.mongodb = mongodb;
}

Expand All @@ -42,35 +41,31 @@ export default class GridFSStore extends StorageAdapter {
}

_getReadStream(fileKey, { start: startPos, end: endPos } = {}) {
const opts = { _id: fileKey._id, root: this.collectionName };
const opts = {};

// Add range if this should be a partial read
if (typeof startPos === "number" && typeof endPos === "number") {
opts.range = { startPos, endPos };
opts.start = startPos;
opts.end = endPos;
}

debug("GridFSStore _getReadStream opts:", opts);

return this.grid.createReadStream(opts);
const _id = new this.mongodb.ObjectId(fileKey._id);
return this.grid.openDownloadStream(_id, opts);
}

_getWriteStream(fileKey, options = {}) {
const opts = {
chunk_size: this.chunkSize, // eslint-disable-line camelcase
content_type: "application/octet-stream", // eslint-disable-line camelcase
filename: fileKey.filename,
mode: "w", // overwrite any existing data
root: this.collectionName,
chunkSizeBytes: this.chunkSize,
contentType: "application/octet-stream",
...options
};

if (fileKey._id) opts._id = fileKey._id;

debug("GridFSStore _getWriteStream opts:", opts);

const writeStream = this.grid.createWriteStream(opts);
const writeStream = this.grid.openUploadStream(fileKey.filename, opts);

writeStream.on("close", (file) => {
writeStream.on("finish", (file) => {
if (!file) {
// gridfs-stream will emit "close" without passing a file
// if there is an error. We can simply exit here because
Expand Down