From ed95e543f4971c1bf6867021086051257153d018 Mon Sep 17 00:00:00 2001 From: Vicente Pastor Pastor Date: Mon, 25 Nov 2019 21:03:50 +0000 Subject: [PATCH] Ran eslint with the --fix option --- index.js | 773 +++++++++++++++++++++---------------------- modules/accounts.js | 14 +- modules/audio.js | 12 +- modules/comments.js | 24 +- modules/playlists.js | 18 +- 5 files changed, 420 insertions(+), 421 deletions(-) diff --git a/index.js b/index.js index a4d7235..5e98270 100644 --- a/index.js +++ b/index.js @@ -4,49 +4,49 @@ * Routes File */ -"use strict"; +'use strict' /* MODULE IMPORTS */ -const http = require("http"); -const bcrypt = require("bcryptjs"); -const Koa = require("koa"); -const Router = require("koa-router"); -const views = require("koa-views"); -const staticDir = require("koa-static"); -const bodyParser = require("koa-bodyparser"); -const koaBody = require("koa-body")({ multipart: true, uploadDir: "." }); -const session = require("koa-session"); -const sqlite = require("sqlite-async"); -const fs = require("fs-extra"); -const mime = require("mime-types"); +const http = require('http') +const bcrypt = require('bcryptjs') +const Koa = require('koa') +const Router = require('koa-router') +const views = require('koa-views') +const staticDir = require('koa-static') +const bodyParser = require('koa-bodyparser') +const koaBody = require('koa-body')({ multipart: true, uploadDir: '.' }) +const session = require('koa-session') +const sqlite = require('sqlite-async') +const fs = require('fs-extra') +const mime = require('mime-types') //const jimp = require('jimp') /* IMPORT CUSTOM MODULES */ -const User = require("./modules/accounts"); -const Audio = require("./modules/audio"); -const Playlists = require("./modules/playlists"); -const Comments = require("./modules/comments") +const User = require('./modules/accounts') +const Audio = require('./modules/audio') +const Playlists = require('./modules/playlists') +const Comments = require('./modules/comments') -const app = new Koa(); -const router = new Router(); +const app = new Koa() +const router = new Router() /* CONFIGURING THE MIDDLEWARE */ -app.keys = ['darkSecret']; -app.use(staticDir('public')); -app.use(bodyParser()); -app.use(session(app)); +app.keys = ['darkSecret'] +app.use(staticDir('public')) +app.use(bodyParser()) +app.use(session(app)) app.use( - views( - `${__dirname}/views`, { extension: "handlebars" }, { map: { handlebars: "handlebars" } } - ) -); - -const port = process.env.PORT || 8081; -const dbName = 'public/website.db'; -const saltRounds = 10; -const reloadPage = {contentView: "content_index", genre: "none"} + views( + `${__dirname}/views`, { extension: 'handlebars' }, { map: { handlebars: 'handlebars' } } + ) +) + +const port = process.env.PORT || 8081 +const dbName = 'public/website.db' +const saltRounds = 10 +const reloadPage = {contentView: 'content_index', genre: 'none'} /** * The secure home page. * @@ -55,18 +55,18 @@ const reloadPage = {contentView: "content_index", genre: "none"} * @authentication This route requires cookie-based authentication. */ -router.get("/", async ctx => { - try { - if (ctx.session.authorised !== true) return ctx.redirect('/login'); - const data = {}; - if (ctx.query.msg) data.msg = ctx.query.msg; - await ctx.render('template_index', reloadPage); //by watrasm, was "index" before dynamic website - reloadPage.contentView = "content_index" - reloadPage.genre = "none" - } catch (err) { - await ctx.render('error', { message: err.message }); - } -}); +router.get('/', async ctx => { + try { + if (ctx.session.authorised !== true) return ctx.redirect('/login') + const data = {} + if (ctx.query.msg) data.msg = ctx.query.msg + await ctx.render('template_index', reloadPage) //by watrasm, was "index" before dynamic website + reloadPage.contentView = 'content_index' + reloadPage.genre = 'none' + } catch (err) { + await ctx.render('error', { message: err.message }) + } +}) /** @@ -75,8 +75,8 @@ router.get("/", async ctx => { * @name Register Page * @route {GET} /register */ -router.get("/register", async ctx => await ctx.render("register")); -router.get("/ratings", async ctx => await ctx.render("ratings")); +router.get('/register', async ctx => await ctx.render('register')) +router.get('/ratings', async ctx => await ctx.render('ratings')) /** * The script to process new user registrations. @@ -85,360 +85,359 @@ router.get("/ratings", async ctx => await ctx.render("ratings")); * @route {POST} /register */ -router.post("/register", koaBody, async ctx => { - try { - // extract the data from the request - const body = ctx.request.body; - const { path, type } = ctx.request.files.avatar; - // call the functions in the module - const user = await new User(dbName); - await user.register(body.user, body.pass); - //await user.uploadPicture('/public', 'image/png') - // redirect to the home page - ctx.redirect(`/?msg=new user "${body.name}" added`); - } catch (err) { - await ctx.render("error", { message: err.message }); - } -}); - -router.get("/login", async ctx => { - const data = {}; - if (ctx.query.msg) data.msg = ctx.query.msg; - if (ctx.query.user) data.user = ctx.query.user; - await ctx.render("login", data); -}); - -router.get("/logout", async ctx => { - ctx.session.authorised = null; - ctx.redirect("/?msg=you are now logged out"); - }) - //changed from get to post and content_search added to dynamically load search page ~by watrasm -router.post("/search", async ctx => { - try { - if (ctx.session.authorised !== true) return ctx.redirect("/login"); - const body = ctx.request.body; - const query = body.q; - - const db = await sqlite.open(dbName); - const searchResult = await db.all(`SELECT * FROM uploads WHERE lower(user) LIKE lower('%${query}%') OR lower(name) LIKE lower('%${query}%') OR lower(artist) LIKE lower('%${query}%');`); - - searchResult.forEach(async result => { - const audioRatings = await db.all(`SELECT "value" FROM audio_ratings WHERE uploadId='${result.uploadID}';`); - let totalRating = sum(audioRatings); - - if (totalRating > 0) - totalRating = totalRating / audioRatings.length; - - result.safeRating = Math.floor(totalRating); - result.rating = Math.round(totalRating * 100) / 100; - }); - - await db.close(); - - await ctx.render("content_search", { - tracks: searchResult, - }); - } catch (err) { - console.log(err); - await ctx.render("error", { message: err.message }); - } -}); -router.post("/playlistRating", async ctx => { - try { - if (ctx.session.authorised !== true) return ctx.redirect("/login"); - const body = ctx.request.body; - const newRating = body.num; - const playlistId = body.playlistId; - - const db = await sqlite.open(dbName); - await db.run(`INSERT INTO playlist_ratings(value, user, playlistId) VALUES(${newRating},'${ctx.session.user}',${playlistId});`); - await db.close(); - } catch (err) { - console.log(err); - await ctx.render("error", { message: err.message }); - } -}); -router.post("/commentRating", async ctx => { - try { - if (ctx.session.authorised !== true) return ctx.redirect("/login"); - const body = ctx.request.body; - const newRating = body.num; - const commentId = body.commentId; - - const db = await sqlite.open(dbName); - await db.run(`INSERT INTO comment_ratings(value, user, commentId) VALUES(${newRating},'${ctx.session.user}',${commentId});`); - await db.close(); - } catch (err) { - console.log(err); - await ctx.render("error", { message: err.message }); - } -}); -router.post("/audioRating", async ctx => { - try { - if (ctx.session.authorised !== true) return ctx.redirect("/login"); - const body = ctx.request.body; - const newRating = body.num; - const songId = body.audioId; - - const db = await sqlite.open(dbName); - await db.run(`INSERT INTO audio_ratings(value, user, uploadId) VALUES(${newRating},'${ctx.session.user}',${songId});`); - await db.close(); - } catch (err) { - console.log(err); - await ctx.render("error", { message: err.message }); - } -}); +router.post('/register', koaBody, async ctx => { + try { + // extract the data from the request + const body = ctx.request.body + const { path, type } = ctx.request.files.avatar + // call the functions in the module + const user = await new User(dbName) + await user.register(body.user, body.pass) + //await user.uploadPicture('/public', 'image/png') + // redirect to the home page + ctx.redirect(`/?msg=new user "${body.name}" added`) + } catch (err) { + await ctx.render('error', { message: err.message }) + } +}) + +router.get('/login', async ctx => { + const data = {} + if (ctx.query.msg) data.msg = ctx.query.msg + if (ctx.query.user) data.user = ctx.query.user + await ctx.render('login', data) +}) + +router.get('/logout', async ctx => { + ctx.session.authorised = null + ctx.redirect('/?msg=you are now logged out') +}) +//changed from get to post and content_search added to dynamically load search page ~by watrasm +router.post('/search', async ctx => { + try { + if (ctx.session.authorised !== true) return ctx.redirect('/login') + const body = ctx.request.body + const query = body.q + + const db = await sqlite.open(dbName) + const searchResult = await db.all(`SELECT * FROM uploads WHERE lower(user) LIKE lower('%${query}%') OR lower(name) LIKE lower('%${query}%') OR lower(artist) LIKE lower('%${query}%');`) + + searchResult.forEach(async result => { + const audioRatings = await db.all(`SELECT "value" FROM audio_ratings WHERE uploadId='${result.uploadID}';`) + let totalRating = sum(audioRatings) + + if (totalRating > 0) + totalRating = totalRating / audioRatings.length + + result.safeRating = Math.floor(totalRating) + result.rating = Math.round(totalRating * 100) / 100 + }) + + await db.close() + + await ctx.render('content_search', { + tracks: searchResult, + }) + } catch (err) { + console.log(err) + await ctx.render('error', { message: err.message }) + } +}) +router.post('/playlistRating', async ctx => { + try { + if (ctx.session.authorised !== true) return ctx.redirect('/login') + const body = ctx.request.body + const newRating = body.num + const playlistId = body.playlistId + + const db = await sqlite.open(dbName) + await db.run(`INSERT INTO playlist_ratings(value, user, playlistId) VALUES(${newRating},'${ctx.session.user}',${playlistId});`) + await db.close() + } catch (err) { + console.log(err) + await ctx.render('error', { message: err.message }) + } +}) +router.post('/commentRating', async ctx => { + try { + if (ctx.session.authorised !== true) return ctx.redirect('/login') + const body = ctx.request.body + const newRating = body.num + const commentId = body.commentId + + const db = await sqlite.open(dbName) + await db.run(`INSERT INTO comment_ratings(value, user, commentId) VALUES(${newRating},'${ctx.session.user}',${commentId});`) + await db.close() + } catch (err) { + console.log(err) + await ctx.render('error', { message: err.message }) + } +}) +router.post('/audioRating', async ctx => { + try { + if (ctx.session.authorised !== true) return ctx.redirect('/login') + const body = ctx.request.body + const newRating = body.num + const songId = body.audioId + + const db = await sqlite.open(dbName) + await db.run(`INSERT INTO audio_ratings(value, user, uploadId) VALUES(${newRating},'${ctx.session.user}',${songId});`) + await db.close() + } catch (err) { + console.log(err) + await ctx.render('error', { message: err.message }) + } +}) //checks if the username or password are invalid and shows the error on login page ~by watrasm -router.post("/checklogin", async ctx => { - try { - const body = ctx.request.body; - const user = await new User(dbName); - let msg = "ok"; - const status = await user.checkLogin(body.user, body.pass); - if (status) { - ctx.session.authorised = true; - ctx.session.user = body.user; - } else { - msg = "Invalid login or password!"; - } - ctx.body = { - status: status, - user: body.user, - pass: body.pass, - msg: msg - }; - return; - } catch (err) { - console.log(err); - await ctx.render("error", { message: err.message }); - } -}); +router.post('/checklogin', async ctx => { + try { + const body = ctx.request.body + const user = await new User(dbName) + let msg = 'ok' + const status = await user.checkLogin(body.user, body.pass) + if (status) { + ctx.session.authorised = true + ctx.session.user = body.user + } else { + msg = 'Invalid login or password!' + } + ctx.body = { + status: status, + user: body.user, + pass: body.pass, + msg: msg + } + return + } catch (err) { + console.log(err) + await ctx.render('error', { message: err.message }) + } +}) function sum(input) { - if (toString.call(input) !== "[object Array]") - return false; - var total = 0; - for(var i=0;i { - try { - if (ctx.session.authorised !== true) return ctx.redirect("/login"); - const body = ctx.request.body; - if (body.genre == "none") { - await ctx.render(body.content); - return; - } - - const db = await sqlite.open(dbName) - - const playlists = await db.all(`SELECT * FROM playlists`) - const userPlaylists = await db.all(`SELECT * FROM playlists WHERE user= '${ctx.session.user}'`) - const uploads = await db.all(`SELECT * FROM uploads`) - - uploads.forEach(async upload => { - const audioRatings = await db.all(`SELECT "value" FROM audio_ratings WHERE uploadId='${upload.uploadID}';`); - let totalRating = sum(audioRatings); - - if (totalRating > 0) - totalRating = totalRating / audioRatings.length; - - upload.safeRating = Math.floor(totalRating); - upload.rating = Math.round(totalRating * 100) / 100; - }); - - - - const songs = await db.all(`SELECT * FROM uploads WHERE genre="${body.genre}";`); - const playlistTracks = await db.all(`SELECT * FROM uploads WHERE playlist LIKE"%${body.genre}%";`); - const playlistComments = await db.all(`SELECT * FROM comments WHERE playlistID="${body.genre}";`); - const playlistRating = await db.all(`SELECT "value" FROM playlist_ratings WHERE playlistId='${body.genre}';`); - - let finalPlaylistRating = sum(playlistRating); - if (finalPlaylistRating > 0) - finalPlaylistRating = Math.round((finalPlaylistRating / playlistRating.length) * 100) / 100; - - songs.forEach(async upload => { - const audioRatings = await db.all(`SELECT "value" FROM audio_ratings WHERE uploadId='${upload.uploadID}';`); - let totalRating = sum(audioRatings); - - if (totalRating > 0) - totalRating = totalRating / audioRatings.length; - - upload.safeRating = Math.floor(totalRating); - upload.rating = Math.round(totalRating * 100) / 100; - }); - - playlistTracks.forEach(async upload => { - const audioRatings = await db.all(`SELECT "value" FROM audio_ratings WHERE uploadId='${upload.uploadID}';`); - let totalRating = sum(audioRatings); - - if (totalRating > 0) - totalRating = totalRating / audioRatings.length; - - upload.safeRating = Math.floor(totalRating); - upload.rating = Math.round(totalRating * 100) / 100; - }); - - playlistComments.forEach(async upload => { - const audioRatings = await db.all(`SELECT "value" FROM comment_ratings WHERE commentID='${upload.commentID}';`); - let totalRating = sum(audioRatings); - - if (totalRating > 0) - totalRating = totalRating / audioRatings.length; - - upload.safeRating = Math.floor(totalRating); - upload.rating = Math.round(totalRating * 100) / 100; - }); - - await db.close() - if (!body.genreTitle) { - body.genreTitle = ""; - } - await ctx.render(body.content, { - genretitle: body.genreTitle.replace("
", " "), - genre: body.genre, - genrerating: finalPlaylistRating, - tracks: songs, - playlistTracks: playlistTracks, - userPlaylists: userPlaylists, - playlist: playlists, - playlistcomments: playlistComments - }); - } catch (err) { - console.log(err); - await ctx.render("error", { message: err.message }); - } +router.post('/loadcontent', async ctx => { + try { + if (ctx.session.authorised !== true) return ctx.redirect('/login') + const body = ctx.request.body + if (body.genre == 'none') { + await ctx.render(body.content) + return + } + + const db = await sqlite.open(dbName) + + const playlists = await db.all('SELECT * FROM playlists') + const userPlaylists = await db.all(`SELECT * FROM playlists WHERE user= '${ctx.session.user}'`) + const uploads = await db.all('SELECT * FROM uploads') + + uploads.forEach(async upload => { + const audioRatings = await db.all(`SELECT "value" FROM audio_ratings WHERE uploadId='${upload.uploadID}';`) + let totalRating = sum(audioRatings) + + if (totalRating > 0) + totalRating = totalRating / audioRatings.length + + upload.safeRating = Math.floor(totalRating) + upload.rating = Math.round(totalRating * 100) / 100 + }) + + + const songs = await db.all(`SELECT * FROM uploads WHERE genre="${body.genre}";`) + const playlistTracks = await db.all(`SELECT * FROM uploads WHERE playlist LIKE"%${body.genre}%";`) + const playlistComments = await db.all(`SELECT * FROM comments WHERE playlistID="${body.genre}";`) + const playlistRating = await db.all(`SELECT "value" FROM playlist_ratings WHERE playlistId='${body.genre}';`) + + let finalPlaylistRating = sum(playlistRating) + if (finalPlaylistRating > 0) + finalPlaylistRating = Math.round(finalPlaylistRating / playlistRating.length * 100) / 100 + + songs.forEach(async upload => { + const audioRatings = await db.all(`SELECT "value" FROM audio_ratings WHERE uploadId='${upload.uploadID}';`) + let totalRating = sum(audioRatings) + + if (totalRating > 0) + totalRating = totalRating / audioRatings.length + + upload.safeRating = Math.floor(totalRating) + upload.rating = Math.round(totalRating * 100) / 100 + }) + + playlistTracks.forEach(async upload => { + const audioRatings = await db.all(`SELECT "value" FROM audio_ratings WHERE uploadId='${upload.uploadID}';`) + let totalRating = sum(audioRatings) + + if (totalRating > 0) + totalRating = totalRating / audioRatings.length + + upload.safeRating = Math.floor(totalRating) + upload.rating = Math.round(totalRating * 100) / 100 + }) + + playlistComments.forEach(async upload => { + const audioRatings = await db.all(`SELECT "value" FROM comment_ratings WHERE commentID='${upload.commentID}';`) + let totalRating = sum(audioRatings) + + if (totalRating > 0) + totalRating = totalRating / audioRatings.length + + upload.safeRating = Math.floor(totalRating) + upload.rating = Math.round(totalRating * 100) / 100 + }) + + await db.close() + if (!body.genreTitle) { + body.genreTitle = '' + } + await ctx.render(body.content, { + genretitle: body.genreTitle.replace('
', ' '), + genre: body.genre, + genrerating: finalPlaylistRating, + tracks: songs, + playlistTracks: playlistTracks, + userPlaylists: userPlaylists, + playlist: playlists, + playlistcomments: playlistComments + }) + } catch (err) { + console.log(err) + await ctx.render('error', { message: err.message }) + } }) /* * The script to upload a new file */ -router.post("/upload", koaBody, async ctx => { - try { - if (ctx.session.authorised !== true) return ctx.redirect("/login"); - const audio = await new Audio(dbName); - const file = ctx.request.files.file; - - if (file.type != "audio/mp3") { - await ctx.render("error", { message: "That is not a song" }); - return; - } - - await audio.upload( - ctx.session.user, - ctx.request.body.songName, - ctx.request.body.genre, - ctx.request.body.artist, - ctx.request.body.album, - ctx.request.body.playlistSelect, - file - ); - ctx.redirect(`/`); - } catch (err) { - console.log(err); - await ctx.render("error", { message: err.message }); - } -}); - -router.post("/listen", koaBody, async ctx => { - try { - const body = ctx.request.body; - const playlists = await new Playlists(dbName); - const file = ctx.request.files.playlist_picture; - const type = file.type; //image/jpeg - - if (type != "image/jpeg" && file.size>0) { - await ctx.render("error", { message: "That is not a jpeg image" }); - return; - } - - await playlists.create(ctx.session.user, body.playlists, file); - reloadPage.contentView = "content_listen" - reloadPage.genre = "listen" - ctx.redirect(`/`); - } catch (err) { - console.log(err); - await ctx.render("error", { message: err.message }); - } -}); - -router.post("/deletePlaylist", koaBody, async ctx => { - try { - const body = ctx.request.body; - const playlists = await new Playlists(dbName); - await playlists.delete(ctx.session.user, ctx.request.body.playlistDelete) - reloadPage.contentView = "content_listen" - reloadPage.genre = "listen" - ctx.redirect(`/`); - } catch (err) { - console.log(err); - await ctx.render("error", { message: err.message }); - } -}); +router.post('/upload', koaBody, async ctx => { + try { + if (ctx.session.authorised !== true) return ctx.redirect('/login') + const audio = await new Audio(dbName) + const file = ctx.request.files.file + + if (file.type != 'audio/mp3') { + await ctx.render('error', { message: 'That is not a song' }) + return + } + + await audio.upload( + ctx.session.user, + ctx.request.body.songName, + ctx.request.body.genre, + ctx.request.body.artist, + ctx.request.body.album, + ctx.request.body.playlistSelect, + file + ) + ctx.redirect('/') + } catch (err) { + console.log(err) + await ctx.render('error', { message: err.message }) + } +}) + +router.post('/listen', koaBody, async ctx => { + try { + const body = ctx.request.body + const playlists = await new Playlists(dbName) + const file = ctx.request.files.playlist_picture + const type = file.type //image/jpeg + + if (type != 'image/jpeg' && file.size>0) { + await ctx.render('error', { message: 'That is not a jpeg image' }) + return + } + + await playlists.create(ctx.session.user, body.playlists, file) + reloadPage.contentView = 'content_listen' + reloadPage.genre = 'listen' + ctx.redirect('/') + } catch (err) { + console.log(err) + await ctx.render('error', { message: err.message }) + } +}) + +router.post('/deletePlaylist', koaBody, async ctx => { + try { + const body = ctx.request.body + const playlists = await new Playlists(dbName) + await playlists.delete(ctx.session.user, ctx.request.body.playlistDelete) + reloadPage.contentView = 'content_listen' + reloadPage.genre = 'listen' + ctx.redirect('/') + } catch (err) { + console.log(err) + await ctx.render('error', { message: err.message }) + } +}) //not deleting the track yet, so far it loads the same page again, will be finished tomorrow -router.post("/deletetrack", async ctx => { - try { - if (ctx.session.authorised !== true) return ctx.redirect("/login"); - const body = ctx.request.body; - - const audio = await new Audio(dbName); - - await audio.delete(body.trackUser, body.trackID, body.trackName); - - //let sql = `SELECT * FROM uploads WHERE genre="${body.genre}";`; - let sql = `SELECT * FROM uploads WHERE genre="${body.genre}";`; - - const db = await sqlite.open(dbName); - const data = await db.all(sql); - await db.close(); - //await ctx.render('pop', {tracks: data}) - - await ctx.render(body.content, { - genretitle: body.genreTitle, - genre: body.genre, - tracks: data, - }); - } catch (err) { - console.log(err); - await ctx.render("error", { message: err.message }); - } +router.post('/deletetrack', async ctx => { + try { + if (ctx.session.authorised !== true) return ctx.redirect('/login') + const body = ctx.request.body + + const audio = await new Audio(dbName) + + await audio.delete(body.trackUser, body.trackID, body.trackName) + + //let sql = `SELECT * FROM uploads WHERE genre="${body.genre}";`; + const sql = `SELECT * FROM uploads WHERE genre="${body.genre}";` + + const db = await sqlite.open(dbName) + const data = await db.all(sql) + await db.close() + //await ctx.render('pop', {tracks: data}) + + await ctx.render(body.content, { + genretitle: body.genreTitle, + genre: body.genre, + tracks: data, + }) + } catch (err) { + console.log(err) + await ctx.render('error', { message: err.message }) + } }) -router.post("/addComment", async ctx => { - try { - const body = ctx.request.body; - const comments = await new Comments(dbName); - await comments.comments(ctx.session.user, body.comments, body.genretitle, body.genre); - - } catch (err) { - console.log(err); - await ctx.render("error", { message: err.message }); - } -}); - -router.post("/deleteComment", async ctx => { - try { - const body = ctx.request.body; - const comments = await new Comments(dbName); - await comments.deleteComments(ctx.request.body.commentID); - - } catch (err) { - console.log(err); - await ctx.render("error", { message: err.message }); - } -}); - -app.use(router.routes()); - -http.createServer(app.callback()).listen(port, "127.0.0.1"); // Force the KOA server to bind to the localhost, restricting access to it from remote. -console.log(`listening on port ${port}`); \ No newline at end of file +router.post('/addComment', async ctx => { + try { + const body = ctx.request.body + const comments = await new Comments(dbName) + await comments.comments(ctx.session.user, body.comments, body.genretitle, body.genre) + + } catch (err) { + console.log(err) + await ctx.render('error', { message: err.message }) + } +}) + +router.post('/deleteComment', async ctx => { + try { + const body = ctx.request.body + const comments = await new Comments(dbName) + await comments.deleteComments(ctx.request.body.commentID) + + } catch (err) { + console.log(err) + await ctx.render('error', { message: err.message }) + } +}) + +app.use(router.routes()) + +http.createServer(app.callback()).listen(port, '127.0.0.1') // Force the KOA server to bind to the localhost, restricting access to it from remote. +console.log(`listening on port ${port}`) diff --git a/modules/accounts.js b/modules/accounts.js index 86b07f0..19be0a9 100644 --- a/modules/accounts.js +++ b/modules/accounts.js @@ -101,21 +101,21 @@ module.exports = class User { throw err } } - //by watrasm - for dynamically loaded login page + //by watrasm - for dynamically loaded login page async checkLogin(username, password) { try { - let sql = `SELECT count(user) AS count FROM users WHERE user="${username}";`; + let sql = `SELECT count(user) AS count FROM users WHERE user="${username}";` const records = await this.db.get(sql) - let result = records.count > 0; + let result = records.count > 0 if (result) { sql = `SELECT pass FROM users WHERE user = "${username}";` const record = await this.db.get(sql) - result = await bcrypt.compare(password, record.pass) + result = await bcrypt.compare(password, record.pass) } - return result; + return result } catch(err) { - return false; + return false } } -} \ No newline at end of file +} diff --git a/modules/audio.js b/modules/audio.js index 7e77ac3..6ca11d7 100644 --- a/modules/audio.js +++ b/modules/audio.js @@ -1,6 +1,6 @@ 'use strict' -const path = require('path'); +const path = require('path') const sqlite = require('sqlite-async') const fs = require('fs-extra') const mime = require('mime-types') @@ -8,18 +8,18 @@ const mime = require('mime-types') module.exports = class Audio { constructor(dbName = ':memory:') { - return (async () => { + return (async() => { this.db = await sqlite.open(dbName) return this })() } async upload(userName, songName, genre, artist, album, playlistSelect, file) { - + if (playlistSelect == undefined) throw new Error('Missing Playlist, please create one on the User Created Playlist page') if (songName.length == 0) throw new Error('Missing Track Name') await this.db.run(`INSERT INTO uploads(name, user, genre, artist, album, playlist) VALUES('${songName}', '${userName}', '${genre}', '${artist}', '${album}', '${playlistSelect}')`) - + const reader = fs.createReadStream(file.path) const stream = fs.createWriteStream(path.join('public/audio', songName += '.mp3')) @@ -27,7 +27,7 @@ module.exports = class Audio { } async delete(userName, uploadID, fileName) { - let sql = `DELETE FROM uploads WHERE uploadID = ${uploadID}` + const sql = `DELETE FROM uploads WHERE uploadID = ${uploadID}` await this.db.run(sql) if (fs.existsSync(`public/audio/${fileName}.mp3`)) { @@ -36,4 +36,4 @@ module.exports = class Audio { } -} \ No newline at end of file +} diff --git a/modules/comments.js b/modules/comments.js index 8bd84dc..4a9b095 100644 --- a/modules/comments.js +++ b/modules/comments.js @@ -1,12 +1,12 @@ 'use strict' -const path = require('path'); +const path = require('path') const sqlite = require('sqlite-async') const fs = require('fs-extra') module.exports = class Comments { constructor(dbName = ':memory:') { - return (async () => { + return (async() => { this.db = await sqlite.open(dbName) await this.db.run(`CREATE TABLE IF NOT EXISTS "comments" ( "playlistID" INTEGER, @@ -26,22 +26,22 @@ module.exports = class Comments { async comments(user, comments, playlist, playlistID) { try { if (comments.length === 0) throw new Error('missing comment') - let sql = `INSERT INTO comments(user, comments, playlist, playlistID) VALUES("${user}", "${comments}", "${playlist}", "${playlistID}")` + const sql = `INSERT INTO comments(user, comments, playlist, playlistID) VALUES("${user}", "${comments}", "${playlist}", "${playlistID}")` await this.db.run(sql) } catch (err) { throw err } - } + } -async deleteComments(commentID) { - try { - let sql = `DELETE FROM comments WHERE commentID = ${commentID}` - await this.db.run(sql) + async deleteComments(commentID) { + try { + const sql = `DELETE FROM comments WHERE commentID = ${commentID}` + await this.db.run(sql) - } catch (err) { - throw err - } + } catch (err) { + throw err + } } -} \ No newline at end of file +} diff --git a/modules/playlists.js b/modules/playlists.js index b93bcbc..131b5cd 100644 --- a/modules/playlists.js +++ b/modules/playlists.js @@ -1,12 +1,12 @@ 'use strict' -const path = require('path'); +const path = require('path') const sqlite = require('sqlite-async') const fs = require('fs-extra') module.exports = class Playlists { constructor(dbName = ':memory:') { - return (async () => { + return (async() => { this.db = await sqlite.open(dbName) return this })() @@ -15,16 +15,16 @@ module.exports = class Playlists { async create(user, playlists, file) { try { if (playlists.length === 0) throw new Error('missing playlist name') - let sql = `INSERT INTO playlists(user, name) VALUES("${user}", "${playlists}")` + const sql = `INSERT INTO playlists(user, name) VALUES("${user}", "${playlists}")` const saveResult = await this.db.run(sql) - let imageName = saveResult.lastID.toString() + '.jpg'; + const imageName = `${saveResult.lastID.toString() }.jpg` if (file.size > 0) { const reader = fs.createReadStream(file.path) const stream = fs.createWriteStream(path.join('public/audio', imageName)) reader.pipe(stream) } else { - fs.copy("public/playlist_image/note.jpg", path.join('public/audio', imageName)) + fs.copy('public/playlist_image/note.jpg', path.join('public/audio', imageName)) } @@ -33,7 +33,7 @@ module.exports = class Playlists { } } -/* async comment(user, comment, playlist) { + /* async comment(user, comment, playlist) { try { if (comment.length === 0) throw new Error('missing comment') let sql = `INSERT INTO comment(user, comment, playlistID) VALUES("${user}", "${comment}", "${playlist})` @@ -46,13 +46,13 @@ module.exports = class Playlists { */ async delete(user, playlists) { try { - let sql = `DELETE FROM playlists WHERE playlistID =${playlists}` + const sql = `DELETE FROM playlists WHERE playlistID =${playlists}` await this.db.run(`DELETE FROM comments WHERE playlistID =${playlists}`) const deleteResult = await this.db.run(sql) - let imageName = playlists.toString() + '.jpg' + const imageName = `${playlists.toString() }.jpg` fs.remove(path.join('public/audio', imageName)) - + } catch (err) { throw err