public function doUploadImage(Guard $guard, Request $request) { if ($guard->guest()) { throw new AppException(AppException::NEED_SIGN_IN); } $uploadedFile = $request->file('file'); if (!$uploadedFile) { throw new FileUploadException(FileUploadException::UploadFail); } if (!$uploadedFile->isValid()) { throw new FileUploadException(FileUploadException::UploadFailWithError, ['error' => $uploadedFile->getErrorMessage()]); } // $chunkSize = $request->get('_chunkSize'); // $chunkNumber = $request->get('_chunkNumber'); $totalSize = $request->get('_totalSize'); $uniName = $request->has('uniName') ? $guard->id() . '-' . md5($request->get('uniName')) : $guard->id() . '-' . md5($uploadedFile->getClientOriginalName() . '-' . $uploadedFile->getClientMimeType()); $filePath = FileManager::rebuildChunkFile($uploadedFile->getRealPath(), $uniName, $totalSize); if ($filePath == false) { return $this->buildResponse(trans('api.image.upload.continue')); } $fileName = $request->get('filename'); if (!$fileName) { $fileName = $uploadedFile->getClientOriginalName(); } /** @var User $user */ $user = $guard->user(); $image = FileManager::UploadImage($filePath, $fileName, $user); return $this->buildResponse(trans('api.image.upload.success'), $image); }
public function doUploadMusic(Guard $guard, Request $request) { if ($guard->guest()) { throw new AppException(AppException::NEED_SIGN_IN); } $uploadedFile = $request->file('file'); if (!$uploadedFile) { throw new FileUploadException(FileUploadException::UploadFail); } if (!$uploadedFile->isValid()) { throw new FileUploadException(FileUploadException::UploadFailWithError, ['error' => $uploadedFile->getErrorMessage()]); } // $chunkSize = $request->get('_chunkSize'); // $chunkNumber = $request->get('_chunkNumber'); $totalSize = $request->get('_totalSize'); $uniName = $request->has('uniName') ? $guard->id() . '-' . md5($request->get('uniName')) : $guard->id() . '-' . md5($uploadedFile->getClientOriginalName() . '-' . $uploadedFile->getClientMimeType()); $filePath = FileManager::rebuildChunkFile($uploadedFile->getRealPath(), $uniName, $totalSize); if ($filePath == false) { return $this->buildResponse(trans('api.music.upload.continue')); } $fileName = $request->get('filename'); if (!$fileName) { $fileName = $uploadedFile->getClientOriginalName(); } /** @var User $user */ $user = $guard->user(); $music = FileManager::UploadMusic($filePath, $fileName, $user); if ($request->has('albumId')) { $id = $request->get('albumId'); /** @var Album $album */ $album = Album::where('id', $id)->first(); if (!$album) { throw new AppException(AppException::ALBUM_NOT_FOUND); } $album->musics()->attach($music); } return $this->buildResponse(trans('api.music.upload.success'), $music); }