]>
git.proxmox.com Git - pve-manager.git/blob - www/manager6/window/UploadToStorage.js
1 Ext
.define('PVE.window.UploadToStorage', {
2 extend
: 'Ext.window.Window',
3 alias
: 'widget.pveStorageUpload',
4 mixins
: ['Proxmox.Mixin.CBind'],
9 title
: gettext('Upload'),
12 iso
: ['.img', '.iso'],
13 vztmpl
: ['.tar.gz', '.tar.xz', '.tar.zst'],
16 cbindData: function(initialConfig
) {
18 const ext
= me
.acceptedExtensions
[me
.content
] || [];
20 me
.url
= `/nodes/${me.nodename}/storage/${me.storage}/upload`;
23 extensions
: ext
.join(', '),
24 filenameRegex
: RegExp('^.*(?:' + ext
.join('|').replaceAll('.', '\\.') + ')$', 'i'),
37 submit: function(button
) {
38 const view
= this.getView();
39 const form
= this.lookup('formPanel').getForm();
40 const abortBtn
= this.lookup('abortBtn');
41 const pbar
= this.lookup('progressBar');
43 const updateProgress = function(per
, bytes
) {
44 let text
= (per
* 100).toFixed(2) + '%';
46 text
+= " (" + Proxmox
.Utils
.format_size(bytes
) + ')';
48 pbar
.updateProgress(per
, text
);
51 const fd
= new FormData();
53 button
.setDisabled(true);
54 abortBtn
.setDisabled(false);
56 fd
.append("content", view
.content
);
58 const fileField
= form
.findField('file');
59 const file
= fileField
.fileInputEl
.dom
.files
[0];
60 fileField
.setDisabled(true);
62 const filenameField
= form
.findField('filename');
63 const filename
= filenameField
.getValue();
64 filenameField
.setDisabled(true);
66 const algorithmField
= form
.findField('checksum-algorithm');
67 algorithmField
.setDisabled(true);
68 if (algorithmField
.getValue() !== '__default__') {
69 fd
.append("checksum-algorithm", algorithmField
.getValue());
71 const checksumField
= form
.findField('checksum');
72 fd
.append("checksum", checksumField
.getValue()?.trim());
73 checksumField
.setDisabled(true);
76 fd
.append("filename", file
, filename
);
78 pbar
.setVisible(true);
81 const xhr
= new XMLHttpRequest();
84 xhr
.addEventListener("load", function(e
) {
85 if (xhr
.status
=== 200) {
88 const result
= JSON
.parse(xhr
.response
);
89 const upid
= result
.data
;
90 Ext
.create('Proxmox.window.TaskViewer', {
93 taskDone
: view
.taskDone
,
103 const err
= Ext
.htmlEncode(xhr
.statusText
);
104 let msg
= `${gettext('Error')} ${xhr.status.toString()}: ${err}`;
105 if (xhr
.responseText
!== "") {
106 const result
= Ext
.decode(xhr
.responseText
);
107 result
.message
= msg
;
108 msg
= Proxmox
.Utils
.extractRequestError(result
, true);
110 Ext
.Msg
.alert(gettext('Error'), msg
, btn
=> view
.close());
113 xhr
.addEventListener("error", function(e
) {
114 const err
= e
.target
.status
.toString();
115 const msg
= `Error '${err}' occurred while receiving the document.`;
116 Ext
.Msg
.alert(gettext('Error'), msg
, btn
=> view
.close());
119 xhr
.upload
.addEventListener("progress", function(evt
) {
120 if (evt
.lengthComputable
) {
121 const percentComplete
= evt
.loaded
/ evt
.total
;
122 updateProgress(percentComplete
, evt
.loaded
);
126 xhr
.open("POST", `/api2/json${view.url}`, true);
130 validitychange: function(f
, valid
) {
131 const submitBtn
= this.lookup('submitBtn');
132 submitBtn
.setDisabled(!valid
);
135 fileChange: function(input
) {
136 const vm
= this.getViewModel();
137 const name
= input
.value
.replace(/^.*(\/|\\)/, '');
138 const fileInput
= input
.fileInputEl
.dom
;
139 vm
.set('filename', name
);
140 vm
.set('size', (fileInput
.files
[0] && Proxmox
.Utils
.format_size(fileInput
.files
[0].size
)) || '-');
141 vm
.set('mimetype', (fileInput
.files
[0] && fileInput
.files
[0].type
) || '-');
144 hashChange: function(field
, value
) {
145 const checksum
= this.lookup('downloadUrlChecksum');
146 if (value
=== '__default__') {
147 checksum
.setDisabled(true);
148 checksum
.setValue("");
150 checksum
.setDisabled(false);
158 reference
: 'formPanel',
172 buttonText
: gettext('Select File'),
174 fieldLabel
: gettext('File'),
176 accept
: '{extensions}',
179 change
: 'fileChange',
186 fieldLabel
: gettext('File name'),
191 regex
: '{filenameRegex}',
193 regexText
: gettext('Wrong file extension'),
196 xtype
: 'displayfield',
198 fieldLabel
: gettext('File size'),
204 xtype
: 'displayfield',
206 fieldLabel
: gettext('MIME type'),
212 xtype
: 'pveHashAlgorithmSelector',
213 name
: 'checksum-algorithm',
214 fieldLabel
: gettext('Hash algorithm'),
217 value
: '__default__',
219 change
: 'hashChange',
225 fieldLabel
: gettext('Checksum'),
228 emptyText
: gettext('none'),
229 reference
: 'downloadUrlChecksum',
232 xtype
: 'progressbar',
235 reference
: 'progressBar',
238 xtype
: 'hiddenfield',
246 validitychange
: 'validitychange',
254 text
: gettext('Abort'),
255 reference
: 'abortBtn',
257 handler: function() {
259 me
.up('pveStorageUpload').close();
263 text
: gettext('Upload'),
264 reference
: 'submitBtn',
280 initComponent: function() {
284 throw "no node name specified";
287 throw "no storage ID specified";
289 if (!me
.acceptedExtensions
[me
.content
]) {
290 throw "content type not supported";