Skip to content
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

Fixes for gridfs #5892

Merged
merged 5 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from 4 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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## Version xx.xx.xx

Fixes:
- [gridfs] fixes for moving to Promises

Dependencies:
- Bump sass from 1.81.0 to 1.83.1
- Bump countly-sdk-nodejs from 24.10.0 to 24.10.1
Expand Down
113 changes: 71 additions & 42 deletions api/utils/countlyFs.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ countlyFs.gridfs = {};
**/
function beforeSave(category, filename, options, callback, done) {
log.d("checking file", filename);
ob.getId(category, filename, function(err, res) {
ob.getId(category, filename, async function(err, res) {
log.d("file state", filename, err, res);
if (options.forceClean) {
ob.clearFile(category, filename, done);
Expand All @@ -80,15 +80,20 @@ countlyFs.gridfs = {};
else if (options.writeMode === "overwrite") {
var bucket = new GridFSBucket(db, { bucketName: category });
log.d("deleting file", filename);
bucket.delete(res, function(error) {
log.d("deleted", filename, error);
if (!error) {
setTimeout(done, 1);
}
else if (callback) {
callback(error);
}
});
let errHandle = null;
try {
await bucket.delete(res);
}
catch (error) {
errHandle = error;
}
log.d("deleted", filename, errHandle);
if (!errHandle) {
setTimeout(done, 1);
}
else if (callback) {
callback(errHandle);
}
}
else {
if (callback) {
Expand Down Expand Up @@ -116,6 +121,7 @@ countlyFs.gridfs = {};
* });
*/
ob.getId = function(category, filename, callback) {
log.d("getId", category, filename);
db.collection(category + ".files").findOne({ filename: filename }, {_id: 1}, function(err, res) {
if (callback) {
callback(err, (res && res._id) ? res._id : false);
Expand Down Expand Up @@ -144,6 +150,7 @@ countlyFs.gridfs = {};
if (!options) {
options = {};
}
log.d("exists", category, dest, options);
var query = {};
if (options.id) {
query._id = options.id;
Expand Down Expand Up @@ -184,7 +191,7 @@ countlyFs.gridfs = {};
if (!options) {
options = {};
}

log.d("saveFile", category, dest, source, options);
var filename = dest.split(path.sep).pop();
beforeSave(category, filename, options, callback, function() {
save(category, filename, fs.createReadStream(source), options, callback);
Expand Down Expand Up @@ -218,6 +225,7 @@ countlyFs.gridfs = {};
if (!options) {
options = {};
}
log.d("saveData", category, dest, typeof data, options);
beforeSave(category, filename, options, callback, function() {
var readStream = new Readable;
readStream.push(data);
Expand Down Expand Up @@ -253,6 +261,7 @@ countlyFs.gridfs = {};
if (!options) {
options = {};
}
log.d("saveStream", category, dest, typeof readStream, options);
beforeSave(category, filename, options, callback, function() {
save(category, filename, readStream, options, callback);
});
Expand All @@ -271,7 +280,7 @@ countlyFs.gridfs = {};
* console.log("Finished", err);
* });
*/
ob.rename = function(category, dest, source, options, callback) {
ob.rename = async function(category, dest, source, options, callback) {
var newname = dest.split(path.sep).pop();
var oldname = source.split(path.sep).pop();
if (typeof options === "function") {
Expand All @@ -281,25 +290,35 @@ countlyFs.gridfs = {};
if (!options) {
options = {};
}

log.d("rename", category, dest, source, options);
if (options.id) {
let bucket = new GridFSBucket(db, { bucketName: category });
bucket.rename(options.id, newname, function(error) {
if (callback) {
callback(error);
}
});
let errHandle = null;
try {
await bucket.rename(options.id, newname);
}
catch (error) {
errHandle = error;
}
if (callback) {
callback(errHandle);
}
}
else {
db.collection(category + ".files").findOne({ filename: oldname }, {_id: 1}, function(err, res) {
db.collection(category + ".files").findOne({ filename: oldname }, {_id: 1}, async function(err, res) {
if (!err) {
if (res && res._id) {
let bucket = new GridFSBucket(db, { bucketName: category });
bucket.rename(res._id, newname, function(error) {
if (callback) {
callback(error);
}
});
let errHandle = null;
try {
await bucket.rename(res._id, newname);
}
catch (error) {
errHandle = error;
}
if (callback) {
callback(errHandle);
}
}
else {
if (callback) {
Expand Down Expand Up @@ -337,7 +356,7 @@ countlyFs.gridfs = {};
if (!options) {
options = {};
}

log.d("deleteFile", category, dest, options);
if (options.id) {
ob.deleteFileById(category, options.id, callback);
}
Expand Down Expand Up @@ -372,13 +391,19 @@ countlyFs.gridfs = {};
* console.log("Finished", err);
* });
*/
ob.deleteAll = function(category, dest, callback) {
ob.deleteAll = async function(category, dest, callback) {
log.d("deleteAll", category, dest);
var bucket = new GridFSBucket(db, { bucketName: category });
bucket.drop(function(error) {
if (callback) {
callback(error);
}
});
let errHandle = null;
try {
await bucket.drop();
}
catch (error) {
errHandle = error;
}
if (callback) {
callback(errHandle);
}
};

/**
Expand All @@ -403,7 +428,7 @@ countlyFs.gridfs = {};
if (!options) {
options = {};
}

log.d("getStream", category, dest, options);
if (callback) {
if (options.id) {
ob.getStreamById(category, options.id, callback);
Expand Down Expand Up @@ -436,7 +461,7 @@ countlyFs.gridfs = {};
if (!options) {
options = {};
}

log.d("getData", category, dest, options);
if (options.id) {
ob.getDataById(category, options.id, callback);
}
Expand Down Expand Up @@ -482,7 +507,7 @@ countlyFs.gridfs = {};
if (!options) {
options = {};
}

log.d("getSize", category, dest, options);
var query = {};
if (options.id) {
query._id = options.id;
Expand Down Expand Up @@ -517,7 +542,7 @@ countlyFs.gridfs = {};
if (!options) {
options = {};
}

log.d("getStats", category, dest, options);
var query = {};
if (options.id) {
query._id = options.id;
Expand Down Expand Up @@ -554,6 +579,7 @@ countlyFs.gridfs = {};
* });
*/
ob.getDataById = function(category, id, callback) {
log.d("getDataById", category, id);
var bucket = new GridFSBucket(db, { bucketName: category });
var downloadStream = bucket.openDownloadStream(id);
downloadStream.on('error', function(error) {
Expand Down Expand Up @@ -585,6 +611,7 @@ countlyFs.gridfs = {};
* });
*/
ob.getStreamById = function(category, id, callback) {
log.d("getStreamById", category, id);
if (callback) {
var bucket = new GridFSBucket(db, { bucketName: category });
callback(null, bucket.openDownloadStream(id));
Expand All @@ -602,17 +629,17 @@ countlyFs.gridfs = {};
* });
*/
ob.deleteFileById = async function(category, id, callback) {
log.d("deleteFileById", category, id);
var bucket = new GridFSBucket(db, { bucketName: category });
let errHandle = null;
try {
await bucket.delete(id);
if (callback) {
callback(null);
}
}
catch (ee) {
if (callback) {
callback(ee);
}
catch (error) {
errHandle = error;
}
if (callback) {
callback(errHandle);
}
};

Expand All @@ -627,6 +654,7 @@ countlyFs.gridfs = {};
* });
*/
ob.clearFile = function(category, filename, callback) {
log.d("clearFile", category, filename);
db.collection(category + ".files").deleteMany({ filename: filename }, function(err1, res1) {
log.d("deleting files", category, { filename: filename }, err1, res1 && res1.result);
db.collection(category + ".chunks").deleteMany({ files_id: filename }, function(err2, res2) {
Expand All @@ -643,6 +671,7 @@ countlyFs.gridfs = {};
* @param {function} callback - function called when files found or query errored, providing error object as first param and a list of filename, creation date and size as secondas second
*/
ob.listFiles = function(category, callback) {
log.d("listFiles", category);
const bucket = new GridFSBucket(db, { bucketName: category });
bucket.find().toArray()
.then((records) => callback(
Expand Down
2 changes: 1 addition & 1 deletion plugins/pluginManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -2226,7 +2226,7 @@ var pluginManager = function pluginManager() {
}
});

var findOptions = ["limit", "sort", "projection", "skip", "hint", "explain", "snapshot", "timeout", "tailable", "batchSize", "returnKey", "maxScan", "min", "max", "showDiskLoc", "comment", "raw", "promoteLongs", "promoteValues", "promoteBuffers", "readPreference", "partial", "maxTimeMS", "collation", "session", "omitReadPreference"];
var findOptions = ["limit", "sort", "projection", "skip", "hint", "explain", "snapshot", "timeout", "tailable", "batchSize", "returnKey", "maxScan", "min", "max", "showDiskLoc", "comment", "raw", "promoteLongs", "promoteValues", "promoteBuffers", "readPreference", "partial", "maxTimeMS", "collation", "session", "omitReadPreference", "timeoutMode", "timeoutMS"];

countlyDb._collection_cache = {};
//overwrite some methods
Expand Down
Loading