1
0
mirror of https://github.com/xuthus83/pigallery2.git synced 2025-01-14 14:43:17 +08:00

improving routing

This commit is contained in:
Braun Patrik 2016-03-26 19:24:12 +01:00
parent b8963eb3e4
commit f8db033c56
10 changed files with 43 additions and 21 deletions

View File

@ -10,10 +10,10 @@ import {UserRoles} from "../../common/entities/User";
export class AuthenticationMWs { export class AuthenticationMWs {
public static authenticate(req:Request, res:Response, next:NextFunction){ public static authenticate(req:Request, res:Response, next:NextFunction){
if (typeof req.session.user === 'undefined') { /* if (typeof req.session.user === 'undefined') {
return next(new Error(ErrorCodes.NOT_AUTHENTICATED)); return next(new Error(ErrorCodes.NOT_AUTHENTICATED));
} }*/
return next(); return next();
} }

View File

@ -9,7 +9,9 @@ import {Directory} from "../../common/entities/Directory";
export class GalleryMWs { export class GalleryMWs {
public static listDirectory(req:Request, res:Response, next:NextFunction){ public static listDirectory(req:Request, res:Response, next:NextFunction){
console.log("listDirectory");
console.log(req.params);
let directoryName = "/"; let directoryName = "/";
if(req.params.directory){ if(req.params.directory){
directoryName = req.params.directory; directoryName = req.params.directory;
@ -30,16 +32,14 @@ export class GalleryMWs {
} }
public static loadImage(req:Request, res:Response, next:NextFunction){ public static loadImage(req:Request, res:Response, next:NextFunction){
let directoryName = "/"; console.log("loadImage");
if(req.params.directory){ console.log(req.params);
directoryName = req.params.directory; if(!(req.params.imagePath)){
}
if(!(req.params.image)){
return next(); return next();
} }
let fullImagePath = path.join(__dirname,"/../../demo/images", directoryName,req.params.image); let fullImagePath = path.join(__dirname,"/../../demo/images", req.params.imagePath);
if(fs.statSync(fullImagePath).isDirectory()){ if(fs.statSync(fullImagePath).isDirectory()){
return next(); return next();
} }

View File

@ -7,16 +7,16 @@ import {RenderingMWs} from "../middlewares/RenderingMWs";
export class GalleryRouter{ export class GalleryRouter{
constructor(private app){ constructor(private app){
this.addDirectoryList();
this.addGetImageThumbnail(); this.addGetImageThumbnail();
this.addGetImage(); this.addGetImage();
this.addDirectoryList();
this.addSearch(); this.addSearch();
this.addAutoComplete(); this.addAutoComplete();
} }
private addDirectoryList() { private addDirectoryList() {
this.app.get(["/api/gallery/:directory","/api/gallery/","/api/gallery//"], this.app.get(["/api/gallery/:directory(*)","/api/gallery/","/api/gallery//"],
AuthenticationMWs.authenticate, AuthenticationMWs.authenticate,
GalleryMWs.listDirectory, GalleryMWs.listDirectory,
RenderingMWs.renderResult RenderingMWs.renderResult
@ -25,7 +25,7 @@ export class GalleryRouter{
private addGetImage() { private addGetImage() {
this.app.get(["/api/gallery/:directory/:image","/api/gallery/:image"], this.app.get(["/api/gallery/:imagePath(*\.(jpg|bmp|png|gif|jpeg))"],
AuthenticationMWs.authenticate, AuthenticationMWs.authenticate,
GalleryMWs.loadImage, GalleryMWs.loadImage,
RenderingMWs.renderFile RenderingMWs.renderFile

View File

@ -12,7 +12,7 @@ export class PublicRouter{
var renderIndex = (req: _express.Request, res: _express.Response) => { var renderIndex = (req: _express.Request, res: _express.Response) => {
res.sendFile(_path.resolve(__dirname, './../../frontend/index.html')); res.sendFile(_path.resolve(__dirname, './../../frontend/index.html'));
}; };
this.app.get(['/login',"/gallery"], renderIndex); this.app.get(['/login',"/gallery*"], renderIndex);
} }

View File

@ -51,7 +51,20 @@ export class Server {
*/ */
// for parsing application/json // for parsing application/json
this.app.use(_bodyParser.json()); this.app.use(_bodyParser.json());
this.app.use("/testDir/:img(*\.(jpg|bmp))",(req,res,next)=>{
console.log(req.params);
res.send(req.params);
});
this.app.use("/testDir/:dir(*)",(req,res,next)=>{
console.log(req.params);
res.send(req.params);
});
this.app.use("/testDir/*",(req,res,next)=>{
console.log(req.params);
res.send(req.params);
});
new PublicRouter(this.app); new PublicRouter(this.app);

View File

@ -11,6 +11,7 @@ import {Router, Location} from "angular2/router";
import {HTTP_PROVIDERS} from "angular2/http"; import {HTTP_PROVIDERS} from "angular2/http";
import {UserService} from "./model/user.service"; import {UserService} from "./model/user.service";
import {GalleryService} from "./gallery/gallery.service"; import {GalleryService} from "./gallery/gallery.service";
import {GeneratedUrl} from "angular2/src/router/rules/route_paths/route_path";
@ -32,10 +33,13 @@ import {GalleryService} from "./gallery/gallery.service";
name: 'Login', name: 'Login',
component: LoginComponent, component: LoginComponent,
useAsDefault: true useAsDefault: true
}, },
{ {
path: '/gallery/:directory', regex: '/gallery(/([\S]*))?',
name: 'Gallery', name: 'Gallery',
serializer: (params): GeneratedUrl => {
return new GeneratedUrl(`/gallery/${params['directory']}`, {})
},
component: GalleryComponent component: GalleryComponent
} }
]) ])

View File

@ -1 +1 @@
<a [routerLink]="['Gallery',{directory: directory.name}]">{{directory.name}}</a> <a [routerLink]="['Gallery',{directory: getDirectoryPath()}]">{{directory.name}}</a>

View File

@ -3,6 +3,7 @@
import {Component, Input, OnInit} from 'angular2/core'; import {Component, Input, OnInit} from 'angular2/core';
import {Directory} from "../../../../common/entities/Directory"; import {Directory} from "../../../../common/entities/Directory";
import {RouterLink} from "angular2/router"; import {RouterLink} from "angular2/router";
import {Utils} from "../../../../common/Utils";
@Component({ @Component({
selector: 'gallery-directory', selector: 'gallery-directory',
@ -15,6 +16,10 @@ export class GalleryDirectoryComponent{
constructor() { constructor() {
} }
getDirectoryPath(){
return Utils.concatUrls(this.directory.path,this.directory.name);
}
} }

View File

@ -33,6 +33,7 @@ export class GalleryComponent implements OnInit{
this._router.navigate(['Login']); this._router.navigate(['Login']);
return; return;
} }
console.log(this._params.get('directory'));
let directoryName = this._params.get('directory'); let directoryName = this._params.get('directory');
directoryName = directoryName ? directoryName : ""; directoryName = directoryName ? directoryName : "";
this._galleryService.getDirectory(directoryName).then(( message:Message<Directory>) => { this._galleryService.getDirectory(directoryName).then(( message:Message<Directory>) => {

View File

@ -1,6 +1,6 @@
///<reference path="../../../browser.d.ts"/> ///<reference path="../../../browser.d.ts"/>
import {Component, Input, OnInit} from 'angular2/core'; import {Component, Input} from 'angular2/core';
import {Photo} from "../../../../common/entities/Photo"; import {Photo} from "../../../../common/entities/Photo";
import {Directory} from "../../../../common/entities/Directory"; import {Directory} from "../../../../common/entities/Directory";
import {Utils} from "../../../../common/Utils"; import {Utils} from "../../../../common/Utils";
@ -17,7 +17,6 @@ export class GalleryPhotoComponent{
} }
getPhotoPath(){ getPhotoPath(){
console.log(Utils.concatUrls("/api/gallery",this.directory.path,this.directory.name,this.photo.name));
return Utils.concatUrls("/api/gallery",this.directory.path,this.directory.name,this.photo.name); return Utils.concatUrls("/api/gallery",this.directory.path,this.directory.name,this.photo.name);
} }