nostrMediaUpload/upload.html
2024-04-14 20:04:50 -04:00

75 lines
2.3 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>File Upload</title>
</head>
<body>
<h1>File Upload</h1>
<form id="uploadForm" enctype="multipart/form-data">
<input type="file" name="file" multiple>
<button type="submit">Upload</button>
</form>
<script>
async function checkPermissions(npub) {
try {
const response = await fetch('https://happytaver.co/.well-known/nostr.json');
const data = await response.json();
return data.hasOwnProperty(npub);
} catch (error) {
console.error('Error checking permissions:', error);
return false;
}
}
document.getElementById('uploadForm').addEventListener('submit', async (event) => {
event.preventDefault();
const fileInput = document.querySelector('input[type="file"]');
const files = fileInput.files;
if (files.length === 0) {
alert('Please select a file to upload.');
return;
}
const npub = localStorage.getItem('npub'); // Assuming npub is stored in localStorage
if (!npub) {
alert('User not authenticated.');
return;
}
const hasPermission = await checkPermissions(npub);
if (!hasPermission) {
alert('User does not have permission to upload.');
return;
}
// Proceed with file upload
const formData = new FormData();
for (const file of files) {
formData.append('files', file);
}
try {
const uploadResponse = await fetch('https://your-upload-endpoint.com/upload', {
method: 'POST',
body: formData
});
const uploadData = await uploadResponse.json();
console.log('Upload successful:', uploadData);
alert('Files uploaded successfully.');
} catch (error) {
console.error('Error uploading files:', error);
alert('Error occurred during file upload.');
}
});
</script>
</body>
</html>