From 1e5248ff9dd4bd41b8eea301faeca02251c88ceb Mon Sep 17 00:00:00 2001 From: ansuz Date: Fri, 14 Jun 2019 11:38:16 +0200 Subject: [PATCH] implement methods for checking if a file exists in the database or the archive --- storage/file.js | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/storage/file.js b/storage/file.js index 58a10ba46..717f4104b 100644 --- a/storage/file.js +++ b/storage/file.js @@ -213,6 +213,21 @@ var removeChannel = function (env, channelName, cb) { Fs.unlink(filename, cb); }; +// pass in the path so we can reuse the same function for archived files +var channelExists = function (filepath, channelName, cb) { + Fs.stat(filepath, function (err, stat) { + if (err) { + if (err.code === 'ENOENT') { + // no, the file doesn't exist + return void cb(void 0, true); + } + return void cb(err); + } + if (!stat.isFile()) { return void cb("E_NOT_FILE"); } + return void cb(void 0, true); + }); +}; + var removeArchivedChannel = function (env, channelName, cb) { var filename = mkArchivePath(env, channelName); Fs.unlink(filename, cb); @@ -596,6 +611,18 @@ module.exports.create = function ( listChannels: function (handler, cb) { listChannels(env.root, handler, cb); }, + isChannelAvailable: function (channelName, cb) { + if (!isValidChannelId(channelName)) { return void cb(new Error('EINVAL')); } + // construct the path + var filepath = mkPath(env, channelName); + channelExists(filepath, channelName, cb); + }, + isChannelArchived: function (channelName, cb) { + if (!isValidChannelId(channelName)) { return void cb(new Error('EINVAL')); } + // construct the path + var filepath = mkArchivePath(env, channelName); + channelExists(filepath, channelName, cb); + }, listArchivedChannels: function (handler, cb) { listChannels(Path.join(env.archiveRoot, 'datastore'), handler, cb); },