/** * 用于接收歌曲上传数据 * * @param Request $request * * @return \Illuminate\Http\JsonResponse */ public function songUpload(Request $request) { /** * 歌曲上传时附带的点歌信息 * * @var Object $msgObj * @property string singer * @property string songName * @property string toWho * @property string contest * @property string pointPerson * @property \DateTime time * @property integer vote */ $msgObj = json_decode($request->get('message')); if (!is_object($msgObj)) { return response()->json('信息未提供完整,无法上传。', 403); } $song = new Song(); $file = $request->file('mis'); if ($file->getError() !== 0) { return response()->json($file->getErrorMessage(), 500); } $filename = $msgObj->singer . '-' . $msgObj->songName . '.' . (empty($file->getExtension()) ? mb_substr($file->getClientOriginalName(), mb_strpos($file->getClientOriginalName(), '.') + 1) : $file->getExtension()); if (!Storage::disk('local')->put('songs/' . $filename, file_get_contents($file->getRealPath()))) { return response()->json('文件保存失败,请联系服务器管理员', 500); } $song->song_name = $msgObj->songName; $song->song_singer = $msgObj->singer; $song->song_reference = Storage::url($filename); $song->song_size = $file->getSize(); $song->saveOrFail(); }