-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
129 lines (110 loc) · 4.97 KB
/
Copy pathscript.js
File metadata and controls
129 lines (110 loc) · 4.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
console.log('Script loaded');
document.addEventListener('DOMContentLoaded', function () {
function loadFiles(page = 1) {
fetch(`../scripts/list_files.php?page=${page}`)
.then(response => response.json())
.then(data => {
const fileList = document.getElementById('fileList');
fileList.innerHTML = '';
if (data.files.length === 0) {
fileList.innerHTML = '<p>No files uploaded yet.</p>';
return;
}
function formatFileSize(bytes) {
const sizes = ['Bytes', 'KB', 'MB', 'GB'];
if (bytes === 0) return '0 Bytes';
const i = Math.floor(Math.log(bytes) / Math.log(1024));
return (bytes / Math.pow(1024, i)).toFixed(2) + " " + sizes[i];
}
data.files.forEach(file => {
const fileDiv = document.createElement('div');
fileDiv.className = 'col-md-4 mb-4';
fileDiv.innerHTML = `
<div class="card shadow-sm h-100">
<div class="card-body d-flex flex-column justify-content-between">
<div>
<h5 class="card-title text-truncate" title="${file.file_name}">${file.file_name}</h5>
<p class="text-muted small">
Size: ${formatFileSize(file.file_size)} <br>
Uploaded: ${new Date(file.upload_date).toLocaleString()}
</p>
</div>
<div class="mt-3">
<a href="${file.file_url}" target="blank" class="btn btn-primary btn-sm mb-2 w-100" download>Download</a>
<button class="delete-btn btn btn-danger btn-sm w-100" data-id="${file.id}">Delete</button>
</div>
</div>
</div>
`;
fileList.appendChild(fileDiv);
});
const pagination = document.createElement('div');
pagination.className = 'pagination';
for (let i = 1; i <= data.total_pages; i++) {
const pageButton = document.createElement('button');
pageButton.textContent = i;
pageButton.className = (i === page) ? 'active' : '';
pageButton.addEventListener('click', () => loadFiles(i));
pagination.appendChild(pageButton);
}
fileList.appendChild(pagination);
document.querySelectorAll('.delete-btn').forEach(button => {
button.addEventListener('click', function () {
const fileId = this.dataset.id;
deleteFile(fileId);
});
});
});
}
function deleteFile(fileId) {
fetch('../scripts/delete.php', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ id: fileId }),
})
.then(response => response.json())
.then(data => {
if (data.success) {
alert('File deleted successfully!');
loadFiles();
} else {
alert('Failed to delete file.');
}
});
}
document.getElementById('uploadForm').addEventListener('submit', function(event) {
event.preventDefault();
const formData = new FormData(this);
const progressBar = document.getElementById('uploadProgress');
progressBar.style.width = '0%';
progressBar.parentElement.style.display = 'block';
const xhr = new XMLHttpRequest();
xhr.open('POST', '../scripts/upload.php', true)
xhr.upload.onprogress = function (event) {
if (event.lengthComputable) {
const percentageComplete = Math.round((event.loaded / event.total) * 100);
progressBar.style.width = percentageComplete + '%';
progressBar.textContent = percentageComplete + '%';
}
};
xhr.onload = function () {
const uploadMessage = document.getElementById('uploadMessage');
if (xhr.status === 200) {
const response = JSON.parse(xhr.responseText);
if (response.success) {
uploadMessage.textContent = 'File uploaded successfully!'
loadFiles();
} else {
uploadMessage.textContent = 'Failed to upload file.';
}
} else {
uploadMessage.textContent = 'An error occurred during the upload.';
}
progressBar.parentElement.style.display = 'none';
};
xhr.send(formData)
});
loadFiles();
});