-
Notifications
You must be signed in to change notification settings - Fork 203
/
Copy pathLibraryActions.ts
331 lines (281 loc) · 8.41 KB
/
LibraryActions.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
import * as fs from 'fs';
import path from 'path';
import electron, { ipcRenderer } from 'electron';
import queue from 'queue';
import store from '../store';
import types from '../action-types';
import * as app from '../../lib/app';
import * as utils from '../../lib/utils';
import * as m3u from '../../lib/utils-m3u';
import { TrackEditableFields, SortBy, TrackModel } from '../../../shared/types/museeks';
import channels from '../../../shared/lib/ipc-channels';
import logger from '../../../shared/lib/logger';
import * as PlaylistsActions from './PlaylistsActions';
import * as ToastsActions from './ToastsActions';
/**
* Load tracks from database
*/
export const refresh = async (): Promise<void> => {
try {
const tracks = await app.db.Track.find().execAsync();
store.dispatch({
type: types.LIBRARY_REFRESH,
payload: {
tracks,
},
});
} catch (err) {
logger.warn(err);
}
};
/**
* Filter tracks by search
*/
export const search = (value: string): void => {
store.dispatch({
type: types.FILTER_SEARCH,
payload: {
search: value,
},
});
};
/**
* Filter tracks by sort query
*/
export const sort = (sortBy: SortBy): void => {
store.dispatch({
type: types.LIBRARY_SORT,
payload: {
sortBy,
},
});
};
const scanPlaylists = async (paths: string[]) => {
return Promise.all(
paths.map(async (filePath) => {
try {
const playlistFiles = m3u.parse(filePath);
const playlistName = path.parse(filePath).name;
const existingTracks: TrackModel[] = await app.db.Track.findAsync({
$or: playlistFiles.map((filePath) => ({ path: filePath })),
});
await PlaylistsActions.create(
playlistName,
existingTracks.map((track) => track._id),
filePath
);
} catch (err) {
logger.warn(err);
}
})
);
};
const scan = {
processed: 0,
total: 0,
};
const scanTracks = async (paths: string[]): Promise<TrackModel[]> => {
return new Promise((resolve, reject) => {
if (paths.length === 0) resolve([]);
try {
// Instantiate queue
let scannedFiles: TrackModel[] = [];
// eslint-disable-next-line
// @ts-ignore Outdated types
// https://github.com/jessetane/queue/pull/15#issuecomment-414091539
const scanQueue = queue();
scanQueue.concurrency = 32;
scanQueue.autostart = true;
scanQueue.on('end', async () => {
scan.processed = 0;
scan.total = 0;
resolve(scannedFiles);
});
scanQueue.on('success', () => {
// Every 100 scans, update progress bar
if (scan.processed % 100 === 0) {
// Progress bar update
store.dispatch({
type: types.LIBRARY_REFRESH_PROGRESS,
payload: {
processed: scan.processed,
total: scan.total,
},
});
// Add tracks to the library view
const tracks = [...scannedFiles];
scannedFiles = []; // Reset current selection
store.dispatch({
type: types.LIBRARY_ADD_TRACKS,
payload: {
tracks,
},
});
}
});
// End queue instantiation
scan.total += paths.length;
paths.forEach((filePath) => {
scanQueue.push(async (callback) => {
try {
// Normalize (back)slashes on Windows
filePath = path.resolve(filePath);
// Check if there is an existing record in the DB
const existingDoc = await app.db.Track.findOneAsync({ path: filePath });
// If there is existing document
if (!existingDoc) {
// Get metadata
const track = await utils.getMetadata(filePath);
const insertedDoc: TrackModel = await app.db.Track.insertAsync(track);
scannedFiles.push(insertedDoc);
}
scan.processed++;
} catch (err) {
logger.warn(err);
}
if (callback) callback();
});
});
} catch (err) {
reject(err);
}
});
};
/**
* Add tracks to Library
*/
export const add = async (pathsToScan: string[]): Promise<TrackModel[]> => {
store.dispatch({
type: types.LIBRARY_REFRESH_START,
});
try {
// Get all valid track paths
// TODO move this whole function to main process
const [supportedTrackFiles, supportedPlaylistsFiles] = await ipcRenderer.invoke(
channels.LIBRARY_SCAN_TRACKS,
pathsToScan
);
if (supportedTrackFiles.length === 0 && supportedPlaylistsFiles.length === 0) {
store.dispatch({
type: types.LIBRARY_REFRESH_END,
});
return [];
}
// 5. Scan tracks then scan playlists
const importedTracks = await scanTracks(supportedTrackFiles);
await scanPlaylists(supportedPlaylistsFiles);
await refresh();
await PlaylistsActions.refresh();
return importedTracks;
} catch (err) {
ToastsActions.add('danger', 'An error occured when scanning the library');
logger.warn(err);
return [];
} finally {
store.dispatch({
type: types.LIBRARY_REFRESH_END,
});
}
};
/**
* remove tracks from library
*/
export const remove = async (tracksIds: string[]): Promise<void> => {
// not calling await on it as it calls the synchonous message box
const options: Electron.MessageBoxOptions = {
buttons: ['Cancel', 'Remove'],
title: 'Remove tracks from library?',
message: `Are you sure you want to remove ${tracksIds.length} element(s) from your library?`,
type: 'warning',
};
const result: electron.MessageBoxReturnValue = await ipcRenderer.invoke(channels.DIALOG_MESSAGE_BOX, options);
if (result.response === 1) {
// button possition, here 'remove'
// Remove tracks from the Track collection
app.db.Track.removeAsync({ _id: { $in: tracksIds } }, { multi: true });
store.dispatch({
type: types.LIBRARY_REMOVE_TRACKS,
payload: {
tracksIds,
},
});
// That would be great to remove those ids from all the playlists, but it's not easy
// and should not cause strange behaviors, all PR for that would be really appreciated
// TODO: see if it's possible to remove the Ids from the selected state of TracksList as it "could" lead to strange behaviors
}
};
/**
* Reset the library
*/
export const reset = async (): Promise<void> => {
try {
const options: Electron.MessageBoxOptions = {
buttons: ['Cancel', 'Reset'],
title: 'Reset library?',
message: 'Are you sure you want to reset your library? All your tracks and playlists will be cleared.',
type: 'warning',
};
const result = await ipcRenderer.invoke(channels.DIALOG_MESSAGE_BOX, options);
if (result.response === 1) {
store.dispatch({
type: types.LIBRARY_REFRESH_START,
});
await app.db.Track.removeAsync({}, { multi: true });
await app.db.Playlist.removeAsync({}, { multi: true });
store.dispatch({
type: types.LIBRARY_RESET,
});
store.dispatch({
type: types.LIBRARY_REFRESH_END,
});
await refresh();
}
} catch (err) {
logger.error(err);
}
};
/**
* Update the play count attribute.
*/
export const incrementPlayCount = async (source: string): Promise<void> => {
const query = { src: source }; // HACK Not great, should be done with an _id
const update = { $inc: { playcount: 1 } };
try {
await app.db.Track.updateAsync(query, update);
} catch (err) {
logger.warn(err);
}
};
/**
* Update the id3 attributes.
* IMPROVE ME: add support for writing metadata (hint: node-id3 does not work
* well).
*
* @param trackId The ID of the track to update
* @param newFields The fields to be updated and their new value
*/
export const updateTrackMetadata = async (trackId: string, newFields: TrackEditableFields): Promise<void> => {
const query = { _id: trackId };
let track: TrackModel = await app.db.Track.findOneAsync(query);
track = {
...track,
...newFields,
loweredMetas: utils.getLoweredMeta(newFields),
};
if (!track) {
throw new Error('No track found while trying to update track metadata');
}
await app.db.Track.updateAsync(query, track);
await refresh();
};
/**
* Set highlight trigger for a track
*/
export const highlightPlayingTrack = (highlight: boolean): void => {
store.dispatch({
type: types.LIBRARY_HIGHLIGHT_PLAYING_TRACK,
payload: {
highlight,
},
});
};