Ejemplo n.º 1
0
 public function doUploadAlbum()
 {
     try {
         if (!Input::hasFile('file')) {
             throw new InvalidArgumentException('缺少参数');
         }
         if (!$this->CurrentUser || !$this->CurrentUser->isAdmin()) {
             throw new PermissionDeniedException('无权上传专辑');
         }
         $theFile = Input::file('file');
         $fileUniName = strtolower(Input::get('name'));
         $fileName = strtolower(Input::get('file_ori_name'));
         $fileUrl = $theFile->getRealPath();
         $chunk = Input::get('chunk');
         $chunks = Input::get('chunks');
         $file = AmaotoUploader::uploadFile($this->CurrentUser->id, $fileUniName, $fileName, $fileUrl, $chunk, $chunks, 'upload');
         // 文件上传完成后--->
         // 判断专辑是否已存在
         $albumId = Input::get('album_id');
         if (is_numeric($albumId) && $albumId > 0) {
             $album = AmaotoAlbum::whereId($albumId)->first();
         } else {
             $album = new AmaotoAlbum();
         }
         if (!$album->title) {
             $album->title = '#未命名专辑#';
         }
         if (!$album->user_id) {
             $album->user_id = $this->CurrentUser->id;
         }
         $album->save();
         $typeArray = explode('/', $file->type);
         $pType = '';
         $sType = '';
         if (is_array($typeArray)) {
             if (isset($typeArray[0])) {
                 $pType = $typeArray[0];
             }
             if (isset($typeArray[1])) {
                 $sType = $typeArray[1];
             }
         }
         switch ($pType) {
             case 'audio':
                 $music = new AmaotoMusic();
                 $music->file_id = $file->id;
                 $music->updateTagsByGetId3();
                 if (!$album->title || $album->title == '#未命名专辑#') {
                     $album->title = $music->tag_album;
                 }
                 if (!$album->artist) {
                     $album->artist = $music->tag_album_artist ? $music->tag_album_artist : $music->tag_artist;
                 }
                 if (!$album->year) {
                     $album->year = $music->tag_year;
                 }
                 if (!$album->genre) {
                     $album->genre = $music->tag_genre;
                 }
                 $album->save();
                 $music->album_id = $album->id;
                 $music->save();
                 return Response::json(array('type' => 'success', 'message' => '上传成功', 'data' => array('file' => $file->toArray(), 'music' => $music->toArray(), 'album' => $album->toArray())));
             case 'image':
                 $album->cover_ori_file_id = $file->id;
                 // 生成300px缩图
                 $width = 300;
                 $height = 300;
                 list($width_orig, $height_orig) = getimagesize(public_path($file->url));
                 $ratio_orig = $width_orig / $height_orig;
                 if ($width / $height > $ratio_orig) {
                     $width = $height * $ratio_orig;
                 } else {
                     $height = $width / $ratio_orig;
                 }
                 $image_p = imagecreatetruecolor($width, $height);
                 $image = imagecreatefromjpeg(public_path($file->url));
                 imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
                 $img300TmpUrl = public_path($file->url) . '.300.tmp';
                 imagejpeg($image_p, $img300TmpUrl);
                 imagedestroy($image_p);
                 $img300file = AmaotoUploader::uploadFile($this->CurrentUser->id, $fileUniName, $fileName, $img300TmpUrl, false, false, 'upload');
                 $album->cover_300_file_id = $img300file->id;
                 $album->save();
                 unlink($img300TmpUrl);
                 return Response::json(array('type' => 'success', 'message' => '上传成功', 'data' => array('file' => $file->toArray(), 'album' => $album->toArray())));
             default:
                 throw new NotSupportedException('不支持该类型文件');
         }
     } catch (NeedMoreDataException $e) {
         return Response::json(array('type' => 'information', 'message' => $e->getMessage()));
     } catch (InvalidArgumentException $e) {
         return Response::json(array('type' => 'warning', 'message' => $e->getMessage()));
     } catch (Exception $e) {
         return Response::json(array('type' => 'error', 'message' => $e->getMessage()));
     }
 }