2016-03-20 02:59:19 +08:00
|
|
|
|
2016-03-20 17:49:49 +08:00
|
|
|
import * as path from 'path';
|
|
|
|
import * as fs from 'fs';
|
2016-03-20 02:59:19 +08:00
|
|
|
import {NextFunction, Request, Response} from "express";
|
|
|
|
import {BaseMWs} from "./BaseMWs";
|
|
|
|
import {Error, ErrorCodes} from "../../common/entities/Error";
|
2016-03-20 17:49:49 +08:00
|
|
|
import {GalleryManager} from "../model/GalleryManager";
|
|
|
|
import {Directory} from "../../common/entities/Directory";
|
2016-03-20 02:59:19 +08:00
|
|
|
|
|
|
|
export class GalleryMWs extends BaseMWs{
|
|
|
|
|
|
|
|
|
|
|
|
public static listDirectory(req:Request, res:Response, next:NextFunction){
|
2016-03-20 17:49:49 +08:00
|
|
|
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);
|
|
|
|
});
|
2016-03-20 02:59:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static renderImage(req:Request, res:Response, next:NextFunction){
|
2016-03-20 17:49:49 +08:00
|
|
|
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);
|
2016-03-20 02:59:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public static renderThumbnail(req:Request, res:Response, next:NextFunction){
|
|
|
|
//TODO: implement
|
|
|
|
return super.renderError(res,new Error(ErrorCodes.GENERAL_ERROR));
|
|
|
|
}
|
|
|
|
|
|
|
|
public static search(req:Request, res:Response, next:NextFunction){
|
|
|
|
//TODO: implement
|
|
|
|
return super.renderError(res,new Error(ErrorCodes.GENERAL_ERROR));
|
|
|
|
}
|
|
|
|
|
|
|
|
public static autocomplete(req:Request, res:Response, next:NextFunction){
|
|
|
|
//TODO: implement
|
|
|
|
return super.renderError(res,new Error(ErrorCodes.GENERAL_ERROR));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|