|
11 | 11 | var _targetInputId = null; |
12 | 12 | var _fileFilter = null; |
13 | 13 | var _selectionMode = 'file'; |
| 14 | + var _apiBasePath = '/api/file-browse'; |
14 | 15 | var _currentPath = '/'; |
15 | 16 | var _selectedPath = null; |
16 | 17 | var _modal = null; |
|
29 | 30 | _modal = new bootstrap.Modal(document.getElementById('fileBrowseModal')); |
30 | 31 | } |
31 | 32 |
|
| 33 | + var modalElement = document.getElementById('fileBrowseModal'); |
| 34 | + if (modalElement) { |
| 35 | + var apiBasePath = modalElement.getAttribute('data-api-base-path'); |
| 36 | + if (apiBasePath) { |
| 37 | + _apiBasePath = apiBasePath; |
| 38 | + } |
| 39 | + } |
| 40 | + |
32 | 41 | updateModalLabels(); |
33 | 42 | updateSelectButton(); |
34 | 43 | _modal.show(); |
|
100 | 109 | } |
101 | 110 | } |
102 | 111 |
|
103 | | - function navigateTo(path) { |
| 112 | + function navigateTo(path, allowRootFallback) { |
104 | 113 | var loading = document.getElementById('fileBrowseLoading'); |
105 | 114 | var error = document.getElementById('fileBrowseError'); |
106 | 115 | var container = document.getElementById('fileBrowseListingContainer'); |
| 116 | + var normalizedPath = normalizeClientPath(path); |
107 | 117 |
|
108 | 118 | loading.style.display = ''; |
109 | 119 | error.style.display = 'none'; |
110 | 120 | container.style.display = 'none'; |
111 | | - _selectedPath = _selectionMode === 'directory' ? normalizeClientPath(path) : null; |
| 121 | + _selectedPath = _selectionMode === 'directory' ? normalizedPath : null; |
112 | 122 | updateSelectButton(); |
113 | 123 |
|
114 | | - fetch('/api/file-browse/' + _gameServerId + '/browse?path=' + encodeURIComponent(path)) |
115 | | - .then(function (response) { |
116 | | - if (response.status === 403) throw new Error('You do not have permission to browse this server\'s files.'); |
117 | | - if (!response.ok) throw new Error('Failed to browse directory (HTTP ' + response.status + ')'); |
118 | | - var contentType = response.headers.get('content-type') || ''; |
119 | | - if (!contentType.includes('application/json')) throw new Error('Unexpected response from server.'); |
120 | | - return response.json(); |
121 | | - }) |
| 124 | + loadDirectory(normalizedPath, allowRootFallback !== false) |
122 | 125 | .then(function (data) { |
123 | 126 | loading.style.display = 'none'; |
124 | 127 | container.style.display = ''; |
|
137 | 140 | }); |
138 | 141 | } |
139 | 142 |
|
| 143 | + function loadDirectory(path, allowRootFallback) { |
| 144 | + return fetch(_apiBasePath + '/' + _gameServerId + '/browse?path=' + encodeURIComponent(path)) |
| 145 | + .then(function (response) { |
| 146 | + if (response.status === 404 && allowRootFallback && path !== '/') { |
| 147 | + return loadDirectory('/', false); |
| 148 | + } |
| 149 | + |
| 150 | + if (response.status === 403) throw new Error('You do not have permission to browse this server\'s files.'); |
| 151 | + if (!response.ok) throw new Error('Failed to browse directory (HTTP ' + response.status + ')'); |
| 152 | + var contentType = response.headers.get('content-type') || ''; |
| 153 | + if (!contentType.includes('application/json')) throw new Error('Unexpected response from server.'); |
| 154 | + return response.json(); |
| 155 | + }); |
| 156 | + } |
| 157 | + |
140 | 158 | function renderBreadcrumb(currentPath) { |
141 | 159 | var breadcrumb = document.getElementById('fileBrowseBreadcrumb'); |
142 | 160 | breadcrumb.innerHTML = ''; |
|
0 commit comments