2016-05-09 23:04:56 +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 {Error, ErrorCodes} from "../../common/entities/Error";
|
2016-04-10 05:09:28 +08:00
|
|
|
import {Directory} from "../../common/entities/Directory";
|
|
|
|
import {Config} from "../config/Config";
|
2016-04-22 19:23:44 +08:00
|
|
|
import {ObjectManagerRepository} from "../model/ObjectManagerRepository";
|
2016-05-05 00:34:54 +08:00
|
|
|
import {AutoCompleteItem} from "../../common/entities/AutoCompleteItem";
|
2016-03-20 02:59:19 +08:00
|
|
|
|
2016-03-26 18:19:10 +08:00
|
|
|
export class GalleryMWs {
|
2016-03-20 02:59:19 +08:00
|
|
|
|
2016-03-26 18:19:10 +08:00
|
|
|
|
2016-05-09 23:04:56 +08:00
|
|
|
private static getImageFolder() {
|
|
|
|
return path.join(__dirname, "/../../", Config.imagesFolder);
|
2016-04-10 05:09:28 +08:00
|
|
|
}
|
|
|
|
|
2016-05-09 23:04:56 +08:00
|
|
|
public static listDirectory(req:Request, res:Response, next:NextFunction) {
|
2016-03-30 04:09:02 +08:00
|
|
|
let directoryName = req.params.directory || "/";
|
2016-04-10 05:09:28 +08:00
|
|
|
let absoluteDirectoryName = path.join(GalleryMWs.getImageFolder(), directoryName);
|
2016-05-09 23:04:56 +08:00
|
|
|
|
|
|
|
if (!fs.statSync(absoluteDirectoryName).isDirectory()) {
|
2016-03-20 17:49:49 +08:00
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
2016-05-09 23:04:56 +08:00
|
|
|
ObjectManagerRepository.getInstance().getGalleryManager().listDirectory(directoryName, (err, directory:Directory) => {
|
|
|
|
if (err || !directory) {
|
|
|
|
return next(new Error(ErrorCodes.GENERAL_ERROR, err));
|
|
|
|
}
|
2016-03-26 18:19:10 +08:00
|
|
|
req.resultPipe = directory;
|
|
|
|
return next();
|
2016-03-20 17:49:49 +08:00
|
|
|
});
|
2016-03-20 02:59:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-05-09 23:04:56 +08:00
|
|
|
public static loadImage(req:Request, res:Response, next:NextFunction) {
|
|
|
|
if (!(req.params.imagePath)) {
|
2016-03-20 17:49:49 +08:00
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
2016-05-09 23:04:56 +08:00
|
|
|
let fullImagePath = path.join(GalleryMWs.getImageFolder(), req.params.imagePath);
|
|
|
|
if (fs.statSync(fullImagePath).isDirectory()) {
|
2016-03-20 17:49:49 +08:00
|
|
|
return next();
|
|
|
|
}
|
2016-05-09 23:04:56 +08:00
|
|
|
|
2016-03-26 18:19:10 +08:00
|
|
|
req.resultPipe = fullImagePath;
|
|
|
|
return next();
|
2016-03-20 02:59:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-05-09 23:04:56 +08:00
|
|
|
public static search(req:Request, res:Response, next:NextFunction) {
|
2016-03-20 02:59:19 +08:00
|
|
|
//TODO: implement
|
2016-03-26 18:19:10 +08:00
|
|
|
return next(new Error(ErrorCodes.GENERAL_ERROR));
|
2016-03-20 02:59:19 +08:00
|
|
|
}
|
|
|
|
|
2016-05-09 23:04:56 +08:00
|
|
|
public static autocomplete(req:Request, res:Response, next:NextFunction) {
|
|
|
|
if (!(req.params.text)) {
|
2016-05-05 00:34:54 +08:00
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
2016-05-09 23:04:56 +08:00
|
|
|
ObjectManagerRepository.getInstance().getSearchManager().autocomplete(req.params.text, (err, items:Array<AutoCompleteItem>) => {
|
|
|
|
if (err || !items) {
|
|
|
|
return next(new Error(ErrorCodes.GENERAL_ERROR, err));
|
2016-05-05 00:34:54 +08:00
|
|
|
}
|
|
|
|
req.resultPipe = items;
|
|
|
|
return next();
|
|
|
|
});
|
2016-03-20 02:59:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|