From 0c8dbd7c6290266323fd89928f11bef0307ae47b Mon Sep 17 00:00:00 2001 From: "Patrik J. Braun" Date: Fri, 30 Nov 2018 15:36:42 +0100 Subject: [PATCH] improving typings --- README.md | 5 ++--- backend/Logger.ts | 8 ++++---- backend/middlewares/SharingMWs.ts | 3 ++- backend/middlewares/user/AuthenticationMWs.ts | 11 ++++++----- backend/routes/SharingRouter.ts | 3 ++- backend/server.ts | 3 ++- common/QueryParams.ts | 5 ++++- frontend/app/app.routing.ts | 3 ++- frontend/app/gallery/gallery.component.ts | 4 ++-- frontend/app/gallery/gallery.service.ts | 13 ++++++++----- frontend/app/gallery/grid/grid.gallery.component.ts | 7 ++++--- .../gallery/lightbox/lightbox.gallery.component.ts | 8 ++++---- frontend/app/gallery/share.service.ts | 6 +++--- frontend/app/model/query.service.ts | 7 +++---- package.json | 12 ++++++------ 15 files changed, 54 insertions(+), 44 deletions(-) diff --git a/README.md b/README.md index 8a3e0bc..32ef2a8 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,9 @@ # PiGallery2 [![npm version](https://badge.fury.io/js/pigallery2.svg)](https://badge.fury.io/js/pigallery2) [![Build Status](https://travis-ci.org/bpatrik/pigallery2.svg?branch=master)](https://travis-ci.org/bpatrik/pigallery2) -[![Coverage Status](https://coveralls.io/repos/github/bpatrik/PiGallery2/badge.svg?branch=master)](https://coveralls.io/github/bpatrik/PiGallery2?branch=master) [![Heroku](https://heroku-badge.herokuapp.com/?app=pigallery2&style=flat)](https://pigallery2.herokuapp.com) -[![Dependency Status](https://david-dm.org/bpatrik/pigallery2.svg)](https://david-dm.org/bpatrik/pigallery2) -[![devDependency Status](https://david-dm.org/bpatrik/pigallery2/dev-status.svg)](https://david-dm.org/bpatrik/pigallery2#info=devDependencies) +[![dependencies Status](https://david-dm.org/bpatrik/pigallery2/status.svg)](https://david-dm.org/bpatrik/pigallery2) +[![devDependencies Status](https://david-dm.org/bpatrik/pigallery2/dev-status.svg)](https://david-dm.org/bpatrik/pigallery2?type=dev) This is a directory-first photo gallery website, optimised for running on low resource servers (especially on raspberry pi) diff --git a/backend/Logger.ts b/backend/Logger.ts index 441d1eb..42aaaee 100644 --- a/backend/Logger.ts +++ b/backend/Logger.ts @@ -2,7 +2,7 @@ import * as winston from 'winston'; export const winstonSettings = { transports: [ - new winston.transports.Console({ + new winston.transports.Console({ level: process.env.NODE_ENV === 'production' ? 'info' : 'silly', handleExceptions: true, json: false, @@ -11,9 +11,9 @@ export const winstonSettings = { return (new Date()).toLocaleString(); }, label: 'innerLabel', - formatter: (options) => { + formatter: (options: any) => { // Return string will be passed to logger. - return options.timestamp() + '[' + winston['config']['colorize'](options.level, options.level.toUpperCase()) + '] ' + + return options.timestamp() + '[' + (winston)['config']['colorize'](options.level, options.level.toUpperCase()) + '] ' + (undefined !== options.message ? options.message : '') + (options.meta && Object.keys(options.meta).length ? '\n\t' + JSON.stringify(options.meta) : ''); }, @@ -23,4 +23,4 @@ export const winstonSettings = { exitOnError: false }; -export const Logger = new winston.Logger(winstonSettings); +export const Logger = new (winston).Logger(winstonSettings); diff --git a/backend/middlewares/SharingMWs.ts b/backend/middlewares/SharingMWs.ts index af0486f..3f10f5e 100644 --- a/backend/middlewares/SharingMWs.ts +++ b/backend/middlewares/SharingMWs.ts @@ -3,6 +3,7 @@ import {CreateSharingDTO, SharingDTO} from '../../common/entities/SharingDTO'; import {ObjectManagerRepository} from '../model/ObjectManagerRepository'; import {ErrorCodes, ErrorDTO} from '../../common/entities/Error'; import {Config} from '../../common/config/private/Config'; +import {QueryParams} from '../../common/QueryParams'; const LOG_TAG = '[SharingMWs]'; @@ -24,7 +25,7 @@ export class SharingMWs { if (Config.Client.Sharing.enabled === false) { return next(); } - const sharingKey = req.params.sharingKey; + const sharingKey = req.params[QueryParams.gallery.sharingKey_long]; try { req.resultPipe = await ObjectManagerRepository.getInstance().SharingManager.findOne({sharingKey: sharingKey}); diff --git a/backend/middlewares/user/AuthenticationMWs.ts b/backend/middlewares/user/AuthenticationMWs.ts index a65fdab..1c43b43 100644 --- a/backend/middlewares/user/AuthenticationMWs.ts +++ b/backend/middlewares/user/AuthenticationMWs.ts @@ -6,6 +6,7 @@ import {ObjectManagerRepository} from '../../model/ObjectManagerRepository'; import {Config} from '../../../common/config/private/Config'; import {PasswordHelper} from '../../model/PasswordHelper'; import {Utils} from '../../../common/Utils'; +import {QueryParams} from '../../../common/QueryParams'; export class AuthenticationMWs { @@ -48,7 +49,7 @@ export class AuthenticationMWs { if (req.session.rememberMe === true) { req.sessionOptions.expires = new Date(Date.now() + Config.Server.sessionTimeout); } else { - delete(req.sessionOptions.expires); + delete (req.sessionOptions.expires); } return next(); } @@ -83,7 +84,7 @@ export class AuthenticationMWs { return next(); } // not enough parameter - if ((!req.query.sk && !req.params.sharingKey)) { + if ((!req.query[QueryParams.gallery.sharingKey_short] && !req.params[QueryParams.gallery.sharingKey_long])) { return next(new ErrorDTO(ErrorCodes.INPUT_ERROR, 'no sharing key provided')); } @@ -91,7 +92,7 @@ export class AuthenticationMWs { const password = (req.body ? req.body.password : null) || null; const sharing = await ObjectManagerRepository.getInstance().SharingManager.findOne({ - sharingKey: req.query.sk || req.params.sharingKey, + sharingKey: req.query[QueryParams.gallery.sharingKey_short] || req.params[QueryParams.gallery.sharingKey_long] }); console.log(sharing); @@ -154,9 +155,9 @@ export class AuthenticationMWs { private static async getSharingUser(req: Request) { if (Config.Client.Sharing.enabled === true && - (!!req.query.sk || !!req.params.sharingKey)) { + (!!req.params[QueryParams.gallery.sharingKey_short] || !!req.params[QueryParams.gallery.sharingKey_long])) { const sharing = await ObjectManagerRepository.getInstance().SharingManager.findOne({ - sharingKey: req.query.sk || req.params.sharingKey, + sharingKey: req.query[QueryParams.gallery.sharingKey_short] || req.params[QueryParams.gallery.sharingKey_long], }); if (!sharing || sharing.expires < Date.now()) { return null; diff --git a/backend/routes/SharingRouter.ts b/backend/routes/SharingRouter.ts index 05511ea..3446536 100644 --- a/backend/routes/SharingRouter.ts +++ b/backend/routes/SharingRouter.ts @@ -3,6 +3,7 @@ import {UserRoles} from '../../common/entities/UserDTO'; import {RenderingMWs} from '../middlewares/RenderingMWs'; import {SharingMWs} from '../middlewares/SharingMWs'; import * as express from 'express'; +import {QueryParams} from '../../common/QueryParams'; export class SharingRouter { public static route(app: express.Express) { @@ -22,7 +23,7 @@ export class SharingRouter { } private static addGetSharing(app: express.Express) { - app.get('/api/share/:sharingKey', + app.get('/api/share/:' + QueryParams.gallery.sharingKey_long, AuthenticationMWs.authenticate, AuthenticationMWs.authorise(UserRoles.LimitedGuest), SharingMWs.getSharing, diff --git a/backend/server.ts b/backend/server.ts index 8eb79b2..de16208 100644 --- a/backend/server.ts +++ b/backend/server.ts @@ -2,6 +2,7 @@ import * as _express from 'express'; import * as _bodyParser from 'body-parser'; import * as cookieParser from 'cookie-parser'; import * as _http from 'http'; +// @ts-ignore import * as locale from 'locale'; import {PublicRouter} from './routes/PublicRouter'; import {UserRouter} from './routes/UserRouter'; @@ -68,7 +69,7 @@ export class Server { constructor() { if (!(process.env.NODE_ENV === 'production')) { - Logger.debug(LOG_TAG, 'Running in DEBUG mode'); + Logger.debug(LOG_TAG, 'Running in DEBUG mode, set env variable NODE_ENV=production to disable '); } this.init(); } diff --git a/common/QueryParams.ts b/common/QueryParams.ts index edb6639..1939eb3 100644 --- a/common/QueryParams.ts +++ b/common/QueryParams.ts @@ -8,6 +8,9 @@ export const QueryParams = { toDate: 'toDate', minResolution: 'fromRes', maxResolution: 'toRes' - } + }, + photo: 'p', + sharingKey_short: 'sk', + sharingKey_long: 'sharingKey' } }; diff --git a/frontend/app/app.routing.ts b/frontend/app/app.routing.ts index cb2b4e2..2c273f4 100644 --- a/frontend/app/app.routing.ts +++ b/frontend/app/app.routing.ts @@ -4,6 +4,7 @@ import {LoginComponent} from './login/login.component'; import {GalleryComponent} from './gallery/gallery.component'; import {AdminComponent} from './admin/admin.component'; import {ShareLoginComponent} from './sharelogin/share-login.component'; +import {QueryParams} from '../../common/QueryParams'; const ROUTES: Routes = [ { @@ -31,7 +32,7 @@ const ROUTES: Routes = [ component: GalleryComponent }, { - path: 'share/:sharingKey', + path: 'share/:' + QueryParams.gallery.sharingKey_long, component: GalleryComponent }, {path: '', redirectTo: '/login', pathMatch: 'full'}, diff --git a/frontend/app/gallery/gallery.component.ts b/frontend/app/gallery/gallery.component.ts index 5dd8b2b..f98141d 100644 --- a/frontend/app/gallery/gallery.component.ts +++ b/frontend/app/gallery/gallery.component.ts @@ -16,7 +16,7 @@ import {ContentWrapper} from '../../../common/entities/ConentWrapper'; import {PageHelper} from '../model/page.helper'; import {SortingMethods} from '../../../common/entities/SortingMethods'; import {PhotoDTO} from '../../../common/entities/PhotoDTO'; -import {QueryService} from '../model/query.service'; +import {QueryParams} from '../../../common/QueryParams'; @Component({ selector: 'app-gallery', @@ -90,7 +90,7 @@ export class GalleryComponent implements OnInit, OnDestroy { if (params['sharingKey'] && params['sharingKey'] !== '') { const sharing = await this.shareService.getSharing(); const qParams: { [key: string]: any } = {}; - qParams[QueryService.SHARING_KEY] = this.shareService.getSharingKey(); + qParams[QueryParams.gallery.sharingKey_short] = this.shareService.getSharingKey(); this._router.navigate(['/gallery', sharing.path], {queryParams: qParams}).catch(console.error); return; } diff --git a/frontend/app/gallery/gallery.service.ts b/frontend/app/gallery/gallery.service.ts index ca58e57..1d9adeb 100644 --- a/frontend/app/gallery/gallery.service.ts +++ b/frontend/app/gallery/gallery.service.ts @@ -10,7 +10,7 @@ import {Config} from '../../../common/config/public/Config'; import {ShareService} from './share.service'; import {NavigationService} from '../model/navigation.service'; import {SortingMethods} from '../../../common/entities/SortingMethods'; -import {QueryService} from '../model/query.service'; +import {QueryParams} from '../../../common/QueryParams'; @Injectable() @@ -25,7 +25,7 @@ export class GalleryService { constructor(private networkService: NetworkService, private galleryCacheService: GalleryCacheService, private _shareService: ShareService, - private navigatoinService: NavigationService) { + private navigationService: NavigationService) { this.content = new BehaviorSubject(new ContentWrapper()); this.sorting = new BehaviorSubject(Config.Client.Other.defaultPhotoSortingMethod); } @@ -44,13 +44,15 @@ export class GalleryService { content.directory = this.galleryCacheService.getDirectory(directoryName); content.searchResult = null; + console.log(content.directory); + this.content.next(content); this.lastRequest.directory = directoryName; const params: { [key: string]: any } = {}; if (Config.Client.Sharing.enabled === true) { if (this._shareService.isSharing()) { - params[QueryService.SHARING_KEY] = this._shareService.getSharingKey(); + params[QueryParams.gallery.sharingKey_short] = this._shareService.getSharingKey(); } } @@ -82,8 +84,9 @@ export class GalleryService { this.content.next(cw); - }).catch(() => { - this.navigatoinService.toGallery(); + }).catch((e) => { + console.error(e); + this.navigationService.toGallery(); }); } diff --git a/frontend/app/gallery/grid/grid.gallery.component.ts b/frontend/app/gallery/grid/grid.gallery.component.ts index f933659..36edd27 100644 --- a/frontend/app/gallery/grid/grid.gallery.component.ts +++ b/frontend/app/gallery/grid/grid.gallery.component.ts @@ -26,6 +26,7 @@ import {QueryService} from '../../model/query.service'; import {GalleryService} from '../gallery.service'; import {SortingMethods} from '../../../../common/entities/SortingMethods'; import {MediaDTO} from '../../../../common/entities/MediaDTO'; +import {QueryParams} from '../../../../common/QueryParams'; @Component({ selector: 'app-gallery-grid', @@ -73,13 +74,13 @@ export class GalleryGridComponent implements OnChanges, OnInit, AfterViewInit, O ngOnInit() { this.subscriptions.route = this.route.queryParams.subscribe((params: Params) => { - if (params[QueryService.PHOTO_PARAM] && params[QueryService.PHOTO_PARAM] !== '') { - this.delayedRenderUpToPhoto = params[QueryService.PHOTO_PARAM]; + if (params[QueryParams.gallery.photo] && params[QueryParams.gallery.photo] !== '') { + this.delayedRenderUpToPhoto = params[QueryParams.gallery.photo]; if (!this.photos || this.photos.length === 0) { return; } - this.renderUpToPhoto(params[QueryService.PHOTO_PARAM]); + this.renderUpToPhoto(params[QueryParams.gallery.photo]); } }); this.subscriptions.sorting = this.galleryService.sorting.subscribe(() => { diff --git a/frontend/app/gallery/lightbox/lightbox.gallery.component.ts b/frontend/app/gallery/lightbox/lightbox.gallery.component.ts index c1b652e..21bd4ac 100644 --- a/frontend/app/gallery/lightbox/lightbox.gallery.component.ts +++ b/frontend/app/gallery/lightbox/lightbox.gallery.component.ts @@ -10,7 +10,6 @@ import { QueryList, ViewChild } from '@angular/core'; -import {PhotoDTO} from '../../../../common/entities/PhotoDTO'; import {GalleryPhotoComponent} from '../grid/photo/photo.grid.gallery.component'; import {Dimension} from '../../model/IRenderable'; import {FullScreenService} from '../fullscreen.service'; @@ -23,6 +22,7 @@ import {ActivatedRoute, Params, Router} from '@angular/router'; import {PageHelper} from '../../model/page.helper'; import {QueryService} from '../../model/query.service'; import {MediaDTO} from '../../../../common/entities/MediaDTO'; +import {QueryParams} from '../../../../common/QueryParams'; export enum LightboxStates { Open = 1, @@ -82,11 +82,11 @@ export class GalleryLightboxComponent implements OnDestroy, OnInit { ngOnInit(): void { this.timer = timer(1000, 2000); this.subscription.route = this.route.queryParams.subscribe((params: Params) => { - if (params[QueryService.PHOTO_PARAM] && params[QueryService.PHOTO_PARAM] !== '') { + if (params[QueryParams.gallery.photo] && params[QueryParams.gallery.photo] !== '') { if (!this.gridPhotoQL) { - return this.delayedPhotoShow = params[QueryService.PHOTO_PARAM]; + return this.delayedPhotoShow = params[QueryParams.gallery.photo]; } - this.onNavigateTo(params[QueryService.PHOTO_PARAM]); + this.onNavigateTo(params[QueryParams.gallery.photo]); } else if (this.status === LightboxStates.Open) { this.delayedPhotoShow = null; this.hideLigthbox(); diff --git a/frontend/app/gallery/share.service.ts b/frontend/app/gallery/share.service.ts index 3ca4056..0a33465 100644 --- a/frontend/app/gallery/share.service.ts +++ b/frontend/app/gallery/share.service.ts @@ -3,7 +3,7 @@ import {NetworkService} from '../model/network/network.service'; import {CreateSharingDTO, SharingDTO} from '../../../common/entities/SharingDTO'; import {Router, RoutesRecognized} from '@angular/router'; import {BehaviorSubject} from 'rxjs'; -import {QueryService} from '../model/query.service'; +import {QueryParams} from '../../../common/QueryParams'; @Injectable() export class ShareService { @@ -28,8 +28,8 @@ export class ShareService { this.router.events.subscribe(val => { if (val instanceof RoutesRecognized) { - this.param = val.state.root.firstChild.params['sharingKey'] || null; - this.queryParam = val.state.root.firstChild.queryParams[QueryService.SHARING_KEY] || null; + this.param = val.state.root.firstChild.params[QueryParams.gallery.sharingKey_long] || null; + this.queryParam = val.state.root.firstChild.queryParams[QueryParams.gallery.sharingKey_short] || null; const changed = this.sharingKey !== this.param || this.queryParam; if (changed) { this.sharingKey = this.param || this.queryParam; diff --git a/frontend/app/model/query.service.ts b/frontend/app/model/query.service.ts index 4713a2f..a13b733 100644 --- a/frontend/app/model/query.service.ts +++ b/frontend/app/model/query.service.ts @@ -2,12 +2,11 @@ import {Injectable} from '@angular/core'; import {ShareService} from '../gallery/share.service'; import {PhotoDTO} from '../../../common/entities/PhotoDTO'; import {MediaDTO} from '../../../common/entities/MediaDTO'; +import {QueryParams} from '../../../common/QueryParams'; @Injectable() export class QueryService { - public static readonly PHOTO_PARAM = 'p'; - public static readonly SHARING_KEY = 'sk'; constructor(private shareService: ShareService) { } @@ -15,10 +14,10 @@ export class QueryService { getParams(media?: MediaDTO): { [key: string]: string } { const query: { [key: string]: string } = {}; if (media) { - query[QueryService.PHOTO_PARAM] = media.name; + query[QueryParams.gallery.photo] = media.name; } if (this.shareService.isSharing()) { - query[QueryService.SHARING_KEY] = this.shareService.getSharingKey(); + query[QueryParams.gallery.sharingKey_short] = this.shareService.getSharingKey(); } return query; } diff --git a/package.json b/package.json index 4b687bb..1a56b06 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "main": "./backend/index.js", "bin": "./backend/index.js", "scripts": { - "install": "tsc && gulp build-prod", + "installx": "tsc && gulp build-prod", "build-release": "gulp build-release", "pretest": "tsc", "test": "ng test && mocha --recursive test/backend/unit && mocha --recursive test/backend/integration && mocha --recursive test/common/unit ", @@ -34,7 +34,7 @@ "cookie-session": "2.0.0-beta.3", "ejs": "2.6.1", "express": "4.16.4", - "fluent-ffmpeg": "^2.1.2", + "fluent-ffmpeg": "2.1.2", "jimp": "0.5.6", "locale": "0.1.0", "reflect-metadata": "0.1.12", @@ -64,16 +64,16 @@ "@ngx-translate/i18n-polyfill": "1.0.0", "@types/bcryptjs": "2.4.2", "@types/chai": "4.1.7", - "@types/cookie-parser": "^1.4.1", + "@types/cookie-parser": "1.4.1", "@types/cookie-session": "2.0.36", - "@types/ejs": "^2.6.0", + "@types/ejs": "2.6.0", "@types/express": "4.16.0", - "@types/fluent-ffmpeg": "^2.1.8", + "@types/fluent-ffmpeg": "2.1.8", "@types/gm": "1.18.2", "@types/jasmine": "3.3.0", "@types/node": "10.12.10", "@types/sharp": "0.21.0", - "@types/winston": "2.4.4", + "@types/winston": "2.3.9", "bootstrap": "4.1.3", "chai": "4.2.0", "codelyzer": "4.5.0",