1010import retrofit2 .Retrofit ;
1111
1212import java .io .File ;
13+ import java .io .IOException ;
14+ import java .net .URLConnection ;
1315import java .util .HashMap ;
1416import java .util .Map ;
1517import java .util .Objects ;
@@ -47,10 +49,6 @@ protected Asset(Retrofit instance, Map<String, Object> header, String uid) {
4749 this .service = instance .create (AssetService .class );
4850 }
4951
50- void validate () {
51- Objects .requireNonNull (this .assetUid , "Asset Uid Can Not Be Null OR Empty" );
52- }
53-
5452
5553 public Asset addParam (String key , Object value ) {
5654 this .params .put (key , value );
@@ -182,7 +180,7 @@ public Call<ResponseBody> find() {
182180 * @since 2022-10-20
183181 */
184182 public Call <ResponseBody > fetch () {
185- this . validate ( );
183+ Objects . requireNonNull ( this . assetUid , "Asset Uid Can Not Be Null OR Empty" );
186184 return this .service .single (this .headers , this .assetUid , this .params );
187185 }
188186
@@ -366,13 +364,30 @@ private MultipartBody.Part createMultipartBody(String filePath, String parentUid
366364 * @since 2022-10-20
367365 */
368366 public Call <ResponseBody > replace (@ NotNull String filePath , @ NotNull String description ) {
369- this . validate ( );
370- MultipartBody .Part assetPath = createMultipartBody (filePath , null , null , null , null );
367+ Objects . requireNonNull ( this . assetUid , "Asset Uid Can Not Be Null OR Empty" );
368+ MultipartBody .Part assetPath = uploadFile (filePath );
371369 RequestBody body = RequestBody .create (MediaType .parse (String .valueOf (MultipartBody .FORM )), description );
372370 return this .service .replace (this .headers , this .assetUid , assetPath , body , this .params );
373371 }
374372
375373
374+ private MultipartBody .Part uploadFile (@ NotNull String filePath ) {
375+ if (!filePath .isEmpty ()) {
376+ File file = new File (filePath );
377+ URLConnection connection = null ;
378+ try {
379+ connection = file .toURL ().openConnection ();
380+ } catch (IOException e ) {
381+ throw new RuntimeException (e );
382+ }
383+ if (file .exists ()) {
384+ RequestBody body = RequestBody .create (MediaType .parse (connection .getContentType ()), file );
385+ return MultipartBody .Part .createFormData ("asset[upload]" , file .getName (), body );
386+ }
387+ }
388+ return null ;
389+ }
390+
376391 /**
377392 * Generate Permanent Asset URL request allows you to generate a permanent URL
378393 * for an asset. This URL remains
@@ -402,7 +417,7 @@ public Call<ResponseBody> replace(@NotNull String filePath, @NotNull String desc
402417 * @since 2022-10-20
403418 */
404419 public Call <ResponseBody > generatePermanentUrl (JSONObject body ) {
405- this . validate ( );
420+ Objects . requireNonNull ( this . assetUid , "Asset Uid Can Not Be Null OR Empty" );
406421 return this .service .generatePermanentUrl (this .headers , this .assetUid , body );
407422 }
408423
@@ -432,7 +447,7 @@ public Call<ResponseBody> generatePermanentUrl(JSONObject body) {
432447 * @since 2022-10-20
433448 */
434449 public Call <ResponseBody > getPermanentUrl (String slugUrl ) {
435- this . validate ( );
450+ Objects . requireNonNull ( this . assetUid , "Asset Uid Can Not Be Null OR Empty" );
436451 return this .service .downloadPermanentUrl (this .headers , this .assetUid , slugUrl , this .params );
437452 }
438453
@@ -449,7 +464,7 @@ public Call<ResponseBody> getPermanentUrl(String slugUrl) {
449464 * @since 0.1.0
450465 */
451466 public Call <ResponseBody > delete () {
452- this . validate ( );
467+ Objects . requireNonNull ( this . assetUid , "Asset Uid Can Not Be Null OR Empty" );
453468 return this .service .delete (this .headers , this .assetUid );
454469 }
455470
@@ -539,7 +554,7 @@ public Call<ResponseBody> setVersionName(int versionNumber,
539554 * @since 0.1.0
540555 */
541556 public Call <ResponseBody > getVersionNameDetails () {
542- this . validate ( );
557+ Objects . requireNonNull ( this . assetUid , "Asset Uid Can Not Be Null OR Empty" );
543558 return this .service .getVersionNameDetails (this .headers , this .assetUid , this .params );
544559 }
545560
@@ -560,7 +575,7 @@ public Call<ResponseBody> getVersionNameDetails() {
560575 * @since 0.1.0
561576 */
562577 public Call <ResponseBody > deleteVersionName (int versionNumber ) {
563- this . validate ( );
578+ Objects . requireNonNull ( this . assetUid , "Asset Uid Can Not Be Null OR Empty" );
564579 return this .service .deleteVersionName (this .headers , this .assetUid , versionNumber );
565580 }
566581
@@ -577,7 +592,7 @@ public Call<ResponseBody> deleteVersionName(int versionNumber) {
577592 * @since 0.1.0
578593 */
579594 public Call <ResponseBody > getReferences () {
580- validate ( );
595+ Objects . requireNonNull ( this . assetUid , "Asset Uid Can Not Be Null OR Empty" );
581596 return this .service .getReferences (this .headers , this .assetUid );
582597 }
583598
@@ -635,7 +650,7 @@ public Call<ResponseBody> getByType(@NotNull String assetType) {
635650 * @since 0.1.0
636651 */
637652 public Call <ResponseBody > updateDetails (JSONObject requestBody ) {
638- validate ( );
653+ Objects . requireNonNull ( this . assetUid , "Asset Uid Can Not Be Null OR Empty" );
639654 return this .service .updateDetails (this .headers , this .assetUid , this .params , requestBody );
640655 }
641656
@@ -661,7 +676,7 @@ public Call<ResponseBody> updateDetails(JSONObject requestBody) {
661676 * @since 0.1.0
662677 */
663678 public Call <ResponseBody > publish (@ NotNull JSONObject requestBody ) {
664- validate ( );
679+ Objects . requireNonNull ( this . assetUid , "Asset Uid Can Not Be Null OR Empty" );
665680 return this .service .publish (this .headers , this .assetUid , requestBody );
666681 }
667682
@@ -687,7 +702,7 @@ public Call<ResponseBody> publish(@NotNull JSONObject requestBody) {
687702 */
688703 public Call <ResponseBody > unpublish (
689704 @ NotNull JSONObject requestBody ) {
690- this . validate ( );
705+ Objects . requireNonNull ( this . assetUid , "Asset Uid Can Not Be Null OR Empty" );
691706 return this .service .unpublish (this .headers , this .assetUid , requestBody );
692707 }
693708
0 commit comments