1
0
mirror of https://github.com/bpatrik/pigallery2.git synced 2025-06-02 23:17:43 +02:00

adding client side config

Implementing search disable
This commit is contained in:
Braun Patrik 2016-05-10 21:33:58 +02:00
parent 4caa499faf
commit f53c537a9d
12 changed files with 152 additions and 53 deletions

View File

@ -1,23 +1,18 @@
import {ConfigLoader} from "./ConfigLoader"; ///<reference path="../../typings/main.d.ts"/>
import {ConfigLoader} from "./../../backend/config/ConfigLoader";
import * as path from "path"; import * as path from "path";
import {ConfigClass, DatabaseType} from "../../common/config/Config";
export enum DatabaseType{ export var Config = new ConfigClass();
memory, mongoDB
}
export class ConfigClass { Config.Server = {
port: 80,
thumbnailSizes: [200],
imagesFolder: "/demo/images",
thumbnailFolder: "/demo/TEMP",
databaseType: DatabaseType.mongoDB
};
constructor() { ConfigLoader.init(Config, path.join(__dirname, './../../config.json'), [["PORT", "Server-port"]]);
ConfigLoader.init(this, path.join(__dirname, './../../config.json'));
}
public PORT:number = 80;
public thumbnailSizes:Array<number> = [200];
public imagesFolder:string = "/demo/images";
public thumbnailFolder:string = "/demo/TEMP";
public databaseType:DatabaseType = DatabaseType.mongoDB;
}
export var Config = new ConfigClass();

View File

@ -3,14 +3,21 @@ import * as optimist from "optimist";
export class ConfigLoader { export class ConfigLoader {
static init(configObject:any, configFilePath?:string) { static init(configObject:any, configFilePath?:string, envAlias:Array<Array<string>> = []) {
this.processConfigFile(configFilePath, configObject); this.processConfigFile(configFilePath, configObject);
this.processArguments(configObject); this.processArguments(configObject);
this.processEnvVariables(configObject); this.processEnvVariables(configObject, envAlias);
} }
private static processEnvVariables(configObject:any) { private static processEnvVariables(configObject:any, envAlias:Array<Array<string>>) {
let varAliases = {};
envAlias.forEach((alias)=> {
if (process.env[alias[0]]) {
varAliases[alias[1]] = process.env[alias[0]];
}
});
this.processHierarchyVar(configObject, varAliases);
this.loadObject(configObject, process.env); this.loadObject(configObject, process.env);
}; };
@ -18,11 +25,15 @@ export class ConfigLoader {
let argv = optimist.argv; let argv = optimist.argv;
delete(argv._); delete(argv._);
delete(argv.$0); delete(argv.$0);
this.processHierarchyVar(configObject, argv);
};
private static processHierarchyVar(configObject:any, vars:any) {
let config = {}; let config = {};
Object.keys(argv).forEach((key)=> { Object.keys(vars).forEach((key)=> {
let keyArray = key.split("-"); let keyArray = key.split("-");
let value = argv[key]; let value = vars[key];
let setObject = (object, keyArray, value) => { let setObject = (object, keyArray, value) => {
let key = keyArray.shift(); let key = keyArray.shift();
@ -37,7 +48,7 @@ export class ConfigLoader {
}); });
this.loadObject(configObject, config); this.loadObject(configObject, config);
}; }
private static processConfigFile(configFilePath:string, configObject:any) { private static processConfigFile(configFilePath:string, configObject:any) {
if (typeof configFilePath !== 'undefined') { if (typeof configFilePath !== 'undefined') {

View File

@ -3,18 +3,18 @@ import * as fs from "fs";
import {NextFunction, Request, Response} from "express"; import {NextFunction, Request, Response} from "express";
import {Error, ErrorCodes} from "../../common/entities/Error"; import {Error, ErrorCodes} from "../../common/entities/Error";
import {Directory} from "../../common/entities/Directory"; import {Directory} from "../../common/entities/Directory";
import {Config} from "../config/Config";
import {ObjectManagerRepository} from "../model/ObjectManagerRepository"; import {ObjectManagerRepository} from "../model/ObjectManagerRepository";
import {AutoCompleteItem} from "../../common/entities/AutoCompleteItem"; import {AutoCompleteItem} from "../../common/entities/AutoCompleteItem";
import {ContentWrapper} from "../../common/entities/ConentWrapper"; import {ContentWrapper} from "../../common/entities/ConentWrapper";
import {SearchResult} from "../../common/entities/SearchResult"; import {SearchResult} from "../../common/entities/SearchResult";
import {Photo} from "../../common/entities/Photo"; import {Photo} from "../../common/entities/Photo";
import {Config} from "../config/Config";
export class GalleryMWs { export class GalleryMWs {
private static getImageFolder() { private static getImageFolder() {
return path.join(__dirname, "/../../", Config.imagesFolder); return path.join(__dirname, "/../../", Config.Server.imagesFolder);
} }
public static listDirectory(req:Request, res:Response, next:NextFunction) { public static listDirectory(req:Request, res:Response, next:NextFunction) {
@ -58,7 +58,14 @@ export class GalleryMWs {
public static search(req:Request, res:Response, next:NextFunction) { public static search(req:Request, res:Response, next:NextFunction) {
if (Config.Client.Search.searchEnabled === false) {
return next();
}
if (!(req.params.text)) {
return next();
}
ObjectManagerRepository.getInstance().getSearchManager().search(req.params.text, (err, result:SearchResult) => { ObjectManagerRepository.getInstance().getSearchManager().search(req.params.text, (err, result:SearchResult) => {
if (err || !result) { if (err || !result) {
return next(new Error(ErrorCodes.GENERAL_ERROR, err)); return next(new Error(ErrorCodes.GENERAL_ERROR, err));
@ -70,6 +77,10 @@ export class GalleryMWs {
public static instantSearch(req:Request, res:Response, next:NextFunction) { public static instantSearch(req:Request, res:Response, next:NextFunction) {
if (Config.Client.Search.instantSearchEnabled === false) {
return next();
}
if (!(req.params.text)) { if (!(req.params.text)) {
return next(); return next();
} }
@ -84,6 +95,9 @@ export class GalleryMWs {
} }
public static autocomplete(req:Request, res:Response, next:NextFunction) { public static autocomplete(req:Request, res:Response, next:NextFunction) {
if (Config.Client.Search.autocompleteEnabled === false) {
return next();
}
if (!(req.params.text)) { if (!(req.params.text)) {
return next(); return next();
} }

View File

@ -13,7 +13,7 @@ import {Config} from "../config/Config";
export class ThumbnailGeneratorMWs { export class ThumbnailGeneratorMWs {
private static getThumbnailFolder() { private static getThumbnailFolder() {
return path.join(__dirname, "/../../", Config.thumbnailFolder); return path.join(__dirname, "/../../", Config.Server.thumbnailFolder);
} }
public static generateThumbnail(req:Request, res:Response, next:NextFunction) { public static generateThumbnail(req:Request, res:Response, next:NextFunction) {
@ -21,11 +21,11 @@ export class ThumbnailGeneratorMWs {
return next(); return next();
let imagePath = req.resultPipe; let imagePath = req.resultPipe;
let size:number = parseInt(req.params.size) || Config.thumbnailSizes[0]; let size:number = parseInt(req.params.size) || Config.Server.thumbnailSizes[0];
let thumbnailFolder = ThumbnailGeneratorMWs.getThumbnailFolder(); let thumbnailFolder = ThumbnailGeneratorMWs.getThumbnailFolder();
if (Config.thumbnailSizes.indexOf(size) === -1) { if (Config.Server.thumbnailSizes.indexOf(size) === -1) {
size = Config.thumbnailSizes[0]; size = Config.Server.thumbnailSizes[0];
} }
let thPath = path.join(thumbnailFolder, ThumbnailGeneratorMWs.generateThumbnailName(imagePath, size)); let thPath = path.join(thumbnailFolder, ThumbnailGeneratorMWs.generateThumbnailName(imagePath, size));

View File

@ -4,6 +4,7 @@ import * as _express from "express";
import {NextFunction, Request, Response} from "express"; import {NextFunction, Request, Response} from "express";
import * as _path from "path"; import * as _path from "path";
import {Utils} from "../../common/Utils"; import {Utils} from "../../common/Utils";
import {Config} from "../config/Config";
export class PublicRouter { export class PublicRouter {
constructor(private app) { constructor(private app) {
@ -16,6 +17,7 @@ export class PublicRouter {
delete user.password; delete user.password;
res.tpl.user = user; res.tpl.user = user;
} }
res.tpl.clientConfig = Config.Client;
return next(); return next();
}); });

View File

@ -11,9 +11,10 @@ import {GalleryRouter} from "./routes/GalleryRouter";
import {AdminRouter} from "./routes/AdminRouter"; import {AdminRouter} from "./routes/AdminRouter";
import {ErrorRouter} from "./routes/ErrorRouter"; import {ErrorRouter} from "./routes/ErrorRouter";
import {SharingRouter} from "./routes/SharingRouter"; import {SharingRouter} from "./routes/SharingRouter";
import {Config, DatabaseType} from "./config/Config"; import {DatabaseType} from "./../common/config/Config";
import {ObjectManagerRepository} from "./model/ObjectManagerRepository"; import {ObjectManagerRepository} from "./model/ObjectManagerRepository";
import {DatabaseManager} from "./model/mongoose/DatabaseManager"; import {DatabaseManager} from "./model/mongoose/DatabaseManager";
import {Config} from "./config/Config";
export class Server { export class Server {
@ -21,7 +22,6 @@ export class Server {
private debug:any; private debug:any;
private app:any; private app:any;
private server:any; private server:any;
private port:number;
constructor() { constructor() {
@ -56,7 +56,7 @@ export class Server {
this.app.use(_bodyParser.json()); this.app.use(_bodyParser.json());
if (Config.databaseType === DatabaseType.memory) { if (Config.Server.databaseType === DatabaseType.memory) {
ObjectManagerRepository.MemoryMongoManagers(); ObjectManagerRepository.MemoryMongoManagers();
} else { } else {
ObjectManagerRepository.InitMongoManagers(); ObjectManagerRepository.InitMongoManagers();
@ -64,8 +64,8 @@ export class Server {
()=> { ()=> {
console.error("MongoDB connection error. Falling back to memory Object Managers"); console.error("MongoDB connection error. Falling back to memory Object Managers");
ObjectManagerRepository.MemoryMongoManagers(); ObjectManagerRepository.MemoryMongoManagers();
Config.setDatabaseType(DatabaseType.memory);
}); });
} }
new PublicRouter(this.app); new PublicRouter(this.app);
@ -79,13 +79,13 @@ export class Server {
// Get PORT from environment and store in Express. // Get PORT from environment and store in Express.
this.app.set('port', Config.PORT); this.app.set('port', Config.Server.port);
// Create HTTP server. // Create HTTP server.
this.server = _http.createServer(this.app); this.server = _http.createServer(this.app);
//Listen on provided PORT, on all network interfaces. //Listen on provided PORT, on all network interfaces.
this.server.listen(Config.PORT); this.server.listen(Config.Server.port);
this.server.on('error', this.onError); this.server.on('error', this.onError);
this.server.on('listening', this.onListening); this.server.on('listening', this.onListening);
@ -101,9 +101,9 @@ export class Server {
throw error; throw error;
} }
var bind = typeof Config.PORT === 'string' var bind = typeof Config.Server.port === 'string'
? 'Pipe ' + Config.PORT ? 'Pipe ' + Config.Server.port
: 'Port ' + Config.PORT; : 'Port ' + Config.Server.port;
// handle specific listen error with friendly messages // handle specific listen error with friendly messages
switch (error.code) { switch (error.code) {

44
common/config/Config.ts Normal file
View File

@ -0,0 +1,44 @@
export enum DatabaseType{
memory, mongoDB
}
interface ServerConfig {
port:number;
thumbnailSizes:Array<number>;
imagesFolder:string;
thumbnailFolder:string;
databaseType:DatabaseType;
}
interface SearchConfig {
searchEnabled:boolean
instantSearchEnabled:boolean
autocompleteEnabled:boolean
}
interface ClientConfig {
Search:SearchConfig;
}
export class ConfigClass {
public Server:ServerConfig = null;
public Client:ClientConfig = {
Search: {
searchEnabled: true,
instantSearchEnabled: true,
autocompleteEnabled: true
}
};
public setDatabaseType(type:DatabaseType) {
this.Server.databaseType = type;
if (type === DatabaseType.memory) {
this.Client.Search.searchEnabled = false;
this.Client.Search.instantSearchEnabled = false;
this.Client.Search.autocompleteEnabled = false;
}
}
}

View File

@ -0,0 +1,12 @@
import {ConfigClass} from "../../../common/config/Config";
import {Utils} from "../../../common/Utils";
declare module ServerInject {
export var ConfigInject;
}
export var Config = new ConfigClass();
if (typeof ServerInject !== "undefined" && typeof ServerInject.ConfigInject !== "undefined") {
Utils.updateKeys(Config.Client, ServerInject.ConfigInject);
}

View File

@ -1,7 +1,7 @@
<gallery-lightbox #lightbox></gallery-lightbox> <gallery-lightbox #lightbox></gallery-lightbox>
<app-frame> <app-frame>
<div navbar> <div navbar>
<gallery-search></gallery-search> <gallery-search *ngIf="showSearchBar"></gallery-search>
</div> </div>
<div body class="container" style="width: 100%; padding:0" *ngIf="_galleryService.content.directory"> <div body class="container" style="width: 100%; padding:0" *ngIf="_galleryService.content.directory">
<div *ngFor="let directory of _galleryService.content.directory.directories"> <div *ngFor="let directory of _galleryService.content.directory.directories">

View File

@ -9,6 +9,7 @@ import {GalleryGridComponent} from "./grid/grid.gallery.component";
import {FrameComponent} from "../frame/frame.component"; import {FrameComponent} from "../frame/frame.component";
import {GalleryLightboxComponent} from "./lightbox/lightbox.gallery.component"; import {GalleryLightboxComponent} from "./lightbox/lightbox.gallery.component";
import {GallerySearchComponent} from "./search/search.gallery.component"; import {GallerySearchComponent} from "./search/search.gallery.component";
import {Config} from "../config/Config";
@Component({ @Component({
selector: 'gallery', selector: 'gallery',
@ -23,10 +24,14 @@ import {GallerySearchComponent} from "./search/search.gallery.component";
export class GalleryComponent implements OnInit { export class GalleryComponent implements OnInit {
public showSearchBar:boolean = true;
constructor(private _galleryService:GalleryService, constructor(private _galleryService:GalleryService,
private _params:RouteParams, private _params:RouteParams,
private _authService:AuthenticationService, private _authService:AuthenticationService,
private _router:Router) { private _router:Router) {
this.showSearchBar = Config.Client.Search.searchEnabled;
} }
ngOnInit() { ngOnInit() {

View File

@ -6,6 +6,7 @@ import {AutoCompleteItem} from "../../../../common/entities/AutoCompleteItem";
import {Message} from "../../../../common/entities/Message"; import {Message} from "../../../../common/entities/Message";
import {GalleryService} from "../gallery.service"; import {GalleryService} from "../gallery.service";
import {FORM_DIRECTIVES} from "@angular/common"; import {FORM_DIRECTIVES} from "@angular/common";
import {Config} from "../../config/Config";
@Component({ @Component({
selector: 'gallery-search', selector: 'gallery-search',
@ -25,13 +26,20 @@ export class GallerySearchComponent {
onSearchChange(event:KeyboardEvent) { onSearchChange(event:KeyboardEvent) {
let searchText = (<HTMLInputElement>event.target).value; let searchText = (<HTMLInputElement>event.target).value;
this.autocomplete(searchText);
this._galleryService.instantSearch(searchText); if (Config.Client.Search.autocompleteEnabled) {
this.autocomplete(searchText);
}
if (Config.Client.Search.instantSearchEnabled) {
this._galleryService.instantSearch(searchText);
}
} }
public onSearch() { public onSearch() {
this._galleryService.search(this.searchText); if (Config.Client.Search.searchEnabled) {
this._galleryService.search(this.searchText);
}
} }
public search(item:AutoCompleteItem) { public search(item:AutoCompleteItem) {
@ -40,13 +48,7 @@ export class GallerySearchComponent {
this.onSearch(); this.onSearch();
} }
private showSuggestions(suggestions:Array<AutoCompleteItem>, searchText:string) {
this.emptyAutoComplete();
suggestions.forEach((item)=> {
let renderItem = new AutoCompleteRenderItem(item.text, searchText);
this.autoCompleteItems.push(renderItem);
});
}
public onFocusLost(event) { public onFocusLost(event) {
this.autoCompleteItems = []; this.autoCompleteItems = [];
@ -61,6 +63,9 @@ export class GallerySearchComponent {
} }
private autocomplete(searchText:string) { private autocomplete(searchText:string) {
if (!Config.Client.Search.autocompleteEnabled) {
return
}
if (searchText.trim().length > 0) { if (searchText.trim().length > 0) {
this._autoCompleteService.autoComplete(searchText).then((message:Message<Array<AutoCompleteItem>>) => { this._autoCompleteService.autoComplete(searchText).then((message:Message<Array<AutoCompleteItem>>) => {
if (message.error) { if (message.error) {
@ -75,6 +80,14 @@ export class GallerySearchComponent {
} }
} }
private showSuggestions(suggestions:Array<AutoCompleteItem>, searchText:string) {
this.emptyAutoComplete();
suggestions.forEach((item)=> {
let renderItem = new AutoCompleteRenderItem(item.text, searchText);
this.autoCompleteItems.push(renderItem);
});
}
} }
class AutoCompleteRenderItem { class AutoCompleteRenderItem {

View File

@ -1,7 +1,7 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
<head> <head>
<base href="/" /> <base href="/"/>
<meta charset="UTF-8"> <meta charset="UTF-8">
<title>PiGallery2</title> <title>PiGallery2</title>
<link rel="shortcut icon" href="icon.png"> <link rel="shortcut icon" href="icon.png">
@ -15,7 +15,10 @@
<pi-gallery2-app>Loading...</pi-gallery2-app> <pi-gallery2-app>Loading...</pi-gallery2-app>
</body> </body>
<script> <script>
var ServerInject = {user: <%- JSON.stringify(user)%>} var ServerInject = {
user: <%- JSON.stringify(user); %>,
ConfigInject: <%- JSON.stringify(clientConfig); %>
}
</script> </script>
<script <script