diff --git a/packages/file-collections-sa-gridfs/package-lock.json b/packages/file-collections-sa-gridfs/package-lock.json index 9be0f0e..47bc81a 100644 --- a/packages/file-collections-sa-gridfs/package-lock.json +++ b/packages/file-collections-sa-gridfs/package-lock.json @@ -2348,11 +2348,6 @@ "path-exists": "^4.0.0" } }, - "flushwritable": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/flushwritable/-/flushwritable-1.0.0.tgz", - "integrity": "sha1-PjKNj95BKtR+c44751C00pAENJg=" - }, "for-in": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/for-in/-/for-in-1.0.2.tgz", @@ -2473,14 +2468,6 @@ "integrity": "sha512-WjKPNJF79dtJAVniUlGGWHYGz2jWxT6VhN/4m1NdkbZ2nOsEF+cI1Edgql5zCRhs/VsQYRvrXctxktVXZUkixw==", "dev": true }, - "gridfs-stream": { - "version": "1.1.1", - "resolved": "https://registry.npmjs.org/gridfs-stream/-/gridfs-stream-1.1.1.tgz", - "integrity": "sha1-PdOhAOwgIaGBKC9utGcJY2B034k=", - "requires": { - "flushwritable": "^1.0.0" - } - }, "has-flag": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-3.0.0.tgz", diff --git a/packages/file-collections-sa-gridfs/package.json b/packages/file-collections-sa-gridfs/package.json index 8955133..4d37dce 100644 --- a/packages/file-collections-sa-gridfs/package.json +++ b/packages/file-collections-sa-gridfs/package.json @@ -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", diff --git a/packages/file-collections-sa-gridfs/src/GridFSStore.js b/packages/file-collections-sa-gridfs/src/GridFSStore.js index f10f3df..a027aad 100644 --- a/packages/file-collections-sa-gridfs/src/GridFSStore.js +++ b/packages/file-collections-sa-gridfs/src/GridFSStore.js @@ -1,4 +1,3 @@ -import Grid from "gridfs-stream"; import StorageAdapter from "@reactioncommerce/file-collections-sa-base"; import debug from "./debug"; @@ -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; } @@ -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