-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrenderer.js
More file actions
524 lines (407 loc) · 11.7 KB
/
Copy pathrenderer.js
File metadata and controls
524 lines (407 loc) · 11.7 KB
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
//frontend
const player = document.getElementById('player');
const userSelect = document.getElementById("userSelect");
window.addEventListener("DOMContentLoaded", async () => {
const openFolderBtn = document.getElementById('openFolder');
openFolderBtn.addEventListener('click', () => {
selectFolderAndLoad();
});
const addUserBtn = document.getElementById("addUserBtn");
const removeUserBtn = document.getElementById("removeUserBtn");
const newUserInput = document.getElementById("newUserInput");
addUserBtn.onclick = () => {
const name = newUserInput.value.trim();
if (!name) {
alert("Enter a username");
return;
}
addUser(name);
newUserInput.value = ""; // clear input
};
removeUserBtn.onclick = () => {
removeUser(currentUser);
};
//load last folder used
const lastFolder = window.fileflixAPI.getLastFolder();
if (lastFolder && window.fileflixAPI.folderExists(lastFolder)) {
loadFolderDirectly(lastFolder);
}
});
userSelect.addEventListener("change", (e) => {
currentUser = e.target.value;
renderView();
});
let libraryTree = {};
let currentPath = [];
let currentFile = null;
let rootFolder = null;
let progressFilePath = null;
let progressData = {};
let lastSave = 0;
let fileMap = {};
let currentUser = "default";
let markedComplete = false;
//when episode ends play next
player.onended = () => {
if (!currentFile || markedComplete) return;
const next = getNextEpisode(currentFile);
if (next) {
playFile(next);
renderView();
}
};
//save video progress
player.ontimeupdate = () => {
if (!currentFile) return;
const now = Date.now();
if (now - lastSave < 2000) return;//every 2secs
const duration = player.duration;
const time = player.currentTime;
saveProgress(currentFile, time, duration);
lastSave = now;
};
function getUserData() {
if (!progressData.users) progressData.users = {};
if (!progressData.users[currentUser]) {
progressData.users[currentUser] = { watchProgress: {} };
}
return progressData.users[currentUser].watchProgress;
}
function loadUsers() {
const users = Object.keys(progressData.users || {});
userSelect.innerHTML = "";
users.forEach(u => {
const opt = document.createElement("option");
opt.value = u;
opt.textContent = u;
userSelect.appendChild(opt);
});
userSelect.value = currentUser;
}
function addUser(username) {
if (!progressData.users) progressData.users = {};
if (progressData.users[username]) {
alert("User already exists");
return;
}
progressData.users[username] = {
watchProgress: {}
};
currentUser = username;
window.fileflixAPI.writeJSON(progressFilePath, progressData);
loadUsers();
renderView();
}
function removeUser(username) {
if (Object.keys(progressData.users).length === 1) {
alert("Cannot delete the last user");
return;
}
if (!progressData.users[username]) return;
if (!confirm(`Delete user "${username}"?`)) return;
delete progressData.users[username];
// fallback user
currentUser = Object.keys(progressData.users)[0] || "default";
window.fileflixAPI.writeJSON(progressFilePath, progressData);
loadUsers();
renderView();
}
//current folder
function getCurrentNode() {
let node = libraryTree;
for (const part of currentPath) {
node = node[part];
}
return node;
}
function getParentNode(path) {
let node = libraryTree;
for (let i = 0; i < path.length - 1; i++) {
node = node[path[i]];
if (!node) return null;
}
return node;
}
async function selectFolderAndLoad() {
const folderPath = await window.fileflixAPI.selectFolder();
if (!folderPath) return;
window.fileflixAPI.saveLastFolder(folderPath);//save folder to auto launch next time
rootFolder = folderPath;
progressFilePath = rootFolder + "/.fileflix.json";
// load saved progress
progressData = window.fileflixAPI.readJSON(progressFilePath) || {};
// load user
if (!progressData.users) {
progressData.users = {
default: { watchProgress: {} }
};
}
currentUser = Object.keys(progressData.users)[0];
loadUsers();
// scan filesystem
const filePaths = window.fileflixAPI.scanFolder(folderPath);
// build file map
fileMap = {};
filePaths.forEach(fp => {
fileMap[fp] = fp;
});
// build tree
libraryTree = buildTreeFromPaths(filePaths, folderPath);
sortTree(libraryTree);
currentPath = [];
renderView();
}
function buildTreeFromPaths(paths, root) {
const tree = {};
paths.forEach(fullPath => {
const relativePath = fullPath.substring(root.length + 1);
const parts = relativePath.split(/[\\/]/);
let current = tree;
for (let i = 0; i < parts.length - 1; i++) {
const folder = parts[i];
if (!current[folder]) {
current[folder] = {};
}
current = current[folder];
}
if (!current._files) {
current._files = [];
}
current._files.push(fullPath);
});
return tree;
}
function sortTree(node) {
// sort files in this folder
if (node._files) {
node._files.sort((a, b) => a.localeCompare(b, undefined, { numeric: true }));
}
// recursively sort subfolders
Object.keys(node).forEach(key => {
if (key !== "_files") {
sortTree(node[key]);
}
});
}
//play video
function playFile(filePath) {
currentFile = filePath;
markedComplete = false;
player.src = "file://" + filePath;
const saved = getUserData()[filePath];
const seekAndPlay = () => {
if (saved && saved.time > 0) {
player.currentTime = saved.time;
}
player.play();
player.removeEventListener("loadeddata", seekAndPlay);
};
player.addEventListener("loadeddata", seekAndPlay);
}
//renders
function renderView() {
const container = document.getElementById("library");
container.innerHTML = "";
// Continue watching--
const continueList = getContinueWatching();
if (continueList.length > 0) {
const section = document.createElement("div");
section.innerHTML = "<h2>Continue Watching</h2>";
continueList.forEach(item => {
const btn = document.createElement("div");
btn.textContent = item.file.split(/[\\/]/).pop();
btn.className = "video-card";
btn.onclick = () => {
playFile(item.file);
};
section.appendChild(btn);
});
container.appendChild(section);
}
// Choose video--
const node = getCurrentNode();
// Breadcrumb UI (path navigation)
const breadcrumb = document.createElement("div");
breadcrumb.innerHTML = "<h2>Library</h2>";
breadcrumb.className = "breadcrumb";
const pathParts = ["Root", ...currentPath];
pathParts.forEach((part, index) => {
const btn = document.createElement("span");
btn.textContent = part;
btn.onclick = () => {
currentPath = pathParts.slice(1, index);
renderView();
};
breadcrumb.appendChild(btn);
if (index < pathParts.length - 1) {
const sep = document.createElement("span");
sep.textContent = " > ";
sep.className = "sep";
breadcrumb.appendChild(sep);
}
});
container.appendChild(breadcrumb);
// Folders
Object.keys(node).forEach(key => {
if (key === "_files") return;
const btn = document.createElement("button");
btn.textContent = key;
btn.className = "folder-button";
btn.onclick = () => {
currentPath.push(key);
renderView();
};
container.appendChild(btn);
});
// Files in current folder
if (node._files) {
node._files.forEach(file => {
const card = document.createElement("div");
card.className = "video-card";
card.textContent = file.split(/[\\/]/).pop();
card.onclick = () => {
playFile(file);
};
container.appendChild(card);
});
}
}
//video has less than 10secs left
function isNearEnd(time, duration) {
return duration && (duration - time) <= 10;
}
function saveProgress(file, time, duration) {
if (!progressFilePath) return;
const userProgress = getUserData();
//if video near end
if (!markedComplete && isNearEnd(time, duration)) {
markedComplete = true;
delete userProgress[file]; // remove from continue watching
const next = getNextEpisode(file);
if (next && next !== file) {
// add next episode at time 0
userProgress[next] = {
time: 0,
duration: 0,
lastWatched: Date.now()
};
}
window.fileflixAPI.writeJSON(progressFilePath, progressData);
renderView();
return;
}
//normal save
userProgress[file] = {
time,
duration,
lastWatched: Date.now()
};
window.fileflixAPI.writeJSON(progressFilePath, progressData);
}
function getContinueWatching() {
const userProgress = getUserData();
return Object.entries(userProgress)
.map(([file, info]) => ({ file, ...info }))
.sort((a, b) => b.lastWatched - a.lastWatched)
.filter(item => !item.duration || item.time < item.duration - 10);
}
function getNextInFolder(currentFile, files) {
const index = files.findIndex(f => f.name === currentFile.name);
return files[index + 1] || null;
}
function getNextVideo(currentFile, node) {
const files = node._files;
if (!files) return null;
const index = files.findIndex(f => f === currentFile);
if (index === -1) return null;
return files[index + 1] || null;
}
function getShowContext(filePath) {
const parts = filePath.split(/[\\/]/);
// Example: Show / Season 01 / Episode 01.mp4
const fileName = parts.pop();
const season = parts.pop();
const show = parts.pop();
return {
showPath: parts.join("/"),
show,
season,
fileName
};
}
function getAllSeasons(currentFile) {
const parts = currentFile.split(/[\\/]/);
const fileName = parts.pop();
const seasonFolder = parts.pop();
const showFolder = parts.pop();
const showNode = libraryTree[showFolder];
if (!showNode) return [];
// collect season folders
const seasons = Object.keys(showNode)
.filter(k => k !== "_files")
.sort(); // Season 01, Season 02
return {
showNode,
seasons,
currentSeason: seasonFolder,
currentFile
};
}
function getNextEpisode(filePath) {
const relative = filePath.replace(rootFolder, "").replace(/^[\\/]/, "");
const parts = relative.split(/[\\/]/);
let node = libraryTree;
// walk to the folder containing the file
for (let i = 0; i < parts.length - 1; i++) {
node = node[parts[i]];
if (!node) return null;
}
// same-folder next episode
if (node._files) {
const files = node._files;
const index = files.indexOf(filePath);
if (index !== -1 && index < files.length - 1) {
return files[index + 1];
}
}
// move up to parent folder (season logic)
const parentParts = parts.slice(0, -1);
const currentFolder = parentParts.pop();
let parentNode = libraryTree;
for (const part of parentParts) {
parentNode = parentNode[part];
if (!parentNode) return null;
}
const folders = Object.keys(parentNode)
.filter(k => k !== "_files")
.sort();
const index = folders.indexOf(currentFolder);
for (let i = index + 1; i < folders.length; i++) {
const nextFolder = parentNode[folders[i]];
if (nextFolder?._files?.length > 0) {
return nextFolder._files[0];
}
}
return null;
}
async function loadFolderDirectly(folderPath) {
rootFolder = folderPath;
progressFilePath = rootFolder + "/.fileflix.json";
progressData = window.fileflixAPI.readJSON(progressFilePath) || {};
if (!progressData.users) {
progressData.users = {
default: { watchProgress: {} }
};
}
currentUser = Object.keys(progressData.users)[0];
loadUsers();
const filePaths = window.fileflixAPI.scanFolder(folderPath);
fileMap = {};
filePaths.forEach(fp => {
fileMap[fp] = fp;
});
libraryTree = buildTreeFromPaths(filePaths, folderPath);
sortTree(libraryTree);
currentPath = [];
renderView();
}