@@ -15,13 +15,14 @@ module.exports = Mn.View.extend({
1515 max_file_size : 5120 ,
1616
1717 ui : {
18- form : 'form' ,
19- domain_names : 'input[name="domain_names"]' ,
20- buttons : '.modal-footer button' ,
21- cancel : 'button.cancel' ,
22- save : 'button.save' ,
23- other_ssl_certificate : '#other_ssl_certificate' ,
24- other_ssl_certificate_key : '#other_ssl_certificate_key'
18+ form : 'form' ,
19+ domain_names : 'input[name="domain_names"]' ,
20+ buttons : '.modal-footer button' ,
21+ cancel : 'button.cancel' ,
22+ save : 'button.save' ,
23+ other_certificate : '#other_certificate' ,
24+ other_certificate_key : '#other_certificate_key' ,
25+ other_intermediate_certificate : '#other_intermediate_certificate'
2526 } ,
2627
2728 events : {
@@ -33,8 +34,8 @@ module.exports = Mn.View.extend({
3334 return ;
3435 }
3536
36- let view = this ;
37- let data = this . ui . form . serializeJSON ( ) ;
37+ let view = this ;
38+ let data = this . ui . form . serializeJSON ( ) ;
3839 data . provider = this . model . get ( 'provider' ) ;
3940
4041 // Manipulate
@@ -46,55 +47,66 @@ module.exports = Mn.View.extend({
4647 data . domain_names = data . domain_names . split ( ',' ) ;
4748 }
4849
49- let method = App . Api . Nginx . Certificates . create ;
50- let is_new = true ;
5150 let ssl_files = [ ] ;
5251
53- if ( this . model . get ( 'id' ) ) {
54- // edit
55- is_new = false ;
56- method = App . Api . Nginx . Certificates . update ;
57- data . id = this . model . get ( 'id' ) ;
58- }
59-
6052 // check files are attached
6153 if ( this . model . get ( 'provider' ) === 'other' && ! this . model . hasSslFiles ( ) ) {
62- if ( ! this . ui . other_ssl_certificate [ 0 ] . files . length || ! this . ui . other_ssl_certificate [ 0 ] . files [ 0 ] . size ) {
63- alert ( 'certificate file is not attached' ) ;
54+ if ( ! this . ui . other_certificate [ 0 ] . files . length || ! this . ui . other_certificate [ 0 ] . files [ 0 ] . size ) {
55+ alert ( 'Certificate file is not attached' ) ;
6456 return ;
6557 } else {
66- if ( this . ui . other_ssl_certificate [ 0 ] . files [ 0 ] . size > this . max_file_size ) {
67- alert ( 'certificate file is too large (> 5kb)' ) ;
58+ if ( this . ui . other_certificate [ 0 ] . files [ 0 ] . size > this . max_file_size ) {
59+ alert ( 'Certificate file is too large (> 5kb)' ) ;
6860 return ;
6961 }
70- ssl_files . push ( { name : 'certificate' , file : this . ui . other_ssl_certificate [ 0 ] . files [ 0 ] } ) ;
62+ ssl_files . push ( { name : 'certificate' , file : this . ui . other_certificate [ 0 ] . files [ 0 ] } ) ;
7163 }
7264
73- if ( ! this . ui . other_ssl_certificate_key [ 0 ] . files . length || ! this . ui . other_ssl_certificate_key [ 0 ] . files [ 0 ] . size ) {
74- alert ( 'certificate key file is not attached' ) ;
65+ if ( ! this . ui . other_certificate_key [ 0 ] . files . length || ! this . ui . other_certificate_key [ 0 ] . files [ 0 ] . size ) {
66+ alert ( 'Certificate key file is not attached' ) ;
7567 return ;
7668 } else {
77- if ( this . ui . other_ssl_certificate_key [ 0 ] . files [ 0 ] . size > this . max_file_size ) {
78- alert ( 'certificate key file is too large (> 5kb)' ) ;
69+ if ( this . ui . other_certificate_key [ 0 ] . files [ 0 ] . size > this . max_file_size ) {
70+ alert ( 'Certificate key file is too large (> 5kb)' ) ;
71+ return ;
72+ }
73+ ssl_files . push ( { name : 'certificate_key' , file : this . ui . other_certificate_key [ 0 ] . files [ 0 ] } ) ;
74+ }
75+
76+ if ( this . ui . other_intermediate_certificate [ 0 ] . files . length && this . ui . other_intermediate_certificate [ 0 ] . files [ 0 ] . size ) {
77+ if ( this . ui . other_intermediate_certificate [ 0 ] . files [ 0 ] . size > this . max_file_size ) {
78+ alert ( 'Intermediate Certificate file is too large (> 5kb)' ) ;
7979 return ;
8080 }
81- ssl_files . push ( { name : 'certificate_key ' , file : this . ui . other_ssl_certificate_key [ 0 ] . files [ 0 ] } ) ;
81+ ssl_files . push ( { name : 'intermediate_certificate ' , file : this . ui . other_intermediate_certificate [ 0 ] . files [ 0 ] } ) ;
8282 }
8383 }
8484
8585 this . ui . buttons . prop ( 'disabled' , true ) . addClass ( 'btn-disabled' ) ;
86- method ( data )
86+
87+ // compile file data
88+ let form_data = new FormData ( ) ;
89+ if ( view . model . get ( 'provider' ) && ssl_files . length ) {
90+ ssl_files . map ( function ( file ) {
91+ form_data . append ( file . name , file . file ) ;
92+ } ) ;
93+ }
94+
95+ new Promise ( resolve => {
96+ if ( view . model . get ( 'provider' ) === 'other' ) {
97+ resolve ( App . Api . Nginx . Certificates . validate ( form_data ) ) ;
98+ } else {
99+ resolve ( ) ;
100+ }
101+ } )
102+ . then ( ( ) => {
103+ return App . Api . Nginx . Certificates . create ( data ) ;
104+ } )
87105 . then ( result => {
88106 view . model . set ( result ) ;
89107
90108 // Now upload the certs if we need to
91- if ( ssl_files . length ) {
92- let form_data = new FormData ( ) ;
93-
94- ssl_files . map ( function ( file ) {
95- form_data . append ( file . name , file . file ) ;
96- } ) ;
97-
109+ if ( view . model . get ( 'provider' ) === 'other' ) {
98110 return App . Api . Nginx . Certificates . upload ( view . model . get ( 'id' ) , form_data )
99111 . then ( result => {
100112 view . model . set ( 'meta' , _ . assign ( { } , view . model . get ( 'meta' ) , result ) ) ;
@@ -103,9 +115,7 @@ module.exports = Mn.View.extend({
103115 } )
104116 . then ( ( ) => {
105117 App . UI . closeModal ( function ( ) {
106- if ( is_new ) {
107- App . Controller . showNginxCertificates ( ) ;
108- }
118+ App . Controller . showNginxCertificates ( ) ;
109119 } ) ;
110120 } )
111121 . catch ( err => {
0 commit comments