75 lines
2.3 KiB
PHP
75 lines
2.3 KiB
PHP
|
<?php
|
||
|
|
||
|
namespace App\Http\Controllers;
|
||
|
|
||
|
use Illuminate\Http\Request;
|
||
|
use App\Http\Controllers\Controller;
|
||
|
use Illuminate\Support\Facades\Storage;
|
||
|
use App\Jobs\TransferSqliteTableJob;
|
||
|
|
||
|
/**
|
||
|
* @OA\Tag(
|
||
|
* name="SqliteUpload",
|
||
|
* description="Sqlite 檔案上傳"
|
||
|
* )
|
||
|
*/
|
||
|
class SqliteUploadController extends Controller
|
||
|
{
|
||
|
/**
|
||
|
* 上傳 Sqlite 檔案
|
||
|
*
|
||
|
* @OA\Post(
|
||
|
* path="/api/upload-sqlite",
|
||
|
* tags={"SqliteUpload"},
|
||
|
* summary="上傳 SQLite 檔案",
|
||
|
* description="接收一個 SQLite 檔案並儲存至 storage/app/sqlite/,並派送資料轉移任務",
|
||
|
* operationId="uploadSqlite",
|
||
|
* security={{"Authorization":{}}},
|
||
|
* @OA\RequestBody(
|
||
|
* required=true,
|
||
|
* @OA\MediaType(
|
||
|
* mediaType="multipart/form-data",
|
||
|
* @OA\Schema(
|
||
|
* required={"file"},
|
||
|
* @OA\Property(
|
||
|
* property="file",
|
||
|
* type="string",
|
||
|
* format="binary",
|
||
|
* description="要上傳的 SQLite 檔案"
|
||
|
* )
|
||
|
* )
|
||
|
* )
|
||
|
* ),
|
||
|
* @OA\Response(
|
||
|
* response=200,
|
||
|
* description="上傳成功",
|
||
|
* @OA\JsonContent(
|
||
|
* @OA\Property(property="message", type="string", example="上傳成功"),
|
||
|
* @OA\Property(property="path", type="string", example="sqlite/tempUser.sqlite")
|
||
|
* )
|
||
|
* ),
|
||
|
* @OA\Response(
|
||
|
* response=422,
|
||
|
* description="驗證錯誤"
|
||
|
* )
|
||
|
* )
|
||
|
*/
|
||
|
public function upload(Request $request)
|
||
|
{
|
||
|
$request->validate([
|
||
|
'file' => 'required|file',
|
||
|
]);
|
||
|
|
||
|
if ($request->file('file')->getClientOriginalExtension() !== 'sqlite') {
|
||
|
return response()->json(['message' => '只允許上傳 .sqlite 檔案'], 422);
|
||
|
}
|
||
|
|
||
|
$filename = $request->file('file')->getClientOriginalName();
|
||
|
$path = $request->file('file')->storeAs('sqlite', $filename, 'local');
|
||
|
TransferSqliteTableJob::dispatch(Storage::disk('local')->path($path));
|
||
|
return response()->json([
|
||
|
'message' => '上傳成功,已派送資料處理任務',
|
||
|
'path' => $path,
|
||
|
]);
|
||
|
}
|
||
|
}
|