1
0

BaseApiController.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use Illuminate\Http\Request;
  4. use App\Http\Controllers\{
  5. Controller,
  6. AvatarController
  7. };
  8. use Auth, Cache, URL;
  9. use Carbon\Carbon;
  10. use App\{
  11. Avatar,
  12. Notification,
  13. Media,
  14. Profile
  15. };
  16. use App\Transformer\Api\{
  17. AccountTransformer,
  18. NotificationTransformer,
  19. MediaTransformer,
  20. StatusTransformer
  21. };
  22. use League\Fractal;
  23. use League\Fractal\Serializer\ArraySerializer;
  24. use App\Jobs\AvatarPipeline\AvatarOptimize;
  25. use App\Jobs\ImageOptimizePipeline\ImageOptimize;
  26. use App\Jobs\VideoPipeline\{
  27. VideoOptimize,
  28. VideoPostProcess,
  29. VideoThumbnail
  30. };
  31. class BaseApiController extends Controller
  32. {
  33. protected $fractal;
  34. public function __construct()
  35. {
  36. $this->middleware('auth');
  37. $this->fractal = new Fractal\Manager();
  38. $this->fractal->setSerializer(new ArraySerializer());
  39. }
  40. public function notification(Request $request, $id)
  41. {
  42. $notification = Notification::findOrFail($id);
  43. $resource = new Fractal\Resource\Item($notification, new NotificationTransformer());
  44. $res = $this->fractal->createData($resource)->toArray();
  45. return response()->json($res);
  46. }
  47. public function notifications(Request $request)
  48. {
  49. $pid = Auth::user()->profile->id;
  50. $timeago = Carbon::now()->subMonths(6);
  51. $notifications = Notification::with('actor')
  52. ->whereProfileId($pid)
  53. ->whereDate('created_at', '>', $timeago)
  54. ->orderBy('created_at','desc')
  55. ->paginate(10);
  56. $resource = new Fractal\Resource\Collection($notifications, new NotificationTransformer());
  57. $res = $this->fractal->createData($resource)->toArray();
  58. return response()->json($res);
  59. }
  60. public function accounts(Request $request, $id)
  61. {
  62. $profile = Profile::findOrFail($id);
  63. $resource = new Fractal\Resource\Item($profile, new AccountTransformer());
  64. $res = $this->fractal->createData($resource)->toArray();
  65. return response()->json($res);
  66. }
  67. public function accountFollowers(Request $request, $id)
  68. {
  69. $profile = Profile::findOrFail($id);
  70. $followers = $profile->followers;
  71. $resource = new Fractal\Resource\Collection($followers, new AccountTransformer());
  72. $res = $this->fractal->createData($resource)->toArray();
  73. return response()->json($res);
  74. }
  75. public function accountFollowing(Request $request, $id)
  76. {
  77. $profile = Profile::findOrFail($id);
  78. $following = $profile->following;
  79. $resource = new Fractal\Resource\Collection($following, new AccountTransformer());
  80. $res = $this->fractal->createData($resource)->toArray();
  81. return response()->json($res);
  82. }
  83. public function accountStatuses(Request $request, $id)
  84. {
  85. $pid = Auth::user()->profile->id;
  86. $profile = Profile::findOrFail($id);
  87. $statuses = $profile->statuses();
  88. if($pid === $profile->id) {
  89. $statuses = $statuses->orderBy('id', 'desc')->paginate(20);
  90. } else {
  91. $statuses = $statuses->whereVisibility('public')->orderBy('id', 'desc')->paginate(20);
  92. }
  93. $resource = new Fractal\Resource\Collection($statuses, new StatusTransformer());
  94. $res = $this->fractal->createData($resource)->toArray();
  95. return response()->json($res);
  96. }
  97. public function followSuggestions(Request $request)
  98. {
  99. $followers = Auth::user()->profile->recommendFollowers();
  100. $resource = new Fractal\Resource\Collection($followers, new AccountTransformer());
  101. $res = $this->fractal->createData($resource)->toArray();
  102. return response()->json($res);
  103. }
  104. public function avatarUpdate(Request $request)
  105. {
  106. $this->validate($request, [
  107. 'upload' => 'required|mimes:jpeg,png,gif|max:'.config('pixelfed.max_avatar_size'),
  108. ]);
  109. try {
  110. $user = Auth::user();
  111. $profile = $user->profile;
  112. $file = $request->file('upload');
  113. $path = (new AvatarController())->getPath($user, $file);
  114. $dir = $path['root'];
  115. $name = $path['name'];
  116. $public = $path['storage'];
  117. $currentAvatar = storage_path('app/'.$profile->avatar->media_path);
  118. $loc = $request->file('upload')->storeAs($public, $name);
  119. $avatar = Avatar::whereProfileId($profile->id)->firstOrFail();
  120. $opath = $avatar->media_path;
  121. $avatar->media_path = "$public/$name";
  122. $avatar->thumb_path = null;
  123. $avatar->change_count = ++$avatar->change_count;
  124. $avatar->last_processed_at = null;
  125. $avatar->save();
  126. Cache::forget("avatar:{$profile->id}");
  127. AvatarOptimize::dispatch($user->profile, $currentAvatar);
  128. } catch (Exception $e) {
  129. }
  130. return response()->json([
  131. 'code' => 200,
  132. 'msg' => 'Avatar successfully updated',
  133. ]);
  134. }
  135. public function showTempMedia(Request $request, int $profileId, $mediaId)
  136. {
  137. if (!$request->hasValidSignature()) {
  138. abort(401);
  139. }
  140. $profile = Auth::user()->profile;
  141. if($profile->id !== $profileId) {
  142. abort(403);
  143. }
  144. $media = Media::whereProfileId($profile->id)->findOrFail($mediaId);
  145. $path = storage_path('app/'.$media->media_path);
  146. return response()->file($path);
  147. }
  148. public function uploadMedia(Request $request)
  149. {
  150. $this->validate($request, [
  151. 'file.*' => function() {
  152. return [
  153. 'required',
  154. 'mimes:' . config('pixelfed.media_types'),
  155. 'max:' . config('pixelfed.max_photo_size'),
  156. ];
  157. },
  158. ]);
  159. $user = Auth::user();
  160. $profile = $user->profile;
  161. if(config('pixelfed.enforce_account_limit') == true) {
  162. $size = Media::whereUserId($user->id)->sum('size') / 1000;
  163. $limit = (int) config('pixelfed.max_account_size');
  164. if ($size >= $limit) {
  165. abort(403, 'Account size limit reached.');
  166. }
  167. }
  168. $recent = Media::whereProfileId($profile->id)->whereNull('status_id')->count();
  169. if($recent > 50) {
  170. abort(403);
  171. }
  172. $monthHash = hash('sha1', date('Y').date('m'));
  173. $userHash = hash('sha1', $user->id.(string) $user->created_at);
  174. $photo = $request->file('file');
  175. $mimes = explode(',', config('pixelfed.media_types'));
  176. if(in_array($photo->getMimeType(), $mimes) == false) {
  177. return;
  178. }
  179. $storagePath = "public/m/{$monthHash}/{$userHash}";
  180. $path = $photo->store($storagePath);
  181. $hash = \hash_file('sha256', $photo);
  182. $media = new Media();
  183. $media->status_id = null;
  184. $media->profile_id = $profile->id;
  185. $media->user_id = $user->id;
  186. $media->media_path = $path;
  187. $media->original_sha256 = $hash;
  188. $media->size = $photo->getSize();
  189. $media->mime = $photo->getMimeType();
  190. $media->filter_class = null;
  191. $media->filter_name = null;
  192. $media->save();
  193. $url = URL::temporarySignedRoute(
  194. 'temp-media', now()->addHours(1), ['profileId' => $profile->id, 'mediaId' => $media->id]
  195. );
  196. switch ($media->mime) {
  197. case 'image/jpeg':
  198. case 'image/png':
  199. ImageOptimize::dispatch($media);
  200. break;
  201. case 'video/mp4':
  202. VideoThumbnail::dispatch($media);
  203. break;
  204. default:
  205. break;
  206. }
  207. $res = [
  208. 'id' => $media->id,
  209. 'type' => $media->activityVerb(),
  210. 'url' => $url,
  211. 'remote_url' => null,
  212. 'preview_url' => $url,
  213. 'text_url' => null,
  214. 'meta' => $media->metadata,
  215. 'description' => null,
  216. ];
  217. return response()->json($res);
  218. }
  219. public function verifyCredentials(Request $request)
  220. {
  221. $profile = Auth::user()->profile;
  222. $resource = new Fractal\Resource\Item($profile, new AccountTransformer());
  223. $res = $this->fractal->createData($resource)->toArray();
  224. return response()->json($res);
  225. }
  226. }