Files CRUD.
Remove Contacts and Calendar + CardDav and CalDav.
This commit is contained in:
47
routes/api/files/create-directory.tsx
Normal file
47
routes/api/files/create-directory.tsx
Normal file
@@ -0,0 +1,47 @@
|
||||
import { Handlers } from 'fresh/server.ts';
|
||||
|
||||
import { Directory, FreshContextState } from '/lib/types.ts';
|
||||
import { createDirectory, getDirectories } from '/lib/data/files.ts';
|
||||
|
||||
interface Data {}
|
||||
|
||||
export interface RequestBody {
|
||||
parentPath: string;
|
||||
name: string;
|
||||
}
|
||||
|
||||
export interface ResponseBody {
|
||||
success: boolean;
|
||||
newDirectories: Directory[];
|
||||
}
|
||||
|
||||
export const handler: Handlers<Data, FreshContextState> = {
|
||||
async POST(request, context) {
|
||||
if (!context.state.user) {
|
||||
return new Response('Unauthorized', { status: 401 });
|
||||
}
|
||||
|
||||
const requestBody = await request.clone().json() as RequestBody;
|
||||
|
||||
if (
|
||||
!requestBody.parentPath || !requestBody.name?.trim() || !requestBody.parentPath.startsWith('/') ||
|
||||
requestBody.parentPath.includes('../')
|
||||
) {
|
||||
return new Response('Bad Request', { status: 400 });
|
||||
}
|
||||
|
||||
// TODO: Verify user has write access to path and get the appropriate ownerUserId
|
||||
|
||||
const createdDirectory = await createDirectory(
|
||||
context.state.user.id,
|
||||
requestBody.parentPath,
|
||||
requestBody.name.trim(),
|
||||
);
|
||||
|
||||
const newDirectories = await getDirectories(context.state.user.id, requestBody.parentPath);
|
||||
|
||||
const responseBody: ResponseBody = { success: createdDirectory, newDirectories };
|
||||
|
||||
return new Response(JSON.stringify(responseBody));
|
||||
},
|
||||
};
|
||||
47
routes/api/files/delete-directory.tsx
Normal file
47
routes/api/files/delete-directory.tsx
Normal file
@@ -0,0 +1,47 @@
|
||||
import { Handlers } from 'fresh/server.ts';
|
||||
|
||||
import { Directory, FreshContextState } from '/lib/types.ts';
|
||||
import { deleteDirectoryOrFile, getDirectories } from '/lib/data/files.ts';
|
||||
|
||||
interface Data {}
|
||||
|
||||
export interface RequestBody {
|
||||
parentPath: string;
|
||||
name: string;
|
||||
}
|
||||
|
||||
export interface ResponseBody {
|
||||
success: boolean;
|
||||
newDirectories: Directory[];
|
||||
}
|
||||
|
||||
export const handler: Handlers<Data, FreshContextState> = {
|
||||
async POST(request, context) {
|
||||
if (!context.state.user) {
|
||||
return new Response('Unauthorized', { status: 401 });
|
||||
}
|
||||
|
||||
const requestBody = await request.clone().json() as RequestBody;
|
||||
|
||||
if (
|
||||
!requestBody.parentPath || !requestBody.name?.trim() || !requestBody.parentPath.startsWith('/') ||
|
||||
requestBody.parentPath.includes('../')
|
||||
) {
|
||||
return new Response('Bad Request', { status: 400 });
|
||||
}
|
||||
|
||||
// TODO: Verify user has write access to path and get the appropriate ownerUserId
|
||||
|
||||
const deletedDirectory = await deleteDirectoryOrFile(
|
||||
context.state.user.id,
|
||||
requestBody.parentPath,
|
||||
requestBody.name.trim(),
|
||||
);
|
||||
|
||||
const newDirectories = await getDirectories(context.state.user.id, requestBody.parentPath);
|
||||
|
||||
const responseBody: ResponseBody = { success: deletedDirectory, newDirectories };
|
||||
|
||||
return new Response(JSON.stringify(responseBody));
|
||||
},
|
||||
};
|
||||
47
routes/api/files/delete.tsx
Normal file
47
routes/api/files/delete.tsx
Normal file
@@ -0,0 +1,47 @@
|
||||
import { Handlers } from 'fresh/server.ts';
|
||||
|
||||
import { DirectoryFile, FreshContextState } from '/lib/types.ts';
|
||||
import { deleteDirectoryOrFile, getFiles } from '/lib/data/files.ts';
|
||||
|
||||
interface Data {}
|
||||
|
||||
export interface RequestBody {
|
||||
parentPath: string;
|
||||
name: string;
|
||||
}
|
||||
|
||||
export interface ResponseBody {
|
||||
success: boolean;
|
||||
newFiles: DirectoryFile[];
|
||||
}
|
||||
|
||||
export const handler: Handlers<Data, FreshContextState> = {
|
||||
async POST(request, context) {
|
||||
if (!context.state.user) {
|
||||
return new Response('Unauthorized', { status: 401 });
|
||||
}
|
||||
|
||||
const requestBody = await request.clone().json() as RequestBody;
|
||||
|
||||
if (
|
||||
!requestBody.parentPath || !requestBody.name?.trim() || !requestBody.parentPath.startsWith('/') ||
|
||||
requestBody.parentPath.includes('../')
|
||||
) {
|
||||
return new Response('Bad Request', { status: 400 });
|
||||
}
|
||||
|
||||
// TODO: Verify user has write access to path/file and get the appropriate ownerUserId
|
||||
|
||||
const deletedFile = await deleteDirectoryOrFile(
|
||||
context.state.user.id,
|
||||
requestBody.parentPath,
|
||||
requestBody.name.trim(),
|
||||
);
|
||||
|
||||
const newFiles = await getFiles(context.state.user.id, requestBody.parentPath);
|
||||
|
||||
const responseBody: ResponseBody = { success: deletedFile, newFiles };
|
||||
|
||||
return new Response(JSON.stringify(responseBody));
|
||||
},
|
||||
};
|
||||
47
routes/api/files/get-directories.tsx
Normal file
47
routes/api/files/get-directories.tsx
Normal file
@@ -0,0 +1,47 @@
|
||||
import { Handlers } from 'fresh/server.ts';
|
||||
|
||||
import { Directory, FreshContextState } from '/lib/types.ts';
|
||||
import { getDirectories } from '/lib/data/files.ts';
|
||||
|
||||
interface Data {}
|
||||
|
||||
export interface RequestBody {
|
||||
parentPath: string;
|
||||
directoryPathToExclude?: string;
|
||||
}
|
||||
|
||||
export interface ResponseBody {
|
||||
success: boolean;
|
||||
directories: Directory[];
|
||||
}
|
||||
|
||||
export const handler: Handlers<Data, FreshContextState> = {
|
||||
async POST(request, context) {
|
||||
if (!context.state.user) {
|
||||
return new Response('Unauthorized', { status: 401 });
|
||||
}
|
||||
|
||||
const requestBody = await request.clone().json() as RequestBody;
|
||||
|
||||
if (
|
||||
!requestBody.parentPath || !requestBody.parentPath.startsWith('/') || requestBody.parentPath.includes('../')
|
||||
) {
|
||||
return new Response('Bad Request', { status: 400 });
|
||||
}
|
||||
|
||||
const directories = await getDirectories(
|
||||
context.state.user.id,
|
||||
requestBody.parentPath,
|
||||
);
|
||||
|
||||
const filteredDirectories = requestBody.directoryPathToExclude
|
||||
? directories.filter((directory) =>
|
||||
`${directory.parent_path}${directory.directory_name}` !== requestBody.directoryPathToExclude
|
||||
)
|
||||
: directories;
|
||||
|
||||
const responseBody: ResponseBody = { success: true, directories: filteredDirectories };
|
||||
|
||||
return new Response(JSON.stringify(responseBody));
|
||||
},
|
||||
};
|
||||
52
routes/api/files/move-directory.tsx
Normal file
52
routes/api/files/move-directory.tsx
Normal file
@@ -0,0 +1,52 @@
|
||||
import { Handlers } from 'fresh/server.ts';
|
||||
|
||||
import { Directory, FreshContextState } from '/lib/types.ts';
|
||||
import { getDirectories, renameDirectoryOrFile } from '/lib/data/files.ts';
|
||||
|
||||
interface Data {}
|
||||
|
||||
export interface RequestBody {
|
||||
oldParentPath: string;
|
||||
newParentPath: string;
|
||||
name: string;
|
||||
}
|
||||
|
||||
export interface ResponseBody {
|
||||
success: boolean;
|
||||
newDirectories: Directory[];
|
||||
}
|
||||
|
||||
export const handler: Handlers<Data, FreshContextState> = {
|
||||
async POST(request, context) {
|
||||
if (!context.state.user) {
|
||||
return new Response('Unauthorized', { status: 401 });
|
||||
}
|
||||
|
||||
const requestBody = await request.clone().json() as RequestBody;
|
||||
|
||||
if (
|
||||
!requestBody.oldParentPath || !requestBody.newParentPath || !requestBody.name?.trim() ||
|
||||
!requestBody.oldParentPath.startsWith('/') ||
|
||||
requestBody.oldParentPath.includes('../') || !requestBody.newParentPath.startsWith('/') ||
|
||||
requestBody.newParentPath.includes('../')
|
||||
) {
|
||||
return new Response('Bad Request', { status: 400 });
|
||||
}
|
||||
|
||||
// TODO: Verify user has write access to old and new paths and get the appropriate ownerUserIds
|
||||
|
||||
const movedDirectory = await renameDirectoryOrFile(
|
||||
context.state.user.id,
|
||||
requestBody.oldParentPath,
|
||||
requestBody.newParentPath,
|
||||
requestBody.name.trim(),
|
||||
requestBody.name.trim(),
|
||||
);
|
||||
|
||||
const newDirectories = await getDirectories(context.state.user.id, requestBody.oldParentPath);
|
||||
|
||||
const responseBody: ResponseBody = { success: movedDirectory, newDirectories };
|
||||
|
||||
return new Response(JSON.stringify(responseBody));
|
||||
},
|
||||
};
|
||||
52
routes/api/files/move.tsx
Normal file
52
routes/api/files/move.tsx
Normal file
@@ -0,0 +1,52 @@
|
||||
import { Handlers } from 'fresh/server.ts';
|
||||
|
||||
import { DirectoryFile, FreshContextState } from '/lib/types.ts';
|
||||
import { getFiles, renameDirectoryOrFile } from '/lib/data/files.ts';
|
||||
|
||||
interface Data {}
|
||||
|
||||
export interface RequestBody {
|
||||
oldParentPath: string;
|
||||
newParentPath: string;
|
||||
name: string;
|
||||
}
|
||||
|
||||
export interface ResponseBody {
|
||||
success: boolean;
|
||||
newFiles: DirectoryFile[];
|
||||
}
|
||||
|
||||
export const handler: Handlers<Data, FreshContextState> = {
|
||||
async POST(request, context) {
|
||||
if (!context.state.user) {
|
||||
return new Response('Unauthorized', { status: 401 });
|
||||
}
|
||||
|
||||
const requestBody = await request.clone().json() as RequestBody;
|
||||
|
||||
if (
|
||||
!requestBody.oldParentPath || !requestBody.newParentPath || !requestBody.name?.trim() ||
|
||||
!requestBody.oldParentPath.startsWith('/') ||
|
||||
requestBody.oldParentPath.includes('../') || !requestBody.newParentPath.startsWith('/') ||
|
||||
requestBody.newParentPath.includes('../')
|
||||
) {
|
||||
return new Response('Bad Request', { status: 400 });
|
||||
}
|
||||
|
||||
// TODO: Verify user has write access to old and new paths/files and get the appropriate ownerUserIds
|
||||
|
||||
const movedFile = await renameDirectoryOrFile(
|
||||
context.state.user.id,
|
||||
requestBody.oldParentPath,
|
||||
requestBody.newParentPath,
|
||||
requestBody.name.trim(),
|
||||
requestBody.name.trim(),
|
||||
);
|
||||
|
||||
const newFiles = await getFiles(context.state.user.id, requestBody.oldParentPath);
|
||||
|
||||
const responseBody: ResponseBody = { success: movedFile, newFiles };
|
||||
|
||||
return new Response(JSON.stringify(responseBody));
|
||||
},
|
||||
};
|
||||
51
routes/api/files/rename-directory.tsx
Normal file
51
routes/api/files/rename-directory.tsx
Normal file
@@ -0,0 +1,51 @@
|
||||
import { Handlers } from 'fresh/server.ts';
|
||||
|
||||
import { Directory, FreshContextState } from '/lib/types.ts';
|
||||
import { getDirectories, renameDirectoryOrFile } from '/lib/data/files.ts';
|
||||
|
||||
interface Data {}
|
||||
|
||||
export interface RequestBody {
|
||||
parentPath: string;
|
||||
oldName: string;
|
||||
newName: string;
|
||||
}
|
||||
|
||||
export interface ResponseBody {
|
||||
success: boolean;
|
||||
newDirectories: Directory[];
|
||||
}
|
||||
|
||||
export const handler: Handlers<Data, FreshContextState> = {
|
||||
async POST(request, context) {
|
||||
if (!context.state.user) {
|
||||
return new Response('Unauthorized', { status: 401 });
|
||||
}
|
||||
|
||||
const requestBody = await request.clone().json() as RequestBody;
|
||||
|
||||
if (
|
||||
!requestBody.parentPath || !requestBody.oldName?.trim() || !requestBody.newName?.trim() ||
|
||||
!requestBody.parentPath.startsWith('/') ||
|
||||
requestBody.parentPath.includes('../')
|
||||
) {
|
||||
return new Response('Bad Request', { status: 400 });
|
||||
}
|
||||
|
||||
// TODO: Verify user has write access to path and get the appropriate ownerUserId
|
||||
|
||||
const movedDirectory = await renameDirectoryOrFile(
|
||||
context.state.user.id,
|
||||
requestBody.parentPath,
|
||||
requestBody.parentPath,
|
||||
requestBody.oldName.trim(),
|
||||
requestBody.newName.trim(),
|
||||
);
|
||||
|
||||
const newDirectories = await getDirectories(context.state.user.id, requestBody.parentPath);
|
||||
|
||||
const responseBody: ResponseBody = { success: movedDirectory, newDirectories };
|
||||
|
||||
return new Response(JSON.stringify(responseBody));
|
||||
},
|
||||
};
|
||||
51
routes/api/files/rename.tsx
Normal file
51
routes/api/files/rename.tsx
Normal file
@@ -0,0 +1,51 @@
|
||||
import { Handlers } from 'fresh/server.ts';
|
||||
|
||||
import { DirectoryFile, FreshContextState } from '/lib/types.ts';
|
||||
import { getFiles, renameDirectoryOrFile } from '/lib/data/files.ts';
|
||||
|
||||
interface Data {}
|
||||
|
||||
export interface RequestBody {
|
||||
parentPath: string;
|
||||
oldName: string;
|
||||
newName: string;
|
||||
}
|
||||
|
||||
export interface ResponseBody {
|
||||
success: boolean;
|
||||
newFiles: DirectoryFile[];
|
||||
}
|
||||
|
||||
export const handler: Handlers<Data, FreshContextState> = {
|
||||
async POST(request, context) {
|
||||
if (!context.state.user) {
|
||||
return new Response('Unauthorized', { status: 401 });
|
||||
}
|
||||
|
||||
const requestBody = await request.clone().json() as RequestBody;
|
||||
|
||||
if (
|
||||
!requestBody.parentPath || !requestBody.oldName?.trim() || !requestBody.newName?.trim() ||
|
||||
!requestBody.parentPath.startsWith('/') ||
|
||||
requestBody.parentPath.includes('../')
|
||||
) {
|
||||
return new Response('Bad Request', { status: 400 });
|
||||
}
|
||||
|
||||
// TODO: Verify user has write access to path/file and get the appropriate ownerUserId
|
||||
|
||||
const movedFile = await renameDirectoryOrFile(
|
||||
context.state.user.id,
|
||||
requestBody.parentPath,
|
||||
requestBody.parentPath,
|
||||
requestBody.oldName.trim(),
|
||||
requestBody.newName.trim(),
|
||||
);
|
||||
|
||||
const newFiles = await getFiles(context.state.user.id, requestBody.parentPath);
|
||||
|
||||
const responseBody: ResponseBody = { success: movedFile, newFiles };
|
||||
|
||||
return new Response(JSON.stringify(responseBody));
|
||||
},
|
||||
};
|
||||
42
routes/api/files/upload.tsx
Normal file
42
routes/api/files/upload.tsx
Normal file
@@ -0,0 +1,42 @@
|
||||
import { Handlers } from 'fresh/server.ts';
|
||||
|
||||
import { DirectoryFile, FreshContextState } from '/lib/types.ts';
|
||||
import { createFile, getFiles } from '/lib/data/files.ts';
|
||||
|
||||
interface Data {}
|
||||
|
||||
export interface ResponseBody {
|
||||
success: boolean;
|
||||
newFiles: DirectoryFile[];
|
||||
}
|
||||
|
||||
export const handler: Handlers<Data, FreshContextState> = {
|
||||
async POST(request, context) {
|
||||
if (!context.state.user) {
|
||||
return new Response('Unauthorized', { status: 401 });
|
||||
}
|
||||
|
||||
const requestBody = await request.clone().formData();
|
||||
|
||||
const parentPath = requestBody.get('parent_path') as string;
|
||||
const name = requestBody.get('name') as string;
|
||||
const contents = requestBody.get('contents') as File;
|
||||
|
||||
if (
|
||||
!parentPath || !name.trim() || !contents || !parentPath.startsWith('/') ||
|
||||
parentPath.includes('../')
|
||||
) {
|
||||
return new Response('Bad Request', { status: 400 });
|
||||
}
|
||||
|
||||
// TODO: Verify user has write access to path and get the appropriate ownerUserId
|
||||
|
||||
const createdFile = await createFile(context.state.user.id, parentPath, name.trim(), await contents.arrayBuffer());
|
||||
|
||||
const newFiles = await getFiles(context.state.user.id, parentPath);
|
||||
|
||||
const responseBody: ResponseBody = { success: createdFile, newFiles };
|
||||
|
||||
return new Response(JSON.stringify(responseBody));
|
||||
},
|
||||
};
|
||||
Reference in New Issue
Block a user