-
Notifications
You must be signed in to change notification settings - Fork 210
/
DiskMangerWorker.ts
142 lines (124 loc) · 4.67 KB
/
DiskMangerWorker.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import * as fs from 'fs';
import {Stats} from 'fs';
import * as path from 'path';
import {DirectoryDTO} from '../../../common/entities/DirectoryDTO';
import {PhotoDTO} from '../../../common/entities/PhotoDTO';
import {ProjectPath} from '../../ProjectPath';
import {Config} from '../../../common/config/private/Config';
import {VideoDTO} from '../../../common/entities/VideoDTO';
import {FileDTO} from '../../../common/entities/FileDTO';
import {MetadataLoader} from './MetadataLoader';
import {Logger} from '../../Logger';
const LOG_TAG = '[DiskManagerTask]';
export class DiskMangerWorker {
private static readonly SupportedEXT = {
photo: [
'.gif',
'.jpeg', '.jpg', '.jpe',
'.png',
'.webp',
'.svg'
],
video: [
'.mp4',
'.webm',
'.ogv',
'.ogg'
],
metaFile: [
'.gpx'
]
};
public static calcLastModified(stat: Stats) {
return Math.max(stat.ctime.getTime(), stat.mtime.getTime());
}
public static normalizeDirPath(dirPath: string) {
return path.normalize(path.join('.' + path.sep, dirPath));
}
public static scanDirectory(relativeDirectoryName: string, maxPhotos: number = null, photosOnly: boolean = false): Promise<DirectoryDTO> {
return new Promise<DirectoryDTO>((resolve, reject) => {
relativeDirectoryName = this.normalizeDirPath(relativeDirectoryName);
const directoryName = path.basename(relativeDirectoryName);
const directoryParent = path.join(path.dirname(relativeDirectoryName), path.sep);
const absoluteDirectoryName = path.join(ProjectPath.ImageFolder, relativeDirectoryName);
const stat = fs.statSync(path.join(ProjectPath.ImageFolder, relativeDirectoryName));
const directory: DirectoryDTO = {
id: null,
parent: null,
name: directoryName,
path: directoryParent,
lastModified: this.calcLastModified(stat),
lastScanned: Date.now(),
directories: [],
isPartial: false,
mediaCount: 0,
media: [],
metaFile: []
};
fs.readdir(absoluteDirectoryName, async (err, list: string[]) => {
if (err) {
return reject(err);
}
try {
for (let i = 0; i < list.length; i++) {
const file = list[i];
const fullFilePath = path.normalize(path.join(absoluteDirectoryName, file));
if (fs.statSync(fullFilePath).isDirectory()) {
if (photosOnly === true) {
continue;
}
const d = await DiskMangerWorker.scanDirectory(path.join(relativeDirectoryName, file),
Config.Server.indexing.folderPreviewSize, true
);
d.lastScanned = 0; // it was not a fully scan
d.isPartial = true;
directory.directories.push(d);
} else if (DiskMangerWorker.isImage(fullFilePath)) {
directory.media.push(<PhotoDTO>{
name: file,
directory: null,
metadata: await MetadataLoader.loadPhotoMetadata(fullFilePath)
});
if (maxPhotos != null && directory.media.length > maxPhotos) {
break;
}
} else if (photosOnly === false && Config.Client.Video.enabled === true &&
DiskMangerWorker.isVideo(fullFilePath)) {
try {
directory.media.push(<VideoDTO>{
name: file,
directory: null,
metadata: await MetadataLoader.loadVideoMetadata(fullFilePath)
});
} catch (e) {
Logger.warn('Media loading error, skipping: ' + file + ', reason: ' + e.toString());
}
} else if (photosOnly === false && Config.Client.MetaFile.enabled === true &&
DiskMangerWorker.isMetaFile(fullFilePath)) {
directory.metaFile.push(<FileDTO>{
name: file,
directory: null,
});
}
}
directory.mediaCount = directory.media.length;
return resolve(directory);
} catch (err) {
return reject({error: err});
}
});
});
}
private static isImage(fullPath: string) {
const extension = path.extname(fullPath).toLowerCase();
return this.SupportedEXT.photo.indexOf(extension) !== -1;
}
private static isVideo(fullPath: string) {
const extension = path.extname(fullPath).toLowerCase();
return this.SupportedEXT.video.indexOf(extension) !== -1;
}
private static isMetaFile(fullPath: string) {
const extension = path.extname(fullPath).toLowerCase();
return this.SupportedEXT.metaFile.indexOf(extension) !== -1;
}
}