the browser-facing portion of osu!
at master 2.8 kB view raw
1<?php 2 3// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the GNU Affero General Public License v3.0. 4// See the LICENCE file in the repository root for full licence text. 5 6namespace App\Http\Controllers\Forum; 7 8use App\Exceptions\ImageProcessorException; 9use App\Models\Forum\Forum; 10use App\Models\Forum\Topic; 11use App\Models\Forum\TopicCover; 12use App\Transformers\Forum\TopicCoverTransformer; 13use Auth; 14use Request; 15 16class TopicCoversController extends Controller 17{ 18 public function __construct() 19 { 20 parent::__construct(); 21 22 $this->middleware('auth', ['only' => [ 23 'destroy', 24 'store', 25 'update', 26 ]]); 27 } 28 29 public function store() 30 { 31 $params = get_params(Request::all(), null, [ 32 'cover_file:file', 33 'forum_id:int', 34 'topic_id:int', 35 ], ['null_missing' => true]); 36 37 if ($params['cover_file'] === null) { 38 abort(422); 39 } 40 41 if ($params['topic_id'] === null) { 42 if ($params['forum_id'] !== null) { 43 $forum = Forum::findOrFail($params['forum_id']); 44 } 45 } else { 46 $topic = Topic::with('forum')->findOrFail($params['topic_id']); 47 48 if ($topic->cover !== null) { 49 abort(422); 50 } 51 52 $forum = $topic->forum; 53 } 54 55 if (!isset($forum)) { 56 abort(422, 'no forum specified'); 57 } 58 59 priv_check('ForumTopicCoverStore', $forum)->ensureCan(); 60 61 try { 62 $cover = TopicCover::upload( 63 $params['cover_file'], 64 Auth::user(), 65 $topic ?? null, 66 ); 67 } catch (ImageProcessorException $e) { 68 return error_popup($e->getMessage()); 69 } 70 71 return json_item($cover, new TopicCoverTransformer()); 72 } 73 74 public function destroy($id) 75 { 76 $cover = TopicCover::find($id); 77 78 if ($cover !== null) { 79 priv_check('ForumTopicCoverEdit', $cover)->ensureCan(); 80 81 $cover->delete(); 82 } 83 84 return json_item($cover, new TopicCoverTransformer()); 85 } 86 87 public function update($id) 88 { 89 $cover = TopicCover::findOrFail($id); 90 91 priv_check('ForumTopicCoverEdit', $cover)->ensureCan(); 92 93 if (Request::hasFile('cover_file') === true) { 94 try { 95 $cover = $cover->updateFile( 96 Request::file('cover_file')->getRealPath(), 97 Auth::user() 98 ); 99 } catch (ImageProcessorException $e) { 100 return error_popup($e->getMessage()); 101 } 102 } 103 104 return json_item($cover, new TopicCoverTransformer()); 105 } 106}