|
6 | 6 | use App\Http\Controllers\Controller;
|
7 | 7 | use Illuminate\Support\Str;
|
8 | 8 | use App\Util\ActivityPub\Helpers;
|
9 |
| -use App\Jobs\LikePipeline\LikePipeline; |
10 |
| -use App\Jobs\StatusPipeline\StatusDelete; |
11 |
| -use App\Jobs\FollowPipeline\FollowPipeline; |
12 | 9 | use Laravel\Passport\Passport;
|
13 |
| -use Auth, Cache, DB; |
| 10 | +use Auth, Cache, DB, URL; |
14 | 11 | use App\{
|
15 | 12 | Follower,
|
16 | 13 | FollowRequest,
|
|
24 | 21 | use League\Fractal;
|
25 | 22 | use App\Transformer\Api\{
|
26 | 23 | AccountTransformer,
|
| 24 | + MediaTransformer, |
27 | 25 | RelationshipTransformer,
|
28 | 26 | StatusTransformer,
|
29 | 27 | };
|
30 | 28 | use App\Http\Controllers\FollowerController;
|
31 | 29 | use League\Fractal\Serializer\ArraySerializer;
|
32 | 30 | use League\Fractal\Pagination\IlluminatePaginatorAdapter;
|
33 | 31 |
|
| 32 | +use App\Jobs\LikePipeline\LikePipeline; |
| 33 | +use App\Jobs\StatusPipeline\StatusDelete; |
| 34 | +use App\Jobs\FollowPipeline\FollowPipeline; |
| 35 | +use App\Jobs\ImageOptimizePipeline\ImageOptimize; |
| 36 | +use App\Jobs\VideoPipeline\{ |
| 37 | + VideoOptimize, |
| 38 | + VideoPostProcess, |
| 39 | + VideoThumbnail |
| 40 | +}; |
| 41 | + |
34 | 42 | use App\Services\NotificationService;
|
35 | 43 |
|
36 | 44 | class ApiV1Controller extends Controller
|
@@ -873,6 +881,87 @@ public function accountListsById(Request $request, $id)
|
873 | 881 | return response()->json([]);
|
874 | 882 | }
|
875 | 883 |
|
| 884 | + /** |
| 885 | + * POST /api/v1/media |
| 886 | + * |
| 887 | + * |
| 888 | + * @return App\Transformer\Api\MediaTransformer |
| 889 | + */ |
| 890 | + public function mediaUpload(Request $request) |
| 891 | + { |
| 892 | + abort_if(!$request->user(), 403); |
| 893 | + |
| 894 | + $this->validate($request, [ |
| 895 | + 'file.*' => function() { |
| 896 | + return [ |
| 897 | + 'required', |
| 898 | + 'mimes:' . config('pixelfed.media_types'), |
| 899 | + 'max:' . config('pixelfed.max_photo_size'), |
| 900 | + ]; |
| 901 | + }, |
| 902 | + 'filter_name' => 'nullable|string|max:24', |
| 903 | + 'filter_class' => 'nullable|alpha_dash|max:24' |
| 904 | + ]); |
| 905 | + |
| 906 | + $user = $request->user(); |
| 907 | + $profile = $user->profile; |
| 908 | + |
| 909 | + if(config('pixelfed.enforce_account_limit') == true) { |
| 910 | + $size = Cache::remember($user->storageUsedKey(), now()->addDays(3), function() use($user) { |
| 911 | + return Media::whereUserId($user->id)->sum('size') / 1000; |
| 912 | + }); |
| 913 | + $limit = (int) config('pixelfed.max_account_size'); |
| 914 | + if ($size >= $limit) { |
| 915 | + abort(403, 'Account size limit reached.'); |
| 916 | + } |
| 917 | + } |
| 918 | + |
| 919 | + $monthHash = hash('sha1', date('Y').date('m')); |
| 920 | + $userHash = hash('sha1', $user->id . (string) $user->created_at); |
| 921 | + |
| 922 | + $photo = $request->file('file'); |
| 923 | + |
| 924 | + $mimes = explode(',', config('pixelfed.media_types')); |
| 925 | + if(in_array($photo->getMimeType(), $mimes) == false) { |
| 926 | + abort(403, 'Invalid or unsupported mime type.'); |
| 927 | + } |
| 928 | + |
| 929 | + $storagePath = "public/m/{$monthHash}/{$userHash}"; |
| 930 | + $path = $photo->store($storagePath); |
| 931 | + $hash = \hash_file('sha256', $photo); |
| 932 | + |
| 933 | + $media = new Media(); |
| 934 | + $media->status_id = null; |
| 935 | + $media->profile_id = $profile->id; |
| 936 | + $media->user_id = $user->id; |
| 937 | + $media->media_path = $path; |
| 938 | + $media->original_sha256 = $hash; |
| 939 | + $media->size = $photo->getSize(); |
| 940 | + $media->mime = $photo->getMimeType(); |
| 941 | + $media->filter_class = $request->input('filter_class'); |
| 942 | + $media->filter_name = $request->input('filter_name'); |
| 943 | + $media->save(); |
| 944 | + |
| 945 | + switch ($media->mime) { |
| 946 | + case 'image/jpeg': |
| 947 | + case 'image/png': |
| 948 | + ImageOptimize::dispatch($media); |
| 949 | + break; |
| 950 | + |
| 951 | + case 'video/mp4': |
| 952 | + VideoThumbnail::dispatch($media); |
| 953 | + $preview_url = '/storage/no-preview.png'; |
| 954 | + $url = '/storage/no-preview.png'; |
| 955 | + break; |
| 956 | + } |
| 957 | + |
| 958 | + $resource = new Fractal\Resource\Item($media, new MediaTransformer()); |
| 959 | + $res = $this->fractal->createData($resource)->toArray(); |
| 960 | + $res['preview_url'] = url('/storage/no-preview.png'); |
| 961 | + $res['url'] = url('/storage/no-preview.png'); |
| 962 | + return response()->json($res); |
| 963 | + } |
| 964 | + |
876 | 965 | public function statusById(Request $request, $id)
|
877 | 966 | {
|
878 | 967 | $status = Status::whereVisibility('public')->findOrFail($id);
|
|
0 commit comments