mirror of
https://github.com/bpatrik/pigallery2.git
synced 2024-12-25 02:04:15 +02:00
implementing directory listing
This commit is contained in:
parent
b0f4d878ff
commit
d6cd1798b5
@ -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){
|
||||
|
@ -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{
|
||||
|
||||
|
@ -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<User>) => 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;
|
||||
}
|
||||
|
||||
}
|
@ -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
|
||||
);
|
||||
};
|
||||
|
@ -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
|
||||
|
@ -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,
|
||||
|
2
common/common-classes.d.ts
vendored
2
common/common-classes.d.ts
vendored
@ -8,3 +8,5 @@
|
||||
///<reference path="./entities/LoginCredential.ts"/>
|
||||
///<reference path="./entities/User.ts"/>
|
||||
///<reference path="./entities/Message.ts"/>
|
||||
///<reference path="./entities/Directory.ts"/>
|
||||
///<reference path="./entities/Photo.ts"/>
|
||||
|
10
common/entities/Directory.ts
Normal file
10
common/entities/Directory.ts
Normal file
@ -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<Directory>,
|
||||
public photos:Array<Photo>){}
|
||||
}
|
4
common/entities/Photo.ts
Normal file
4
common/entities/Photo.ts
Normal file
@ -0,0 +1,4 @@
|
||||
|
||||
export class Photo{
|
||||
constructor(public id:number,public name:string){}
|
||||
}
|
@ -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);
|
||||
|
@ -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",
|
||||
|
Loading…
Reference in New Issue
Block a user