]> git.proxmox.com Git - pve-manager.git/blob - www/manager6/window/DownloadUrlToStorage.js
ui: download: use view model for state handling
[pve-manager.git] / www / manager6 / window / DownloadUrlToStorage.js
1 Ext.define('PVE.window.DownloadUrlToStorage', {
2 extend: 'Proxmox.window.Edit',
3 alias: 'widget.pveStorageDownloadUrl',
4 mixins: ['Proxmox.Mixin.CBind'],
5
6 isCreate: true,
7
8 method: 'POST',
9
10 showTaskViewer: true,
11
12 title: gettext('Download from URL'),
13 submitText: gettext('Download'),
14
15 cbindData: function(initialConfig) {
16 var me = this;
17 return {
18 nodename: me.nodename,
19 storage: me.storage,
20 content: me.content,
21 };
22 },
23
24 cbind: {
25 url: '/nodes/{nodename}/storage/{storage}/download-url',
26 },
27
28
29 viewModel: {
30 data: {
31 size: '-',
32 mimetype: '-',
33 enableQuery: true,
34 },
35 },
36
37 controller: {
38 xclass: 'Ext.app.ViewController',
39
40 urlChange: function(field) {
41 this.resetMetaInfo();
42 this.setQueryEnabled();
43 },
44 setQueryEnabled: function() {
45 this.getViewModel().set('enableQuery', true);
46 },
47 resetMetaInfo: function() {
48 let vm = this.getViewModel();
49 vm.set('size', '-');
50 vm.set('mimetype', '-');
51 },
52
53 urlCheck: function(field) {
54 let me = this;
55 let view = me.getView();
56
57 const queryParam = view.getValues();
58
59 me.getViewModel().set('enableQuery', false);
60 me.resetMetaInfo();
61 let urlField = view.down('[name=url]');
62
63 Proxmox.Utils.API2Request({
64 url: `/nodes/${view.nodename}/query-url-metadata`,
65 method: 'GET',
66 params: {
67 url: queryParam.url,
68 'verify-certificates': queryParam['verify-certificates'],
69 },
70 waitMsgTarget: view,
71 failure: res => {
72 urlField.setValidation(res.result.message);
73 urlField.validate();
74 Ext.MessageBox.alert(gettext('Error'), res.htmlStatus);
75 // re-enable so one can directly requery, e.g., if it was just a network hiccup
76 me.setQueryEnabled();
77 },
78 success: function(res, opt) {
79 urlField.setValidation();
80 urlField.validate();
81
82 let data = res.result.data;
83 view.setValues({
84 filename: data.filename || "",
85 size: (data.size && Proxmox.Utils.format_size(data.size)) || gettext("Unknown"),
86 mimetype: data.mimetype || gettext("Unknown"),
87 });
88 },
89 });
90 },
91
92 hashChange: function(field) {
93 let checksum = Ext.getCmp('downloadUrlChecksum');
94 if (field.getValue() === '__default__') {
95 checksum.setDisabled(true);
96 checksum.setValue("");
97 checksum.allowBlank = true;
98 } else {
99 checksum.setDisabled(false);
100 checksum.allowBlank = false;
101 }
102 },
103 },
104
105 items: [
106 {
107 xtype: 'inputpanel',
108 border: false,
109 columnT: [
110 {
111 xtype: 'fieldcontainer',
112 layout: 'hbox',
113 fieldLabel: gettext('URL'),
114 items: [
115 {
116 xtype: 'textfield',
117 name: 'url',
118 emptyText: gettext("Enter URL to download"),
119 allowBlank: false,
120 flex: 1,
121 listeners: {
122 change: 'urlChange',
123 },
124 },
125 {
126 xtype: 'button',
127 name: 'check',
128 text: gettext('Query URL'),
129 margin: '0 0 0 5',
130 bind: {
131 disabled: '{!enableQuery}',
132 },
133 listeners: {
134 click: 'urlCheck',
135 },
136 },
137 ],
138 },
139 {
140 xtype: 'textfield',
141 name: 'filename',
142 allowBlank: false,
143 fieldLabel: gettext('File name'),
144 emptyText: gettext("Please (re-)query URL to get meta information"),
145 },
146 ],
147 column1: [
148 {
149 xtype: 'displayfield',
150 name: 'size',
151 fieldLabel: gettext('File size'),
152 bind: {
153 value: '{size}',
154 },
155 },
156 ],
157 column2: [
158 {
159 xtype: 'displayfield',
160 name: 'mimetype',
161 fieldLabel: gettext('MIME type'),
162 bind: {
163 value: '{mimetype}',
164 },
165 },
166 ],
167 advancedColumn1: [
168 {
169 xtype: 'pveHashAlgorithmSelector',
170 name: 'checksum-algorithm',
171 fieldLabel: gettext('Hash algorithm'),
172 allowBlank: true,
173 hasNoneOption: true,
174 value: '__default__',
175 listeners: {
176 change: 'hashChange',
177 },
178 },
179 {
180 xtype: 'textfield',
181 name: 'checksum',
182 fieldLabel: gettext('Checksum'),
183 allowBlank: true,
184 disabled: true,
185 emptyText: gettext('none'),
186 id: 'downloadUrlChecksum',
187 },
188 ],
189 advancedColumn2: [
190 {
191 xtype: 'proxmoxcheckbox',
192 name: 'verify-certificates',
193 fieldLabel: gettext('Verify certificates'),
194 uncheckedValue: 0,
195 checked: true,
196 listeners: {
197 change: 'urlChange',
198 },
199 },
200 ],
201 },
202 {
203 xtype: 'hiddenfield',
204 name: 'content',
205 cbind: {
206 value: '{content}',
207 },
208 },
209 ],
210
211 initComponent: function() {
212 var me = this;
213
214 if (!me.nodename) {
215 throw "no node name specified";
216 }
217 if (!me.storage) {
218 throw "no storage ID specified";
219 }
220
221 me.callParent();
222 },
223 });
224