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