Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1b45fb4b8a | ||
|
|
53ecf1041c |
+28
-9
@@ -139,6 +139,17 @@
|
|||||||
document.getElementById('maxUploadSizeLabel').textContent = formatBytes(maxUploadSize);
|
document.getElementById('maxUploadSizeLabel').textContent = formatBytes(maxUploadSize);
|
||||||
document.getElementById('maxUploadSizeText').textContent = formatBytes(maxUploadSize);
|
document.getElementById('maxUploadSizeText').textContent = formatBytes(maxUploadSize);
|
||||||
|
|
||||||
|
function clearResults() {
|
||||||
|
results.replaceChildren();
|
||||||
|
}
|
||||||
|
|
||||||
|
function showMessage(message) {
|
||||||
|
const div = document.createElement('div');
|
||||||
|
div.className = 'result-item';
|
||||||
|
div.textContent = message;
|
||||||
|
results.replaceChildren(div);
|
||||||
|
}
|
||||||
|
|
||||||
uploadArea.addEventListener('click', () => fileInput.click());
|
uploadArea.addEventListener('click', () => fileInput.click());
|
||||||
|
|
||||||
uploadArea.addEventListener('dragover', (e) => {
|
uploadArea.addEventListener('dragover', (e) => {
|
||||||
@@ -174,14 +185,14 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (oversizedFiles.length > 0) {
|
if (oversizedFiles.length > 0) {
|
||||||
results.innerHTML = '<div class="result-item">Upload failed: the following file(s) exceed the maximum size of ' +
|
showMessage('Upload failed: the following file(s) exceed the maximum size of ' +
|
||||||
formatBytes(maxUploadSize) + ': ' + oversizedFiles.join(', ') + '</div>';
|
formatBytes(maxUploadSize) + ': ' + oversizedFiles.join(', '));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (maxUploadSize && totalSize > maxUploadSize) {
|
if (maxUploadSize && totalSize > maxUploadSize) {
|
||||||
results.innerHTML = '<div class="result-item">Upload failed: total upload size (' + formatBytes(totalSize) +
|
showMessage('Upload failed: total upload size (' + formatBytes(totalSize) +
|
||||||
') exceeds the maximum of ' + formatBytes(maxUploadSize) + '</div>';
|
') exceeds the maximum of ' + formatBytes(maxUploadSize));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -192,7 +203,7 @@
|
|||||||
|
|
||||||
progressContainer.style.display = 'block';
|
progressContainer.style.display = 'block';
|
||||||
progressBar.style.width = '0%';
|
progressBar.style.width = '0%';
|
||||||
results.innerHTML = '';
|
clearResults();
|
||||||
|
|
||||||
const xhr = new XMLHttpRequest();
|
const xhr = new XMLHttpRequest();
|
||||||
|
|
||||||
@@ -219,13 +230,13 @@
|
|||||||
message = xhr.responseText;
|
message = xhr.responseText;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
results.innerHTML = '<div class="result-item">Upload failed: ' + message + '</div>';
|
showMessage('Upload failed: ' + message);
|
||||||
}
|
}
|
||||||
progressBar.style.width = '100%';
|
progressBar.style.width = '100%';
|
||||||
});
|
});
|
||||||
|
|
||||||
xhr.addEventListener('error', () => {
|
xhr.addEventListener('error', () => {
|
||||||
results.innerHTML = '<div class="result-item">Upload failed: Network error</div>';
|
showMessage('Upload failed: Network error');
|
||||||
});
|
});
|
||||||
|
|
||||||
xhr.open('POST', '/upload');
|
xhr.open('POST', '/upload');
|
||||||
@@ -233,11 +244,19 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
function displayResults(urls) {
|
function displayResults(urls) {
|
||||||
results.innerHTML = '<h3>Download URLs:</h3>';
|
const heading = document.createElement('h3');
|
||||||
|
heading.textContent = 'Download URLs:';
|
||||||
|
results.replaceChildren(heading);
|
||||||
|
|
||||||
urls.forEach(url => {
|
urls.forEach(url => {
|
||||||
const div = document.createElement('div');
|
const div = document.createElement('div');
|
||||||
div.className = 'result-item';
|
div.className = 'result-item';
|
||||||
div.innerHTML = '<a href="' + url + '" target="_blank">' + url + '</a>';
|
const link = document.createElement('a');
|
||||||
|
link.href = url;
|
||||||
|
link.target = '_blank';
|
||||||
|
link.rel = 'noopener noreferrer';
|
||||||
|
link.textContent = url;
|
||||||
|
div.appendChild(link);
|
||||||
results.appendChild(div);
|
results.appendChild(div);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -179,9 +179,11 @@ func handleUpload(w http.ResponseWriter, r *http.Request, start time.Time) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
targetPath := filepath.Join(uploadDir, id, filename)
|
targetDir := filepath.Join(uploadDir, id)
|
||||||
|
targetPath := filepath.Join(targetDir, filename)
|
||||||
file, err := os.Create(targetPath)
|
file, err := os.Create(targetPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
os.Remove(targetDir)
|
||||||
http.Error(w, "Failed to create file", http.StatusInternalServerError)
|
http.Error(w, "Failed to create file", http.StatusInternalServerError)
|
||||||
logRequest(r, 500, time.Since(start))
|
logRequest(r, 500, time.Since(start))
|
||||||
return
|
return
|
||||||
@@ -193,6 +195,7 @@ func handleUpload(w http.ResponseWriter, r *http.Request, start time.Time) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
file.Close()
|
file.Close()
|
||||||
os.Remove(targetPath)
|
os.Remove(targetPath)
|
||||||
|
os.Remove(targetDir)
|
||||||
if isMaxBytesError(err) {
|
if isMaxBytesError(err) {
|
||||||
http.Error(w, fmt.Sprintf("File exceeds maximum upload size of %d bytes", maxUploadSize), http.StatusRequestEntityTooLarge)
|
http.Error(w, fmt.Sprintf("File exceeds maximum upload size of %d bytes", maxUploadSize), http.StatusRequestEntityTooLarge)
|
||||||
logRequest(r, 413, time.Since(start))
|
logRequest(r, 413, time.Since(start))
|
||||||
@@ -238,6 +241,7 @@ func handleMultipartUpload(w http.ResponseWriter, r *http.Request, start time.Ti
|
|||||||
logRequest(r, 400, time.Since(start))
|
logRequest(r, 400, time.Since(start))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
defer r.MultipartForm.RemoveAll()
|
||||||
|
|
||||||
files := r.MultipartForm.File["files"]
|
files := r.MultipartForm.File["files"]
|
||||||
if len(files) == 0 {
|
if len(files) == 0 {
|
||||||
@@ -263,11 +267,13 @@ func handleMultipartUpload(w http.ResponseWriter, r *http.Request, start time.Ti
|
|||||||
}
|
}
|
||||||
|
|
||||||
filename := filepath.Base(fileHeader.Filename)
|
filename := filepath.Base(fileHeader.Filename)
|
||||||
targetPath := filepath.Join(uploadDir, id, filename)
|
targetDir := filepath.Join(uploadDir, id)
|
||||||
|
targetPath := filepath.Join(targetDir, filename)
|
||||||
|
|
||||||
dst, err := os.Create(targetPath)
|
dst, err := os.Create(targetPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
src.Close()
|
src.Close()
|
||||||
|
os.Remove(targetDir)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -277,6 +283,7 @@ func handleMultipartUpload(w http.ResponseWriter, r *http.Request, start time.Ti
|
|||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
os.Remove(targetPath)
|
os.Remove(targetPath)
|
||||||
|
os.Remove(targetDir)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -324,7 +331,7 @@ func handleDownload(w http.ResponseWriter, r *http.Request, start time.Time) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
w.Header().Set("Content-Type", detectContentType(filename))
|
w.Header().Set("Content-Type", detectContentType(filename))
|
||||||
w.Header().Set("Content-Disposition", fmt.Sprintf("inline; filename=\"%s\"", filename))
|
w.Header().Set("Content-Disposition", mime.FormatMediaType("inline", map[string]string{"filename": filename}))
|
||||||
w.Header().Set("Content-Length", fmt.Sprintf("%d", stat.Size()))
|
w.Header().Set("Content-Length", fmt.Sprintf("%d", stat.Size()))
|
||||||
|
|
||||||
io.Copy(w, file)
|
io.Copy(w, file)
|
||||||
|
|||||||
Reference in New Issue
Block a user