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
+4 -5
View File
@@ -159,18 +159,17 @@ function download(apiConfig, sourceFilePath, callback) {
callback(null, fileStream);
}
function exists(apiConfig, sourceFilePath, callback) {
async function exists(apiConfig, sourceFilePath) {
assert.strictEqual(typeof apiConfig, 'object');
assert.strictEqual(typeof sourceFilePath, 'string');
assert.strictEqual(typeof callback, 'function');
// do not use existsSync because it does not return EPERM etc
if (!safe.fs.statSync(sourceFilePath)) {
if (safe.error && safe.error.code === 'ENOENT') return callback(null, false);
if (safe.error) return callback(new BoxError(BoxError.EXTERNAL_ERROR, `Exists ${sourceFilePath}: ${safe.error.message}`));
if (safe.error && safe.error.code === 'ENOENT') return false;
if (safe.error) throw new BoxError(BoxError.EXTERNAL_ERROR, `Exists ${sourceFilePath}: ${safe.error.message}`);
}
callback(null, true);
return true;
}
function listDir(apiConfig, dir, batchSize, iteratorCallback, callback) {
+8 -11
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;
}
}
+2 -3
View File
@@ -73,12 +73,11 @@ function upload(apiConfig, backupFilePath, sourceStream, callback) {
callback(new BoxError(BoxError.NOT_IMPLEMENTED, 'upload is not implemented'));
}
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');
callback(new BoxError(BoxError.NOT_IMPLEMENTED, 'exists is not implemented'));
throw new BoxError(BoxError.NOT_IMPLEMENTED, 'exists is not implemented');
}
function download(apiConfig, backupFilePath, callback) {
+3 -4
View File
@@ -49,14 +49,13 @@ function upload(apiConfig, backupFilePath, sourceStream, callback) {
callback(null);
}
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');
debug('exists: %s', backupFilePath);
debug(`exists: ${backupFilePath}`);
callback(null, false);
return false;
}
function download(apiConfig, backupFilePath, callback) {
+9 -12
View File
@@ -138,10 +138,9 @@ function upload(apiConfig, backupFilePath, sourceStream, callback) {
});
}
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 credentials = getS3Config(apiConfig);
@@ -153,13 +152,12 @@ function exists(apiConfig, backupFilePath, callback) {
Key: backupFilePath
};
s3.headObject(params, function (error) {
if (!Object.keys(this.httpResponse.headers).some(h => h.startsWith('x-amz'))) return callback(new BoxError(BoxError.EXTERNAL_ERROR, 'not a s3 endpoint'));
if (error && S3_NOT_FOUND(error)) return callback(null, false);
if (error) return callback(new BoxError(BoxError.EXTERNAL_ERROR, `Error headObject ${backupFilePath}. Message: ${error.message} HTTP Code: ${error.code}`));
const [error] = await safe(s3.headObject(params).promise());
if (!Object.keys(this.httpResponse.headers).some(h => h.startsWith('x-amz'))) throw new BoxError(BoxError.EXTERNAL_ERROR, 'not a s3 endpoint');
if (error && S3_NOT_FOUND(error)) return false;
if (error) throw new BoxError(BoxError.EXTERNAL_ERROR, `Error headObject ${backupFilePath}. Message: ${error.message} HTTP Code: ${error.code}`);
callback(null, true);
});
return true;
} else { // list dir contents
const listParams = {
Bucket: apiConfig.bucket,
@@ -167,11 +165,10 @@ function exists(apiConfig, backupFilePath, callback) {
MaxKeys: 1
};
s3.listObjects(listParams, function (error, listData) {
if (error) return callback(new BoxError(BoxError.EXTERNAL_ERROR, `Error listing objects ${backupFilePath}. Message: ${error.message} HTTP Code: ${error.code}`));
const [error, listData] = await safe(s3.listObjects(listParams).promise());
if (error) throw new BoxError(BoxError.EXTERNAL_ERROR, `Error listing objects ${backupFilePath}. Message: ${error.message} HTTP Code: ${error.code}`);
callback(null, listData.Contents.length !== 0);
});
return listData.Contents.length !== 0;
}
}