storage: make exists async

This commit is contained in:
Girish Ramakrishnan
2022-04-14 08:07:03 -05:00
parent 11f7be2065
commit d54c03f0a0
6 changed files with 28 additions and 39 deletions

View File

@@ -102,21 +102,19 @@ function upload(apiConfig, backupFilePath, sourceStream, callback) {
sourceStream.pipe(uploadStream);
}
function exists(apiConfig, backupFilePath, callback) {
async function exists(apiConfig, backupFilePath) {
assert.strictEqual(typeof apiConfig, 'object');
assert.strictEqual(typeof backupFilePath, 'string');
assert.strictEqual(typeof callback, 'function');
const bucket = getBucket(apiConfig);
if (!backupFilePath.endsWith('/')) {
const file = bucket.file(backupFilePath);
file.getMetadata(function (error) {
if (error && error.code === 404) return callback(null, false);
if (error) return callback(new BoxError(BoxError.EXTERNAL_ERROR, error.message));
const [error] = await safe(file.getMetadata());
if (error && error.code === 404) return false;
if (error) throw new BoxError(BoxError.EXTERNAL_ERROR, error.message);
callback(null, true);
});
return true;
} else {
const query = {
prefix: backupFilePath,
@@ -124,11 +122,10 @@ function exists(apiConfig, backupFilePath, callback) {
autoPaginate: true
};
bucket.getFiles(query, function (error, files) {
if (error) return callback(error);
const [error, files] = await safe(bucket.getFiles(query));
if (error) throw new BoxError(BoxError.EXTERNAL_ERROR, error.message);
callback(null, files.length !== 0);
});
return files.length !== 0;
}
}