2018-03-31 03:30:30 +08:00
|
|
|
import * as path from 'path';
|
2020-01-03 05:11:59 +08:00
|
|
|
import {promises as fsp} from 'fs';
|
2021-05-03 20:49:41 +08:00
|
|
|
import * as archiver from 'archiver';
|
2018-03-31 03:30:30 +08:00
|
|
|
import {NextFunction, Request, Response} from 'express';
|
|
|
|
import {ErrorCodes, ErrorDTO} from '../../common/entities/Error';
|
2021-04-18 21:48:35 +08:00
|
|
|
import {DirectoryDTO, DirectoryDTOUtils} from '../../common/entities/DirectoryDTO';
|
2019-02-16 00:47:09 +08:00
|
|
|
import {ObjectManagers} from '../model/ObjectManagers';
|
2018-03-31 03:30:30 +08:00
|
|
|
import {ContentWrapper} from '../../common/entities/ConentWrapper';
|
|
|
|
import {PhotoDTO} from '../../common/entities/PhotoDTO';
|
|
|
|
import {ProjectPath} from '../ProjectPath';
|
|
|
|
import {Config} from '../../common/config/private/Config';
|
2021-04-18 21:48:35 +08:00
|
|
|
import {UserDTO, UserDTOUtils} from '../../common/entities/UserDTO';
|
|
|
|
import {MediaDTO, MediaDTOUtils} from '../../common/entities/MediaDTO';
|
2018-11-18 03:15:48 +08:00
|
|
|
import {VideoDTO} from '../../common/entities/VideoDTO';
|
2018-12-06 00:29:33 +08:00
|
|
|
import {Utils} from '../../common/Utils';
|
2018-12-09 19:02:02 +08:00
|
|
|
import {QueryParams} from '../../common/QueryParams';
|
2019-12-15 00:27:01 +08:00
|
|
|
import {VideoProcessing} from '../model/fileprocessing/VideoProcessing';
|
2021-01-17 22:56:15 +08:00
|
|
|
import {SearchQueryDTO, SearchQueryTypes} from '../../common/entities/SearchQueryDTO';
|
2021-04-06 17:32:31 +08:00
|
|
|
import {LocationLookupException} from '../exceptions/LocationLookupException';
|
2021-04-28 19:37:37 +08:00
|
|
|
import {SupportedFormats} from '../../common/SupportedFormats';
|
2018-03-31 03:30:30 +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
|
|
|
|
2021-04-18 21:48:35 +08:00
|
|
|
public static async listDirectory(req: Request, res: Response, next: NextFunction): Promise<any> {
|
2018-05-10 01:56:02 +08:00
|
|
|
const directoryName = req.params.directory || '/';
|
|
|
|
const absoluteDirectoryName = path.join(ProjectPath.ImageFolder, directoryName);
|
2020-01-03 05:11:59 +08:00
|
|
|
try {
|
|
|
|
if ((await fsp.stat(absoluteDirectoryName)).isDirectory() === false) {
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
} catch (e) {
|
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 {
|
2019-02-16 00:47:09 +08:00
|
|
|
const directory = await ObjectManagers.getInstance()
|
2018-12-09 19:02:02 +08:00
|
|
|
.GalleryManager.listDirectory(directoryName,
|
2021-04-18 21:48:35 +08:00
|
|
|
parseInt(req.query[QueryParams.gallery.knownLastModified] as string, 10),
|
|
|
|
parseInt(req.query[QueryParams.gallery.knownLastScanned] as string, 10));
|
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] !== '/*') {
|
2021-04-18 21:48:35 +08:00
|
|
|
(directory as DirectoryDTO).directories = (directory as DirectoryDTO).directories.filter((d): boolean =>
|
|
|
|
UserDTOUtils.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
|
|
|
|
2021-04-28 19:37:37 +08:00
|
|
|
public static async zipDirectory(req: Request, res: Response, next: NextFunction): Promise<any> {
|
|
|
|
if (Config.Client.Other.enableDownloadZip === false) {
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
const directoryName = req.params.directory || '/';
|
|
|
|
const absoluteDirectoryName = path.join(ProjectPath.ImageFolder, directoryName);
|
|
|
|
try {
|
|
|
|
if ((await fsp.stat(absoluteDirectoryName)).isDirectory() === false) {
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
res.set('Content-Type', 'application/zip');
|
|
|
|
res.set('Content-Disposition', 'attachment; filename=Gallery.zip');
|
|
|
|
|
2021-05-04 08:07:43 +08:00
|
|
|
const archive = archiver('zip', {
|
|
|
|
store: true, // disable compression
|
|
|
|
});
|
2021-04-28 19:37:37 +08:00
|
|
|
|
|
|
|
res.on('close', function() {
|
|
|
|
console.log('zip ' + archive.pointer() + ' bytes');
|
|
|
|
});
|
|
|
|
|
|
|
|
archive.on('error', function(err: any) {
|
|
|
|
throw err;
|
|
|
|
});
|
|
|
|
|
|
|
|
archive.pipe(res);
|
|
|
|
|
2021-05-03 20:49:41 +08:00
|
|
|
// append photos in absoluteDirectoryName
|
|
|
|
// using case-insensitive glob of extensions
|
2021-04-28 19:37:37 +08:00
|
|
|
for (const ext of SupportedFormats.WithDots.Photos) {
|
|
|
|
archive.glob(`*${ext}`, {cwd:absoluteDirectoryName, nocase:true});
|
|
|
|
}
|
2021-05-03 20:49:41 +08:00
|
|
|
// append videos in absoluteDirectoryName
|
|
|
|
// using case-insensitive glob of extensions
|
2021-04-28 19:37:37 +08:00
|
|
|
for (const ext of SupportedFormats.WithDots.Videos) {
|
|
|
|
archive.glob(`*${ext}`, {cwd:absoluteDirectoryName, nocase:true});
|
|
|
|
}
|
|
|
|
|
2021-05-03 20:49:41 +08:00
|
|
|
await archive.finalize();
|
2021-04-28 19:37:37 +08:00
|
|
|
return next();
|
|
|
|
|
|
|
|
} catch (err) {
|
|
|
|
return next(new ErrorDTO(ErrorCodes.GENERAL_ERROR, 'Error creating zip', err));
|
|
|
|
}
|
|
|
|
}
|
2016-12-28 03:55:51 +08:00
|
|
|
|
2021-04-18 21:48:35 +08:00
|
|
|
public static cleanUpGalleryResults(req: Request, res: Response, next: NextFunction): any {
|
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();
|
|
|
|
}
|
2017-03-18 07:11:53 +08:00
|
|
|
|
2021-04-18 21:48:35 +08:00
|
|
|
const cleanUpMedia = (media: MediaDTO[]): void => {
|
|
|
|
media.forEach((m): void => {
|
|
|
|
if (MediaDTOUtils.isPhoto(m)) {
|
|
|
|
delete (m as VideoDTO).metadata.bitRate;
|
|
|
|
delete (m as VideoDTO).metadata.duration;
|
|
|
|
} else if (MediaDTOUtils.isVideo(m)) {
|
|
|
|
delete (m as PhotoDTO).metadata.rating;
|
|
|
|
delete (m as PhotoDTO).metadata.caption;
|
|
|
|
delete (m as PhotoDTO).metadata.cameraData;
|
|
|
|
delete (m as PhotoDTO).metadata.orientation;
|
|
|
|
delete (m as PhotoDTO).metadata.orientation;
|
|
|
|
delete (m as PhotoDTO).metadata.keywords;
|
|
|
|
delete (m as PhotoDTO).metadata.positionData;
|
2018-11-18 03:15:48 +08:00
|
|
|
}
|
2018-12-06 00:29:33 +08:00
|
|
|
Utils.removeNullOrEmptyObj(m);
|
2018-11-18 03:15:48 +08:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
if (cw.directory) {
|
2021-04-18 21:48:35 +08:00
|
|
|
DirectoryDTOUtils.packDirectory(cw.directory);
|
2021-03-28 04:31:19 +08:00
|
|
|
// TODO: remove when typeorm inheritance is fixed (and handles proper inheritance)
|
2018-11-18 03:15:48 +08:00
|
|
|
cleanUpMedia(cw.directory.media);
|
|
|
|
}
|
|
|
|
if (cw.searchResult) {
|
|
|
|
cleanUpMedia(cw.searchResult.media);
|
2016-06-22 22:34:44 +08:00
|
|
|
}
|
|
|
|
|
2016-05-10 03:43:52 +08:00
|
|
|
|
2019-12-15 00:27:01 +08:00
|
|
|
if (Config.Client.Media.Video.enabled === false) {
|
2018-11-19 03:26:29 +08:00
|
|
|
if (cw.directory) {
|
2021-04-18 21:48:35 +08:00
|
|
|
const removeVideos = (dir: DirectoryDTO): void => {
|
|
|
|
dir.media = dir.media.filter((m): boolean => !MediaDTOUtils.isVideo(m));
|
2019-02-11 00:18:54 +08:00
|
|
|
if (dir.directories) {
|
2021-04-18 21:48:35 +08:00
|
|
|
dir.directories.forEach((d): void => removeVideos(d));
|
2019-02-11 00:18:54 +08:00
|
|
|
}
|
2018-11-19 03:26:29 +08:00
|
|
|
};
|
|
|
|
removeVideos(cw.directory);
|
|
|
|
}
|
|
|
|
if (cw.searchResult) {
|
2021-04-18 21:48:35 +08:00
|
|
|
cw.searchResult.media = cw.searchResult.media.filter((m): boolean => !MediaDTOUtils.isVideo(m));
|
2018-11-19 03:26:29 +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
|
|
|
|
2021-04-18 21:48:35 +08:00
|
|
|
public static async loadFile(req: Request, res: Response, next: NextFunction): Promise<any> {
|
2018-12-02 06:53:58 +08:00
|
|
|
if (!(req.params.mediaPath)) {
|
2017-06-11 04:32:56 +08:00
|
|
|
return next();
|
|
|
|
}
|
2018-12-02 06:53:58 +08:00
|
|
|
const fullMediaPath = path.join(ProjectPath.ImageFolder, req.params.mediaPath);
|
2016-03-20 02:59:19 +08:00
|
|
|
|
2019-12-09 21:05:06 +08:00
|
|
|
// check if file exist
|
2020-01-03 05:11:59 +08:00
|
|
|
try {
|
|
|
|
if ((await fsp.stat(fullMediaPath)).isDirectory()) {
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
} catch (e) {
|
2019-07-19 05:06:54 +08:00
|
|
|
return next(new ErrorDTO(ErrorCodes.GENERAL_ERROR, 'no such file:' + req.params.mediaPath, 'can\'t find file: ' + fullMediaPath));
|
2017-06-11 04:32:56 +08:00
|
|
|
}
|
2017-07-18 05:12:12 +08:00
|
|
|
|
|
|
|
|
2018-11-05 02:28:32 +08:00
|
|
|
req.resultPipe = fullMediaPath;
|
2017-06-11 04:32:56 +08:00
|
|
|
return next();
|
|
|
|
}
|
2016-05-10 03:43:52 +08:00
|
|
|
|
2021-04-18 21:48:35 +08:00
|
|
|
public static async loadBestFitVideo(req: Request, res: Response, next: NextFunction): Promise<any> {
|
2019-12-15 00:27:01 +08:00
|
|
|
if (!(req.resultPipe)) {
|
2019-12-09 21:05:06 +08:00
|
|
|
return next();
|
|
|
|
}
|
2019-12-15 17:52:56 +08:00
|
|
|
const fullMediaPath: string = req.resultPipe;
|
2019-12-09 21:05:06 +08:00
|
|
|
|
2019-12-27 04:03:10 +08:00
|
|
|
const convertedVideo = VideoProcessing.generateConvertedFilePath(fullMediaPath);
|
2019-12-09 21:05:06 +08:00
|
|
|
|
|
|
|
// check if transcoded video exist
|
2020-01-03 05:11:59 +08:00
|
|
|
try {
|
2020-01-08 05:48:54 +08:00
|
|
|
await fsp.access(convertedVideo);
|
2019-12-09 21:05:06 +08:00
|
|
|
req.resultPipe = convertedVideo;
|
2020-01-03 05:11:59 +08:00
|
|
|
} catch (e) {
|
2019-12-09 21:05:06 +08:00
|
|
|
}
|
|
|
|
|
2020-01-03 05:11:59 +08:00
|
|
|
|
2019-12-09 21:05:06 +08:00
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
2016-05-16 17:03:11 +08:00
|
|
|
|
2021-04-18 21:48:35 +08:00
|
|
|
public static async search(req: Request, res: Response, next: NextFunction): Promise<any> {
|
2021-03-14 23:52:37 +08:00
|
|
|
if (Config.Client.Search.enabled === false || !(req.params.searchQueryDTO)) {
|
2017-06-11 04:32:56 +08:00
|
|
|
return next();
|
|
|
|
}
|
2016-05-16 17:03:11 +08:00
|
|
|
|
2021-04-18 21:48:35 +08:00
|
|
|
const query: SearchQueryDTO = JSON.parse(req.params.searchQueryDTO as any);
|
2021-01-17 22:56:15 +08:00
|
|
|
|
2017-07-08 06:18:24 +08:00
|
|
|
try {
|
2021-01-17 22:56:15 +08:00
|
|
|
const result = await ObjectManagers.getInstance().SearchManager.search(query);
|
2016-05-10 01:14:33 +08:00
|
|
|
|
2021-04-18 21:48:35 +08:00
|
|
|
result.directories.forEach((dir): MediaDTO[] => dir.media = dir.media || []);
|
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) {
|
2021-04-06 17:32:31 +08:00
|
|
|
if (err instanceof LocationLookupException) {
|
|
|
|
return next(new ErrorDTO(ErrorCodes.LocationLookUp_ERROR, 'Cannot find location: ' + err.location, 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
|
|
|
|
2021-01-17 22:56:15 +08:00
|
|
|
|
2021-04-18 21:48:35 +08:00
|
|
|
public static async autocomplete(req: Request, res: Response, next: NextFunction): Promise<any> {
|
2021-01-17 22:56:15 +08:00
|
|
|
if (Config.Client.Search.AutoComplete.enabled === false) {
|
2017-06-11 04:32:56 +08:00
|
|
|
return next();
|
|
|
|
}
|
|
|
|
if (!(req.params.text)) {
|
|
|
|
return next();
|
2016-05-10 01:14:33 +08:00
|
|
|
}
|
|
|
|
|
2021-01-17 22:56:15 +08:00
|
|
|
let type: SearchQueryTypes = SearchQueryTypes.any_text;
|
|
|
|
if (req.query[QueryParams.gallery.search.type]) {
|
2021-04-18 21:48:35 +08:00
|
|
|
type = parseInt(req.query[QueryParams.gallery.search.type] as string, 10);
|
2021-01-17 22:56:15 +08:00
|
|
|
}
|
2017-07-08 06:18:24 +08:00
|
|
|
try {
|
2021-01-17 22:56:15 +08:00
|
|
|
req.resultPipe = await ObjectManagers.getInstance().SearchManager.autocomplete(req.params.text, type);
|
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
|
|
|
}
|
2021-01-17 22:56:15 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
}
|
|
|
|
|
2021-01-17 22:56:15 +08:00
|
|
|
|
2021-04-18 21:48:35 +08:00
|
|
|
public static async getRandomImage(req: Request, res: Response, next: NextFunction): Promise<any> {
|
2021-03-14 23:52:37 +08:00
|
|
|
if (Config.Client.RandomPhoto.enabled === false || !(req.params.searchQueryDTO)) {
|
2017-06-11 04:32:56 +08:00
|
|
|
return next();
|
2016-03-20 02:59:19 +08:00
|
|
|
}
|
2021-03-14 23:52:37 +08:00
|
|
|
|
2017-07-08 06:18:24 +08:00
|
|
|
try {
|
2021-04-18 21:48:35 +08:00
|
|
|
const query: SearchQueryDTO = JSON.parse(req.params.searchQueryDTO as any);
|
2021-01-17 22:56:15 +08:00
|
|
|
|
|
|
|
const photo = await ObjectManagers.getInstance()
|
|
|
|
.SearchManager.getRandomPhoto(query);
|
|
|
|
if (!photo) {
|
|
|
|
return next(new ErrorDTO(ErrorCodes.INPUT_ERROR, 'No photo found'));
|
|
|
|
}
|
|
|
|
|
|
|
|
req.params.mediaPath = path.join(photo.directory.path, photo.directory.name, photo.name);
|
2017-06-11 04:32:56 +08:00
|
|
|
return next();
|
2021-01-17 22:56:15 +08:00
|
|
|
} catch (e) {
|
|
|
|
return next(new ErrorDTO(ErrorCodes.GENERAL_ERROR, 'Can\'t get random photo: ' + e.toString()));
|
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
|
|
|
}
|