| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBoards\App\Http\Controllers; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use FluentBoards\Framework\Http\Request\Request; |
| 7 |
use FluentBoards\App\Services\JsonImportService; |
| 8 |
|
| 9 |
/** Expose administrator-only JSON import endpoints for the free plugin. */ |
| 10 |
class ImportController extends Controller |
| 11 |
{ |
| 12 |
/** Import a complete uploaded JSON export. */ |
| 13 |
public function importFile(Request $request) |
| 14 |
{ |
| 15 |
return $this->runImport('importFile', $request); |
| 16 |
} |
| 17 |
|
| 18 |
/** Stage one chunk of a JSON export. */ |
| 19 |
public function uploadJsonChunk(Request $request) |
| 20 |
{ |
| 21 |
return $this->runImport('uploadJsonChunk', $request); |
| 22 |
} |
| 23 |
|
| 24 |
/** Initialize or resume a paginated board import. */ |
| 25 |
public function processJsonImport(Request $request) |
| 26 |
{ |
| 27 |
return $this->runImport('processJsonImport', $request); |
| 28 |
} |
| 29 |
|
| 30 |
/** Delegate import work and translate validation failures into API errors. */ |
| 31 |
private function runImport($method, Request $request) |
| 32 |
{ |
| 33 |
$service = new JsonImportService(function ($source, $data) { |
| 34 |
return $this->importExternalSource($source, $data); |
| 35 |
}); |
| 36 |
|
| 37 |
try { |
| 38 |
return $this->sendSuccess($service->$method($request)); |
| 39 |
} catch (Exception $exception) { |
| 40 |
return $this->sendError(['message' => $exception->getMessage()], 422); |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
/** Allow the Pro controller to supply third-party JSON importers. */ |
| 45 |
protected function importExternalSource($source, $importData) |
| 46 |
{ |
| 47 |
return null; |
| 48 |
} |
| 49 |
} |
| 50 |
|