-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheckDirectories.js
148 lines (125 loc) · 3.73 KB
/
checkDirectories.js
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
143
144
145
146
147
148
import fs from 'fs';
import chalk from 'chalk';
import path from 'path';
export const checkDirectories = () => {
let sourceIsEmpty = false;
if (!fs.existsSync(process.env.SOURCE_DIR)) {
console.error(
chalk.yellow.bold('WARNING — NO SD CARD\n\n') +
'Cannot find ' +
process.env.SOURCE_DIR +
'\n\n' +
chalk.bold(' Is the SD card mounted?\n\n') +
chalk.dim(' Is the path correct in .env?\n')
);
sourceIsEmpty = true;
}
let subSourceDirs = null;
let dcimDirPath = null;
if (!sourceIsEmpty) {
// get all sub directories in source
subSourceDirs = fs
.readdirSync(process.env.SOURCE_DIR, {
withFileTypes: true,
})
.filter((dirent) => dirent.isDirectory())
.map((dirent) => dirent.name)
.map((dir) => process.env.SOURCE_DIR + dir + '/');
// search for DCIM folders within subSourceDirs
dcimDirPath = [];
subSourceDirs.forEach((dir) => {
if (fs.existsSync(dir + 'DCIM/')) {
dcimDirPath.push(dir + 'DCIM/');
}
});
// if there are multiple folders report a warning
if (dcimDirPath.length > 1) {
console.error(
chalk.yellow.bold('WARNING\n\n') +
'Multiple DCIM folders found! ' +
'There are too many drives connected to the computer.\n' +
'Please connect ' +
chalk.bold('only one drive containing your video files.') +
'\n'
);
process.exit(0);
}
dcimDirPath = dcimDirPath[0];
}
if (!fs.existsSync(process.env.TARGET_DIR)) {
console.error(
chalk.yellow.bold('WARNING\n\n') +
`${chalk.white.bgRed.bold('Cannot find the directory:')} ${
process.env.TARGET_DIR
}
${chalk.bold('Is the external hard drived mounted?\n')}
${chalk.dim('Is the path correct in .env?')}
`
);
process.exit(0);
}
if (!fs.existsSync(process.env.CLOUD_DIR)) {
console.error(
`${chalk.white.bgRed.bold('I cannot find the directory:')} ${
process.env.CLOUD_DIR
}
${chalk.dim('Is the path correct in .env?')}
`
);
process.exit(0);
}
// get all video folder matching the pattern defined in .env
// ... if sd card path was found earlier
let videoFolders = null;
let videoFiles = null;
let videoFilePaths = null;
let videoPaths = null;
if (!sourceIsEmpty) {
videoFolders = fs
.readdirSync(dcimDirPath)
.filter((i) => i.includes(process.env.VIDEO_DIR_PATTERN));
if (videoFolders.length === 0) {
console.error(
chalk.yellow.bold('WARNING — SD CARD EMPTY\n\n') +
`Cannot find any directories containing: '${
process.env.VIDEO_DIR_PATTERN
}' at ${dcimDirPath}
${chalk.bold('Is your SD card empty?')}
${chalk.dim(
'Check if the pattern on your SD card matches the one defined in .env'
)}
`
);
sourceIsEmpty = true;
}
videoPaths = videoFolders.map((i) => dcimDirPath + i);
videoFilePaths = [];
videoFiles = [];
videoPaths.forEach((dir) => {
fs.readdirSync(dir).forEach((file) => {
videoFiles.push(file);
videoFilePaths.push(dir + '/' + file);
});
});
// make sure only mp4 files are included
videoFiles = videoFiles.filter(
(file) =>
path.extname(file).toLowerCase() === '.mp4' ||
path.extname(file).toLowerCase() === '.mts'
);
// make sure only mp4 files are included
videoFilePaths = videoFilePaths.filter(
(file) =>
path.extname(file).toLowerCase() === '.mp4' ||
path.extname(file).toLowerCase() === '.mts'
);
}
return {
videoFolders,
videoPaths,
videoFiles,
videoFilePaths,
dcimDirPath,
sourceIsEmpty,
};
};