1
0
mirror of https://github.com/BookStackApp/BookStack.git synced 2025-08-09 10:22:51 +03:00

Merge branch 'development' of github.com:LM-Nishant/BookStack into LM-Nishant-development

This commit is contained in:
Dan Brown
2025-07-18 09:19:32 +01:00
5 changed files with 171 additions and 3 deletions

View File

@@ -4,6 +4,7 @@ namespace BookStack\Exports\Controllers;
use BookStack\Entities\Queries\BookQueries;
use BookStack\Exports\ExportFormatter;
use BookStack\Exports\ZipExports\ZipExportBuilder;
use BookStack\Http\ApiController;
use Throwable;
@@ -63,4 +64,19 @@ class BookExportApiController extends ApiController
return $this->download()->directly($markdown, $book->slug . '.md');
}
}
/**
* Export a book to a contained ZIP export file.
* @throws NotFoundException
*/
public function exportZip(int $id, ZipExportBuilder $builder)
{
$book = $this->queries->findVisibleByIdOrFail($id);
$bookName= $book->getShortName();
$zip = $builder->buildForBook($book);
return $this->download()->streamedFileDirectly($zip, $bookName . '.zip', filesize($zip), true);
}
}

View File

@@ -4,6 +4,7 @@ namespace BookStack\Exports\Controllers;
use BookStack\Entities\Queries\ChapterQueries;
use BookStack\Exports\ExportFormatter;
use BookStack\Exports\ZipExports\ZipExportBuilder;
use BookStack\Http\ApiController;
use Throwable;
@@ -63,4 +64,13 @@ class ChapterExportApiController extends ApiController
return $this->download()->directly($markdown, $chapter->slug . '.md');
}
}
public function exportZip(int $id, ZipExportBuilder $builder)
{
$chapter = $this->queries->findVisibleByIdOrFail($id);
$chapterName= $chapter->getShortName();
$zip = $builder->buildForChapter($chapter);
return $this->download()->streamedFileDirectly($zip, $chapterName . '.zip', filesize($zip), true);
}
}

View File

@@ -0,0 +1,121 @@
<?php
declare(strict_types=1);
namespace BookStack\Exports\Controllers;
use BookStack\Exceptions\ZipImportException;
use BookStack\Exceptions\ZipValidationException;
use BookStack\Exports\ImportRepo;
use BookStack\Http\Controller;
use BookStack\Uploads\AttachmentService;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
class ImportApiController extends Controller
{
public function __construct(
protected ImportRepo $imports,
) {
$this->middleware('can:content-import');
}
/**
* List existing imports visible to the user.
*/
public function list(): JsonResponse
{
$imports = $this->imports->getVisibleImports();
return response()->json([
'status' => 'success',
'imports' => $imports,
]);
}
/**
* Upload, validate and store an import file.
*/
public function upload(Request $request): JsonResponse
{
$this->validate($request, [
'file' => ['required', ...AttachmentService::getFileValidationRules()]
]);
$file = $request->file('file');
try {
$import = $this->imports->storeFromUpload($file);
} catch (ZipValidationException $exception) {
return response()->json([
'status' => 'error',
'message' => 'Validation failed',
'errors' => $exception->errors,
], 422);
}
return response()->json([
'status' => 'success',
'import' => $import,
], 201);
}
/**
* Show details of a pending import.
*/
public function read(int $id): JsonResponse
{
$import = $this->imports->findVisible($id);
return response()->json([
'status' => 'success',
'import' => $import,
'data' => $import->decodeMetadata(),
]);
}
/**
* Run the import process.
*/
public function create(int $id, Request $request): JsonResponse
{
$import = $this->imports->findVisible($id);
$parent = null;
if ($import->type === 'page' || $import->type === 'chapter') {
$data = $this->validate($request, [
'parent' => ['required', 'string'],
]);
$parent = $data['parent'];
}
try {
$entity = $this->imports->runImport($import, $parent);
} catch (ZipImportException $exception) {
return response()->json([
'status' => 'error',
'message' => 'Import failed',
'errors' => $exception->errors,
], 500);
}
return response()->json([
'status' => 'success',
'entity' => $entity,
]);
}
/**
* Delete a pending import.
*/
public function delete(int $id): JsonResponse
{
$import = $this->imports->findVisible($id);
$this->imports->deleteImport($import);
return response()->json([
'status' => 'success',
'message' => 'Import deleted successfully',
]);
}
}

View File

@@ -4,6 +4,7 @@ namespace BookStack\Exports\Controllers;
use BookStack\Entities\Queries\PageQueries;
use BookStack\Exports\ExportFormatter;
use BookStack\Exports\ZipExports\ZipExportBuilder;
use BookStack\Http\ApiController;
use Throwable;
@@ -63,4 +64,15 @@ class PageExportApiController extends ApiController
return $this->download()->directly($markdown, $page->slug . '.md');
}
}
public function exportZip(int $id, ZipExportBuilder $builder)
{
$page = $this->queries->findVisibleByIdOrFail($id);
$pageSlug = $page->slug;
$zip = $builder->buildForPage($page);
return $this->download()->streamedFileDirectly($zip, $pageSlug . '.zip', filesize($zip), true);
}
}