@@ -23,8 +23,8 @@ class BunnyAPI
23
23
private $ connection ;
24
24
private array $ data ;
25
25
private int $ stream_library_id ;
26
- private string $ stream_collection_guid = '' ;
27
- private string $ stream_video_guid = '' ;
26
+ private string $ stream_collection_guid ;
27
+ private string $ stream_video_guid ;
28
28
29
29
public function __construct (int $ execution_time = 240 , bool $ json_header = false )
30
30
{
@@ -711,7 +711,7 @@ public function costCalculator(int $bytes): array
711
711
* Bunny net video stream section
712
712
*
713
713
*/
714
- //Library -> collection -> video
714
+ //Stream library -> collection -> video
715
715
public function setStreamLibraryId (int $ library_id ): void
716
716
{
717
717
$ this ->stream_library_id = $ library_id ;
@@ -732,85 +732,140 @@ public function getVideoCollections(): array
732
732
return $ this ->APIcall ('GET ' , "library/ {$ this ->stream_library_id }/collections " , [], false , true );
733
733
}
734
734
735
- public function getStreamCollections (int $ library_id = 0 , int $ page = 1 , int $ items_pp = 100 , string $ order_by = 'date ' ): array
735
+ public function getStreamCollections (int $ page = 1 , int $ items_pp = 100 , string $ order_by = 'date ' ): array
736
736
{
737
- if ($ library_id === 0 ) {
738
- $ library_id = $ this ->stream_library_id ;
739
- }
740
- return $ this ->APIcall ('GET ' , "library/ $ library_id/collections?page= $ page&itemsPerPage= $ items_pp&orderBy= $ order_by " , [], false , true );
737
+ $ this ->checkStreamLibraryIdSet ();
738
+ return $ this ->APIcall ('GET ' , "library/ {$ this ->stream_library_id }/collections?page= $ page&itemsPerPage= $ items_pp&orderBy= $ order_by " , [], false , true );
741
739
}
742
740
743
- public function getStreamForCollection (int $ library_id = 0 , string $ collection_guid = '' ): array
741
+ public function getStreamForCollection (): array
744
742
{
745
- if ($ library_id === 0 ) {
746
- $ library_id = $ this ->stream_library_id ;
747
- }
748
- if (empty ($ collection_guid )) {
749
- $ collection_guid = $ this ->stream_collection_guid ;
750
- }
751
- return $ this ->APIcall ('GET ' , "library/ $ library_id/collections/ $ collection_guid " , [], false , true );
743
+ $ this ->checkStreamLibraryIdSet ();
744
+ $ this ->checkStreamCollectionGuidSet ();
745
+ return $ this ->APIcall ('GET ' , "library/ {$ this ->stream_library_id }/collections/ " . $ this ->stream_collection_guid , [], false , true );
752
746
}
753
747
754
- public function updateCollection (int $ library_id , string $ collection_guid , string $ video_library_id , int $ video_count , int $ total_size ): array
748
+ public function updateCollection (string $ updated_collection_name ): array
755
749
{
756
- return $ this ->APIcall ('POST ' , "library/ $ library_id/collections/ $ collection_guid " , array ("videoLibraryId " => $ video_library_id , "videoCount " => $ video_count , "totalSize " => $ total_size ), false , true );
750
+ $ this ->checkStreamLibraryIdSet ();
751
+ return $ this ->APIcall ('POST ' , "library/ {$ this ->stream_library_id }/collections/ " . $ this ->stream_collection_guid , array ("name " => $ updated_collection_name ), false , true );
757
752
}
758
753
759
- public function deleteCollection (int $ library_id , string $ collection_id ): array
754
+ public function deleteCollection (): array
760
755
{
761
- return $ this ->APIcall ('DELETE ' , "library/ $ library_id/collections/ $ collection_id " , [], false , true );
756
+ $ this ->checkStreamLibraryIdSet ();
757
+ $ this ->checkStreamCollectionGuidSet ();
758
+ return $ this ->APIcall ('DELETE ' , "library/ {$ this ->stream_library_id }/collections/ " . $ this ->stream_collection_guid , [], false , true );
762
759
}
763
760
764
- public function createCollection (int $ library_id , string $ video_library_id , int $ video_count , int $ total_size ): array
761
+ public function createCollection (string $ new_collection_name ): array
765
762
{
766
- return $ this ->APIcall ('POST ' , "library/ $ library_id/collections " , array ("videoLibraryId " => $ video_library_id , "videoCount " => $ video_count , "totalSize " => $ total_size ), false , true );
763
+ $ this ->checkStreamLibraryIdSet ();
764
+ return $ this ->APIcall ('POST ' , "library/ {$ this ->stream_library_id }/collections " , array ("name " => $ new_collection_name ), false , true );
767
765
}
768
766
769
767
public function listVideos (int $ page = 1 , int $ items_pp = 100 , string $ order_by = 'date ' ): array
770
768
{
771
- if (!isset ($ this ->stream_library_id )) {
772
- return array ('response ' => 'fail ' , 'action ' => __FUNCTION__ , 'message ' => 'You must set library id with: setStreamLibraryId() ' );
773
- }
769
+ $ this ->checkStreamLibraryIdSet ();
774
770
return $ this ->APIcall ('GET ' , "library/ {$ this ->stream_library_id }/videos?page= $ page&itemsPerPage= $ items_pp&orderBy= $ order_by " , [], false , true );
775
771
}
776
772
777
- public function getVideo (int $ library_id , string $ video_guid ): array
773
+ public function listVideosForCollectionId (int $ page = 1 , int $ items_pp = 100 , string $ order_by = ' date ' ): array
778
774
{
779
- return $ this ->APIcall ('GET ' , "library/ $ library_id/videos/ $ video_guid " , [], false , true );
775
+ $ this ->checkStreamLibraryIdSet ();
776
+ $ this ->checkStreamCollectionGuidSet ();
777
+ return $ this ->APIcall ('GET ' , "library/ {$ this ->stream_library_id }/videos?collection= {$ this ->stream_collection_guid }&page= $ page&itemsPerPage= $ items_pp&orderBy= $ order_by " , [], false , true );
780
778
}
781
779
782
- public function deleteVideo ( int $ library_id , string $ video_guid ): array
780
+ public function getVideo ( string $ video_guid ): array
783
781
{
784
- return $ this ->APIcall ('DELETE ' , "library/ $ library_id/videos/ $ video_guid " , [], false , true );
782
+ $ this ->checkStreamLibraryIdSet ();
783
+ return $ this ->APIcall ('GET ' , "library/ {$ this ->stream_library_id }/videos/ $ video_guid " , [], false , true );
785
784
}
786
785
787
- public function createVideo ( int $ library_id , string $ video_title , string $ collection_guid = '' ): array
786
+ public function deleteVideo ( string $ video_guid ): array
788
787
{
789
- if (!empty ($ collection_guid )) {
790
- return $ this ->APIcall ('POST ' , "library/ $ library_id/videos?title= $ video_title&collectionId= $ collection_guid " , [], false , true );
791
- }
792
- return $ this ->APIcall ('POST ' , "library/ $ library_id/videos?title= $ video_title " , [], false , true );
788
+ $ this ->checkStreamLibraryIdSet ();
789
+ return $ this ->APIcall ('DELETE ' , "library/ {$ this ->stream_library_id }/videos/ $ video_guid " , [], false , true );
790
+ }
791
+
792
+ public function createVideo (string $ video_title ): array
793
+ {
794
+ $ this ->checkStreamLibraryIdSet ();
795
+ return $ this ->APIcall ('POST ' , "library/ {$ this ->stream_library_id }/videos " , array ("title " => $ video_title ), false , true );
796
+ }
797
+
798
+ public function createVideoForCollection (string $ video_title ): array
799
+ {
800
+ $ this ->checkStreamLibraryIdSet ();
801
+ $ this ->checkStreamCollectionGuidSet ();
802
+ return $ this ->APIcall ('POST ' , "library/ {$ this ->stream_library_id }/videos " , array ("title " => $ video_title , "collectionId " => $ this ->stream_collection_guid ), false , true );
793
803
}
794
804
795
- public function uploadVideo (int $ library_id , string $ video_guid , string $ video_to_upload ): array
805
+ public function uploadVideo (string $ video_guid , string $ video_to_upload ): array
796
806
{
797
807
//Need to use createVideo() first to get video guid
798
- return $ this ->APIcall ('PUT ' , "library/ $ library_id/videos/ $ video_guid " , array ('file ' => $ video_to_upload ), false , true );
808
+ $ this ->checkStreamLibraryIdSet ();
809
+ return $ this ->APIcall ('PUT ' , "library/ {$ this ->stream_library_id }/videos/ " . $ video_guid , array ('file ' => $ video_to_upload ), false , true );
810
+
811
+ }
812
+
813
+ public function setThumbnail (string $ video_guid , string $ thumbnail_url ): array
814
+ {
815
+ $ this ->checkStreamLibraryIdSet ();
816
+ return $ this ->APIcall ('POST ' , "library/ {$ this ->stream_library_id }/videos/ $ video_guid/thumbnail? $ thumbnail_url " , [], false , true );
817
+
818
+ }
819
+
820
+ public function addCaptions (string $ video_guid , string $ srclang , string $ label , string $ captions_file ): array
821
+ {
822
+ $ this ->checkStreamLibraryIdSet ();
823
+ return $ this ->APIcall ('POST ' , "library/ {$ this ->stream_library_id }/videos/ $ video_guid/captions/ $ srclang?label= $ label&captionsFile= $ captions_file " , [], false , true );
799
824
}
800
825
801
- public function setThumbnail ( int $ library_id , string $ video_guid , string $ thumbnail_url ): array
826
+ public function deleteCaptions ( string $ video_guid , string $ srclang ): array
802
827
{
803
- return $ this ->APIcall ('POST ' , "library/ $ library_id/videos/ $ video_guid/thumbnail? $ thumbnail_url " , [], false , true );
828
+ $ this ->checkStreamLibraryIdSet ();
829
+ return $ this ->APIcall ('DELETE ' , "library/ {$ this ->stream_library_id }/videos/ $ video_guid/captions/ $ srclang " , [], false , true );
804
830
}
805
831
806
- public function addCaptions (int $ library_id , string $ video_guid , string $ srclang , string $ label , string $ captions_file ): array
832
+ public function videoResolutionsArray (string $ video_guid ): array
833
+ {
834
+ $ this ->checkStreamLibraryIdSet ();
835
+ $ data = $ this ->APIcall ('GET ' , "library/ {$ this ->stream_library_id }/videos/ $ video_guid " , [], false , true );
836
+ return explode (", " , $ data ['availableResolutions ' ]);
837
+ }
838
+
839
+
840
+ public function videoSize (string $ video_guid , string $ size_type = 'MB ' , bool $ format = false , float $ decimals = 2 ): float
807
841
{
808
- return $ this ->APIcall ('POST ' , "library/ $ library_id/videos/ $ video_guid/captions/ $ srclang?label= $ label&captionsFile= $ captions_file " , [], false , true );
842
+ $ this ->checkStreamLibraryIdSet ();
843
+ $ data = $ this ->APIcall ('GET ' , "library/ {$ this ->stream_library_id }/videos/ $ video_guid " , [], false , true );
844
+ return $ this ->convertBytes ($ data ['storageSize ' ], $ size_type , $ format , $ decimals );
845
+ }
846
+
847
+ private function checkStreamLibraryIdSet (): void
848
+ {
849
+ try {
850
+ if (!isset ($ this ->stream_library_id )) {
851
+ throw new BunnyAPIException ("You must set the stream library id first. Use setStreamLibraryId() " );
852
+ }
853
+ } catch (BunnyAPIException $ e ) {//display error message
854
+ echo $ e ->errorMessage ();
855
+ exit ;
856
+ }
809
857
}
810
858
811
- public function deleteCaptions ( int $ library_id , string $ video_guid , string $ srclang ): array
859
+ private function checkStreamCollectionGuidSet ( ): void
812
860
{
813
- return $ this ->APIcall ('DELETE ' , "library/ $ library_id/videos/ $ video_guid/captions/ $ srclang " , [], false , true );
861
+ try {
862
+ if (!isset ($ this ->stream_collection_guid )) {
863
+ throw new BunnyAPIException ("You must set the stream collection guid first. Use setStreamCollectionGuid() " );
864
+ }
865
+ } catch (BunnyAPIException $ e ) {//display error message
866
+ echo $ e ->errorMessage ();
867
+ exit ;
868
+ }
814
869
}
815
870
816
871
}
0 commit comments