forked from Stage-Whisper/Stage-Whisper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
loadDatabase.ts
180 lines (160 loc) · 7.19 KB
/
loadDatabase.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import { LoadDatabaseResponse } from '../../channels';
import { ipcMain, IpcMainInvokeEvent } from 'electron';
// File to import all data from the data folder
import { join } from 'path';
import { readdirSync, readFileSync } from 'fs';
import { app } from 'electron';
import { entry, entryTranscription } from '../../types';
import { Channels } from '../../channels';
// Paths
const rootPath = app.getPath('userData'); // Path to the top level of the data folder
const storePath = join(rootPath, 'store'); // Path to the store folder
const dataPath = join(storePath, 'data'); // Path to the data folder
// Layout of the app data
// store/
// ├── app_preferences.json { darkmode, langauge, ... }
// ├── app_config.json { version, repo, ... }
// └── data/
// ├── entry_{uuidv4}/
// │ ├── entry_config.json { inQueue, title, model, etc }
// │ ├── audio/
// │ │ ├── file.mp3 | file.opus | file.wav
// │ │ └── parameters.json { addedOn, language, type }
// │ └── transcriptions/
// │ └── {uuid}/
// │ ├── parameters.json { language, transcribedOn, model, ... }
// │ ├── transcript.vtt (Use VTT as source of truth and compile to txt)
// │ └── transcript.txt
// └── entry_1bfb7987-da1d-4a02-87a9-e841c5dd4e29/
// ├── entry_config.json
// ├── audio/
// │ ├── fancy_twice_sample.opus
// │ └── parameters.json {addedOn: 30-12-9999, language: Korean, tpye: Opus
// └── transcriptions/
// ├── 3a8b37f4-3a41-4522-b3cc-1c6e28a3ab75/
// │ ├── parameters.json { langauge: English, transcribedOn: 30-12-9999, model: baseEn, ... }
// │ ├── transcript.vtt
// │ └── transcript.txt
// └── 7fcf511c-f9d5-4bdc-a427-19a28f6e8ca1/
// ├── parameters.json { langauge: Korean, transcribedOn: 30-12-9999, model: large, ... }
// ├── transcript.vtt
// └── transcript.txt
// Error handling
// enum getEntriesErrors {
// NO_DATA_FOLDER = 'NO_DATA_FOLDER',
// NO_CONFIG_FILE = 'NO_CONFIG_FILE',
// NO_AUDIO_FOLDER = 'NO_AUDIO_FOLDER',
// NO_AUDIO_FILE = 'NO_AUDIO_FILE',
// NO_TRANSCRIPTIONS_FOLDER = 'NO_TRANSCRIPTIONS_FOLDER',
// NO_ENTRIES = 'NO_ENTRIES'
// }
// Get all entries
export default ipcMain.handle(
Channels.loadDatabase,
async (_event: IpcMainInvokeEvent): Promise<LoadDatabaseResponse> => {
const entries: entry[] = [];
console.log('Getting entries');
console.log('Data path: ', dataPath);
// Get all entries
try {
const entryFolders = readdirSync(dataPath, { withFileTypes: true }).filter((dirent) => dirent.isDirectory());
console.log('Found %s Entry Folders', entryFolders.length);
// For each entry folder get the config and audio files and add them to the entries array
try {
entryFolders.forEach((entryFolder) => {
// Check if the entry folder has a config file
const entryPath = join(dataPath, entryFolder.name);
const configPath = join(entryPath, 'entry_config.json');
if (!readdirSync(join(entryPath)).includes('entry_config.json')) {
// throw new Error(`Entry ${entryFolder.name} does not have a config file`);
console.log(`Entry ${entryFolder.name} does not have a config file`);
return;
}
// Check if the entry folder has an audio folder
const audioFolderPath = join(entryPath, 'audio');
if (!readdirSync(join(entryPath)).includes('audio')) {
throw new Error(`Entry ${entryFolder.name} does not have an audio folder`);
}
// Check if the entry folder has a transcriptions folder and if it has any transcriptions
const transcriptionFolderPath = join(entryPath, 'transcriptions');
if (!readdirSync(join(entryPath)).includes('transcriptions')) {
throw new Error(`Entry ${entryFolder.name} does not have a transcriptions folder`);
}
// Get the config file
const config = JSON.parse(readFileSync(configPath, 'utf8')) as entry['config'];
// Get the audio file
const audio = JSON.parse(readFileSync(join(audioFolderPath, 'parameters.json'), 'utf8')) as entry['audio'];
// Get the transcriptions
const transcriptions: entryTranscription[] = [];
readdirSync(transcriptionFolderPath, { withFileTypes: true })
.filter((dirent) => dirent.isDirectory())
.forEach((transcriptionFolder) => {
// Get the parameters file
const parameters = JSON.parse(
readFileSync(join(transcriptionFolderPath, transcriptionFolder.name, 'parameters.json'), 'utf8')
);
// Get the transcript file
const transcript = readFileSync(
join(transcriptionFolderPath, transcriptionFolder.name, 'transcript.vtt'),
'utf8'
);
// Add the transcription to the transcriptions array
const transcription: entryTranscription = {
uuid: transcriptionFolder.name,
transcribedOn: parameters.transcribedOn,
language: parameters.language,
model: parameters.model,
path: join(transcriptionFolderPath, transcriptionFolder.name),
vtt: transcript,
status: parameters.status,
progress: parameters.progress,
translated: parameters.translated,
completedOn: parameters.completedOn,
error: parameters.error
};
transcriptions.push(transcription);
});
// Add the entry to the entries array
const entry: entry = {
path: join(entryPath),
config: {
name: config.name,
description: config.description,
inQueue: config.inQueue,
queueWeight: config.queueWeight,
created: config.created,
tags: config.tags,
activeTranscription: config.activeTranscription,
uuid: config.uuid
},
audio: {
name: audio.name,
addedOn: audio.addedOn,
fileLength: audio.fileLength,
language: audio.language,
type: audio.type,
path: join(audioFolderPath, audio.name)
},
transcriptions: transcriptions
};
entries.push(entry);
});
return { entries: entries };
} catch (error) {
console.error(error);
if (error instanceof Error) {
return { entries: entries, error: error.message };
} else {
return { entries: entries, error: 'Unknown error' };
}
}
} catch (error) {
console.error(error);
if (error instanceof Error) {
return { entries: entries, error: error.message };
} else {
return { entries: entries, error: 'Unknown error' };
}
}
}
);