theguywhosucks commited on
Commit
1eee5fa
·
verified ·
1 Parent(s): 722897a

Upload 17 files

Browse files
Files changed (1) hide show
  1. dashboardlogic.js +36 -25
dashboardlogic.js CHANGED
@@ -17,6 +17,17 @@ function withTokens(obj = {}) {
17
  };
18
  }
19
 
 
 
 
 
 
 
 
 
 
 
 
20
  let client;
21
  let currentUser = null;
22
  let currentPassword = null;
@@ -52,19 +63,15 @@ async function initDashboard() {
52
  }
53
 
54
  try {
55
- client = await Client.connect("pockit-cloud/main", {
56
- hf_token: getReadToken() || getWriteToken()
57
- });
58
- console.log('✓ Connected to API');
59
-
60
- // Update account info
61
  const initials = currentUser.substring(0, 2).toUpperCase();
62
  document.querySelector('.avatar').textContent = initials;
63
  document.querySelector('.account-name').textContent = currentUser;
64
 
65
- // Load data
66
  await loadUserData();
67
  await loadFiles();
 
68
 
69
  } catch (err) {
70
  console.error('Init error:', err);
@@ -77,7 +84,7 @@ async function initDashboard() {
77
  async function loadUserData() {
78
  try {
79
  console.log('Loading user data...');
80
- const result = await client.predict("/search_user", withTokens({ user_id: currentUser }));
81
  const [log, isDev, isPro, isTester, capacityUsed, usedTotal] = result.data;
82
 
83
  // Determine role
@@ -124,7 +131,7 @@ async function loadFiles() {
124
  try {
125
  console.log('Loading files...');
126
  loading(true);
127
- const result = await client.predict("/get_files_secure", withTokens({
128
  user_id: currentUser,
129
  password: currentPassword,
130
  }));
@@ -303,7 +310,7 @@ async function loadPreviewContent(file) {
303
  previewContainer.innerHTML = '<span style="color:#aaa">Loading preview...</span>';
304
  try {
305
  // Try /handle_preview_secure first
306
- const result = await client.predict("/handle_preview_secure", withTokens({
307
  user_id: currentUser,
308
  password: currentPassword,
309
  filename: file.name,
@@ -321,7 +328,7 @@ async function loadPreviewContent(file) {
321
  previewHtml = `<pre class="pv-code">${escapeHtml(result.data[4])}</pre>`;
322
  } else {
323
  // Try /get_preview_text_action for text preview
324
- const textResult = await client.predict("/get_preview_text_action", withTokens({
325
  user_id: currentUser,
326
  password: currentPassword,
327
  filename: file.name,
@@ -356,7 +363,7 @@ async function deleteFile(id) {
356
 
357
  try {
358
  loading(true);
359
- const result = await client.predict("/delete_file_secure", withTokens({
360
  user_id: currentUser,
361
  password: currentPassword,
362
  filename: file.name,
@@ -415,7 +422,7 @@ async function downloadFile(id) {
415
  try {
416
  loading(true);
417
  // Use /get_download_link_action to get the raw download link
418
- const result = await client.predict("/get_download_link_action", withTokens({
419
  user_id: currentUser,
420
  password: currentPassword,
421
  filename: file.name,
@@ -434,7 +441,7 @@ async function downloadFile(id) {
434
  status: response.status,
435
  statusText: response.statusText
436
  });
437
- throw new Error('Network response was not ok (status ' + response.status + '): ' + response.statusText);
438
  }
439
  const blob = await response.blob();
440
  const url = window.URL.createObjectURL(blob);
@@ -455,7 +462,7 @@ async function downloadFile(id) {
455
  } catch (err) {
456
  console.error('Download error:', err);
457
  loading(false);
458
- showToast('Download failed: ' + (err && err.message ? err.message : ''));
459
  }
460
  }
461
 
@@ -467,20 +474,22 @@ async function downloadPreviewFile() {
467
  }
468
  }
469
 
470
- // Upload file
471
  async function handleUpload(input) {
472
  if (!input.files || !input.files[0]) return;
473
  const file = input.files[0];
474
  try {
475
  loading(true);
476
- // Use File object and pass custom_name for filename preservation
477
- console.log('Uploading file:', file, 'type:', file.constructor.name);
478
- const result = await client.predict("/upload_file_secure", withTokens({
479
- user_id: currentUser,
480
- password: currentPassword,
481
- filepath: file,
482
- custom_name: file.name,
483
- }));
 
 
484
  console.log('Uploaded:', file.name);
485
  // Update storage
486
  const [status, capacityUsed, usedTotal] = result.data;
@@ -549,7 +558,7 @@ async function changePassword() {
549
 
550
  try {
551
  loading(true);
552
- const result = await client.predict("/update_password", withTokens({
553
  user_id: currentUser,
554
  new_password: p1,
555
  }));
@@ -615,6 +624,7 @@ window.toggleSelect = toggleSelect;
615
  window.clearSelection = clearSelection;
616
  window.handleSelectAll = handleSelectAll;
617
  window.openPreview = openPreview;
 
618
  window.deleteFile = deleteFile;
619
  window.deleteSelected = deleteSelected;
620
  window.deletePreviewFile = deletePreviewFile;
@@ -631,3 +641,4 @@ window.openModal = openModal;
631
  window.closeModal = closeModal;
632
  window.loading = loading;
633
  window.showToast = showToast;
 
 
17
  };
18
  }
19
 
20
+ // Helper to call secure server-side proxy
21
+ async function callSecureApi(fn, args) {
22
+ const resp = await fetch('/api/proxy', {
23
+ method: 'POST',
24
+ headers: { 'Content-Type': 'application/json' },
25
+ body: JSON.stringify({ fn, args })
26
+ });
27
+ if (!resp.ok) throw new Error('Proxy error: ' + resp.status);
28
+ return await resp.json();
29
+ }
30
+
31
  let client;
32
  let currentUser = null;
33
  let currentPassword = null;
 
63
  }
64
 
65
  try {
66
+ // Update account info first
 
 
 
 
 
67
  const initials = currentUser.substring(0, 2).toUpperCase();
68
  document.querySelector('.avatar').textContent = initials;
69
  document.querySelector('.account-name').textContent = currentUser;
70
 
71
+ // Load data using proxy
72
  await loadUserData();
73
  await loadFiles();
74
+ console.log('✓ Dashboard loaded');
75
 
76
  } catch (err) {
77
  console.error('Init error:', err);
 
84
  async function loadUserData() {
85
  try {
86
  console.log('Loading user data...');
87
+ const result = await callSecureApi("/search_user", withTokens({ user_id: currentUser }));
88
  const [log, isDev, isPro, isTester, capacityUsed, usedTotal] = result.data;
89
 
90
  // Determine role
 
131
  try {
132
  console.log('Loading files...');
133
  loading(true);
134
+ const result = await callSecureApi("/get_files_secure", withTokens({
135
  user_id: currentUser,
136
  password: currentPassword,
137
  }));
 
310
  previewContainer.innerHTML = '<span style="color:#aaa">Loading preview...</span>';
311
  try {
312
  // Try /handle_preview_secure first
313
+ const result = await callSecureApi("/handle_preview_secure", withTokens({
314
  user_id: currentUser,
315
  password: currentPassword,
316
  filename: file.name,
 
328
  previewHtml = `<pre class="pv-code">${escapeHtml(result.data[4])}</pre>`;
329
  } else {
330
  // Try /get_preview_text_action for text preview
331
+ const textResult = await callSecureApi("/get_preview_text_action", withTokens({
332
  user_id: currentUser,
333
  password: currentPassword,
334
  filename: file.name,
 
363
 
364
  try {
365
  loading(true);
366
+ const result = await callSecureApi("/delete_file_secure", withTokens({
367
  user_id: currentUser,
368
  password: currentPassword,
369
  filename: file.name,
 
422
  try {
423
  loading(true);
424
  // Use /get_download_link_action to get the raw download link
425
+ const result = await callSecureApi("/get_download_link_action", withTokens({
426
  user_id: currentUser,
427
  password: currentPassword,
428
  filename: file.name,
 
441
  status: response.status,
442
  statusText: response.statusText
443
  });
444
+ throw new Error('Network response was not ok');
445
  }
446
  const blob = await response.blob();
447
  const url = window.URL.createObjectURL(blob);
 
462
  } catch (err) {
463
  console.error('Download error:', err);
464
  loading(false);
465
+ showToast('Download failed');
466
  }
467
  }
468
 
 
474
  }
475
  }
476
 
477
+ // Upload file - use proxy endpoint for upload
478
  async function handleUpload(input) {
479
  if (!input.files || !input.files[0]) return;
480
  const file = input.files[0];
481
  try {
482
  loading(true);
483
+ const formData = new FormData();
484
+ formData.append('file', file);
485
+ formData.append('user_id', currentUser);
486
+ formData.append('password', currentPassword);
487
+ formData.append('custom_name', file.name);
488
+ const resp = await fetch('/api/upload', {
489
+ method: 'POST',
490
+ body: formData
491
+ });
492
+ const result = await resp.json();
493
  console.log('Uploaded:', file.name);
494
  // Update storage
495
  const [status, capacityUsed, usedTotal] = result.data;
 
558
 
559
  try {
560
  loading(true);
561
+ const result = await callSecureApi("/update_password", withTokens({
562
  user_id: currentUser,
563
  new_password: p1,
564
  }));
 
624
  window.clearSelection = clearSelection;
625
  window.handleSelectAll = handleSelectAll;
626
  window.openPreview = openPreview;
627
+ window.loadPreviewContent = loadPreviewContent;
628
  window.deleteFile = deleteFile;
629
  window.deleteSelected = deleteSelected;
630
  window.deletePreviewFile = deletePreviewFile;
 
641
  window.closeModal = closeModal;
642
  window.loading = loading;
643
  window.showToast = showToast;
644
+