nexus33rd commited on
Commit
e7ff9ac
·
verified ·
1 Parent(s): d831bc1

Upload 20 files

Browse files
Files changed (2) hide show
  1. app.py +4 -4
  2. static/app.js +56 -37
app.py CHANGED
@@ -470,10 +470,10 @@ def serialize_torrent(torrent, state_entry: dict | None = None) -> dict:
470
  else:
471
  display_speed_label = f"{upload_provider} Upload" if gofile_uploading else "Download"
472
  display_speed_meta = f"{gofile_progress}%" if gofile_uploading else ""
473
- if not gofile_uploading and torrent_progress >= 1 and torrent_upspeed > 0:
474
- display_speed = format_speed(torrent_upspeed)
475
- display_speed_label = "Seed Upload"
476
- display_speed_meta = ""
477
 
478
  can_gofile_upload = (
479
  bool(content_path)
 
470
  else:
471
  display_speed_label = f"{upload_provider} Upload" if gofile_uploading else "Download"
472
  display_speed_meta = f"{gofile_progress}%" if gofile_uploading else ""
473
+ if not gofile_uploading and torrent_progress >= 1:
474
+ display_speed = format_speed(torrent_upspeed)
475
+ display_speed_label = "Seed Upload"
476
+ display_speed_meta = ""
477
 
478
  can_gofile_upload = (
479
  bool(content_path)
static/app.js CHANGED
@@ -57,7 +57,8 @@
57
  let pendingFolderUpload = null;
58
  let selectedFolderUploadMode = "zip";
59
  const dismissedStatusMessages = new Set();
60
- const pendingTorrentActions = new Map();
 
61
 
62
  function escapeHtml(value) {
63
  return String(value ?? "").replace(/[&<>"']/g, (char) => ({
@@ -204,27 +205,28 @@
204
  `;
205
  }
206
 
207
- function getPendingTorrentAction(hash) {
208
- const pending = pendingTorrentActions.get(hash);
209
- if (!pending) {
210
- return null;
211
- }
212
- if (Date.now() > pending.expiresAt) {
213
- pendingTorrentActions.delete(hash);
214
- return null;
215
- }
216
- return pending;
217
  }
218
 
219
- function setPendingTorrentAction(hash, action, ttlMs = 4000) {
220
- pendingTorrentActions.set(hash, {
221
- action,
222
- expiresAt: Date.now() + ttlMs,
223
- });
 
224
  }
225
 
226
- function clearPendingTorrentAction(hash) {
227
- pendingTorrentActions.delete(hash);
 
 
 
 
 
 
 
 
228
  }
229
 
230
  function applyOptimisticTorrentAction(action, hash) {
@@ -260,18 +262,18 @@
260
  renderTorrents();
261
  }
262
 
263
- function reconcilePendingTorrentState(nextTorrents) {
264
  nextTorrents.forEach((torrent) => {
265
- const pending = getPendingTorrentAction(torrent.hash);
266
- if (!pending) {
267
  return;
268
  }
269
 
270
  const rawState = String(torrent.state || "");
271
- if (pending.action === "pause") {
272
  const serverPaused = ["pausedDL", "pausedUP"].includes(rawState);
273
  if (serverPaused) {
274
- clearPendingTorrentAction(torrent.hash);
275
  return;
276
  }
277
  torrent.state = Number(torrent.progress || 0) >= 100 ? "pausedUP" : "pausedDL";
@@ -280,15 +282,18 @@
280
  torrent.can_resume = true;
281
  torrent.dlspeed = "0 B/s";
282
  torrent.upspeed = "0 B/s";
 
 
 
283
  if (torrent.display_speed_label === "Seed Upload") {
284
  torrent.display_speed = "0 B/s";
285
  }
286
  }
287
 
288
- if (pending.action === "resume") {
289
  const serverResumed = !["pausedDL", "pausedUP"].includes(rawState);
290
  if (serverResumed) {
291
- clearPendingTorrentAction(torrent.hash);
292
  return;
293
  }
294
  const isComplete = Number(torrent.progress || 0) >= 100;
@@ -296,6 +301,7 @@
296
  torrent.state_label = isComplete ? "Seeding" : "Downloading";
297
  torrent.can_pause = !torrent.gofile_uploading;
298
  torrent.can_resume = false;
 
299
  }
300
  });
301
  }
@@ -436,10 +442,10 @@
436
 
437
  function actionButtons(torrent) {
438
  const buttons = [];
439
- const pending = getPendingTorrentAction(torrent.hash);
440
 
441
- if (pending && (pending.action === "pause" || pending.action === "resume")) {
442
- const isPause = pending.action === "pause";
443
  buttons.push(`
444
  <button type="button" disabled class="font-display rounded-xl border border-white/10 bg-white/[0.04] px-3 py-2 text-xs font-medium text-white/75 opacity-60 transition">
445
  <i class="fa-solid fa-spinner mr-2 text-[11px] animate-spin"></i>${isPause ? "Pausing..." : "Resuming..."}
@@ -818,7 +824,7 @@
818
  }
819
  const data = await response.json();
820
  state.torrents = Array.isArray(data.torrents) ? data.torrents : [];
821
- reconcilePendingTorrentState(state.torrents);
822
  state.stats = data.stats || {};
823
  state.events = Array.isArray(data.events) ? data.events : [];
824
  updateConnection(true, state.stats.version || "-");
@@ -871,18 +877,27 @@
871
  : `/api/torrents/${hash}/${action}`;
872
 
873
  if (action === "pause" || action === "resume") {
874
- setPendingTorrentAction(hash, action);
875
- applyOptimisticTorrentAction(action, hash);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
876
  }
877
 
878
- const response = await fetch(target, {
879
- method,
880
- headers: { Accept: "application/json" },
881
- });
882
- const payload = await response.json();
883
  if (!response.ok || !payload.success) {
884
  if (action === "pause" || action === "resume") {
885
- clearPendingTorrentAction(hash);
886
  await refreshTorrents();
887
  }
888
  showToast(payload.message || "Aksi torrent gagal.", "error");
@@ -902,6 +917,10 @@
902
  showToast(payload.message || "Aksi berhasil diproses.");
903
  }
904
 
 
 
 
 
905
  await refreshTorrents();
906
  }
907
 
 
57
  let pendingFolderUpload = null;
58
  let selectedFolderUploadMode = "zip";
59
  const dismissedStatusMessages = new Set();
60
+ const inFlightTorrentActions = new Map();
61
+ const optimisticTorrentStates = new Map();
62
 
63
  function escapeHtml(value) {
64
  return String(value ?? "").replace(/[&<>"']/g, (char) => ({
 
205
  `;
206
  }
207
 
208
+ function getInFlightTorrentAction(hash) {
209
+ return inFlightTorrentActions.get(hash) || null;
 
 
 
 
 
 
 
 
210
  }
211
 
212
+ function setInFlightTorrentAction(hash, action) {
213
+ inFlightTorrentActions.set(hash, action);
214
+ }
215
+
216
+ function clearInFlightTorrentAction(hash) {
217
+ inFlightTorrentActions.delete(hash);
218
  }
219
 
220
+ function setOptimisticTorrentState(hash, action) {
221
+ optimisticTorrentStates.set(hash, action);
222
+ }
223
+
224
+ function clearOptimisticTorrentState(hash) {
225
+ optimisticTorrentStates.delete(hash);
226
+ }
227
+
228
+ function getOptimisticTorrentState(hash) {
229
+ return optimisticTorrentStates.get(hash) || null;
230
  }
231
 
232
  function applyOptimisticTorrentAction(action, hash) {
 
262
  renderTorrents();
263
  }
264
 
265
+ function reconcileOptimisticTorrentState(nextTorrents) {
266
  nextTorrents.forEach((torrent) => {
267
+ const optimisticAction = getOptimisticTorrentState(torrent.hash);
268
+ if (!optimisticAction) {
269
  return;
270
  }
271
 
272
  const rawState = String(torrent.state || "");
273
+ if (optimisticAction === "pause") {
274
  const serverPaused = ["pausedDL", "pausedUP"].includes(rawState);
275
  if (serverPaused) {
276
+ clearOptimisticTorrentState(torrent.hash);
277
  return;
278
  }
279
  torrent.state = Number(torrent.progress || 0) >= 100 ? "pausedUP" : "pausedDL";
 
282
  torrent.can_resume = true;
283
  torrent.dlspeed = "0 B/s";
284
  torrent.upspeed = "0 B/s";
285
+ torrent.status_message = "";
286
+ torrent.display_speed = "0 B/s";
287
+ torrent.display_speed_label = Number(torrent.progress || 0) >= 100 ? "Seed Upload" : "Download";
288
  if (torrent.display_speed_label === "Seed Upload") {
289
  torrent.display_speed = "0 B/s";
290
  }
291
  }
292
 
293
+ if (optimisticAction === "resume") {
294
  const serverResumed = !["pausedDL", "pausedUP"].includes(rawState);
295
  if (serverResumed) {
296
+ clearOptimisticTorrentState(torrent.hash);
297
  return;
298
  }
299
  const isComplete = Number(torrent.progress || 0) >= 100;
 
301
  torrent.state_label = isComplete ? "Seeding" : "Downloading";
302
  torrent.can_pause = !torrent.gofile_uploading;
303
  torrent.can_resume = false;
304
+ torrent.status_message = "";
305
  }
306
  });
307
  }
 
442
 
443
  function actionButtons(torrent) {
444
  const buttons = [];
445
+ const inFlightAction = getInFlightTorrentAction(torrent.hash);
446
 
447
+ if (inFlightAction && (inFlightAction === "pause" || inFlightAction === "resume")) {
448
+ const isPause = inFlightAction === "pause";
449
  buttons.push(`
450
  <button type="button" disabled class="font-display rounded-xl border border-white/10 bg-white/[0.04] px-3 py-2 text-xs font-medium text-white/75 opacity-60 transition">
451
  <i class="fa-solid fa-spinner mr-2 text-[11px] animate-spin"></i>${isPause ? "Pausing..." : "Resuming..."}
 
824
  }
825
  const data = await response.json();
826
  state.torrents = Array.isArray(data.torrents) ? data.torrents : [];
827
+ reconcileOptimisticTorrentState(state.torrents);
828
  state.stats = data.stats || {};
829
  state.events = Array.isArray(data.events) ? data.events : [];
830
  updateConnection(true, state.stats.version || "-");
 
877
  : `/api/torrents/${hash}/${action}`;
878
 
879
  if (action === "pause" || action === "resume") {
880
+ setInFlightTorrentAction(hash, action);
881
+ renderTorrents();
882
+ }
883
+
884
+ let response;
885
+ let payload;
886
+ try {
887
+ response = await fetch(target, {
888
+ method,
889
+ headers: { Accept: "application/json" },
890
+ });
891
+ payload = await response.json();
892
+ } finally {
893
+ if (action === "pause" || action === "resume") {
894
+ clearInFlightTorrentAction(hash);
895
+ }
896
  }
897
 
 
 
 
 
 
898
  if (!response.ok || !payload.success) {
899
  if (action === "pause" || action === "resume") {
900
+ clearOptimisticTorrentState(hash);
901
  await refreshTorrents();
902
  }
903
  showToast(payload.message || "Aksi torrent gagal.", "error");
 
917
  showToast(payload.message || "Aksi berhasil diproses.");
918
  }
919
 
920
+ if (action === "pause" || action === "resume") {
921
+ setOptimisticTorrentState(hash, action);
922
+ applyOptimisticTorrentAction(action, hash);
923
+ }
924
  await refreshTorrents();
925
  }
926