34 lines
1.1 KiB
TypeScript
34 lines
1.1 KiB
TypeScript
import type { Actions } from './$types';
|
|
import { createEbook } from '$lib/api';
|
|
import { redirect } from '@sveltejs/kit';
|
|
|
|
export const actions: Actions = {
|
|
default: async ({ request, fetch }) => {
|
|
const data = await request.formData();
|
|
const title = data.get('title') as string;
|
|
const author = data.get('author') as string;
|
|
const description = data.get('description') as string;
|
|
const file = data.get('file') as File;
|
|
|
|
if (!title || !file) {
|
|
return { success: false, error: 'Title and file are required' };
|
|
}
|
|
|
|
try {
|
|
// For now, simulate upload - in reality, the backend would handle file upload
|
|
// and return the ebook data
|
|
const ebookData = {
|
|
title,
|
|
author: author || null,
|
|
description: description || null,
|
|
file_path: `/uploads/${file.name}`, // Placeholder
|
|
};
|
|
|
|
await createEbook(ebookData);
|
|
throw redirect(303, '/');
|
|
} catch (err) {
|
|
if (err instanceof Response) throw err; // Redirect
|
|
return { success: false, error: err instanceof Error ? err.message : 'Upload failed' };
|
|
}
|
|
},
|
|
}; |