Skip to content

Commit 2e46231

Browse files
committed
feat(api): 上传文件 (#77)
1 parent a9a4037 commit 2e46231

File tree

9 files changed

+122
-9
lines changed

9 files changed

+122
-9
lines changed

Diff for: app/Http/Controllers/FileController.php

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Http\Controllers;
6+
7+
use App\Http\Requests\FileRequest;
8+
use Illuminate\Support\Facades\Storage;
9+
use Jiannei\Response\Laravel\Support\Facades\Response;
10+
use Throwable;
11+
12+
class FileController extends Controller
13+
{
14+
/**
15+
* Display a listing of the resource.
16+
*/
17+
public function index()
18+
{
19+
//
20+
}
21+
22+
/**
23+
* Show the form for creating a new resource.
24+
*/
25+
public function create()
26+
{
27+
//
28+
}
29+
30+
/**
31+
* Store a newly created resource in storage.
32+
*/
33+
public function store(FileRequest $request)
34+
{
35+
try {
36+
$path = $request->file('file')->store('files');
37+
38+
return Response::created([
39+
'path' => $path,
40+
'full_path' => Storage::url($path),
41+
], trans('messages.success.uploaded'));
42+
} catch (Throwable $e) {
43+
return Response::fail($e->getMessage() ?: trans('messages.failed.uploaded'));
44+
}
45+
}
46+
47+
/**
48+
* Display the specified resource.
49+
*/
50+
public function show()
51+
{
52+
//
53+
}
54+
55+
/**
56+
* Show the form for editing the specified resource.
57+
*/
58+
public function edit()
59+
{
60+
//
61+
}
62+
63+
/**
64+
* Update the specified resource in storage.
65+
*/
66+
public function update(FileRequest $request)
67+
{
68+
//
69+
}
70+
71+
/**
72+
* Remove the specified resource from storage.
73+
*/
74+
public function destroy()
75+
{
76+
//
77+
}
78+
}

Diff for: app/Http/Requests/FileRequest.php

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Http\Requests;
6+
7+
class FileRequest extends Request
8+
{
9+
/**
10+
* Get the validation rules that apply to the request.
11+
*
12+
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
13+
*/
14+
public function rules(): array
15+
{
16+
return match ($this->method()) {
17+
'POST' => [
18+
'file' => ['required', 'file', 'mimes:jpg,jpeg,png', 'max:10240'],
19+
],
20+
default => [],
21+
};
22+
}
23+
}

Diff for: lang/en/messages.php

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
'current_password_not_match' => 'Current password does not match.',
1313
'password_same_current_password' => 'New password is the same as the current password.',
1414
'reset_password' => 'Failed to reset password.',
15+
'uploaded' => 'Failed to upload file.',
1516
],
1617
'success' => [
1718
'verification_code_issued' => 'Verification code issued successfully.',
@@ -21,5 +22,6 @@
2122
'created' => 'Created successfully.',
2223
'updated' => 'Updated successfully.',
2324
'reset_password' => 'Password reset successfully.',
25+
'uploaded' => 'Uploaded successfully.',
2426
],
2527
];

Diff for: lang/en/validation.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,14 @@
8989
],
9090
'max_digits' => 'The :attribute field must not have more than :max digits.',
9191
'messages' => [
92-
'phone' => [
93-
'phone' => 'The :attribute field must be a valid phone number.',
94-
],
9592
'holiday' => [
9693
'format' => [
9794
'in' => 'The format :format is not supported. Supported formats are: :values.',
9895
],
9996
],
97+
'phone' => [
98+
'phone' => 'The :attribute field must be a valid phone number.',
99+
],
100100
],
101101
'mimes' => 'The :attribute must be a file of type: :values.',
102102
'mimetypes' => 'The :attribute must be a file of type: :values.',
@@ -192,6 +192,7 @@
192192
'duration' => 'duration',
193193
'email' => 'email',
194194
'excerpt' => 'excerpt',
195+
'file' => 'file',
195196
'filter' => 'filter',
196197
'finished_at' => 'finished at',
197198
'first_alias' => 'first alias',

Diff for: lang/ja/messages.php

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
'current_password_not_match' => '現在のパスワードが正しくありません。',
1313
'password_same_current_password' => '新しいパスワードが現在のパスワードと同じです。',
1414
'reset_password' => 'パスワードのリセットに失敗しました。',
15+
'uploaded' => 'ファイルのアップロードに失敗しました。',
1516
],
1617
'success' => [
1718
'verification_code_issued' => '認証コードを発行しました。',
@@ -21,5 +22,6 @@
2122
'created' => '作成に成功しました。',
2223
'updated' => '更新に成功しました。',
2324
'reset_password' => 'パスワードをリセットしました。',
25+
'uploaded' => 'アップロードに成功しました。',
2426
],
2527
];

Diff for: lang/ja/validation.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,14 @@
8989
],
9090
'max_digits' => ':Attributeは、:max桁以下の数字である必要があります。',
9191
'messages' => [
92-
'phone' => [
93-
'phone' => ':Attributeは有効な電話番号ではありません。',
94-
],
9592
'holiday' => [
9693
'format' => [
9794
'in' => 'フォーマット「:format」は無効です,:values のいずれかを指定してください。',
9895
],
9996
],
97+
'phone' => [
98+
'phone' => ':Attributeは有効な電話番号ではありません。',
99+
],
100100
],
101101
'mimes' => ':Attributeには、以下のファイルタイプを指定してください。:values',
102102
'mimetypes' => ':Attributeには、以下のファイルタイプを指定してください。:values',
@@ -192,6 +192,7 @@
192192
'duration' => '期間',
193193
'email' => 'eメール',
194194
'excerpt' => '抜粋',
195+
'file' => 'ファイル',
195196
'filter' => 'フィルタ',
196197
'finished_at' => 'に終了しました',
197198
'first_alias' => '名前の別名',

Diff for: lang/zh_CN/messages.php

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
'current_password_not_match' => '当前密码不匹配。',
1313
'password_same_current_password' => '新密码与当前密码相同。',
1414
'reset_password' => '重置密码失败。',
15+
'uploaded' => '文件上传失败。',
1516
],
1617
'success' => [
1718
'verification_code_issued' => '验证码发放成功。',
@@ -21,5 +22,6 @@
2122
'created' => '创建成功。',
2223
'updated' => '更新成功。',
2324
'reset_password' => '密码重置成功。',
25+
'uploaded' => '上传成功。',
2426
],
2527
];

Diff for: lang/zh_CN/validation.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,14 @@
8989
],
9090
'max_digits' => ':Attribute 不能超过 :max 位数。',
9191
'messages' => [
92-
'phone' => [
93-
'phone' => '电话号码格式不正确。',
94-
],
9592
'holiday' => [
9693
'format' => [
9794
'in' => '不支持的格式: :format,支持的格式有: :values。',
9895
],
9996
],
97+
'phone' => [
98+
'phone' => '电话号码格式不正确。',
99+
],
100100
],
101101
'mimes' => ':Attribute 必须是一个 :values 类型的文件。',
102102
'mimetypes' => ':Attribute 必须是一个 :values 类型的文件。',
@@ -192,6 +192,7 @@
192192
'duration' => '期间',
193193
'email' => '邮箱',
194194
'excerpt' => '摘要',
195+
'file' => '文件',
195196
'filter' => '过滤',
196197
'finished_at' => '完成于',
197198
'first_alias' => '第一个别名',

Diff for: routes/api.php

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
declare(strict_types=1);
44

55
use App\Http\Controllers\AuthorizationController;
6+
use App\Http\Controllers\FileController;
67
use App\Http\Controllers\UserController;
78
use App\Http\Controllers\VerificationCodeController;
89
use Illuminate\Http\Request;
@@ -28,6 +29,8 @@
2829
Route::middleware('throttle:' . config('api.rate_limits.access'))->group(function () {
2930
// 某个用户的详情
3031
Route::get('users/{user}', [UserController::class, 'show'])->name('users.show');
32+
// 上传文件
33+
Route::post('files', [FileController::class, 'store'])->name('files.store');
3134
// 登录后可以访问的接口
3235
Route::middleware('auth:sanctum')->group(function () {
3336
// 当前登录用户信息

0 commit comments

Comments
 (0)