Advanced file sharing
This is a WIP for advanced file sharing, but I won't pursue this for now since using symlinks in the file system works for me, and this is adding a ton of complexity I don't want or need right now.
This commit is contained in:
@@ -1,47 +1,29 @@
|
||||
import { join } from 'std/path/join.ts';
|
||||
|
||||
// import Database, { sql } from '/lib/interfaces/database.ts';
|
||||
import Database, { sql } from '/lib/interfaces/database.ts';
|
||||
import { getFilesRootPath } from '/lib/config.ts';
|
||||
import { Directory, DirectoryFile, FileShare } from '/lib/types.ts';
|
||||
import { Directory, DirectoryFile, FileShare, FileShareLink } from '/lib/types.ts';
|
||||
import { sortDirectoriesByName, sortEntriesByName, sortFilesByName, TRASH_PATH } from '/lib/utils/files.ts';
|
||||
|
||||
// const db = new Database();
|
||||
const db = new Database();
|
||||
|
||||
export async function getDirectories(userId: string, path: string): Promise<Directory[]> {
|
||||
const rootPath = join(getFilesRootPath(), userId, path);
|
||||
|
||||
// const directoryShares = await db.query<FileShare>(sql`SELECT * FROM "bewcloud_file_shares"
|
||||
// WHERE "parent_path" = $2
|
||||
// AND "type" = 'directory'
|
||||
// AND (
|
||||
// "owner_user_id" = $1
|
||||
// OR ANY("user_ids_with_read_access") = $1
|
||||
// OR ANY("user_ids_with_write_access") = $1
|
||||
// )`, [
|
||||
// userId,
|
||||
// path,
|
||||
// ]);
|
||||
|
||||
const directoryShares: FileShare[] = [];
|
||||
|
||||
// TODO: Remove this mock test
|
||||
if (path === '/') {
|
||||
directoryShares.push({
|
||||
id: 'test-ing-123',
|
||||
owner_user_id: userId,
|
||||
parent_path: '/',
|
||||
name: 'Testing',
|
||||
type: 'directory',
|
||||
user_ids_with_read_access: [],
|
||||
user_ids_with_write_access: [],
|
||||
extra: {
|
||||
read_share_links: [],
|
||||
write_share_links: [],
|
||||
},
|
||||
updated_at: new Date('2024-04-01'),
|
||||
created_at: new Date('2024-03-31'),
|
||||
});
|
||||
}
|
||||
const directoryShares = await db.query<FileShare>(
|
||||
sql`SELECT * FROM "bewcloud_file_shares"
|
||||
WHERE "parent_path" = $2
|
||||
AND "type" = 'directory'
|
||||
AND (
|
||||
"owner_user_id" = $1
|
||||
OR ANY("user_ids_with_read_access") = $1
|
||||
OR ANY("user_ids_with_write_access") = $1
|
||||
)`,
|
||||
[
|
||||
userId,
|
||||
path,
|
||||
],
|
||||
);
|
||||
|
||||
const directories: Directory[] = [];
|
||||
|
||||
@@ -66,7 +48,26 @@ export async function getDirectories(userId: string, path: string): Promise<Dire
|
||||
directories.push(directory);
|
||||
}
|
||||
|
||||
// TODO: Add directoryShares that aren't owned by this user
|
||||
// Add directoryShares that aren't owned by this user
|
||||
const foreignDirectoryShares = directoryShares.filter((directoryShare) => directoryShare.owner_user_id !== userId);
|
||||
for (const share of foreignDirectoryShares) {
|
||||
const stat = await Deno.stat(join(getFilesRootPath(), share.owner_user_id, path, share.name));
|
||||
|
||||
const hasWriteAccess = share.user_ids_with_write_access.includes(userId);
|
||||
|
||||
const directory: Directory = {
|
||||
owner_user_id: share.owner_user_id,
|
||||
parent_path: path,
|
||||
directory_name: share.name,
|
||||
has_write_access: hasWriteAccess,
|
||||
file_share: share,
|
||||
size_in_bytes: stat.size,
|
||||
updated_at: stat.mtime || new Date(),
|
||||
created_at: stat.birthtime || new Date(),
|
||||
};
|
||||
|
||||
directories.push(directory);
|
||||
}
|
||||
|
||||
directories.sort(sortDirectoriesByName);
|
||||
|
||||
@@ -76,19 +77,20 @@ export async function getDirectories(userId: string, path: string): Promise<Dire
|
||||
export async function getFiles(userId: string, path: string): Promise<DirectoryFile[]> {
|
||||
const rootPath = join(getFilesRootPath(), userId, path);
|
||||
|
||||
// const fileShares = await db.query<FileShare>(sql`SELECT * FROM "bewcloud_file_shares"
|
||||
// WHERE "parent_path" = $2
|
||||
// AND "type" = 'file'
|
||||
// AND (
|
||||
// "owner_user_id" = $1
|
||||
// OR ANY("user_ids_with_read_access") = $1
|
||||
// OR ANY("user_ids_with_write_access") = $1
|
||||
// )`, [
|
||||
// userId,
|
||||
// path,
|
||||
// ]);
|
||||
|
||||
const fileShares: FileShare[] = [];
|
||||
const fileShares = await db.query<FileShare>(
|
||||
sql`SELECT * FROM "bewcloud_file_shares"
|
||||
WHERE "parent_path" = $2
|
||||
AND "type" = 'file'
|
||||
AND (
|
||||
"owner_user_id" = $1
|
||||
OR ANY("user_ids_with_read_access") = $1
|
||||
OR ANY("user_ids_with_write_access") = $1
|
||||
)`,
|
||||
[
|
||||
userId,
|
||||
path,
|
||||
],
|
||||
);
|
||||
|
||||
const files: DirectoryFile[] = [];
|
||||
|
||||
@@ -113,7 +115,28 @@ export async function getFiles(userId: string, path: string): Promise<DirectoryF
|
||||
files.push(file);
|
||||
}
|
||||
|
||||
// TODO: Add fileShares that aren't owned by this user
|
||||
// Add fileShares that aren't owned by this user
|
||||
const foreignFileShares = fileShares.filter((fileShare) => fileShare.owner_user_id !== userId);
|
||||
for (const share of foreignFileShares) {
|
||||
const stat = await Deno.stat(join(getFilesRootPath(), share.owner_user_id, path, share.name));
|
||||
|
||||
const hasWriteAccess = share.user_ids_with_write_access.includes(userId);
|
||||
|
||||
const file: DirectoryFile = {
|
||||
owner_user_id: share.owner_user_id,
|
||||
parent_path: path,
|
||||
file_name: share.name,
|
||||
has_write_access: hasWriteAccess,
|
||||
file_share: share,
|
||||
size_in_bytes: stat.size,
|
||||
updated_at: stat.mtime || new Date(),
|
||||
created_at: stat.birthtime || new Date(),
|
||||
};
|
||||
|
||||
files.push(file);
|
||||
}
|
||||
|
||||
// TODO: Check fileshare directories and list files from there too
|
||||
|
||||
files.sort(sortFilesByName);
|
||||
|
||||
@@ -129,7 +152,7 @@ async function getPathEntries(userId: string, path: string): Promise<Deno.DirEnt
|
||||
await Deno.stat(rootPath);
|
||||
} catch (error) {
|
||||
if (error.toString().includes('NotFound')) {
|
||||
await Deno.mkdir(rootPath, { recursive: true });
|
||||
await Deno.mkdir(join(rootPath, TRASH_PATH), { recursive: true });
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -171,7 +194,22 @@ export async function renameDirectoryOrFile(
|
||||
try {
|
||||
await Deno.rename(join(oldRootPath, oldName), join(newRootPath, newName));
|
||||
|
||||
// TODO: Update any matching file shares
|
||||
// Update any matching file shares
|
||||
await db.query<FileShare>(
|
||||
sql`UPDATE "bewcloud_file_shares" SET
|
||||
"parent_path" = $4,
|
||||
"name" = $5
|
||||
WHERE "parent_path" = $2
|
||||
AND "name" = $3
|
||||
AND "owner_user_id" = $1`,
|
||||
[
|
||||
userId,
|
||||
oldPath,
|
||||
oldName,
|
||||
newPath,
|
||||
newName,
|
||||
],
|
||||
);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return false;
|
||||
@@ -190,7 +228,18 @@ export async function deleteDirectoryOrFile(userId: string, path: string, name:
|
||||
const trashPath = join(getFilesRootPath(), userId, TRASH_PATH);
|
||||
await Deno.rename(join(rootPath, name), join(trashPath, name));
|
||||
|
||||
// TODO: Delete any matching file shares
|
||||
// Delete any matching file shares
|
||||
await db.query<FileShare>(
|
||||
sql`DELETE FROM "bewcloud_file_shares"
|
||||
WHERE "parent_path" = $2
|
||||
AND "name" = $3
|
||||
AND "owner_user_id" = $1`,
|
||||
[
|
||||
userId,
|
||||
path,
|
||||
name,
|
||||
],
|
||||
);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
@@ -259,3 +308,220 @@ export async function getFile(
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export async function createFileShare(
|
||||
userId: string,
|
||||
path: string,
|
||||
name: string,
|
||||
type: 'directory' | 'file',
|
||||
userIdsWithReadAccess: string[],
|
||||
userIdsWithWriteAccess: string[],
|
||||
readShareLinks: FileShareLink[],
|
||||
writeShareLinks: FileShareLink[],
|
||||
): Promise<FileShare> {
|
||||
const extra: FileShare['extra'] = {
|
||||
read_share_links: readShareLinks,
|
||||
write_share_links: writeShareLinks,
|
||||
};
|
||||
|
||||
const newFileShare = (await db.query<FileShare>(
|
||||
sql`INSERT INTO "bewcloud_file_shares" (
|
||||
"owner_user_id",
|
||||
"owner_parent_path",
|
||||
"parent_path",
|
||||
"name",
|
||||
"type",
|
||||
"user_ids_with_read_access",
|
||||
"user_ids_with_write_access",
|
||||
"extra"
|
||||
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
|
||||
RETURNING *`,
|
||||
[
|
||||
userId,
|
||||
path,
|
||||
'/',
|
||||
name,
|
||||
type,
|
||||
userIdsWithReadAccess,
|
||||
userIdsWithWriteAccess,
|
||||
JSON.stringify(extra),
|
||||
],
|
||||
))[0];
|
||||
|
||||
return newFileShare;
|
||||
}
|
||||
|
||||
export async function updateFileShare(fileShare: FileShare) {
|
||||
await db.query(
|
||||
sql`UPDATE "bewcloud_file_shares" SET
|
||||
"owner_parent_path" = $2,
|
||||
"parent_path" = $3,
|
||||
"name" = $4,
|
||||
"user_ids_with_read_access" = $5,
|
||||
"user_ids_with_write_access" = $6,
|
||||
"extra" = $7
|
||||
WHERE "id" = $1`,
|
||||
[
|
||||
fileShare.id,
|
||||
fileShare.owner_parent_path,
|
||||
fileShare.parent_path,
|
||||
fileShare.name,
|
||||
fileShare.user_ids_with_read_access,
|
||||
fileShare.user_ids_with_write_access,
|
||||
JSON.stringify(fileShare.extra),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
export async function getDirectoryAccess(
|
||||
userId: string,
|
||||
parentPath: string,
|
||||
name?: string,
|
||||
): Promise<{ hasReadAccess: boolean; hasWriteAccess: boolean; ownerUserId: string; ownerParentPath: string }> {
|
||||
const rootPath = join(getFilesRootPath(), userId, parentPath, name || '');
|
||||
|
||||
// If it exists in the correct filesystem path, it's the user's
|
||||
try {
|
||||
await Deno.stat(rootPath);
|
||||
|
||||
return { hasReadAccess: true, hasWriteAccess: true, ownerUserId: userId, ownerParentPath: parentPath };
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
|
||||
// Otherwise check if it's been shared with them
|
||||
const parentPaths: string[] = [];
|
||||
let nextParentPath: string | null = rootPath;
|
||||
|
||||
while (nextParentPath !== null) {
|
||||
parentPaths.push(nextParentPath);
|
||||
|
||||
nextParentPath = `/${nextParentPath.split('/').filter(Boolean).slice(0, -1).join('/')}`;
|
||||
|
||||
if (nextParentPath === '/') {
|
||||
parentPaths.push(nextParentPath);
|
||||
nextParentPath = null;
|
||||
}
|
||||
}
|
||||
|
||||
const fileShare = (await db.query<FileShare>(
|
||||
sql`SELECT * FROM "bewcloud_file_shares"
|
||||
WHERE "parent_path" = ANY($2)
|
||||
AND "name" = $3
|
||||
AND "type" = 'directory'
|
||||
AND (
|
||||
ANY("user_ids_with_read_access") = $1
|
||||
OR ANY("user_ids_with_write_access") = $1
|
||||
)
|
||||
ORDER BY "parent_path" ASC
|
||||
LIMIT 1`,
|
||||
[
|
||||
userId,
|
||||
parentPaths,
|
||||
name,
|
||||
],
|
||||
))[0];
|
||||
|
||||
if (fileShare) {
|
||||
return {
|
||||
hasReadAccess: fileShare.user_ids_with_read_access.includes(userId) ||
|
||||
fileShare.user_ids_with_write_access.includes(userId),
|
||||
hasWriteAccess: fileShare.user_ids_with_write_access.includes(userId),
|
||||
ownerUserId: fileShare.owner_user_id,
|
||||
ownerParentPath: fileShare.owner_parent_path,
|
||||
};
|
||||
}
|
||||
|
||||
return { hasReadAccess: false, hasWriteAccess: false, ownerUserId: userId, ownerParentPath: parentPath };
|
||||
}
|
||||
|
||||
export async function getFileAccess(
|
||||
userId: string,
|
||||
parentPath: string,
|
||||
name: string,
|
||||
): Promise<{ hasReadAccess: boolean; hasWriteAccess: boolean; ownerUserId: string; ownerParentPath: string }> {
|
||||
const rootPath = join(getFilesRootPath(), userId, parentPath, name);
|
||||
|
||||
// If it exists in the correct filesystem path, it's the user's
|
||||
try {
|
||||
await Deno.stat(rootPath);
|
||||
|
||||
return { hasReadAccess: true, hasWriteAccess: true, ownerUserId: userId, ownerParentPath: parentPath };
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
|
||||
// Otherwise check if it's been shared with them
|
||||
let fileShare = (await db.query<FileShare>(
|
||||
sql`SELECT * FROM "bewcloud_file_shares"
|
||||
WHERE "parent_path" = $2
|
||||
AND "name" = $3
|
||||
AND "type" = 'file'
|
||||
AND (
|
||||
ANY("user_ids_with_read_access") = $1
|
||||
OR ANY("user_ids_with_write_access") = $1
|
||||
)
|
||||
ORDER BY "parent_path" ASC
|
||||
LIMIT 1`,
|
||||
[
|
||||
userId,
|
||||
parentPath,
|
||||
name,
|
||||
],
|
||||
))[0];
|
||||
|
||||
if (fileShare) {
|
||||
return {
|
||||
hasReadAccess: fileShare.user_ids_with_read_access.includes(userId) ||
|
||||
fileShare.user_ids_with_write_access.includes(userId),
|
||||
hasWriteAccess: fileShare.user_ids_with_write_access.includes(userId),
|
||||
ownerUserId: fileShare.owner_user_id,
|
||||
ownerParentPath: fileShare.owner_parent_path,
|
||||
};
|
||||
}
|
||||
|
||||
// Otherwise check if it's a parent directory has been shared with them, which would also give them access
|
||||
const parentPaths: string[] = [];
|
||||
let nextParentPath: string | null = rootPath;
|
||||
|
||||
while (nextParentPath !== null) {
|
||||
parentPaths.push(nextParentPath);
|
||||
|
||||
nextParentPath = `/${nextParentPath.split('/').filter(Boolean).slice(0, -1).join('/')}`;
|
||||
|
||||
if (nextParentPath === '/') {
|
||||
parentPaths.push(nextParentPath);
|
||||
nextParentPath = null;
|
||||
}
|
||||
}
|
||||
|
||||
fileShare = (await db.query<FileShare>(
|
||||
sql`SELECT * FROM "bewcloud_file_shares"
|
||||
WHERE "parent_path" = ANY($2)
|
||||
AND "name" = $3
|
||||
AND "type" = 'directory'
|
||||
AND (
|
||||
ANY("user_ids_with_read_access") = $1
|
||||
OR ANY("user_ids_with_write_access") = $1
|
||||
)
|
||||
ORDER BY "parent_path" ASC
|
||||
LIMIT 1`,
|
||||
[
|
||||
userId,
|
||||
parentPaths,
|
||||
name,
|
||||
],
|
||||
))[0];
|
||||
|
||||
if (fileShare) {
|
||||
return {
|
||||
hasReadAccess: fileShare.user_ids_with_read_access.includes(userId) ||
|
||||
fileShare.user_ids_with_write_access.includes(userId),
|
||||
hasWriteAccess: fileShare.user_ids_with_write_access.includes(userId),
|
||||
ownerUserId: fileShare.owner_user_id,
|
||||
ownerParentPath: fileShare.owner_parent_path,
|
||||
};
|
||||
}
|
||||
|
||||
return { hasReadAccess: false, hasWriteAccess: false, ownerUserId: userId, ownerParentPath: parentPath };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user