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