diff --git a/backend/middlewares/GalleryMWs.ts b/backend/middlewares/GalleryMWs.ts index 22596461..f6544397 100644 --- a/backend/middlewares/GalleryMWs.ts +++ b/backend/middlewares/GalleryMWs.ts @@ -1,22 +1,51 @@ -import {UserManager} from "../model/UserManager"; +import * as path from 'path'; +import * as fs from 'fs'; import {NextFunction, Request, Response} from "express"; import {BaseMWs} from "./BaseMWs"; import {Error, ErrorCodes} from "../../common/entities/Error"; -import Util = jasmine.Util; +import {GalleryManager} from "../model/GalleryManager"; +import {Directory} from "../../common/entities/Directory"; export class GalleryMWs extends BaseMWs{ public static listDirectory(req:Request, res:Response, next:NextFunction){ - //TODO: implement - return super.renderError(res,new Error(ErrorCodes.GENERAL_ERROR)); + let directoryName = "/"; + if(req.params.directory){ + directoryName = req.params.directory; + } + + let absoluteDirectoryName = path.join(__dirname,"/../../demo/images", directoryName); + if(!fs.statSync(absoluteDirectoryName).isDirectory()){ + return next(); + } + + GalleryManager.listDirectory(directoryName,(err,directory:Directory) => { + if(err || !directory){ + return super.renderError(res,new Error(ErrorCodes.GENERAL_ERROR,err)); + } + + return super.renderMessage(res,directory); + }); } public static renderImage(req:Request, res:Response, next:NextFunction){ - //TODO: implement - return super.renderError(res,new Error(ErrorCodes.GENERAL_ERROR)); + let directoryName = "/"; + if(req.params.directory){ + directoryName = req.params.directory; + } + if(!(req.params.image)){ + return next(); + } + + let fullImagePath = path.join(__dirname,"/../../demo/images", directoryName,req.params.image); + if(fs.statSync(fullImagePath).isDirectory()){ + return next(); + } + + return res.sendFile(fullImagePath); } public static renderThumbnail(req:Request, res:Response, next:NextFunction){ diff --git a/backend/middlewares/UserMWs.ts b/backend/middlewares/UserMWs.ts index 14fad145..5574e0f5 100644 --- a/backend/middlewares/UserMWs.ts +++ b/backend/middlewares/UserMWs.ts @@ -3,7 +3,6 @@ import {UserManager} from "../model/UserManager"; import {NextFunction, Request, Response} from "express"; import {BaseMWs} from "./BaseMWs"; import {Error, ErrorCodes} from "../../common/entities/Error"; -import Util = jasmine.Util; export class UserMWs extends BaseMWs{ diff --git a/backend/model/GalleryManager.ts b/backend/model/GalleryManager.ts index 0ef077bd..c6e9d309 100644 --- a/backend/model/GalleryManager.ts +++ b/backend/model/GalleryManager.ts @@ -1,31 +1,64 @@ -import {User} from "../../common/entities/User"; -export class UserManager { +import * as fs from 'fs'; +import * as path from 'path'; +import * as mime from 'mime'; - private static users = [new User(1,"TestUser","test@test.hu","122345")]; +import {Directory} from "../../common/entities/Directory"; +import {Photo} from "../../common/entities/Photo"; - public static findOne(filter,cb:(error: any,result:User) => void){ - return cb(null, UserManager.users[0]); +export class GalleryManager { + + + public static listDirectory(relativeDirectoryName, cb:(error: any,result:Directory) => void){ + let directoryName = path.basename(relativeDirectoryName); + let directoryParent = path.join( path.dirname(relativeDirectoryName),"/"); + let absoluteDirectoryName = path.join(__dirname,"/../../demo/images", relativeDirectoryName); + + let directory = new Directory(1,directoryName,directoryParent,new Date(),[],[]); + + fs.readdir(absoluteDirectoryName, function (err, list) { + + if(err){ + return cb(err,null); + } + + + for (let i = 0; i < list.length; i++) { + let file = list[i]; + let fullFilePath = path.resolve(absoluteDirectoryName, file); + if(fs.statSync(fullFilePath).isDirectory()){ + directory.directories.push(new Directory(2,file,relativeDirectoryName,new Date(),[],[])); + } + + if(GalleryManager.isImage(fullFilePath)){ + directory.photos.push(new Photo(1,file)); + } + } + + return cb(err, directory); + + }); } - public static find(filter,cb:(error: any,result:Array) => void){ - return cb(null, UserManager.users); - } + private static isImage(fullPath){ + let imageMimeTypes = [ + 'image/bmp', + 'image/gif', + 'image/jpeg', + 'image/png', + 'image/pjpeg', + 'image/tiff', + 'image/webp', + 'image/x-tiff', + 'image/x-windows-bmp' + ]; - public static createUser(user,cb:(error: any,result:User) => void){ - UserManager.users.push(user); - return cb(null, user); - } + var extension = mime.lookup(fullPath); - public static deleteUser(id:number,cb:(error: any,result:string) => void){ - UserManager.users = UserManager.users.filter(u => u.id != id); - return cb(null, "ok"); - } - - public static changeRole(request:any,cb:(error: any,result:string) => void){ - return cb(null,"ok"); - } - public static changePassword(request:any,cb:(error: any,result:string) => void){ - return cb(null,"ok"); + if (imageMimeTypes.indexOf(extension) !== -1) { + return true; + } + + return false; } } \ No newline at end of file diff --git a/backend/routes/GalleryRouter.ts b/backend/routes/GalleryRouter.ts index 264926a9..2021f856 100644 --- a/backend/routes/GalleryRouter.ts +++ b/backend/routes/GalleryRouter.ts @@ -15,16 +15,16 @@ export class GalleryRouter{ } private addDirectoryList() { - this.app.get("/api/gallery/:directory", - AuthenticationMWs.authenticate, + this.app.get(["/api/gallery/:directory","/api/gallery/"], + // AuthenticationMWs.authenticate, GalleryMWs.listDirectory ); }; private addGetImage() { - this.app.get("/api/gallery/:directory/:image", - AuthenticationMWs.authenticate, + this.app.get(["/api/gallery/:directory/:image","/api/gallery/:image"], + // AuthenticationMWs.authenticate, GalleryMWs.renderImage ); }; diff --git a/backend/routes/SharingRouter.ts b/backend/routes/SharingRouter.ts index cf774d0c..5357a73e 100644 --- a/backend/routes/SharingRouter.ts +++ b/backend/routes/SharingRouter.ts @@ -19,7 +19,7 @@ export class AdminRouter{ }; private addUpdateSharing() { - this.app.update("/api/share/:directory", + this.app.post("/api/share/:directory", AuthenticationMWs.authenticate, AuthenticationMWs.authorise(UserRoles.User) //TODO: implement diff --git a/backend/routes/UserRouter.ts b/backend/routes/UserRouter.ts index 82cadbad..885052c4 100644 --- a/backend/routes/UserRouter.ts +++ b/backend/routes/UserRouter.ts @@ -27,7 +27,7 @@ export class UserRouter{ private addChangePassword() { - this.app.update("/api/user/:id/password", + this.app.post("/api/user/:id/password", AuthenticationMWs.authenticate, UserRequestConstrainsMWs.forceSelfRequest, UserMWs.changePassword, @@ -65,7 +65,7 @@ export class UserRouter{ }; private addChangeRole() { - this.app.update("/api/user/:id/role", + this.app.post("/api/user/:id/role", AuthenticationMWs.authenticate, AuthenticationMWs.authorise(UserRoles.Admin), UserRequestConstrainsMWs.notSelfRequestOr2Admins, diff --git a/common/common-classes.d.ts b/common/common-classes.d.ts index d23a4f42..385eb636 100644 --- a/common/common-classes.d.ts +++ b/common/common-classes.d.ts @@ -8,3 +8,5 @@ /// /// /// +/// +/// diff --git a/common/entities/Directory.ts b/common/entities/Directory.ts new file mode 100644 index 00000000..98126299 --- /dev/null +++ b/common/entities/Directory.ts @@ -0,0 +1,10 @@ +import {Photo} from "./Photo"; +export class Directory{ + + constructor(public id:number, + public name:string, + public path:string, + public lastUpdate:Date, + public directories:Array, + public photos:Array){} +} \ No newline at end of file diff --git a/common/entities/Photo.ts b/common/entities/Photo.ts new file mode 100644 index 00000000..cbc50517 --- /dev/null +++ b/common/entities/Photo.ts @@ -0,0 +1,4 @@ + +export class Photo{ + constructor(public id:number,public name:string){} +} \ No newline at end of file diff --git a/frontend/app/model/network.service.ts b/frontend/app/model/network.service.ts index f05300d2..1599f7f8 100644 --- a/frontend/app/model/network.service.ts +++ b/frontend/app/model/network.service.ts @@ -33,9 +33,6 @@ export class NetworkService{ return this.callJson("get",url,data); } - protected updateJson(url:string, data:any = {}){ - return this.callJson("update",url,data); - } protected deleteJson(url:string, data:any = {}){ return this.callJson("delete",url,data); diff --git a/package.json b/package.json index 73e927f4..3baded01 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "PiGallery2", - "version": "0.0.1", + "version": "0.0.5", "private": true, "description": "This is a photo gallery optimised for running low resource servers (especially on raspberry pi)", "author": "Braun Patrik", @@ -29,6 +29,7 @@ "es7-reflect-metadata": "^1.6.0", "express": "^4.13.4", "express-session": "^1.13.0", + "mime": "^1.3.4", "morgan": "^1.7.0", "protractor": "^3.2.1", "reflect-metadata": "0.1.2",