2021-04-18 21:48:35 +08:00
|
|
|
import {MediaDTO, MediaDTOUtils} from './MediaDTO';
|
2018-11-26 07:26:29 +08:00
|
|
|
import {FileDTO} from './FileDTO';
|
2021-04-02 03:48:38 +08:00
|
|
|
import {PhotoDTO, PreviewPhotoDTO} from './PhotoDTO';
|
|
|
|
import {Utils} from '../Utils';
|
2016-12-28 03:55:51 +08:00
|
|
|
|
2021-04-02 03:48:38 +08:00
|
|
|
export interface DirectoryBaseDTO {
|
|
|
|
name: string;
|
|
|
|
path: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface DirectoryDTO<S extends FileDTO = MediaDTO> extends DirectoryBaseDTO {
|
2017-06-11 04:32:56 +08:00
|
|
|
id: number;
|
|
|
|
name: string;
|
|
|
|
path: string;
|
2017-07-20 02:47:09 +08:00
|
|
|
lastModified: number;
|
|
|
|
lastScanned: number;
|
2017-07-22 01:14:22 +08:00
|
|
|
isPartial?: boolean;
|
2019-12-31 18:12:59 +08:00
|
|
|
parent: DirectoryDTO<S>;
|
2019-01-07 06:15:52 +08:00
|
|
|
mediaCount: number;
|
2019-12-31 18:12:59 +08:00
|
|
|
directories: DirectoryDTO<S>[];
|
2021-04-02 03:48:38 +08:00
|
|
|
preview: PreviewPhotoDTO;
|
2019-12-31 18:12:59 +08:00
|
|
|
media: S[];
|
2018-11-26 07:26:29 +08:00
|
|
|
metaFile: FileDTO[];
|
2017-06-11 04:32:56 +08:00
|
|
|
}
|
2017-07-08 06:18:24 +08:00
|
|
|
|
2021-04-18 21:48:35 +08:00
|
|
|
export const DirectoryDTOUtils = {
|
|
|
|
unpackDirectory: (dir: DirectoryDTO): void => {
|
2018-11-24 18:50:11 +08:00
|
|
|
dir.media.forEach((media: MediaDTO) => {
|
|
|
|
media.directory = dir;
|
2017-07-08 06:18:24 +08:00
|
|
|
});
|
2018-11-30 22:37:01 +08:00
|
|
|
|
|
|
|
if (dir.metaFile) {
|
|
|
|
dir.metaFile.forEach((file: FileDTO) => {
|
|
|
|
file.directory = dir;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dir.directories) {
|
|
|
|
dir.directories.forEach((directory: DirectoryDTO) => {
|
2021-04-18 21:48:35 +08:00
|
|
|
DirectoryDTOUtils.unpackDirectory(directory);
|
2018-11-30 22:37:01 +08:00
|
|
|
directory.parent = dir;
|
|
|
|
});
|
|
|
|
}
|
2021-04-18 21:48:35 +08:00
|
|
|
},
|
2018-11-19 03:26:29 +08:00
|
|
|
|
2021-04-18 21:48:35 +08:00
|
|
|
packDirectory: (dir: DirectoryDTO): DirectoryDTO => {
|
2021-04-02 03:48:38 +08:00
|
|
|
if (dir.preview) {
|
|
|
|
dir.preview.directory = {
|
|
|
|
path: dir.preview.directory.path,
|
|
|
|
name: dir.preview.directory.name,
|
|
|
|
};
|
|
|
|
|
|
|
|
// make sure that it is not a same object as one of the photo in the media[]
|
|
|
|
// as the next foreach would remove the directory
|
|
|
|
dir.preview = Utils.clone(dir.preview);
|
|
|
|
}
|
|
|
|
|
2018-12-18 07:05:12 +08:00
|
|
|
if (dir.media) {
|
|
|
|
dir.media.forEach((media: MediaDTO) => {
|
|
|
|
media.directory = null;
|
|
|
|
});
|
|
|
|
}
|
2021-04-02 03:48:38 +08:00
|
|
|
|
2018-11-26 07:26:29 +08:00
|
|
|
if (dir.metaFile) {
|
|
|
|
dir.metaFile.forEach((file: FileDTO) => {
|
|
|
|
file.directory = null;
|
|
|
|
});
|
|
|
|
}
|
2018-11-24 18:50:11 +08:00
|
|
|
if (dir.directories) {
|
|
|
|
dir.directories.forEach((directory: DirectoryDTO) => {
|
2021-04-18 21:48:35 +08:00
|
|
|
DirectoryDTOUtils.packDirectory(directory);
|
2018-11-24 18:50:11 +08:00
|
|
|
directory.parent = null;
|
|
|
|
});
|
|
|
|
}
|
2018-11-19 03:26:29 +08:00
|
|
|
|
2021-03-28 18:43:13 +08:00
|
|
|
return dir;
|
|
|
|
|
2021-04-18 21:48:35 +08:00
|
|
|
},
|
|
|
|
filterPhotos: (dir: DirectoryDTO): PhotoDTO[] => {
|
|
|
|
return dir.media.filter(m => MediaDTOUtils.isPhoto(m)) as PhotoDTO[];
|
|
|
|
},
|
|
|
|
filterVideos: (dir: DirectoryDTO): PhotoDTO[] => {
|
|
|
|
return dir.media.filter(m => MediaDTOUtils.isPhoto(m)) as PhotoDTO[];
|
|
|
|
}
|
|
|
|
};
|