mirror of
https://github.com/Tzahi12345/YoutubeDL-Material.git
synced 2026-04-16 11:51:30 +03:00
Initialization on auth component happens in a separate function, users_db primarily sits in app.js
Fixed bug where current download would set to null, but maincomponent still tried to parse it
This commit is contained in:
@@ -32,9 +32,13 @@ var app = express();
|
|||||||
|
|
||||||
// database setup
|
// database setup
|
||||||
const FileSync = require('lowdb/adapters/FileSync')
|
const FileSync = require('lowdb/adapters/FileSync')
|
||||||
|
|
||||||
const adapter = new FileSync('./appdata/db.json');
|
const adapter = new FileSync('./appdata/db.json');
|
||||||
const db = low(adapter)
|
const db = low(adapter)
|
||||||
|
|
||||||
|
const users_adapter = new FileSync('./appdata/users.json');
|
||||||
|
const users_db = low(users_adapter);
|
||||||
|
|
||||||
// check if debug mode
|
// check if debug mode
|
||||||
let debugMode = process.env.YTDL_MODE === 'debug';
|
let debugMode = process.env.YTDL_MODE === 'debug';
|
||||||
|
|
||||||
@@ -62,7 +66,8 @@ const logger = winston.createLogger({
|
|||||||
});
|
});
|
||||||
|
|
||||||
config_api.setLogger(logger);
|
config_api.setLogger(logger);
|
||||||
subscriptions_api.initialize(db, logger);
|
subscriptions_api.initialize(db, users_db, logger);
|
||||||
|
auth_api.initialize(users_db, logger);
|
||||||
|
|
||||||
// var GithubContent = require('github-content');
|
// var GithubContent = require('github-content');
|
||||||
|
|
||||||
@@ -84,6 +89,12 @@ db.defaults(
|
|||||||
files_to_db_migration_complete: false
|
files_to_db_migration_complete: false
|
||||||
}).write();
|
}).write();
|
||||||
|
|
||||||
|
users_db.defaults(
|
||||||
|
{
|
||||||
|
users: []
|
||||||
|
}
|
||||||
|
).write();
|
||||||
|
|
||||||
// config values
|
// config values
|
||||||
var frontendUrl = null;
|
var frontendUrl = null;
|
||||||
var backendUrl = null;
|
var backendUrl = null;
|
||||||
|
|||||||
@@ -1,16 +1,11 @@
|
|||||||
const low = require('lowdb')
|
|
||||||
const FileSync = require('lowdb/adapters/FileSync');
|
|
||||||
const adapter = new FileSync('./appdata/users.json');
|
|
||||||
const db = low(adapter);
|
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const config_api = require('../config');
|
const config_api = require('../config');
|
||||||
var subscriptions_api = require('../subscriptions')
|
var subscriptions_api = require('../subscriptions')
|
||||||
const fs = require('fs-extra');
|
const fs = require('fs-extra');
|
||||||
db.defaults(
|
var jwt = require('jsonwebtoken');
|
||||||
{
|
const { uuid } = require('uuidv4');
|
||||||
users: []
|
var bcrypt = require('bcrypt');
|
||||||
}
|
|
||||||
).write();
|
|
||||||
|
|
||||||
var LocalStrategy = require('passport-local').Strategy;
|
var LocalStrategy = require('passport-local').Strategy;
|
||||||
var JwtStrategy = require('passport-jwt').Strategy,
|
var JwtStrategy = require('passport-jwt').Strategy,
|
||||||
@@ -18,35 +13,56 @@ var JwtStrategy = require('passport-jwt').Strategy,
|
|||||||
|
|
||||||
// other required vars
|
// other required vars
|
||||||
let logger = null;
|
let logger = null;
|
||||||
|
var users_db = null;
|
||||||
|
let SERVER_SECRET = null;
|
||||||
|
let JWT_EXPIRATION = null;
|
||||||
|
let opts = null;
|
||||||
|
let saltRounds = null;
|
||||||
|
|
||||||
exports.setLogger = function(input_logger) {
|
exports.initialize = function(input_users_db, input_logger) {
|
||||||
|
setLogger(input_logger)
|
||||||
|
setDB(input_users_db);
|
||||||
|
|
||||||
|
/*************************
|
||||||
|
* Authentication module
|
||||||
|
************************/
|
||||||
|
saltRounds = 10;
|
||||||
|
|
||||||
|
JWT_EXPIRATION = (60 * 60); // one hour
|
||||||
|
|
||||||
|
SERVER_SECRET = null;
|
||||||
|
if (users_db.get('jwt_secret').value()) {
|
||||||
|
SERVER_SECRET = users_db.get('jwt_secret').value();
|
||||||
|
} else {
|
||||||
|
SERVER_SECRET = uuid();
|
||||||
|
users_db.set('jwt_secret', SERVER_SECRET).write();
|
||||||
|
}
|
||||||
|
|
||||||
|
opts = {}
|
||||||
|
opts.jwtFromRequest = ExtractJwt.fromUrlQueryParameter('jwt');
|
||||||
|
opts.secretOrKey = SERVER_SECRET;
|
||||||
|
/*opts.issuer = 'example.com';
|
||||||
|
opts.audience = 'example.com';*/
|
||||||
|
|
||||||
|
exports.passport.use(new JwtStrategy(opts, function(jwt_payload, done) {
|
||||||
|
const user = users_db.get('users').find({uid: jwt_payload.user.uid}).value();
|
||||||
|
if (user) {
|
||||||
|
return done(null, user);
|
||||||
|
} else {
|
||||||
|
return done(null, false);
|
||||||
|
// or you could create a new account
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
function setLogger(input_logger) {
|
||||||
logger = input_logger;
|
logger = input_logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*************************
|
function setDB(input_users_db) {
|
||||||
* Authentication module
|
users_db = input_users_db;
|
||||||
************************/
|
|
||||||
var bcrypt = require('bcrypt');
|
|
||||||
const saltRounds = 10;
|
|
||||||
|
|
||||||
var jwt = require('jsonwebtoken');
|
|
||||||
const JWT_EXPIRATION = (60 * 60); // one hour
|
|
||||||
|
|
||||||
const { uuid } = require('uuidv4');
|
|
||||||
let SERVER_SECRET = null;
|
|
||||||
if (db.get('jwt_secret').value()) {
|
|
||||||
SERVER_SECRET = db.get('jwt_secret').value();
|
|
||||||
} else {
|
|
||||||
SERVER_SECRET = uuid();
|
|
||||||
db.set('jwt_secret', SERVER_SECRET).write();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var opts = {}
|
|
||||||
opts.jwtFromRequest = ExtractJwt.fromUrlQueryParameter('jwt');
|
|
||||||
opts.secretOrKey = SERVER_SECRET;
|
|
||||||
/*opts.issuer = 'example.com';
|
|
||||||
opts.audience = 'example.com';*/
|
|
||||||
|
|
||||||
exports.passport = require('passport');
|
exports.passport = require('passport');
|
||||||
|
|
||||||
exports.passport.serializeUser(function(user, done) {
|
exports.passport.serializeUser(function(user, done) {
|
||||||
@@ -82,17 +98,17 @@ exports.registerUser = function(req, res) {
|
|||||||
created: Date.now()
|
created: Date.now()
|
||||||
};
|
};
|
||||||
// check if user exists
|
// check if user exists
|
||||||
if (db.get('users').find({uid: userid}).value()) {
|
if (users_db.get('users').find({uid: userid}).value()) {
|
||||||
// user id is taken!
|
// user id is taken!
|
||||||
logger.error('Registration failed: UID is already taken!');
|
logger.error('Registration failed: UID is already taken!');
|
||||||
res.status(409).send('UID is already taken!');
|
res.status(409).send('UID is already taken!');
|
||||||
} else if (db.get('users').find({name: username}).value()) {
|
} else if (users_db.get('users').find({name: username}).value()) {
|
||||||
// user name is taken!
|
// user name is taken!
|
||||||
logger.error('Registration failed: User name is already taken!');
|
logger.error('Registration failed: User name is already taken!');
|
||||||
res.status(409).send('User name is already taken!');
|
res.status(409).send('User name is already taken!');
|
||||||
} else {
|
} else {
|
||||||
// add to db
|
// add to db
|
||||||
db.get('users').push(new_user).write();
|
users_db.get('users').push(new_user).write();
|
||||||
logger.verbose(`New user created: ${new_user.name}`);
|
logger.verbose(`New user created: ${new_user.name}`);
|
||||||
res.send({
|
res.send({
|
||||||
user: new_user
|
user: new_user
|
||||||
@@ -123,21 +139,13 @@ exports.registerUser = function(req, res) {
|
|||||||
* This checks that the credentials are valid.
|
* This checks that the credentials are valid.
|
||||||
* If so, passes the user info to the next middleware.
|
* If so, passes the user info to the next middleware.
|
||||||
************************************************/
|
************************************************/
|
||||||
exports.passport.use(new JwtStrategy(opts, function(jwt_payload, done) {
|
|
||||||
const user = db.get('users').find({uid: jwt_payload.user.uid}).value();
|
|
||||||
if (user) {
|
|
||||||
return done(null, user);
|
|
||||||
} else {
|
|
||||||
return done(null, false);
|
|
||||||
// or you could create a new account
|
|
||||||
}
|
|
||||||
}));
|
|
||||||
|
|
||||||
exports.passport.use(new LocalStrategy({
|
exports.passport.use(new LocalStrategy({
|
||||||
usernameField: 'userid',
|
usernameField: 'userid',
|
||||||
passwordField: 'password'},
|
passwordField: 'password'},
|
||||||
function(username, password, done) {
|
function(username, password, done) {
|
||||||
const user = db.get('users').find({name: username}).value();
|
const user = users_db.get('users').find({name: username}).value();
|
||||||
if (!user) { console.log('user not found'); return done(null, false); }
|
if (!user) { console.log('user not found'); return done(null, false); }
|
||||||
if (user) {
|
if (user) {
|
||||||
return done(null, bcrypt.compareSync(password, user.passhash) ? user : false);
|
return done(null, bcrypt.compareSync(password, user.passhash) ? user : false);
|
||||||
@@ -147,7 +155,7 @@ exports.passport.use(new LocalStrategy({
|
|||||||
|
|
||||||
/*passport.use(new BasicStrategy(
|
/*passport.use(new BasicStrategy(
|
||||||
function(userid, plainTextPassword, done) {
|
function(userid, plainTextPassword, done) {
|
||||||
const user = db.get('users').find({name: userid}).value();
|
const user = users_db.get('users').find({name: userid}).value();
|
||||||
if (user) {
|
if (user) {
|
||||||
var hashedPwd = user.passhash;
|
var hashedPwd = user.passhash;
|
||||||
return bcrypt.compare(plainTextPassword, hashedPwd);
|
return bcrypt.compare(plainTextPassword, hashedPwd);
|
||||||
@@ -232,22 +240,22 @@ exports.ensureAuthenticatedElseError = function(req, res, next) {
|
|||||||
// video stuff
|
// video stuff
|
||||||
|
|
||||||
exports.getUserVideos = function(user_uid, type) {
|
exports.getUserVideos = function(user_uid, type) {
|
||||||
const user = db.get('users').find({uid: user_uid}).value();
|
const user = users_db.get('users').find({uid: user_uid}).value();
|
||||||
return user['files'][type];
|
return user['files'][type];
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.getUserVideo = function(user_uid, file_uid, type, requireSharing = false) {
|
exports.getUserVideo = function(user_uid, file_uid, type, requireSharing = false) {
|
||||||
if (!type) {
|
if (!type) {
|
||||||
file = db.get('users').find({uid: user_uid}).get(`files.audio`).find({uid: file_uid}).value();
|
file = users_db.get('users').find({uid: user_uid}).get(`files.audio`).find({uid: file_uid}).value();
|
||||||
if (!file) {
|
if (!file) {
|
||||||
file = db.get('users').find({uid: user_uid}).get(`files.video`).find({uid: file_uid}).value();
|
file = users_db.get('users').find({uid: user_uid}).get(`files.video`).find({uid: file_uid}).value();
|
||||||
if (file) type = 'video';
|
if (file) type = 'video';
|
||||||
} else {
|
} else {
|
||||||
type = 'audio';
|
type = 'audio';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!file && type) file = db.get('users').find({uid: user_uid}).get(`files.${type}`).find({uid: file_uid}).value();
|
if (!file && type) file = users_db.get('users').find({uid: user_uid}).get(`files.${type}`).find({uid: file_uid}).value();
|
||||||
|
|
||||||
// prevent unauthorized users from accessing the file info
|
// prevent unauthorized users from accessing the file info
|
||||||
if (requireSharing && !file['sharingEnabled']) file = null;
|
if (requireSharing && !file['sharingEnabled']) file = null;
|
||||||
@@ -256,54 +264,54 @@ exports.getUserVideo = function(user_uid, file_uid, type, requireSharing = false
|
|||||||
}
|
}
|
||||||
|
|
||||||
exports.addPlaylist = function(user_uid, new_playlist, type) {
|
exports.addPlaylist = function(user_uid, new_playlist, type) {
|
||||||
db.get('users').find({uid: user_uid}).get(`playlists.${type}`).push(new_playlist).write();
|
users_db.get('users').find({uid: user_uid}).get(`playlists.${type}`).push(new_playlist).write();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.updatePlaylist = function(user_uid, playlistID, new_filenames, type) {
|
exports.updatePlaylist = function(user_uid, playlistID, new_filenames, type) {
|
||||||
db.get('users').find({uid: user_uid}).get(`playlists.${type}`).find({id: playlistID}).assign({fileNames: new_filenames});
|
users_db.get('users').find({uid: user_uid}).get(`playlists.${type}`).find({id: playlistID}).assign({fileNames: new_filenames});
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.removePlaylist = function(user_uid, playlistID, type) {
|
exports.removePlaylist = function(user_uid, playlistID, type) {
|
||||||
db.get('users').find({uid: user_uid}).get(`playlists.${type}`).remove({id: playlistID}).write();
|
users_db.get('users').find({uid: user_uid}).get(`playlists.${type}`).remove({id: playlistID}).write();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.getUserPlaylists = function(user_uid, type) {
|
exports.getUserPlaylists = function(user_uid, type) {
|
||||||
const user = db.get('users').find({uid: user_uid}).value();
|
const user = users_db.get('users').find({uid: user_uid}).value();
|
||||||
return user['playlists'][type];
|
return user['playlists'][type];
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.getUserPlaylist = function(user_uid, playlistID, type) {
|
exports.getUserPlaylist = function(user_uid, playlistID, type) {
|
||||||
let playlist = null;
|
let playlist = null;
|
||||||
if (!type) {
|
if (!type) {
|
||||||
playlist = db.get('users').find({uid: user_uid}).get(`playlists.audio`).find({id: playlistID}).value();
|
playlist = users_db.get('users').find({uid: user_uid}).get(`playlists.audio`).find({id: playlistID}).value();
|
||||||
if (!playlist) {
|
if (!playlist) {
|
||||||
playlist = db.get('users').find({uid: user_uid}).get(`playlists.video`).find({id: playlistID}).value();
|
playlist = users_db.get('users').find({uid: user_uid}).get(`playlists.video`).find({id: playlistID}).value();
|
||||||
if (playlist) type = 'video';
|
if (playlist) type = 'video';
|
||||||
} else {
|
} else {
|
||||||
type = 'audio';
|
type = 'audio';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!playlist) playlist = db.get('users').find({uid: user_uid}).get(`playlists.${type}`).find({id: playlistID}).value();
|
if (!playlist) playlist = users_db.get('users').find({uid: user_uid}).get(`playlists.${type}`).find({id: playlistID}).value();
|
||||||
return playlist;
|
return playlist;
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.registerUserFile = function(user_uid, file_object, type) {
|
exports.registerUserFile = function(user_uid, file_object, type) {
|
||||||
db.get('users').find({uid: user_uid}).get(`files.${type}`)
|
users_db.get('users').find({uid: user_uid}).get(`files.${type}`)
|
||||||
.remove({
|
.remove({
|
||||||
path: file_object['path']
|
path: file_object['path']
|
||||||
}).write();
|
}).write();
|
||||||
|
|
||||||
db.get('users').find({uid: user_uid}).get(`files.${type}`)
|
users_db.get('users').find({uid: user_uid}).get(`files.${type}`)
|
||||||
.push(file_object)
|
.push(file_object)
|
||||||
.write();
|
.write();
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.deleteUserFile = function(user_uid, file_uid, type, blacklistMode = false) {
|
exports.deleteUserFile = function(user_uid, file_uid, type, blacklistMode = false) {
|
||||||
let success = false;
|
let success = false;
|
||||||
const file_obj = 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) {
|
||||||
const usersFileFolder = config_api.getConfigItem('ytdl_users_base_path');
|
const usersFileFolder = config_api.getConfigItem('ytdl_users_base_path');
|
||||||
const ext = type === 'audio' ? '.mp3' : '.mp4';
|
const ext = type === 'audio' ? '.mp3' : '.mp4';
|
||||||
@@ -320,7 +328,7 @@ exports.deleteUserFile = function(user_uid, file_uid, type, blacklistMode = fals
|
|||||||
}
|
}
|
||||||
|
|
||||||
const full_path = path.join(usersFileFolder, user_uid, type, file_obj.id + ext);
|
const full_path = path.join(usersFileFolder, user_uid, type, file_obj.id + ext);
|
||||||
db.get('users').find({uid: user_uid}).get(`files.${type}`)
|
users_db.get('users').find({uid: user_uid}).get(`files.${type}`)
|
||||||
.remove({
|
.remove({
|
||||||
uid: file_uid
|
uid: file_uid
|
||||||
}).write();
|
}).write();
|
||||||
@@ -371,7 +379,7 @@ exports.deleteUserFile = function(user_uid, file_uid, type, blacklistMode = fals
|
|||||||
|
|
||||||
exports.changeSharingMode = function(user_uid, file_uid, type, is_playlist, enabled) {
|
exports.changeSharingMode = function(user_uid, file_uid, type, is_playlist, enabled) {
|
||||||
let success = false;
|
let success = false;
|
||||||
const user_db_obj = db.get('users').find({uid: user_uid});
|
const user_db_obj = users_db.get('users').find({uid: user_uid});
|
||||||
if (user_db_obj.value()) {
|
if (user_db_obj.value()) {
|
||||||
const file_db_obj = is_playlist ? user_db_obj.get(`playlists.${type}`).find({uid: file_uid}) : user_db_obj.get(`files.${type}`).find({uid: file_uid});
|
const file_db_obj = is_playlist ? user_db_obj.get(`playlists.${type}`).find({uid: file_uid}) : user_db_obj.get(`files.${type}`).find({uid: file_uid});
|
||||||
if (file_db_obj.value()) {
|
if (file_db_obj.value()) {
|
||||||
|
|||||||
@@ -11,11 +11,12 @@ const debugMode = process.env.YTDL_MODE === 'debug';
|
|||||||
|
|
||||||
var logger = null;
|
var logger = null;
|
||||||
var db = null;
|
var db = null;
|
||||||
function setDB(input_db) { db = input_db; }
|
var users_db = null;
|
||||||
|
function setDB(input_db, input_users_db) { db = input_db; users_db = input_users_db }
|
||||||
function setLogger(input_logger) { logger = input_logger; }
|
function setLogger(input_logger) { logger = input_logger; }
|
||||||
|
|
||||||
function initialize(input_db, input_logger) {
|
function initialize(input_db, input_users_db, input_logger) {
|
||||||
setDB(input_db);
|
setDB(input_db, input_users_db);
|
||||||
setLogger(input_logger);
|
setLogger(input_logger);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1129,6 +1129,9 @@ export class MainComponent implements OnInit {
|
|||||||
}
|
}
|
||||||
|
|
||||||
getCurrentDownload() {
|
getCurrentDownload() {
|
||||||
|
if (!this.current_download) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
const ui_uid = this.current_download['ui_uid'] ? this.current_download['ui_uid'] : this.current_download['uid'];
|
const ui_uid = this.current_download['ui_uid'] ? this.current_download['ui_uid'] : this.current_download['uid'];
|
||||||
this.postsService.getCurrentDownload(this.postsService.session_id, ui_uid).subscribe(res => {
|
this.postsService.getCurrentDownload(this.postsService.session_id, ui_uid).subscribe(res => {
|
||||||
if (res['download']) {
|
if (res['download']) {
|
||||||
|
|||||||
Reference in New Issue
Block a user