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-12-28 03:55:51 +08:00
|
|
|
import {DirectoryDTO} from "../../common/entities/DirectoryDTO";
|
2016-04-22 19:23:44 +08:00
|
|
|
import {ObjectManagerRepository} from "../model/ObjectManagerRepository";
|
2016-05-16 17:03:11 +08:00
|
|
|
import {AutoCompleteItem, SearchTypes} from "../../common/entities/AutoCompleteItem";
|
2016-05-10 01:14:33 +08:00
|
|
|
import {ContentWrapper} from "../../common/entities/ConentWrapper";
|
2016-12-28 18:50:05 +08:00
|
|
|
import {SearchResultDTO} from "../../common/entities/SearchResult";
|
2016-12-28 03:55:51 +08:00
|
|
|
import {PhotoDTO} from "../../common/entities/PhotoDTO";
|
2016-05-11 03:33:58 +08:00
|
|
|
import {Config} from "../config/Config";
|
2016-06-22 22:34:44 +08:00
|
|
|
import {ProjectPath} from "../ProjectPath";
|
2017-06-04 04:35:47 +08:00
|
|
|
import {Logger} from "../Logger";
|
2016-03-20 02:59:19 +08:00
|
|
|
|
2017-06-04 04:35:47 +08:00
|
|
|
|
|
|
|
const LOG_TAG = "[GalleryMWs]";
|
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
|
|
|
|
2017-03-18 07:11:53 +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-06-22 22:34:44 +08:00
|
|
|
let absoluteDirectoryName = path.join(ProjectPath.ImageFolder, 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-12-28 03:55:51 +08:00
|
|
|
ObjectManagerRepository.getInstance().getGalleryManager().listDirectory(directoryName, (err, directory: DirectoryDTO) => {
|
2016-05-09 23:04:56 +08:00
|
|
|
if (err || !directory) {
|
2017-06-04 04:35:47 +08:00
|
|
|
Logger.warn(LOG_TAG, "Error during listing the directory", err);
|
2016-12-28 22:35:27 +08:00
|
|
|
console.error(err);
|
2016-05-09 23:04:56 +08:00
|
|
|
return next(new Error(ErrorCodes.GENERAL_ERROR, err));
|
|
|
|
}
|
2017-03-18 07:11:53 +08:00
|
|
|
|
2016-05-10 01:14:33 +08:00
|
|
|
req.resultPipe = new ContentWrapper(directory, null);
|
2016-06-26 17:08:05 +08:00
|
|
|
|
2016-06-26 17:09:46 +08:00
|
|
|
return next();
|
2016-03-20 17:49:49 +08:00
|
|
|
});
|
2016-03-20 02:59:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-03-18 07:11:53 +08:00
|
|
|
public static removeCyclicDirectoryReferences(req: Request, res: Response, next: NextFunction) {
|
2016-06-22 22:34:44 +08:00
|
|
|
if (!req.resultPipe)
|
|
|
|
return next();
|
|
|
|
|
2017-03-18 07:11:53 +08:00
|
|
|
let cw: ContentWrapper = req.resultPipe;
|
|
|
|
let removeDirs = (dir) => {
|
|
|
|
dir.photos.forEach((photo: PhotoDTO) => {
|
2016-06-22 22:34:44 +08:00
|
|
|
photo.directory = null;
|
|
|
|
});
|
2016-12-28 03:55:51 +08:00
|
|
|
|
2017-03-18 07:11:53 +08:00
|
|
|
dir.directories.forEach((directory: DirectoryDTO) => {
|
|
|
|
removeDirs(directory);
|
|
|
|
directory.parent = null;
|
2016-12-28 03:55:51 +08:00
|
|
|
});
|
2017-03-18 07:11:53 +08:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
if (cw.directory) {
|
|
|
|
removeDirs(cw.directory);
|
2016-06-22 22:34:44 +08:00
|
|
|
}
|
|
|
|
|
2017-03-18 07:11:53 +08:00
|
|
|
|
2016-06-22 22:34:44 +08:00
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
2016-05-10 03:43:52 +08:00
|
|
|
|
2017-03-18 07:11:53 +08:00
|
|
|
public static loadImage(req: Request, res: Response, next: NextFunction) {
|
2016-05-09 23:04:56 +08:00
|
|
|
if (!(req.params.imagePath)) {
|
2016-03-20 17:49:49 +08:00
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
2016-06-22 22:34:44 +08:00
|
|
|
let fullImagePath = path.join(ProjectPath.ImageFolder, req.params.imagePath);
|
2016-05-09 23:04:56 +08:00
|
|
|
if (fs.statSync(fullImagePath).isDirectory()) {
|
2016-03-20 17:49:49 +08:00
|
|
|
return next();
|
|
|
|
}
|
2016-05-09 23:04:56 +08:00
|
|
|
|
2016-05-13 05:00:38 +08:00
|
|
|
//check if thumbnail already exist
|
|
|
|
if (fs.existsSync(fullImagePath) === false) {
|
|
|
|
return next(new Error(ErrorCodes.GENERAL_ERROR, "no such file :" + fullImagePath));
|
|
|
|
}
|
|
|
|
|
2016-03-26 18:19:10 +08:00
|
|
|
req.resultPipe = fullImagePath;
|
|
|
|
return next();
|
2016-03-20 02:59:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-03-18 07:11:53 +08:00
|
|
|
public static search(req: Request, res: Response, next: NextFunction) {
|
2016-05-11 03:33:58 +08:00
|
|
|
if (Config.Client.Search.searchEnabled === false) {
|
|
|
|
return next();
|
|
|
|
}
|
2016-05-10 03:43:52 +08:00
|
|
|
|
2016-05-11 03:33:58 +08:00
|
|
|
if (!(req.params.text)) {
|
|
|
|
return next();
|
|
|
|
}
|
2016-05-16 17:03:11 +08:00
|
|
|
|
2017-03-18 07:11:53 +08:00
|
|
|
let type: SearchTypes;
|
2016-05-16 17:03:11 +08:00
|
|
|
if (req.query.type) {
|
|
|
|
type = parseInt(req.query.type);
|
|
|
|
}
|
|
|
|
|
2016-12-28 18:50:05 +08:00
|
|
|
ObjectManagerRepository.getInstance().getSearchManager().search(req.params.text, type, (err, result: SearchResultDTO) => {
|
2016-05-10 03:43:52 +08:00
|
|
|
if (err || !result) {
|
|
|
|
return next(new Error(ErrorCodes.GENERAL_ERROR, err));
|
|
|
|
}
|
|
|
|
req.resultPipe = new ContentWrapper(null, result);
|
|
|
|
return next();
|
|
|
|
});
|
2016-03-20 02:59:19 +08:00
|
|
|
}
|
|
|
|
|
2016-05-10 01:14:33 +08:00
|
|
|
|
2017-03-18 07:11:53 +08:00
|
|
|
public static instantSearch(req: Request, res: Response, next: NextFunction) {
|
2016-05-11 03:33:58 +08:00
|
|
|
if (Config.Client.Search.instantSearchEnabled === false) {
|
|
|
|
return next();
|
|
|
|
}
|
2017-03-18 07:11:53 +08:00
|
|
|
|
2016-05-10 03:43:52 +08:00
|
|
|
if (!(req.params.text)) {
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
2016-05-16 17:03:11 +08:00
|
|
|
|
2016-12-28 18:50:05 +08:00
|
|
|
ObjectManagerRepository.getInstance().getSearchManager().instantSearch(req.params.text, (err, result: SearchResultDTO) => {
|
2016-05-10 03:43:52 +08:00
|
|
|
if (err || !result) {
|
|
|
|
return next(new Error(ErrorCodes.GENERAL_ERROR, err));
|
|
|
|
}
|
|
|
|
req.resultPipe = new ContentWrapper(null, result);
|
|
|
|
return next();
|
|
|
|
});
|
2016-05-10 01:14:33 +08:00
|
|
|
}
|
|
|
|
|
2017-03-18 07:11:53 +08:00
|
|
|
public static autocomplete(req: Request, res: Response, next: NextFunction) {
|
2016-05-11 03:33:58 +08:00
|
|
|
if (Config.Client.Search.autocompleteEnabled === false) {
|
|
|
|
return next();
|
|
|
|
}
|
2016-05-09 23:04:56 +08:00
|
|
|
if (!(req.params.text)) {
|
2016-05-05 00:34:54 +08:00
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
2017-03-18 07:11:53 +08:00
|
|
|
ObjectManagerRepository.getInstance().getSearchManager().autocomplete(req.params.text, (err, items: Array<AutoCompleteItem>) => {
|
2016-05-09 23:04:56 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|