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 2 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
89 changes: 54 additions & 35 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 @@ -271,7 +276,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 @@ -284,22 +289,32 @@ countlyFs.gridfs = {};

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 @@ -372,13 +387,18 @@ countlyFs.gridfs = {};
* console.log("Finished", err);
* });
*/
ob.deleteAll = function(category, dest, callback) {
ob.deleteAll = async function(category, dest, callback) {
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 Down Expand Up @@ -603,16 +623,15 @@ countlyFs.gridfs = {};
*/
ob.deleteFileById = async function(category, id, callback) {
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 Down
Loading