]>
git.proxmox.com Git - proxmox-widget-toolkit.git/blob - src/window/FileBrowser.js
570ad8f5a2201f3c7e26b0049660bab264ae1878
1 Ext
.define('proxmox-file-tree', {
2 extend
: 'Ext.data.Model',
5 'filepath', 'text', 'type', 'size',
9 if (data
.size
=== undefined) {
11 } else if (data
.type
=== 'd') {
12 let fs
= data
.size
=== 1 ? gettext('{0} item') : gettext('{0} items');
13 return Ext
.String
.format(fs
, data
.size
);
16 return Proxmox
.Utils
.format_size(data
.size
);
22 dateFormat
: 'timestamp',
26 calculate: function(data
) {
27 let icon
= Proxmox
.Schema
.pxarFileTypes
[data
.type
]?.icon
?? 'file-o';
28 if (data
.expanded
&& data
.type
=== 'd') {
29 icon
= 'folder-open-o';
31 return `fa fa-${icon}`;
35 idProperty
: 'filepath',
38 Ext
.define("Proxmox.window.FileBrowser", {
39 extend
: "Ext.window.Window",
47 // the base-URL to get the list of files. required.
50 // the base download URL, e.g., something like '/api2/...'
53 // extra parameters set as proxy paramns and for an actual download request
56 // the file types for which the download button should be enabled
57 downloadableFileTypes
: {
58 'h': true, // hardlinks
59 'f': true, // "normal" files
60 'd': true, // directories
63 // enable tar download, this will add a menu to the
64 // "Download" button when the selection can be downloaded as
68 // prefix to prepend to downloaded file names
73 xclass
: 'Ext.app.ViewController',
75 buildUrl: function(baseurl
, params
) {
76 let url
= new URL(baseurl
, window
.location
.origin
);
77 for (const [key
, value
] of Object
.entries(params
)) {
78 url
.searchParams
.append(key
, value
);
84 downloadTar: function() {
85 this.downloadFile(true);
88 downloadZip: function() {
89 this.downloadFile(false);
92 downloadFile: function(tar
) {
94 let view
= me
.getView();
95 let tree
= me
.lookup('tree');
96 let selection
= tree
.getSelection();
97 if (!selection
|| selection
.length
< 1) return;
99 let data
= selection
[0].data
;
101 let params
= { ...view
.extraParams
};
102 params
.filepath
= data
.filepath
;
104 let atag
= document
.createElement('a');
105 atag
.download
= view
.downloadPrefix
+ data
.text
;
106 if (data
.type
=== 'd') {
109 atag
.download
+= ".tar.zst";
111 atag
.download
+= ".zip";
114 atag
.href
= me
.buildUrl(view
.downloadURL
, params
);
118 fileChanged: function() {
120 let view
= me
.getView();
121 let tree
= me
.lookup('tree');
122 let selection
= tree
.getSelection();
123 if (!selection
|| selection
.length
< 1) return;
125 let data
= selection
[0].data
;
126 let st
= Ext
.String
.format(gettext('Selected "{0}"'), atob(data
.filepath
));
127 view
.lookup('selectText').setText(st
);
129 let canDownload
= view
.downloadURL
&& view
.downloadableFileTypes
[data
.type
];
130 let enableMenu
= view
.enableTar
&& data
.type
=== 'd';
132 let downloadBtn
= view
.lookup('downloadBtn');
133 downloadBtn
.setDisabled(!canDownload
|| enableMenu
);
134 downloadBtn
.setHidden(!canDownload
|| enableMenu
);
136 let menuBtn
= view
.lookup('menuBtn');
137 menuBtn
.setDisabled(!canDownload
|| !enableMenu
);
138 menuBtn
.setHidden(!canDownload
|| !enableMenu
);
141 errorHandler: function(error
, msg
) {
143 if (error
?.status
=== 503) {
146 me
.lookup('downloadBtn').setDisabled(true);
147 me
.lookup('menuBtn').setDisabled(true);
148 if (me
.initialLoadDone
) {
149 Ext
.Msg
.alert(gettext('Error'), msg
);
155 init: function(view
) {
157 let tree
= me
.lookup('tree');
160 throw "no list URL given";
163 let store
= tree
.getStore();
164 let proxy
= store
.getProxy();
166 let errorCallback
= (error
, msg
) => me
.errorHandler(error
, msg
);
167 proxy
.setUrl(view
.listURL
);
168 proxy
.setTimeout(60*1000);
169 proxy
.setExtraParams(view
.extraParams
);
171 tree
.mon(store
, 'beforeload', () => {
172 Proxmox
.Utils
.setErrorMask(tree
, true);
174 tree
.mon(store
, 'load', (treestore
, rec
, success
, operation
, node
) => {
176 Proxmox
.Utils
.setErrorMask(tree
, false);
179 if (!node
.loadCount
) {
180 node
.loadCount
= 0; // ensure its numeric
182 // trigger a reload if we got a 503 answer from the proxy
183 if (operation
?.error
?.status
=== 503 && node
.loadCount
< 10) {
190 let error
= operation
.getError();
191 let msg
= Proxmox
.Utils
.getResponseErrorMessage(error
);
192 if (!errorCallback(error
, msg
)) {
193 Proxmox
.Utils
.setErrorMask(tree
, msg
);
195 Proxmox
.Utils
.setErrorMask(tree
, false);
198 store
.load((rec
, op
, success
) => {
199 let root
= store
.getRoot();
200 root
.expand(); // always expand invisible root node
201 if (view
.archive
=== 'all') {
202 root
.expandChildren(false);
203 } else if (view
.archive
) {
204 let child
= root
.findChild('text', view
.archive
);
207 setTimeout(function() {
208 tree
.setSelection(child
);
209 tree
.getView().focusRow(child
);
212 } else if (root
.childNodes
.length
=== 1) {
213 root
.firstChild
.expand();
215 me
.initialLoadDone
= success
;
221 selectionchange
: 'fileChanged',
235 model
: 'proxmox-file-tree',
237 nodeParam
: 'filepath',
251 text
: gettext('Name'),
255 renderer
: Ext
.String
.htmlEncode
,
258 text
: gettext('Size'),
259 dataIndex
: 'sizedisplay',
261 sorterFn: function(a
, b
) {
262 if (a
.data
.type
=== 'd' && b
.data
.type
!== 'd') {
264 } else if (a
.data
.type
!== 'd' && b
.data
.type
=== 'd') {
268 let asize
= a
.data
.size
|| 0;
269 let bsize
= b
.data
.size
|| 0;
271 return asize
- bsize
;
276 text
: gettext('Modified'),
281 text
: gettext('Type'),
283 renderer
: (v
) => Proxmox
.Schema
.pxarFileTypes
[v
]?.label
?? Proxmox
.Utils
.unknownText
,
293 reference
: 'selectText',
296 text
: gettext('Download'),
298 handler
: 'downloadZip',
299 reference
: 'downloadBtn',
304 text
: gettext('Download as'),
306 reference
: 'menuBtn',
310 iconCls
: 'fa fa-fw fa-file-zip-o',
311 text
: gettext('.zip'),
312 handler
: 'downloadZip',
313 reference
: 'downloadZip',
316 iconCls
: 'fa fa-fw fa-archive',
317 text
: gettext('.tar.zst'),
318 handler
: 'downloadTar',
319 reference
: 'downloadTar',