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

implementing gallery photo rendering

This commit is contained in:
Braun Patrik 2016-03-20 16:54:30 +01:00
parent d6cd1798b5
commit 9f55214e38
16 changed files with 119 additions and 28 deletions

View File

@ -34,23 +34,20 @@ export class AuthenticationMWs extends BaseMWs{
public static login(req:Request, res:Response, next:NextFunction){ public static login(req:Request, res:Response, next:NextFunction){
//not enough parameter //not enough parameter
if ((typeof req.body === 'undefined') || (typeof req.body.logincredential === 'undefined') || (typeof req.body.logincredential.username === 'undefined') || if ((typeof req.body === 'undefined') || (typeof req.body.loginCredential === 'undefined') || (typeof req.body.loginCredential.username === 'undefined') ||
(typeof req.body.logincredential.password === 'undefined')) { (typeof req.body.loginCredential.password === 'undefined')) {
return next(); return next();
} }
//lets find the user //lets find the user
UserManager.findOne({ UserManager.findOne({
username: req.body.logincredential.username username: req.body.loginCredential.username,
password: req.body.loginCredential.password
}, (err, result) => { }, (err, result) => {
if ((err) || (!result)) { if ((err) || (!result)) {
return super.renderError(res,new Error(ErrorCodes.CREDENTIAL_NOT_FOUND)); return super.renderError(res,new Error(ErrorCodes.CREDENTIAL_NOT_FOUND));
} }
//check password
if (result.password !== req.body.logincredential.password) {
return super.renderError(res,new Error(ErrorCodes.CREDENTIAL_NOT_FOUND));
}
req.session.user = result; req.session.user = result;
@ -61,6 +58,10 @@ export class AuthenticationMWs extends BaseMWs{
public static renderUser(req:Request, res:Response, next:NextFunction){ public static renderUser(req:Request, res:Response, next:NextFunction){
if(!(req.session.user)){
return super.renderError(res,new Error(ErrorCodes.GENERAL_ERROR));
}
let user = Utils.clone(req.session.user); let user = Utils.clone(req.session.user);
delete user.password; delete user.password;
super.renderMessage(res,user); super.renderMessage(res,user);

View File

@ -16,7 +16,7 @@ export class GalleryRouter{
private addDirectoryList() { private addDirectoryList() {
this.app.get(["/api/gallery/:directory","/api/gallery/"], this.app.get(["/api/gallery/:directory","/api/gallery/"],
// AuthenticationMWs.authenticate, AuthenticationMWs.authenticate,
GalleryMWs.listDirectory GalleryMWs.listDirectory
); );
}; };
@ -24,7 +24,7 @@ export class GalleryRouter{
private addGetImage() { private addGetImage() {
this.app.get(["/api/gallery/:directory/:image","/api/gallery/:image"], this.app.get(["/api/gallery/:directory/:image","/api/gallery/:image"],
// AuthenticationMWs.authenticate, AuthenticationMWs.authenticate,
GalleryMWs.renderImage GalleryMWs.renderImage
); );
}; };

View File

@ -10,7 +10,7 @@ export class PublicRouter{
this.app.use('/node_modules',_express.static(_path.resolve(__dirname, './../../node_modules'))); this.app.use('/node_modules',_express.static(_path.resolve(__dirname, './../../node_modules')));
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

@ -2,6 +2,7 @@
import * as _express from 'express'; import * as _express from 'express';
import * as _session from 'express-session'; import * as _session from 'express-session';
import * as _bodyParser from 'body-parser';
import * as _debug from 'debug'; import * as _debug from 'debug';
import * as _http from 'http'; import * as _http from 'http';
import {PublicRouter} from "./routes/PublicRouter"; import {PublicRouter} from "./routes/PublicRouter";
@ -39,7 +40,12 @@ export class Server {
resave: true, resave: true,
saveUninitialized: false saveUninitialized: false
})); }));
/**
* Parse parameters in POST
*/
// for parsing application/json
this.app.use(_bodyParser.json());
new PublicRouter(this.app); new PublicRouter(this.app);
new UserRouter(this.app); new UserRouter(this.app);

View File

@ -1,5 +1,5 @@
export class LoginCredential{ export class LoginCredential{
constructor(public username?:string, public password?:string){ constructor(public username:string = null, public password:string = null){
} }
} }

View File

@ -10,6 +10,7 @@ import {User} from "../../common/entities/User";
import {Router, Location} from "angular2/router"; 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";
@ -22,6 +23,7 @@ import {UserService} from "./model/user.service";
HTTP_PROVIDERS, HTTP_PROVIDERS,
ROUTER_PROVIDERS, ROUTER_PROVIDERS,
UserService, UserService,
GalleryService,
AuthenticationService AuthenticationService
] ]
}) })

View File

@ -1 +1,4 @@
<h1>Gallery</h1> <h1>Gallery</h1>
<div *ngIf="directory" *ngFor="#photo of directory.photos" >
<gallery-photo *ngIf="photo" [photo]="photo" >a photo</gallery-photo>
</div>

View File

@ -2,15 +2,26 @@
import {Component, OnInit} from 'angular2/core'; import {Component, OnInit} from 'angular2/core';
import {AuthenticationService} from "../model/authentication.service"; import {AuthenticationService} from "../model/authentication.service";
import {Router,Location} from "angular2/router"; import {Router, Location, RouteParams} from "angular2/router";
import {GalleryService} from "./gallery.service";
import {Directory} from "../../../common/entities/Directory";
import {Message} from "../../../common/entities/Message";
import {GalleryPhotoComponent} from "./photo/photo.gallery.component";
@Component({ @Component({
selector: 'gallery', selector: 'gallery',
templateUrl: 'app/gallery/gallery.component.html' templateUrl: 'app/gallery/gallery.component.html',
directives:[GalleryPhotoComponent]
}) })
export class GalleryComponent implements OnInit{ export class GalleryComponent implements OnInit{
constructor(private _authService: AuthenticationService, private _router: Router, private _location:Location) { directory:Directory = new Directory(-1,"","/",new Date(),[],[]);
constructor(private _galleryService:GalleryService,
private _params: RouteParams,
private _authService: AuthenticationService,
private _router: Router,
private _location:Location) {
} }
@ -18,7 +29,18 @@ export class GalleryComponent implements OnInit{
if (!this._authService.isAuthenticated()) { if (!this._authService.isAuthenticated()) {
this._location.replaceState('/'); // clears browser history so they can't navigate with back button this._location.replaceState('/'); // clears browser history so they can't navigate with back button
this._router.navigate(['Login']); this._router.navigate(['Login']);
return;
} }
let directoryName = this._params.get('directory');
directoryName = directoryName ? directoryName : "";
this._galleryService.getDirectory(directoryName).then(( message:Message<Directory>) => {
if(message.errors){
//TODO: implement
return;
}
this.directory = message.result;
});
} }
} }

View File

@ -0,0 +1,25 @@
///<reference path="../../browser.d.ts"/>
import {Injectable} from 'angular2/core';
import {NetworkService} from "../model/network.service";
import {Http} from "angular2/http";
import {Message} from "../../../common/entities/Message";
import {Directory} from "../../../common/entities/Directory";
@Injectable()
export class GalleryService extends NetworkService{
constructor(_http:Http){
super(_http);
}
public getDirectory(directoryName:string): Promise<Message<Directory>>{
return this.getJson("/gallery/"+directoryName);
}
}

View File

@ -0,0 +1 @@
<img [src]="getPhotoPath()" width="200px">

View File

@ -0,0 +1,21 @@
///<reference path="../../../browser.d.ts"/>
import {Component, Input, OnInit} from 'angular2/core';
import {Photo} from "../../../../common/entities/Photo";
@Component({
selector: 'gallery-photo',
templateUrl: 'app/gallery/photo/photo.gallery.component.html'
})
export class GalleryPhotoComponent{
@Input() photo: Photo;
constructor() {
}
getPhotoPath(){
return "/api/gallery/"+this.photo.name;
}
}

View File

@ -23,7 +23,7 @@ export class LoginComponent implements OnInit{
} }
} }
onLogin(){ onLogin(){
this._authService.login(this.loginCredential); this._authService.login(this.loginCredential);
} }
} }

View File

@ -20,7 +20,7 @@ export class AuthenticationService{
public login(credential:LoginCredential){ public login(credential:LoginCredential){
this._userService.login(credential).then( (message:Message<User>) =>{ this._userService.login(credential).then( (message:Message<User>) =>{
console.log(message); console.log(message);
if(message.errors && message.errors.length > 0){ if(message.errors){
console.log(message.errors); console.log(message.errors);
}else{ }else{
this._user = message.result; this._user = message.result;

View File

@ -11,30 +11,37 @@ export class NetworkService{
constructor(protected _http:Http){ constructor(protected _http:Http){
} }
private callJson(method:string, url:string, data:any = {}){ private callJson<T>(method:string, url:string, data:any = {}): Promise<T>{
let body = JSON.stringify({ data }); let body = JSON.stringify( data );
let headers = new Headers({ 'Content-Type': 'application/json' }); let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers }); let options = new RequestOptions({ headers: headers });
console.log(this._http.post(this._baseUrl+url, body, options));
if(method == "get"){
return this._http[method](this._baseUrl+url, options)
.toPromise()
.then(res => <Message<any>> res.json())
.catch(NetworkService.handleError);
}
return this._http[method](this._baseUrl+url, body, options) return this._http[method](this._baseUrl+url, body, options)
.toPromise() .toPromise()
.then(res => <Message<any>> res.json()) .then(res => <Message<any>> res.json())
.catch(NetworkService.handleError); .catch(NetworkService.handleError);
} }
protected postJson(url:string, data:any = {}){ protected postJson<T>(url:string, data:any = {}): Promise<T>{
return this.callJson("post",url,data); return this.callJson("post",url,data);
} }
protected putJson(url:string, data:any = {}){ protected putJson<T>(url:string, data:any = {}): Promise<T>{
return this.callJson("put",url,data); return this.callJson("put",url,data);
} }
protected getJson(url:string, data:any = {}){ protected getJson<T>(url:string): Promise<T>{
return this.callJson("get",url,data); return this.callJson("get",url);
} }
protected deleteJson(url:string, data:any = {}){ protected deleteJson<T>(url:string, data:any = {}): Promise<T>{
return this.callJson("delete",url,data); return this.callJson("delete",url,data);
} }

View File

@ -4,6 +4,8 @@ import {Injectable} from 'angular2/core';
import {LoginCredential} from "../../../common/entities/LoginCredential"; import {LoginCredential} from "../../../common/entities/LoginCredential";
import {Http} from "angular2/http"; import {Http} from "angular2/http";
import {NetworkService} from "./network.service"; import {NetworkService} from "./network.service";
import {User} from "../../../common/entities/User";
import {Message} from "../../../common/entities/Message";
@Injectable() @Injectable()
export class UserService extends NetworkService{ export class UserService extends NetworkService{
@ -15,8 +17,8 @@ export class UserService extends NetworkService{
} }
public login(credential:LoginCredential){ public login(credential:LoginCredential): Promise<Message<User>>{
return this.postJson("/user/login",credential); return this.postJson("/user/login",{"loginCredential": credential});
} }

View File

@ -22,6 +22,7 @@
}, },
"dependencies": { "dependencies": {
"angular2": "^2.0.0-beta.11", "angular2": "^2.0.0-beta.11",
"body-parser": "^1.15.0",
"core-js": "^2.2.1", "core-js": "^2.2.1",
"debug": "^2.2.0", "debug": "^2.2.0",
"es6-promise": "^3.1.2", "es6-promise": "^3.1.2",