10
10
import retrofit2 .Retrofit ;
11
11
12
12
import java .io .File ;
13
+ import java .io .IOException ;
14
+ import java .net .URLConnection ;
13
15
import java .util .HashMap ;
14
16
import java .util .Map ;
15
17
import java .util .Objects ;
@@ -47,10 +49,6 @@ protected Asset(Retrofit instance, Map<String, Object> header, String uid) {
47
49
this .service = instance .create (AssetService .class );
48
50
}
49
51
50
- void validate () {
51
- Objects .requireNonNull (this .assetUid , "Asset Uid Can Not Be Null OR Empty" );
52
- }
53
-
54
52
55
53
public Asset addParam (String key , Object value ) {
56
54
this .params .put (key , value );
@@ -182,7 +180,7 @@ public Call<ResponseBody> find() {
182
180
* @since 2022-10-20
183
181
*/
184
182
public Call <ResponseBody > fetch () {
185
- this . validate ( );
183
+ Objects . requireNonNull ( this . assetUid , "Asset Uid Can Not Be Null OR Empty" );
186
184
return this .service .single (this .headers , this .assetUid , this .params );
187
185
}
188
186
@@ -366,13 +364,30 @@ private MultipartBody.Part createMultipartBody(String filePath, String parentUid
366
364
* @since 2022-10-20
367
365
*/
368
366
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 );
371
369
RequestBody body = RequestBody .create (MediaType .parse (String .valueOf (MultipartBody .FORM )), description );
372
370
return this .service .replace (this .headers , this .assetUid , assetPath , body , this .params );
373
371
}
374
372
375
373
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
+
376
391
/**
377
392
* Generate Permanent Asset URL request allows you to generate a permanent URL
378
393
* for an asset. This URL remains
@@ -402,7 +417,7 @@ public Call<ResponseBody> replace(@NotNull String filePath, @NotNull String desc
402
417
* @since 2022-10-20
403
418
*/
404
419
public Call <ResponseBody > generatePermanentUrl (JSONObject body ) {
405
- this . validate ( );
420
+ Objects . requireNonNull ( this . assetUid , "Asset Uid Can Not Be Null OR Empty" );
406
421
return this .service .generatePermanentUrl (this .headers , this .assetUid , body );
407
422
}
408
423
@@ -432,7 +447,7 @@ public Call<ResponseBody> generatePermanentUrl(JSONObject body) {
432
447
* @since 2022-10-20
433
448
*/
434
449
public Call <ResponseBody > getPermanentUrl (String slugUrl ) {
435
- this . validate ( );
450
+ Objects . requireNonNull ( this . assetUid , "Asset Uid Can Not Be Null OR Empty" );
436
451
return this .service .downloadPermanentUrl (this .headers , this .assetUid , slugUrl , this .params );
437
452
}
438
453
@@ -449,7 +464,7 @@ public Call<ResponseBody> getPermanentUrl(String slugUrl) {
449
464
* @since 0.1.0
450
465
*/
451
466
public Call <ResponseBody > delete () {
452
- this . validate ( );
467
+ Objects . requireNonNull ( this . assetUid , "Asset Uid Can Not Be Null OR Empty" );
453
468
return this .service .delete (this .headers , this .assetUid );
454
469
}
455
470
@@ -539,7 +554,7 @@ public Call<ResponseBody> setVersionName(int versionNumber,
539
554
* @since 0.1.0
540
555
*/
541
556
public Call <ResponseBody > getVersionNameDetails () {
542
- this . validate ( );
557
+ Objects . requireNonNull ( this . assetUid , "Asset Uid Can Not Be Null OR Empty" );
543
558
return this .service .getVersionNameDetails (this .headers , this .assetUid , this .params );
544
559
}
545
560
@@ -560,7 +575,7 @@ public Call<ResponseBody> getVersionNameDetails() {
560
575
* @since 0.1.0
561
576
*/
562
577
public Call <ResponseBody > deleteVersionName (int versionNumber ) {
563
- this . validate ( );
578
+ Objects . requireNonNull ( this . assetUid , "Asset Uid Can Not Be Null OR Empty" );
564
579
return this .service .deleteVersionName (this .headers , this .assetUid , versionNumber );
565
580
}
566
581
@@ -577,7 +592,7 @@ public Call<ResponseBody> deleteVersionName(int versionNumber) {
577
592
* @since 0.1.0
578
593
*/
579
594
public Call <ResponseBody > getReferences () {
580
- validate ( );
595
+ Objects . requireNonNull ( this . assetUid , "Asset Uid Can Not Be Null OR Empty" );
581
596
return this .service .getReferences (this .headers , this .assetUid );
582
597
}
583
598
@@ -635,7 +650,7 @@ public Call<ResponseBody> getByType(@NotNull String assetType) {
635
650
* @since 0.1.0
636
651
*/
637
652
public Call <ResponseBody > updateDetails (JSONObject requestBody ) {
638
- validate ( );
653
+ Objects . requireNonNull ( this . assetUid , "Asset Uid Can Not Be Null OR Empty" );
639
654
return this .service .updateDetails (this .headers , this .assetUid , this .params , requestBody );
640
655
}
641
656
@@ -661,7 +676,7 @@ public Call<ResponseBody> updateDetails(JSONObject requestBody) {
661
676
* @since 0.1.0
662
677
*/
663
678
public Call <ResponseBody > publish (@ NotNull JSONObject requestBody ) {
664
- validate ( );
679
+ Objects . requireNonNull ( this . assetUid , "Asset Uid Can Not Be Null OR Empty" );
665
680
return this .service .publish (this .headers , this .assetUid , requestBody );
666
681
}
667
682
@@ -687,7 +702,7 @@ public Call<ResponseBody> publish(@NotNull JSONObject requestBody) {
687
702
*/
688
703
public Call <ResponseBody > unpublish (
689
704
@ NotNull JSONObject requestBody ) {
690
- this . validate ( );
705
+ Objects . requireNonNull ( this . assetUid , "Asset Uid Can Not Be Null OR Empty" );
691
706
return this .service .unpublish (this .headers , this .assetUid , requestBody );
692
707
}
693
708
0 commit comments