mirror of
https://github.com/neocities/neocities.git
synced 2025-08-22 17:10:56 +02:00
use api for new file create via dashboard, deprecate old method
This commit is contained in:
parent
c7dd74bb0d
commit
3f34b3ae62
8 changed files with 177 additions and 176 deletions
|
@ -36,8 +36,54 @@ $('#createDir').on('shown', function () {
|
|||
|
||||
$('#createFile').on('shown', function () {
|
||||
$('#newFileInput').focus();
|
||||
$('#newFileInput').val('');
|
||||
clearCreateFileError();
|
||||
})
|
||||
|
||||
function showCreateFileError(message) {
|
||||
var errorDiv = $('#createFileError');
|
||||
errorDiv.text(message);
|
||||
errorDiv.show();
|
||||
}
|
||||
|
||||
function clearCreateFileError() {
|
||||
var errorDiv = $('#createFileError');
|
||||
errorDiv.hide();
|
||||
errorDiv.text('');
|
||||
}
|
||||
|
||||
function fileExists(filePath) {
|
||||
// Check if a file with this path already exists in the current file listing
|
||||
// Each file has a hidden div containing the file path
|
||||
var exists = false;
|
||||
var lowerFilePath = filePath.toLowerCase();
|
||||
|
||||
$('#filesDisplay .file .overlay div[style*="display: none"]').each(function() {
|
||||
var existingPath = $(this).text().trim();
|
||||
if (existingPath.toLowerCase() === lowerFilePath) {
|
||||
exists = true;
|
||||
return false; // Break out of each loop
|
||||
}
|
||||
});
|
||||
|
||||
return exists;
|
||||
}
|
||||
|
||||
$('#newFileInput').on('keypress', function(e) {
|
||||
if (e.which === 13) { // Enter key
|
||||
handleCreateFile();
|
||||
}
|
||||
})
|
||||
|
||||
function handleCreateFile() {
|
||||
var filename = $('#newFileInput').val();
|
||||
var dir = $('#createFileDir').val();
|
||||
var csrfToken = $('#createFileCSRFToken').val();
|
||||
|
||||
// Don't hide modal yet - wait for success or error
|
||||
createFileViaAPI(filename, dir, csrfToken);
|
||||
}
|
||||
|
||||
function listView() {
|
||||
if(localStorage)
|
||||
localStorage.setItem('viewType', 'list')
|
||||
|
@ -153,5 +199,98 @@ function reloadDashboardFiles() {
|
|||
});
|
||||
}
|
||||
|
||||
function createFileViaAPI(filename, dir, csrfToken) {
|
||||
clearCreateFileError();
|
||||
showUploadProgress();
|
||||
|
||||
filename = filename.replace(/[^a-zA-Z0-9_\-.]/g, '');
|
||||
|
||||
if (!filename || filename.trim() === '') {
|
||||
hideUploadProgress();
|
||||
showCreateFileError('You must provide a file name.');
|
||||
return;
|
||||
}
|
||||
|
||||
var extMatch = filename.match(/\.([^.]+)$/);
|
||||
|
||||
// Check if extension is allowed for editing (if there is one)
|
||||
if (extMatch) {
|
||||
var extension = extMatch[1].toLowerCase();
|
||||
var validExtensions = [
|
||||
'html', 'htm', 'txt', 'js', 'css', 'scss', 'md', 'manifest', 'less',
|
||||
'webmanifest', 'xml', 'json', 'opml', 'rdf', 'svg', 'gpg', 'pgp',
|
||||
'resolvehandle', 'pls', 'yaml', 'yml', 'toml', 'osdx', 'mjs', 'cjs',
|
||||
'ts', 'py', 'rss', 'glsl'
|
||||
];
|
||||
|
||||
if (validExtensions.indexOf(extension) === -1) {
|
||||
hideUploadProgress();
|
||||
showCreateFileError('Must be an editable file type (' + validExtensions.join(', ') + ') or a file with no extension.');
|
||||
return;
|
||||
}
|
||||
}
|
||||
// Files with no extension are allowed (they're treated as text files)
|
||||
|
||||
var fullPath = dir ? joinPaths(dir, filename) : filename;
|
||||
|
||||
// Check if file already exists
|
||||
if (fileExists(fullPath)) {
|
||||
hideUploadProgress();
|
||||
showCreateFileError('A file with this name already exists. Please choose a different name.');
|
||||
return;
|
||||
}
|
||||
|
||||
var isHTML = /\.(html|htm)$/i.test(filename);
|
||||
|
||||
// Create default HTML template content
|
||||
var htmlTemplate = '<!DOCTYPE html>\n' +
|
||||
'<html>\n' +
|
||||
' <head>\n' +
|
||||
' <meta charset="UTF-8">\n' +
|
||||
' <meta name="viewport" content="width=device-width, initial-scale=1.0">\n' +
|
||||
' <title>My Page</title>\n' +
|
||||
' <link href="/style.css" rel="stylesheet" type="text/css" media="all">\n' +
|
||||
' </head>\n' +
|
||||
' <body>\n' +
|
||||
' </body>\n' +
|
||||
'</html>';
|
||||
|
||||
var content = isHTML ? htmlTemplate : '';
|
||||
|
||||
// Create a blob with the content
|
||||
var blob = new Blob([content], { type: 'text/plain' });
|
||||
|
||||
// Create FormData for the API upload
|
||||
var formData = new FormData();
|
||||
formData.append('csrf_token', csrfToken);
|
||||
formData.append(fullPath, blob, filename);
|
||||
|
||||
$.ajax({
|
||||
url: '/api/upload',
|
||||
type: 'POST',
|
||||
data: formData,
|
||||
processData: false,
|
||||
contentType: false,
|
||||
success: function(response) {
|
||||
hideUploadProgress();
|
||||
alertClear();
|
||||
alertType('success');
|
||||
var escapedName = $('<div>').text(fullPath).html(); // HTML escape
|
||||
alertAdd(escapedName + ' was created! <a style="color: #FFFFFF; text-decoration: underline" href="/site_files/text_editor/' + encodeURIComponent(fullPath) + '">Click here to edit it</a>.');
|
||||
reloadDashboardFiles();
|
||||
$('#createFile').modal('hide'); // Hide modal on success
|
||||
},
|
||||
error: function(xhr) {
|
||||
hideUploadProgress();
|
||||
try {
|
||||
var errorData = JSON.parse(xhr.responseText);
|
||||
showCreateFileError(errorData.message || 'Failed to create file');
|
||||
} catch(e) {
|
||||
showCreateFileError('Failed to create file');
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// for first time load
|
||||
reInitDashboardFiles();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue