-
Notifications
You must be signed in to change notification settings - Fork 0
/
filenameHandler.js
145 lines (90 loc) · 3.55 KB
/
filenameHandler.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
/****************************************************************************
* filenameHandler.js
* openacousticdevices.info
* September 2024
*****************************************************************************/
'use strict';
/* Operation types */
const SPLIT = 0;
const DOWNSAMPLE = 1;
const EXPAND = 2;
const SYNC = 3;
/* Filename and dates regexes */
const regexes = [
/^([A-Za-z-_0-9]+_)?(\d{8}_\d{6})(_SYNC)?\.WAV$/,
/^([A-Za-z-_0-9]+_)?(\d{8}_\d{6})((_\d{3})|(_SYNC))?\.WAV$/,
/^([0-9A-F]{16}_)?((\d{8}_)?\d{6})T\.WAV$/,
/^([0-9A-F]{16}_)?(\d{8}_\d{6})\.WAV$/
];
const DATE_REGEX = /Recorded at (\d\d):(\d\d):(\d\d) (\d\d)\/(\d\d)\/(\d\d\d\d) \(UTC([-|+]\d+)?:?(\d\d)?\)/;
const DOWNSAMPLE_TIMESTRING_REGEX = /(\d{8}_\d{6}_\d{3})\.WAV$/;
const POSTFIX_REGEX = /_SYNC\.WAV$/;
/* Public functions */
function getFilenameRegex (type) {
return regexes[type];
}
function checkFilenameAgainstHeader (type, filename, comment, deviceID) {
/* Match with filename regex */
const matches = filename.match(regexes[type]);
if (!matches) {
return {
success: false,
error: 'File name is not valid.'
};
}
/* Extract filename time string */
let originalTimestring = matches[2];
if (type === DOWNSAMPLE) {
const downsampleMatches = filename.match(DOWNSAMPLE_TIMESTRING_REGEX);
if (downsampleMatches) originalTimestring = downsampleMatches[1];
}
/* Check existing prefix */
const existingPrefix = matches[1] ? matches[1] : '';
if (type === EXPAND || type === SYNC) {
if (matches[1]) {
const prefix = matches[1].substring(0, matches[1].length - 1);
if ('AudioMoth ' + prefix !== deviceID) {
return {
success: false,
error: 'Device ID in the input WAV file header does not match the file name.'
};
}
}
}
/* Check existing postfix */
const existingPostfix = (type === SPLIT || type === DOWNSAMPLE) && POSTFIX_REGEX.test(filename) ? '_SYNC' : '';
/* Check time against comments field */
let originalTimestamp = null;
if (type === SPLIT || type === EXPAND || type === SYNC) {
if (DATE_REGEX.test(comment) === false) {
return {
success: false,
error: 'Cannot find timestamp in the input WAV file header.'
};
}
const dateMatches = DATE_REGEX.exec(comment);
const expectedTimestring = type === EXPAND && !matches[3] ? dateMatches[1] + dateMatches[2] + dateMatches[3] : dateMatches[6] + dateMatches[5] + dateMatches[4] + '_' + dateMatches[1] + dateMatches[2] + dateMatches[3];
if (expectedTimestring !== originalTimestring) {
return {
success: false,
error: 'Timestamp in the input WAV file header does not match the file name.'
};
}
originalTimestamp = Date.UTC(dateMatches[6], dateMatches[5] - 1, dateMatches[4], dateMatches[1], dateMatches[2], dateMatches[3]);
}
/* Return success */
return {
success: true,
existingPrefix: existingPrefix,
existingPostfix: existingPostfix,
originalTimestamp: originalTimestamp,
originalTimestring: originalTimestring
};
}
/* Exports */
exports.getFilenameRegex = getFilenameRegex;
exports.checkFilenameAgainstHeader = checkFilenameAgainstHeader;
exports.SPLIT = SPLIT;
exports.DOWNSAMPLE = DOWNSAMPLE;
exports.EXPAND = EXPAND;
exports.SYNC = SYNC;