Use async methods in auth and subscriptions

This commit is contained in:
Tiger Oakes
2020-09-26 15:14:37 -07:00
parent 2c43ce3c47
commit f535d18cb9
3 changed files with 188 additions and 208 deletions

View File

@@ -940,7 +940,7 @@ async function deleteAudioFile(name, customPath = null, blacklistMode = false) {
// use subscriptions API to remove video from the archive file, and write it to the blacklist // use subscriptions API to remove video from the archive file, and write it to the blacklist
if (await fs.pathExists(archive_path)) { if (await fs.pathExists(archive_path)) {
const line = id ? subscriptions_api.removeIDFromArchive(archive_path, id) : null; const line = id ? await subscriptions_api.removeIDFromArchive(archive_path, id) : null;
if (blacklistMode && line) await writeToBlacklist('audio', line); if (blacklistMode && line) await writeToBlacklist('audio', line);
} else { } else {
logger.info('Could not find archive file for audio files. Creating...'); logger.info('Could not find archive file for audio files. Creating...');
@@ -1015,7 +1015,7 @@ async function deleteVideoFile(name, customPath = null, blacklistMode = false) {
// use subscriptions API to remove video from the archive file, and write it to the blacklist // use subscriptions API to remove video from the archive file, and write it to the blacklist
if (await fs.pathExists(archive_path)) { if (await fs.pathExists(archive_path)) {
const line = id ? subscriptions_api.removeIDFromArchive(archive_path, id) : null; const line = id ? await subscriptions_api.removeIDFromArchive(archive_path, id) : null;
if (blacklistMode && line) await writeToBlacklist('video', line); if (blacklistMode && line) await writeToBlacklist('video', line);
} else { } else {
logger.info('Could not find archive file for videos. Creating...'); logger.info('Could not find archive file for videos. Creating...');
@@ -2429,7 +2429,7 @@ app.post('/api/deleteMp3', optionalJwt, async (req, res) => {
var blacklistMode = req.body.blacklistMode; var blacklistMode = req.body.blacklistMode;
if (req.isAuthenticated()) { if (req.isAuthenticated()) {
let success = auth_api.deleteUserFile(req.user.uid, uid, 'audio', blacklistMode); let success = await auth_api.deleteUserFile(req.user.uid, uid, 'audio', blacklistMode);
res.send(success); res.send(success);
return; return;
} }
@@ -2460,7 +2460,7 @@ app.post('/api/deleteMp4', optionalJwt, async (req, res) => {
var blacklistMode = req.body.blacklistMode; var blacklistMode = req.body.blacklistMode;
if (req.isAuthenticated()) { if (req.isAuthenticated()) {
let success = auth_api.deleteUserFile(req.user.uid, uid, 'video', blacklistMode); let success = await auth_api.deleteUserFile(req.user.uid, uid, 'video', blacklistMode);
res.send(success); res.send(success);
return; return;
} }

View File

@@ -139,12 +139,12 @@ exports.registerUser = function(req, res) {
exports.passport.use(new LocalStrategy({ exports.passport.use(new LocalStrategy({
usernameField: 'username', usernameField: 'username',
passwordField: 'password'}, passwordField: 'password'},
function(username, password, done) { async function(username, password, done) {
const user = users_db.get('users').find({name: username}).value(); const user = users_db.get('users').find({name: username}).value();
if (!user) { logger.error(`User ${username} not found`); return done(null, false); } if (!user) { logger.error(`User ${username} not found`); return done(null, false); }
if (user.auth_method && user.auth_method !== 'internal') { return done(null, false); } if (user.auth_method && user.auth_method !== 'internal') { return done(null, false); }
if (user) { if (user) {
return done(null, bcrypt.compareSync(password, user.passhash) ? user : false); return done(null, (await bcrypt.compare(password, user.passhash)) ? user : false);
} }
} }
)); ));
@@ -226,15 +226,13 @@ exports.ensureAuthenticatedElseError = function(req, res, next) {
// change password // change password
exports.changeUserPassword = async function(user_uid, new_pass) { exports.changeUserPassword = async function(user_uid, new_pass) {
return new Promise(resolve => { try {
bcrypt.hash(new_pass, saltRounds) const hash = await bcrypt.hash(new_pass, saltRounds);
.then(function(hash) {
users_db.get('users').find({uid: user_uid}).assign({passhash: hash}).write(); users_db.get('users').find({uid: user_uid}).assign({passhash: hash}).write();
resolve(true); return true;
}).catch(err => { } catch (err) {
resolve(false); return false;
}); }
});
} }
// change user permissions // change user permissions
@@ -352,7 +350,7 @@ exports.registerUserFile = function(user_uid, file_object, type) {
.write(); .write();
} }
exports.deleteUserFile = function(user_uid, file_uid, type, blacklistMode = false) { exports.deleteUserFile = async function(user_uid, file_uid, type, blacklistMode = false) {
let success = false; let success = false;
const file_obj = users_db.get('users').find({uid: user_uid}).get(`files.${type}`).find({uid: file_uid}).value(); const file_obj = users_db.get('users').find({uid: user_uid}).get(`files.${type}`).find({uid: file_uid}).value();
if (file_obj) { if (file_obj) {
@@ -375,20 +373,20 @@ exports.deleteUserFile = function(user_uid, file_uid, type, blacklistMode = fals
.remove({ .remove({
uid: file_uid uid: file_uid
}).write(); }).write();
if (fs.existsSync(full_path)) { if (await fs.pathExists(full_path)) {
// remove json and file // remove json and file
const json_path = path.join(usersFileFolder, user_uid, type, file_obj.id + '.info.json'); const json_path = path.join(usersFileFolder, user_uid, type, file_obj.id + '.info.json');
const alternate_json_path = path.join(usersFileFolder, user_uid, type, file_obj.id + ext + '.info.json'); const alternate_json_path = path.join(usersFileFolder, user_uid, type, file_obj.id + ext + '.info.json');
let youtube_id = null; let youtube_id = null;
if (fs.existsSync(json_path)) { if (await fs.pathExists(json_path)) {
youtube_id = fs.readJSONSync(json_path).id; youtube_id = await fs.readJSON(json_path).id;
fs.unlinkSync(json_path); await fs.unlink(json_path);
} else if (fs.existsSync(alternate_json_path)) { } else if (await fs.pathExists(alternate_json_path)) {
youtube_id = fs.readJSONSync(alternate_json_path).id; youtube_id = await fs.readJSON(alternate_json_path).id;
fs.unlinkSync(alternate_json_path); await fs.unlink(alternate_json_path);
} }
fs.unlinkSync(full_path); await fs.unlink(full_path);
// do archive stuff // do archive stuff
@@ -397,17 +395,17 @@ exports.deleteUserFile = function(user_uid, file_uid, type, blacklistMode = fals
const archive_path = path.join(usersFileFolder, user_uid, 'archives', `archive_${type}.txt`); const archive_path = path.join(usersFileFolder, user_uid, 'archives', `archive_${type}.txt`);
// use subscriptions API to remove video from the archive file, and write it to the blacklist // use subscriptions API to remove video from the archive file, and write it to the blacklist
if (fs.existsSync(archive_path)) { if (await fs.pathExists(archive_path)) {
const line = youtube_id ? subscriptions_api.removeIDFromArchive(archive_path, youtube_id) : null; const line = youtube_id ? await subscriptions_api.removeIDFromArchive(archive_path, youtube_id) : null;
if (blacklistMode && line) { if (blacklistMode && line) {
let blacklistPath = path.join(usersFileFolder, user_uid, 'archives', `blacklist_${type}.txt`); let blacklistPath = path.join(usersFileFolder, user_uid, 'archives', `blacklist_${type}.txt`);
// adds newline to the beginning of the line // adds newline to the beginning of the line
line = '\n' + line; line = '\n' + line;
fs.appendFileSync(blacklistPath, line); await fs.appendFile(blacklistPath, line);
} }
} else { } else {
logger.info(`Could not find archive file for ${type} files. Creating...`); logger.info(`Could not find archive file for ${type} files. Creating...`);
fs.ensureFileSync(archive_path); await fs.ensureFile(archive_path);
} }
} }
} }

View File

@@ -79,17 +79,18 @@ async function getSubscriptionInfo(sub, user_uid = null) {
else else
basePath = config_api.getConfigItem('ytdl_subscriptions_base_path'); basePath = config_api.getConfigItem('ytdl_subscriptions_base_path');
return new Promise(resolve => {
// get videos // get videos
let downloadConfig = ['--dump-json', '--playlist-end', '1']; let downloadConfig = ['--dump-json', '--playlist-end', '1'];
let useCookies = config_api.getConfigItem('ytdl_use_cookies'); let useCookies = config_api.getConfigItem('ytdl_use_cookies');
if (useCookies) { if (useCookies) {
if (fs.existsSync(path.join(__dirname, 'appdata', 'cookies.txt'))) { if (await fs.pathExists(path.join(__dirname, 'appdata', 'cookies.txt'))) {
downloadConfig.push('--cookies', path.join('appdata', 'cookies.txt')); downloadConfig.push('--cookies', path.join('appdata', 'cookies.txt'));
} else { } else {
logger.warn('Cookies file could not be found. You can either upload one, or disable \'use cookies\' in the Advanced tab in the settings.'); logger.warn('Cookies file could not be found. You can either upload one, or disable \'use cookies\' in the Advanced tab in the settings.');
} }
} }
return new Promise(resolve => {
youtubedl.exec(sub.url, downloadConfig, {}, function(err, output) { youtubedl.exec(sub.url, downloadConfig, {}, function(err, output) {
if (debugMode) { if (debugMode) {
logger.info('Subscribe: got info for subscription ' + sub.id); logger.info('Subscribe: got info for subscription ' + sub.id);
@@ -152,7 +153,6 @@ async function getSubscriptionInfo(sub, user_uid = null) {
} }
async function unsubscribe(sub, deleteMode, user_uid = null) { async function unsubscribe(sub, deleteMode, user_uid = null) {
return new Promise(async resolve => {
let basePath = null; let basePath = null;
if (user_uid) if (user_uid)
basePath = path.join(config_api.getConfigItem('ytdl_users_base_path'), user_uid, 'subscriptions'); basePath = path.join(config_api.getConfigItem('ytdl_users_base_path'), user_uid, 'subscriptions');
@@ -172,19 +172,17 @@ async function unsubscribe(sub, deleteMode, user_uid = null) {
} }
const appendedBasePath = getAppendedBasePath(sub, basePath); const appendedBasePath = getAppendedBasePath(sub, basePath);
if (deleteMode && fs.existsSync(appendedBasePath)) { if (deleteMode && (await fs.pathExists(appendedBasePath))) {
if (sub.archive && fs.existsSync(sub.archive)) { if (sub.archive && (await fs.pathExists(sub.archive))) {
const archive_file_path = path.join(sub.archive, 'archive.txt'); const archive_file_path = path.join(sub.archive, 'archive.txt');
// deletes archive if it exists // deletes archive if it exists
if (fs.existsSync(archive_file_path)) { if (await fs.pathExists(archive_file_path)) {
fs.unlinkSync(archive_file_path); await fs.unlink(archive_file_path);
} }
fs.rmdirSync(sub.archive); await fs.rmdir(sub.archive);
} }
deleteFolderRecursive(appendedBasePath); await fs.remove(appendedBasePath);
} }
});
} }
async function deleteSubscriptionFile(sub, file, deleteForever, file_uid = null, user_uid = null) { async function deleteSubscriptionFile(sub, file, deleteForever, file_uid = null, user_uid = null) {
@@ -202,7 +200,7 @@ async function deleteSubscriptionFile(sub, file, deleteForever, file_uid = null,
const name = file; const name = file;
let retrievedID = null; let retrievedID = null;
sub_db.get('videos').remove({uid: file_uid}).write(); sub_db.get('videos').remove({uid: file_uid}).write();
return new Promise(resolve => {
let filePath = appendedBasePath; let filePath = appendedBasePath;
const ext = (sub.type && sub.type === 'audio') ? '.mp3' : '.mp4' const ext = (sub.type && sub.type === 'audio') ? '.mp3' : '.mp4'
var jsonPath = path.join(__dirname,filePath,name+'.info.json'); var jsonPath = path.join(__dirname,filePath,name+'.info.json');
@@ -210,53 +208,50 @@ async function deleteSubscriptionFile(sub, file, deleteForever, file_uid = null,
var imageFilePath = path.join(__dirname,filePath,name+'.jpg'); var imageFilePath = path.join(__dirname,filePath,name+'.jpg');
var altImageFilePath = path.join(__dirname,filePath,name+'.jpg'); var altImageFilePath = path.join(__dirname,filePath,name+'.jpg');
jsonExists = fs.existsSync(jsonPath); const [jsonExists, videoFileExists, imageFileExists, altImageFileExists] = await Promise.all([
videoFileExists = fs.existsSync(videoFilePath); fs.pathExists(jsonPath),
imageFileExists = fs.existsSync(imageFilePath); fs.pathExists(videoFilePath),
altImageFileExists = fs.existsSync(altImageFilePath); fs.pathExists(imageFilePath),
fs.pathExists(altImageFilePath),
]);
if (jsonExists) { if (jsonExists) {
retrievedID = JSON.parse(fs.readFileSync(jsonPath, 'utf8'))['id']; retrievedID = JSON.parse(await fs.readFile(jsonPath, 'utf8'))['id'];
fs.unlinkSync(jsonPath); await fs.unlink(jsonPath);
} }
if (imageFileExists) { if (imageFileExists) {
fs.unlinkSync(imageFilePath); await fs.unlink(imageFilePath);
} }
if (altImageFileExists) { if (altImageFileExists) {
fs.unlinkSync(altImageFilePath); await fs.unlink(altImageFilePath);
} }
if (videoFileExists) { if (videoFileExists) {
fs.unlink(videoFilePath, function(err) { await fs.unlink(videoFilePath);
if (fs.existsSync(jsonPath) || fs.existsSync(videoFilePath)) { if ((await fs.pathExists(jsonPath)) || (await fs.pathExists(videoFilePath))) {
resolve(false); return false;
} else { } else {
// check if the user wants the video to be redownloaded (deleteForever === false) // check if the user wants the video to be redownloaded (deleteForever === false)
if (!deleteForever && useArchive && sub.archive && retrievedID) { if (!deleteForever && useArchive && sub.archive && retrievedID) {
const archive_path = path.join(sub.archive, 'archive.txt') const archive_path = path.join(sub.archive, 'archive.txt')
// if archive exists, remove line with video ID // if archive exists, remove line with video ID
if (fs.existsSync(archive_path)) { if (await fs.pathExists(archive_path)) {
removeIDFromArchive(archive_path, retrievedID); await removeIDFromArchive(archive_path, retrievedID);
} }
} }
resolve(true); return true;
} }
});
} else { } else {
// TODO: tell user that the file didn't exist // TODO: tell user that the file didn't exist
resolve(true); return true;
} }
});
} }
async function getVideosForSub(sub, user_uid = null) { async function getVideosForSub(sub, user_uid = null) {
return new Promise(resolve => {
if (!subExists(sub.id, user_uid)) { if (!subExists(sub.id, user_uid)) {
resolve(false); return false;
return;
} }
// get sub_db // get sub_db
@@ -338,7 +333,7 @@ async function getVideosForSub(sub, user_uid = null) {
let useCookies = config_api.getConfigItem('ytdl_use_cookies'); let useCookies = config_api.getConfigItem('ytdl_use_cookies');
if (useCookies) { if (useCookies) {
if (fs.existsSync(path.join(__dirname, 'appdata', 'cookies.txt'))) { if (await fs.pathExists(path.join(__dirname, 'appdata', 'cookies.txt'))) {
downloadConfig.push('--cookies', path.join('appdata', 'cookies.txt')); downloadConfig.push('--cookies', path.join('appdata', 'cookies.txt'));
} else { } else {
logger.warn('Cookies file could not be found. You can either upload one, or disable \'use cookies\' in the Advanced tab in the settings.'); logger.warn('Cookies file could not be found. You can either upload one, or disable \'use cookies\' in the Advanced tab in the settings.');
@@ -351,6 +346,8 @@ async function getVideosForSub(sub, user_uid = null) {
// get videos // get videos
logger.verbose('Subscription: getting videos for subscription ' + sub.name); logger.verbose('Subscription: getting videos for subscription ' + sub.name);
return new Promise(resolve => {
youtubedl.exec(sub.url, downloadConfig, {}, function(err, output) { youtubedl.exec(sub.url, downloadConfig, {}, function(err, output) {
logger.verbose('Subscription: finished check for ' + sub.name); logger.verbose('Subscription: finished check for ' + sub.name);
if (err && !output) { if (err && !output) {
@@ -456,23 +453,8 @@ function getAppendedBasePath(sub, base_path) {
return path.join(base_path, (sub.isPlaylist ? 'playlists/' : 'channels/'), sub.name); return path.join(base_path, (sub.isPlaylist ? 'playlists/' : 'channels/'), sub.name);
} }
// https://stackoverflow.com/a/32197381/8088021 async function removeIDFromArchive(archive_path, id) {
const deleteFolderRecursive = function(folder_to_delete) { let data = await fs.readFile(archive_path, {encoding: 'utf-8'});
if (fs.existsSync(folder_to_delete)) {
fs.readdirSync(folder_to_delete).forEach((file, index) => {
const curPath = path.join(folder_to_delete, file);
if (fs.lstatSync(curPath).isDirectory()) { // recurse
deleteFolderRecursive(curPath);
} else { // delete file
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(folder_to_delete);
}
};
function removeIDFromArchive(archive_path, id) {
let data = fs.readFileSync(archive_path, {encoding: 'utf-8'});
if (!data) { if (!data) {
logger.error('Archive could not be found.'); logger.error('Archive could not be found.');
return; return;
@@ -493,7 +475,7 @@ function removeIDFromArchive(archive_path, id) {
// UPDATE FILE WITH NEW DATA // UPDATE FILE WITH NEW DATA
const updatedData = dataArray.join('\n'); const updatedData = dataArray.join('\n');
fs.writeFileSync(archive_path, updatedData); await fs.writeFile(archive_path, updatedData);
if (line) return line; if (line) return line;
if (err) throw err; if (err) throw err;
} }