Skip to content

Commit

Permalink
Merge pull request #182 from WKHAllen/dev
Browse files Browse the repository at this point in the history
Release 1.2.5
  • Loading branch information
WKHAllen authored Aug 10, 2020
2 parents 491bcb3 + f198ab6 commit d6c69b4
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 27 deletions.
9 changes: 6 additions & 3 deletions src/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@ function logProxyError(err: Error) {
console.log('Error:', err.message);
}

export function proxy(res: Response, url: string, method: HttpMethod = 'GET', callback?: () => void) {
export function proxy(res: Response, url: string, method: HttpMethod = 'GET', callback?: (err: any) => void) {
const options = {
method: method
};

fetch(url, options)
.then(proxyRes => {
proxyRes.body.pipe(res);
if (callback) callback();
if (callback) callback(null);
})
.catch(err => logProxyError(err));
.catch(err => {
logProxyError(err);
if (callback) callback(err);
});
}
16 changes: 12 additions & 4 deletions src/routes/image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ router.get('/book/:bookId', (req: Request, res: Response, next: NextFunction) =>
services.BookService.getBookInfo(req.params.bookId, (bookInfo) => {
if (bookInfo) {
if (bookInfo.imageurl) {
proxy(res, smallerImageURL(bookInfo.imageurl));
proxy(res, smallerImageURL(bookInfo.imageurl), 'GET', (err) => {
if (err) {
res.redirect('/img/favicon-gray.png');
}
});
} else {
res.redirect('/img/favicon.png');
res.redirect('/img/favicon-gray.png');
}
} else {
next(); // 404
Expand All @@ -25,9 +29,13 @@ router.get('/user/:userId', (req: Request, res: Response, next: NextFunction) =>
services.UserService.getUserInfoByUserId(req.params.userId, (userInfo) => {
if (userInfo) {
if (userInfo.imageurl) {
proxy(res, smallerImageURL(userInfo.imageurl));
proxy(res, smallerImageURL(userInfo.imageurl), 'GET', (err) => {
if (err) {
res.redirect('/img/favicon-gray.png');
}
});
} else {
res.redirect('/img/favicon.png');
res.redirect('/img/favicon-gray.png');
}
} else {
next(); // 404
Expand Down
20 changes: 10 additions & 10 deletions src/routes/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,55 +249,55 @@ export function smallerImageURL(imageUrl: string, width: number = 300) {
// Authorize/authenticate
export function auth(req: Request, res: Response, next: NextFunction) {
if (!req.session || !req.session.sessionId) {
return res.status(401).render('401', { title: 'Permission denied', after: req.originalUrl });
return renderPage(req, res, '401', { title: 'Permission denied', after: req.originalUrl }, 401);
} else {
services.AuthService.auth(req.session.sessionId, (valid) => {
if (valid) next();
else return res.status(401).render('401', { title: 'Permission denied', after: req.originalUrl });
else return renderPage(req, res, '401', { title: 'Permission denied', after: req.originalUrl }, 401);
});
}
}

// Authenticate an admin
export function adminAuth (req: Request, res: Response, next: NextFunction) {
export function adminAuth(req: Request, res: Response, next: NextFunction) {
if (!req.session || !req.session.sessionId) {
return res.status(401).render('401', { title: 'Permission denied', after: req.originalUrl });
return renderPage(req, res, '401', { title: 'Permission denied', after: req.originalUrl }, 401);
} else {
services.AuthService.auth(req.session.sessionId, (valid) => {
if (valid) {
services.AuthService.getAuthUser(req.session.sessionId, (userId) => {
services.UserService.isAdmin(userId, (admin) => {
if (admin) next();
else return res.status(401).render('not-admin', { title: 'Permission denied', after: req.originalUrl });
else return renderPage(req, res, 'not-admin', { title: 'Permission denied', after: req.originalUrl }, 401);
});
});
} else {
return res.status(401).render('401', { title: 'Permission denied', after: req.originalUrl });
return renderPage(req, res, '401', { title: 'Permission denied', after: req.originalUrl }, 401);
}
});
}
}

// Render a page
export function renderPage(req: Request, res: Response, page: string, options: any) {
export function renderPage(req: Request, res: Response, page: string, options: any, status: number = 200) {
options = options || {};
options.url = req.originalUrl;
services.MetaService.getMeta('Version', (version) => {
options.version = version;
if (!req.session || !req.session.sessionId) {
options.loggedIn = false;
res.render(page, options);
return res.status(status).render(page, options);
} else {
services.MiscService.getNavInfo(req.session.sessionId, (result) => {
if (!result) {
options.loggedIn = false;
res.render(page, options);
return res.status(status).render(page, options);
} else {
options.loggedIn = true;
options.userId = result.userid;
options.userFirstName = result.firstname;
options.admin = result.admin;
res.render(page, options);
return res.status(status).render(page, options);
}
});
}
Expand Down
4 changes: 2 additions & 2 deletions src/services/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export module AdminService {
export function getUsers(orderBy: string, orderDirection: string, callback?: rowsCallback) {
var sql = `
SELECT
firstname, lastname, email, joinTimestamp,
userId, firstname, lastname, email, joinTimestamp,
itemsListed, itemsSold, moneyMade
FROM NBUser
WHERE verified = 1
Expand All @@ -103,7 +103,7 @@ export module AdminService {
export function getBooks(orderBy: string, orderDirection: string, callback?: rowsCallback) {
var sql = `
SELECT
bookId, title, author,
bookId, title, author, NBUser.userId AS userId,
CONCAT(NBUser.firstname, ' ', NBUser.lastname) AS listedBy,
Department.name as department,
courseNumber, price, listedTimestamp
Expand Down
Binary file added src/static/img/favicon-gray.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/views/admin-books.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ <h1 class="centered">Books</h1>
<tr>
<td><a href="/book/{{this.bookid}}">{{this.title}}</a></td>
<td>{{this.author}}</td>
<td>{{this.listedby}}</td>
<td><a href="/user/{{this.userid}}">{{this.listedby}}</a></td>
<td>{{this.department}}</td>
<td>{{this.coursenumber}}</td>
<td>${{this.price}}</td>
Expand Down
4 changes: 2 additions & 2 deletions src/views/admin-users.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ <h1 class="centered">Users</h1>
<tbody>
{{#each users}}
<tr>
<td>{{this.firstname}}</td>
<td>{{this.lastname}}</td>
<td><a href="/user/{{this.userid}}">{{this.firstname}}</a></td>
<td><a href="/user/{{this.userid}}">{{this.lastname}}</a></td>
<td>{{this.email}}</td>
<td class="timestamp">{{this.jointimestamp}}</td>
<td>{{this.itemslisted}}</td>
Expand Down
8 changes: 4 additions & 4 deletions src/views/book.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div class="background-image" id="show">
<div class="container">
<a href="/" class="btn btn-light mb-2"><i class="fas fa-chevron-left mr-2"></i>Back</a>
<button class="btn btn-light mb-2" onclick="window.location.href = document.referrer || '/';"><i class="fas fa-chevron-left mr-2"></i>Back</button>
<div class="content-card">
<div class="row">
<!-- Image -->
Expand Down Expand Up @@ -47,17 +47,17 @@ <h6 class="mt-4">Description:</h6>
<small class="photo-creds centered d-block mt-2">photo by <a href="https://unsplash.com/@henry_be" target="_blank">Henry Be</a> via <a href="https://unsplash.com/photos/lc7xcWebECc" target="_blank">Unsplash</a></small>

</div>
<div id="blur">

</div>
<div id="blur"></div>
<div class="blurred-modal" id="request">
<div class="space-between">
<h5>Request to buy</h5>
<span><button class="close-modal" onclick="hideModal('request')"><i class="fas fa-times"></i></button></span>
</div>
<h6>You can contact <b><a href="/user/{{ownerUserId}}">{{firstname}} {{lastname}}</a></b> via:</h6>
<a class="btn btn-outline-info ml-3" id="contact-link" href="" target="_blank">
{{#if loggedIn}}
<span id="contact-icon"></span> <span id="platform">{{contactPlatform}}</span>: <span id="contact-value">{{contactInfo}}</span>
{{/if}}
</a>
</div>

Expand Down
2 changes: 1 addition & 1 deletion src/views/layouts/default.html
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
</li>
{{else}}
<li class="nav-item">
<a class="nav-link" href="/login">Login</a>
<a class="nav-link" href="/login?after={{url}}">Login</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/register">Sign Up</a>
Expand Down

0 comments on commit d6c69b4

Please sign in to comment.