jax-daemon 0.1.11

End-to-end encrypted storage buckets with peer-to-peer synchronization
Documentation
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
// Bucket Creation Module
const BucketCreation = {
  init(apiUrl) {
    const form = document.getElementById("createBucketForm");
    if (!form) return;

    form.addEventListener("submit", async (e) => {
      e.preventDefault();

      const nameInput = document.getElementById("bucketName");
      const status = document.getElementById("createStatus");
      const name = nameInput.value.trim();

      if (!name) {
        this.showStatus(status, "Please enter a bucket name", "error");
        return;
      }

      this.showStatus(status, "Creating bucket...", "info");

      try {
        const response = await fetch(`${apiUrl}/api/v0/bucket`, {
          method: "POST",
          headers: { "Content-Type": "application/json" },
          body: JSON.stringify({ name: name }),
        });

        if (response.ok) {
          this.showStatus(
            status,
            "Bucket created successfully! Reloading...",
            "success",
          );
          setTimeout(() => window.location.reload(), 1000);
        } else {
          const error = await response.text();
          this.showStatus(status, "Failed to create bucket: " + error, "error");
        }
      } catch (error) {
        this.showStatus(
          status,
          "Failed to create bucket: " + error.message,
          "error",
        );
      }
    });
  },

  showStatus(element, message, type) {
    element.className =
      "p-4 " +
      (type === "error"
        ? "bg-red-100 text-red-800"
        : type === "success"
          ? "bg-green-100 text-green-800"
          : "bg-blue-100 text-blue-800");
    element.textContent = message;
    element.classList.remove("hidden");
  },
};

// File Upload Module
const FileUpload = {
  init(apiUrl, bucketId) {
    const form = document.getElementById("uploadForm");
    if (!form) return;

    form.addEventListener("submit", async (e) => {
      e.preventDefault();

      const fileInput = document.getElementById("fileInput");
      const status = document.getElementById("uploadStatus");

      if (!fileInput.files.length) {
        this.showStatus(status, "Please select a file", "error");
        return;
      }

      const file = fileInput.files[0];
      const path = window.JAX_CURRENT_PATH || "/";

      const formData = new FormData();
      formData.append("bucket_id", bucketId);
      formData.append("mount_path", path);
      formData.append("file", file);

      this.showStatus(status, "Uploading...", "info");

      try {
        const response = await fetch(`${apiUrl}/api/v0/bucket/add`, {
          method: "POST",
          body: formData,
        });

        if (response.ok) {
          this.showStatus(
            status,
            "File uploaded successfully! Reloading...",
            "success",
          );
          // setTimeout(() => window.location.reload(), 1000);
        } else {
          const error = await response.text();
          this.showStatus(status, "Upload failed: " + error, "error");
        }
      } catch (error) {
        this.showStatus(status, "Upload failed: " + error.message, "error");
      }
    });
  },

  showStatus(element, message, type) {
    element.className =
      "p-4 " +
      (type === "error"
        ? "bg-red-100 text-red-800"
        : type === "success"
          ? "bg-green-100 text-green-800"
          : "bg-blue-100 text-blue-800");
    element.textContent = message;
    element.classList.remove("hidden");
  },
};

// Bucket Share Module removed - now inline in share_modal.html

// File Rename Module
const FileRename = {
  init(apiUrl, bucketId) {
    const form = document.getElementById("renameForm");
    if (!form) return;

    form.addEventListener("submit", async (e) => {
      e.preventDefault();

      const oldPath = document.getElementById("renameOldPath").value;
      const newName = document.getElementById("renameNewName").value.trim();
      const status = document.getElementById("renameStatus");

      if (!newName) {
        this.showStatus(status, "Please enter a new name", "error");
        return;
      }

      // Build new path: parent_dir + new_name
      const oldPathParts = oldPath.split("/");
      oldPathParts.pop(); // Remove filename
      const parentPath = oldPathParts.join("/") || "/";
      const newPath =
        parentPath === "/" ? "/" + newName : parentPath + "/" + newName;

      this.showStatus(status, "Renaming...", "info");

      try {
        const response = await fetch(`${apiUrl}/api/v0/bucket/rename`, {
          method: "POST",
          headers: { "Content-Type": "application/json" },
          body: JSON.stringify({
            bucket_id: bucketId,
            old_path: oldPath,
            new_path: newPath,
          }),
        });

        if (response.ok) {
          this.showStatus(
            status,
            "Renamed successfully! Reloading...",
            "success",
          );
          setTimeout(() => window.location.reload(), 1000);
        } else {
          const error = await response.text();
          this.showStatus(status, "Rename failed: " + error, "error");
        }
      } catch (error) {
        this.showStatus(status, "Rename failed: " + error.message, "error");
      }
    });
  },

  showStatus(element, message, type) {
    element.className =
      "p-4 " +
      (type === "error"
        ? "bg-red-100 text-red-800"
        : type === "success"
          ? "bg-green-100 text-green-800"
          : "bg-blue-100 text-blue-800");
    element.textContent = message;
    element.classList.remove("hidden");
  },
};

// New File Module
const NewFile = {
  init(apiUrl, bucketId, currentPath) {
    const form = document.getElementById("newFileForm");
    if (!form) return;

    form.addEventListener("submit", async (e) => {
      e.preventDefault();

      const fileNameInput = document.getElementById("newFileName");
      const contentInput = document.getElementById("newFileContent");
      const status = document.getElementById("newFileStatus");

      const fileName = fileNameInput.value.trim();
      const content = contentInput.value || "";

      if (!fileName) {
        this.showStatus(status, "Please enter a file name", "error");
        return;
      }

      // Validate file extension
      if (!fileName.endsWith(".txt") && !fileName.endsWith(".md")) {
        this.showStatus(
          status,
          "Only .txt and .md files are supported",
          "error",
        );
        return;
      }

      // Build the full path
      const path = currentPath.endsWith("/")
        ? currentPath + fileName
        : currentPath + "/" + fileName;

      this.showStatus(status, "Creating file...", "info");

      try {
        // Create a blob from the content
        const blob = new Blob([content], { type: "text/plain" });
        const file = new File([blob], fileName);

        // Upload using the add endpoint
        const formData = new FormData();
        formData.append("bucket_id", bucketId);
        formData.append("mount_path", path);
        formData.append("file", file);

        const response = await fetch(`${apiUrl}/api/v0/bucket/add`, {
          method: "POST",
          body: formData,
        });

        if (response.ok) {
          this.showStatus(
            status,
            "File created! Redirecting to editor...",
            "success",
          );
          // Redirect to editor
          setTimeout(() => {
            window.location.href = `/buckets/${bucketId}/edit?path=${encodeURIComponent(path)}`;
          }, 500);
        } else {
          const error = await response.text();
          this.showStatus(status, "Failed to create file: " + error, "error");
        }
      } catch (error) {
        this.showStatus(
          status,
          "Failed to create file: " + error.message,
          "error",
        );
      }
    });
  },

  showStatus(element, message, type) {
    element.className =
      "p-4 " +
      (type === "error"
        ? "bg-red-100 text-red-800"
        : type === "success"
          ? "bg-green-100 text-green-800"
          : "bg-blue-100 text-blue-800");
    element.textContent = message;
    element.classList.remove("hidden");
  },
};

// File Delete Module
const FileDelete = {
  init(apiUrl, bucketId) {
    // Delete is handled by global confirmDelete function
    window.confirmDelete = async () => {
      const path = document.getElementById("deleteItemPath").value;
      const status = document.getElementById("deleteStatus");

      this.showStatus(status, "Deleting...", "info");

      try {
        const response = await fetch(`${apiUrl}/api/v0/bucket/delete`, {
          method: "POST",
          headers: { "Content-Type": "application/json" },
          body: JSON.stringify({
            bucket_id: bucketId,
            path: path,
          }),
        });

        if (response.ok) {
          this.showStatus(
            status,
            "Deleted successfully! Reloading...",
            "success",
          );
          setTimeout(() => window.location.reload(), 1000);
        } else {
          const error = await response.text();
          this.showStatus(status, "Delete failed: " + error, "error");
        }
      } catch (error) {
        this.showStatus(status, "Delete failed: " + error.message, "error");
      }
    };
  },

  showStatus(element, message, type) {
    element.className =
      "p-4 " +
      (type === "error"
        ? "bg-red-100 text-red-800"
        : type === "success"
          ? "bg-green-100 text-green-800"
          : "bg-blue-100 text-blue-800");
    element.textContent = message;
    element.classList.remove("hidden");
  },
};

// File Move Module
const FileMove = {
  directories: [],
  apiUrl: null,
  bucketId: null,

  init(apiUrl, bucketId) {
    this.apiUrl = apiUrl;
    this.bucketId = bucketId;

    const form = document.getElementById("moveForm");
    if (!form) return;

    form.addEventListener("submit", async (e) => {
      e.preventDefault();

      const sourcePath = document.getElementById("moveSourcePath").value;
      const destDir = document.getElementById("moveDestDir").value;
      const destName = document.getElementById("moveDestName").value.trim();
      const status = document.getElementById("moveStatus");

      if (!destName) {
        this.showStatus(status, "Please enter a name", "error");
        return;
      }

      // Build full destination path
      const destPath = destDir === "/" ? "/" + destName : destDir + "/" + destName;

      this.showStatus(status, "Moving...", "info");

      try {
        const response = await fetch(`${apiUrl}/api/v0/bucket/mv`, {
          method: "POST",
          headers: { "Content-Type": "application/json" },
          body: JSON.stringify({
            bucket_id: bucketId,
            source_path: sourcePath,
            dest_path: destPath,
          }),
        });

        if (response.ok) {
          this.showStatus(
            status,
            "Moved successfully! Reloading...",
            "success",
          );
          setTimeout(() => window.location.reload(), 1000);
        } else {
          const error = await response.text();
          this.showStatus(status, "Move failed: " + error, "error");
        }
      } catch (error) {
        this.showStatus(status, "Move failed: " + error.message, "error");
      }
    });
  },

  async fetchDirectories() {
    if (!this.apiUrl || !this.bucketId) return;

    try {
      const response = await fetch(`${this.apiUrl}/api/v0/bucket/ls`, {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify({
          bucket_id: this.bucketId,
          path: "/",
          deep: true,
        }),
      });

      if (response.ok) {
        const data = await response.json();
        // Filter to only directories and sort
        this.directories = data.items
          .filter((item) => item.is_dir)
          .map((item) => item.path)
          .sort();
      }
    } catch (error) {
      console.error("Failed to fetch directories:", error);
    }
  },

  populateDirectoryDropdown(currentDir) {
    const select = document.getElementById("moveDestDir");
    if (!select) return;

    // Clear existing options except root
    select.innerHTML = '<option value="/">/ (root)</option>';

    // Add directories
    this.directories.forEach((dir) => {
      const option = document.createElement("option");
      option.value = dir;
      option.textContent = dir;
      if (dir === currentDir) {
        option.selected = true;
      }
      select.appendChild(option);
    });
  },

  showStatus(element, message, type) {
    element.className =
      "p-4 " +
      (type === "error"
        ? "bg-red-100 text-red-800"
        : type === "success"
          ? "bg-green-100 text-green-800"
          : "bg-blue-100 text-blue-800");
    element.textContent = message;
    element.classList.remove("hidden");
  },
};

// File Editor Module removed - now using inline editor in file_viewer

// Global modal functions for bucket_explorer.html
function openRenameModal(path, name, isDir) {
  document.getElementById("renameOldPath").value = path;
  document.getElementById("renameNewName").value = name;
  document.getElementById("renameItemType").textContent = isDir
    ? "Directory"
    : "File";
  UIkit.modal("#rename-modal").show();
}

function openDeleteModal(path, name, isDir) {
  document.getElementById("deleteItemPath").value = path;
  document.getElementById("deleteItemName").textContent = name;
  document.getElementById("deleteItemType").textContent = isDir
    ? "Directory"
    : "File";
  UIkit.modal("#delete-modal").show();
}

async function openMoveModal(path, name, isDir) {
  document.getElementById("moveSourcePath").value = path;
  document.getElementById("moveSourceDisplay").value = path;
  document.getElementById("moveDestName").value = name;
  document.getElementById("moveItemType").textContent = isDir
    ? "Directory"
    : "File";

  // Get current directory from path
  const pathParts = path.split("/");
  pathParts.pop(); // Remove filename
  const currentDir = pathParts.join("/") || "/";

  // Fetch directories and populate dropdown
  await FileMove.fetchDirectories();
  FileMove.populateDirectoryDropdown(currentDir);

  UIkit.modal("#move-modal").show();
}

// Initialize modules when DOM is ready
document.addEventListener("DOMContentLoaded", function () {
  // Get API URL from data attribute on body or window
  const apiUrl = window.JAX_API_URL || "http://localhost:3000";
  const bucketId = window.JAX_BUCKET_ID;
  const currentPath = window.JAX_CURRENT_PATH || "/";

  BucketCreation.init(apiUrl);
  if (bucketId) {
    FileRename.init(apiUrl, bucketId);
    FileDelete.init(apiUrl, bucketId);
    FileMove.init(apiUrl, bucketId);
    NewFile.init(apiUrl, bucketId, currentPath);
  }
});