StoryController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Support\Str;
  5. use App\Media;
  6. use App\Profile;
  7. use App\Story;
  8. use App\StoryView;
  9. use App\Services\StoryService;
  10. use Cache, Storage;
  11. use Image as Intervention;
  12. use App\Services\FollowerService;
  13. use App\Services\MediaPathService;
  14. use FFMpeg;
  15. use FFMpeg\Coordinate\Dimension;
  16. use FFMpeg\Format\Video\X264;
  17. class StoryController extends Controller
  18. {
  19. public function apiV1Add(Request $request)
  20. {
  21. abort_if(!config('instance.stories.enabled') || !$request->user(), 404);
  22. $this->validate($request, [
  23. 'file' => function() {
  24. return [
  25. 'required',
  26. 'mimes:image/jpeg,image/png,video/mp4',
  27. 'max:' . config('pixelfed.max_photo_size'),
  28. ];
  29. },
  30. ]);
  31. $user = $request->user();
  32. if(Story::whereProfileId($user->profile_id)->where('expires_at', '>', now())->count() >= Story::MAX_PER_DAY) {
  33. abort(400, 'You have reached your limit for new Stories today.');
  34. }
  35. $photo = $request->file('file');
  36. $path = $this->storePhoto($photo, $user);
  37. $story = new Story();
  38. $story->duration = 3;
  39. $story->profile_id = $user->profile_id;
  40. $story->type = Str::endsWith($photo->getMimeType(), 'mp4') ? 'video' :'photo';
  41. $story->mime = $photo->getMimeType();
  42. $story->path = $path;
  43. $story->local = true;
  44. $story->size = $photo->getSize();
  45. $story->save();
  46. $url = $story->path;
  47. if($story->type === 'video') {
  48. $video = FFMpeg::open($path);
  49. $width = $video->getVideoStream()->get('width');
  50. $height = $video->getVideoStream()->get('height');
  51. if($width !== 1080 || $height !== 1920) {
  52. Storage::delete($story->path);
  53. $story->delete();
  54. abort(422, 'Invalid video dimensions, must be 1080x1920');
  55. }
  56. }
  57. return [
  58. 'code' => 200,
  59. 'msg' => 'Successfully added',
  60. 'media_id' => (string) $story->id,
  61. 'media_url' => url(Storage::url($url)) . '?v=' . time(),
  62. 'media_type' => $story->type
  63. ];
  64. }
  65. protected function storePhoto($photo, $user)
  66. {
  67. $mimes = explode(',', config('pixelfed.media_types'));
  68. if(in_array($photo->getMimeType(), [
  69. 'image/jpeg',
  70. 'image/png',
  71. 'video/mp4'
  72. ]) == false) {
  73. abort(400, 'Invalid media type');
  74. return;
  75. }
  76. $storagePath = MediaPathService::story($user->profile);
  77. $path = $photo->store($storagePath);
  78. if(in_array($photo->getMimeType(), ['image/jpeg','image/png'])) {
  79. $fpath = storage_path('app/' . $path);
  80. $img = Intervention::make($fpath);
  81. $img->orientate();
  82. $img->save($fpath, config('pixelfed.image_quality'));
  83. $img->destroy();
  84. }
  85. return $path;
  86. }
  87. public function cropPhoto(Request $request)
  88. {
  89. abort_if(!config('instance.stories.enabled') || !$request->user(), 404);
  90. $this->validate($request, [
  91. 'media_id' => 'required|integer|min:1',
  92. 'width' => 'required',
  93. 'height' => 'required',
  94. 'x' => 'required',
  95. 'y' => 'required'
  96. ]);
  97. $user = $request->user();
  98. $id = $request->input('media_id');
  99. $width = round($request->input('width'));
  100. $height = round($request->input('height'));
  101. $x = round($request->input('x'));
  102. $y = round($request->input('y'));
  103. $story = Story::whereProfileId($user->profile_id)->findOrFail($id);
  104. $path = storage_path('app/' . $story->path);
  105. if(!is_file($path)) {
  106. abort(400, 'Invalid or missing media.');
  107. }
  108. if($story->type === 'photo') {
  109. $img = Intervention::make($path);
  110. $img->crop($width, $height, $x, $y);
  111. $img->save($path, config('pixelfed.image_quality'));
  112. }
  113. return [
  114. 'code' => 200,
  115. 'msg' => 'Successfully cropped',
  116. ];
  117. }
  118. public function publishStory(Request $request)
  119. {
  120. abort_if(!config('instance.stories.enabled') || !$request->user(), 404);
  121. $this->validate($request, [
  122. 'media_id' => 'required',
  123. 'duration' => 'required|integer|min:3|max:10'
  124. ]);
  125. $id = $request->input('media_id');
  126. $user = $request->user();
  127. $story = Story::whereProfileId($user->profile_id)
  128. ->findOrFail($id);
  129. $story->active = true;
  130. $story->duration = $request->input('duration', 10);
  131. $story->expires_at = now()->addHours(24);
  132. $story->save();
  133. return [
  134. 'code' => 200,
  135. 'msg' => 'Successfully published',
  136. ];
  137. }
  138. public function apiV1Delete(Request $request, $id)
  139. {
  140. abort_if(!config('instance.stories.enabled') || !$request->user(), 404);
  141. $user = $request->user();
  142. $story = Story::whereProfileId($user->profile_id)
  143. ->findOrFail($id);
  144. if(Storage::exists($story->path) == true) {
  145. Storage::delete($story->path);
  146. }
  147. $story->delete();
  148. return [
  149. 'code' => 200,
  150. 'msg' => 'Successfully deleted'
  151. ];
  152. }
  153. public function apiV1Recent(Request $request)
  154. {
  155. abort_if(!config('instance.stories.enabled') || !$request->user(), 404);
  156. $profile = $request->user()->profile;
  157. $following = $profile->following->pluck('id')->toArray();
  158. if(config('database.default') == 'pgsql') {
  159. $db = Story::with('profile')
  160. ->whereActive(true)
  161. ->whereIn('profile_id', $following)
  162. ->where('expires_at', '>', now())
  163. ->distinct('profile_id')
  164. ->take(9)
  165. ->get();
  166. } else {
  167. $db = Story::with('profile')
  168. ->whereActive(true)
  169. ->whereIn('profile_id', $following)
  170. ->where('created_at', '>', now()->subDay())
  171. ->orderByDesc('expires_at')
  172. ->groupBy('profile_id')
  173. ->take(9)
  174. ->get();
  175. }
  176. $stories = $db->map(function($s, $k) {
  177. return [
  178. 'id' => (string) $s->id,
  179. 'photo' => $s->profile->avatarUrl(),
  180. 'name' => $s->profile->username,
  181. 'link' => $s->profile->url(),
  182. 'lastUpdated' => (int) $s->created_at->format('U'),
  183. 'seen' => $s->seen(),
  184. 'items' => [],
  185. 'pid' => (string) $s->profile->id
  186. ];
  187. });
  188. return response()->json($stories, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
  189. }
  190. public function apiV1Fetch(Request $request, $id)
  191. {
  192. abort_if(!config('instance.stories.enabled') || !$request->user(), 404);
  193. $authed = $request->user()->profile;
  194. $profile = Profile::findOrFail($id);
  195. if($id == $authed->id) {
  196. $publicOnly = true;
  197. } else {
  198. $publicOnly = (bool) $profile->followedBy($authed);
  199. }
  200. $stories = Story::whereProfileId($profile->id)
  201. ->whereActive(true)
  202. ->orderBy('expires_at', 'desc')
  203. ->where('expires_at', '>', now())
  204. ->when(!$publicOnly, function($query, $publicOnly) {
  205. return $query->wherePublic(true);
  206. })
  207. ->get()
  208. ->map(function($s, $k) {
  209. return [
  210. 'id' => (string) $s->id,
  211. 'type' => Str::endsWith($s->path, '.mp4') ? 'video' :'photo',
  212. 'length' => 3,
  213. 'src' => url(Storage::url($s->path)),
  214. 'preview' => null,
  215. 'link' => null,
  216. 'linkText' => null,
  217. 'time' => $s->created_at->format('U'),
  218. 'expires_at' => (int) $s->expires_at->format('U'),
  219. 'created_ago' => $s->created_at->diffForHumans(null, true, true),
  220. 'seen' => $s->seen()
  221. ];
  222. })->toArray();
  223. return response()->json($stories, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
  224. }
  225. public function apiV1Item(Request $request, $id)
  226. {
  227. abort_if(!config('instance.stories.enabled') || !$request->user(), 404);
  228. $authed = $request->user()->profile;
  229. $story = Story::with('profile')
  230. ->whereActive(true)
  231. ->where('expires_at', '>', now())
  232. ->findOrFail($id);
  233. $profile = $story->profile;
  234. if($story->profile_id == $authed->id) {
  235. $publicOnly = true;
  236. } else {
  237. $publicOnly = (bool) $profile->followedBy($authed);
  238. }
  239. abort_if(!$publicOnly, 403);
  240. $res = [
  241. 'id' => (string) $story->id,
  242. 'type' => Str::endsWith($story->path, '.mp4') ? 'video' :'photo',
  243. 'length' => 10,
  244. 'src' => url(Storage::url($story->path)),
  245. 'preview' => null,
  246. 'link' => null,
  247. 'linkText' => null,
  248. 'time' => $story->created_at->format('U'),
  249. 'expires_at' => (int) $story->expires_at->format('U'),
  250. 'seen' => $story->seen()
  251. ];
  252. return response()->json($res, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
  253. }
  254. public function apiV1Profile(Request $request, $id)
  255. {
  256. abort_if(!config('instance.stories.enabled') || !$request->user(), 404);
  257. $authed = $request->user()->profile;
  258. $profile = Profile::findOrFail($id);
  259. if($id == $authed->id) {
  260. $publicOnly = true;
  261. } else {
  262. $publicOnly = (bool) $profile->followedBy($authed);
  263. }
  264. $stories = Story::whereProfileId($profile->id)
  265. ->whereActive(true)
  266. ->orderBy('expires_at')
  267. ->where('expires_at', '>', now())
  268. ->when(!$publicOnly, function($query, $publicOnly) {
  269. return $query->wherePublic(true);
  270. })
  271. ->get()
  272. ->map(function($s, $k) {
  273. return [
  274. 'id' => $s->id,
  275. 'type' => Str::endsWith($s->path, '.mp4') ? 'video' :'photo',
  276. 'length' => 10,
  277. 'src' => url(Storage::url($s->path)),
  278. 'preview' => null,
  279. 'link' => null,
  280. 'linkText' => null,
  281. 'time' => $s->created_at->format('U'),
  282. 'expires_at' => (int) $s->expires_at->format('U'),
  283. 'seen' => $s->seen()
  284. ];
  285. })->toArray();
  286. if(count($stories) == 0) {
  287. return [];
  288. }
  289. $cursor = count($stories) - 1;
  290. $stories = [[
  291. 'id' => (string) $stories[$cursor]['id'],
  292. 'photo' => $profile->avatarUrl(),
  293. 'name' => $profile->username,
  294. 'link' => $profile->url(),
  295. 'lastUpdated' => (int) now()->format('U'),
  296. 'seen' => null,
  297. 'items' => $stories,
  298. 'pid' => (string) $profile->id
  299. ]];
  300. return response()->json($stories, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
  301. }
  302. public function apiV1Viewed(Request $request)
  303. {
  304. abort_if(!config('instance.stories.enabled') || !$request->user(), 404);
  305. $this->validate($request, [
  306. 'id' => 'required|integer|min:1|exists:stories',
  307. ]);
  308. $id = $request->input('id');
  309. $authed = $request->user()->profile;
  310. $story = Story::with('profile')
  311. ->where('expires_at', '>', now())
  312. ->orderByDesc('expires_at')
  313. ->findOrFail($id);
  314. $profile = $story->profile;
  315. if($story->profile_id == $authed->id) {
  316. return [];
  317. }
  318. $publicOnly = (bool) $profile->followedBy($authed);
  319. abort_if(!$publicOnly, 403);
  320. StoryView::firstOrCreate([
  321. 'story_id' => $id,
  322. 'profile_id' => $authed->id
  323. ]);
  324. $story->view_count = $story->view_count + 1;
  325. $story->save();
  326. return ['code' => 200];
  327. }
  328. public function apiV1Exists(Request $request, $id)
  329. {
  330. abort_if(!config('instance.stories.enabled') || !$request->user(), 404);
  331. $res = (bool) Story::whereProfileId($id)
  332. ->whereActive(true)
  333. ->where('expires_at', '>', now())
  334. ->count();
  335. return response()->json($res);
  336. }
  337. public function apiV1Me(Request $request)
  338. {
  339. abort_if(!config('instance.stories.enabled') || !$request->user(), 404);
  340. $profile = $request->user()->profile;
  341. $stories = Story::whereProfileId($profile->id)
  342. ->whereActive(true)
  343. ->orderBy('expires_at')
  344. ->where('expires_at', '>', now())
  345. ->get()
  346. ->map(function($s, $k) {
  347. return [
  348. 'id' => $s->id,
  349. 'type' => Str::endsWith($s->path, '.mp4') ? 'video' :'photo',
  350. 'length' => 3,
  351. 'src' => url(Storage::url($s->path)),
  352. 'preview' => null,
  353. 'link' => null,
  354. 'linkText' => null,
  355. 'time' => $s->created_at->format('U'),
  356. 'expires_at' => (int) $s->expires_at->format('U'),
  357. 'seen' => true
  358. ];
  359. })->toArray();
  360. $ts = count($stories) ? last($stories)['time'] : null;
  361. $res = [
  362. 'id' => (string) $profile->id,
  363. 'photo' => $profile->avatarUrl(),
  364. 'name' => $profile->username,
  365. 'link' => $profile->url(),
  366. 'lastUpdated' => $ts,
  367. 'seen' => true,
  368. 'items' => $stories
  369. ];
  370. return response()->json($res, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
  371. }
  372. public function compose(Request $request)
  373. {
  374. abort_if(!config('instance.stories.enabled') || !$request->user(), 404);
  375. return view('stories.compose');
  376. }
  377. public function iRedirect(Request $request)
  378. {
  379. abort_if(!config('instance.stories.enabled') || !$request->user(), 404);
  380. $user = $request->user();
  381. abort_if(!$user, 404);
  382. $username = $user->username;
  383. return redirect("/stories/{$username}");
  384. }
  385. }