Add path to bookmarks, add logout button

This commit is contained in:
2025-10-10 00:12:14 +02:00
parent 06d04a1abc
commit 9a72c85019
18 changed files with 206 additions and 647 deletions

View File

@@ -1,11 +1,13 @@
import { writable } from "svelte/store"
import { fs_check_response, fs_path_url } from "./FilesystemAPI"
import { fs_check_response, fs_path_url, type FSNode } from "./FilesystemAPI"
import { loading_finish, loading_start } from "lib/Loading"
import { get_user } from "./PixeldrainAPI"
const bookmarks_file = "/me/.fnx/bookmarks.json"
export type Bookmark = {
id: string,
path: string,
icon: string,
label: string,
}
@@ -22,52 +24,88 @@ export let bookmarks_store = writable<Bookmark[]>(
)
export const bookmarks_get = async (): Promise<Bookmark[]> => {
let bookmarks: Bookmark[] = []
try {
bookmarks = await fs_check_response(
loading_start()
const user = await get_user()
if (
user.username === undefined ||
user.username === "" ||
user.subscription.filesystem_access === undefined ||
user.subscription.filesystem_access === false
) {
return []
}
const bookmarks = await fs_check_response(
await fetch(fs_path_url(bookmarks_file), { cache: "no-store" })
)
) as Bookmark[]
// Sanity checks
for (const bookmark of bookmarks) {
if (typeof bookmark.id !== "string") { bookmark.id = "" }
if (typeof bookmark.path !== "string") { bookmark.path = "" }
if (typeof bookmark.icon !== "string") { bookmark.icon = "" }
if (typeof bookmark.label !== "string") { bookmark.label = "" }
}
console.debug("Fetched", bookmarks.length, "bookmarks:", bookmarks)
bookmarks_store.set(bookmarks)
return bookmarks
} catch (err) {
// If the bookmarks were not found when we return an empty bookmarks
// list
if (err.value !== "path_not_found") {
if (
err.value !== "path_not_found" &&
err.value !== "forbidden" &&
err.value !== "authentication_required"
) {
throw err
} else {
return []
}
} finally {
loading_finish()
}
console.debug("Fetched", bookmarks.length, "bookmarks:", bookmarks)
bookmarks_store.set(bookmarks)
return bookmarks
}
export const bookmarks_save = async (bookmarks: Bookmark[]) => {
await fs_check_response(
await fetch(
fs_path_url(bookmarks_file) + "?make_parents=true",
{ method: "PUT", body: JSON.stringify(bookmarks) },
try {
loading_start()
await fs_check_response(
await fetch(
fs_path_url(bookmarks_file) + "?make_parents=true",
{ method: "PUT", body: JSON.stringify(bookmarks) },
)
)
)
console.debug("Saved", bookmarks.length, "bookmarks:", bookmarks)
bookmarks_store.set(bookmarks)
console.debug("Saved", bookmarks.length, "bookmarks:", bookmarks)
bookmarks_store.set(bookmarks)
} finally {
loading_finish()
}
}
export const bookmark_add = async (bm: Bookmark): Promise<Bookmark[]> => {
let bookmarks: Bookmark[] = []
export const bookmark_add = async (node: FSNode): Promise<Bookmark[]> => {
try {
loading_start()
// Get bookmarks
bookmarks = await bookmarks_get()
const bookmarks = await bookmarks_get()
// Add new bookmark
bookmarks.push(bm)
bookmarks.push({
id: node.id,
path: node.path,
icon: "bookmark",
label: node.name,
})
// Save new bookmarks
await bookmarks_save(bookmarks)
return bookmarks
} finally {
loading_finish()
}
return bookmarks
}
export const bookmark_del = async (id: string): Promise<Bookmark[]> => {
@@ -94,3 +132,12 @@ export const bookmark_del = async (id: string): Promise<Bookmark[]> => {
}
return bookmarks
}
export const is_bookmark = (bookmarks: Bookmark[], id: string): boolean => {
for (const bm of bookmarks) {
if (bm.id === id) {
return true
}
}
return false
}

View File

@@ -55,6 +55,12 @@ export const drop_target = (
const entry: FileSystemEntry | null = e.dataTransfer.items[i].webkitGetAsEntry();
if (entry !== null) {
read_dir_recursive(entry);
continue
}
const file: File | null = e.dataTransfer.items[i].getAsFile();
if (file !== null) {
args.upload([file]);
continue
}
}
} else if (e.dataTransfer && e.dataTransfer.files && e.dataTransfer.files.length > 0) {

View File

@@ -55,6 +55,14 @@ export const node_is_shared = (node: FSNode): boolean => {
}
return false
}
export const path_is_shared = (path: FSNode[]): boolean => {
for (let i = 0; i < path.length; i++) {
if (node_is_shared(path[i])) {
return true
}
}
return false
}
export type FSNodeProperties = {
branding_enabled?: string,
@@ -364,7 +372,7 @@ export const fs_share_path = (path: FSNode[]): string => {
// Find the last node in the path that has a public ID
for (let i = path.length - 1; i >= 0; i--) {
if (path[i].id !== undefined && path[i].id !== "me") {
if (node_is_shared(path[i])) {
bucket_idx = i
break
}

View File

@@ -132,6 +132,16 @@ export const put_user = async (data: Object) => {
}
}
export const logout_user = async (redirect_path: string) => {
check_response(await fetch(
get_endpoint() + "/user/session",
{ method: "DELETE" },
))
document.cookie = "pd_auth_key=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
window.location.pathname = redirect_path
}
export type VATRate = {
name: string,
vat: number,