mirror of
https://github.com/nxshock/simplefileshare.git
synced 2024-11-27 03:11:01 +05:00
Implement multiple file upload
This commit is contained in:
parent
09463c3b24
commit
c41ff56ffa
2
PKGBUILD
2
PKGBUILD
@ -1,5 +1,5 @@
|
||||
pkgname=simplefileshare
|
||||
pkgver=0.1.3
|
||||
pkgver=0.1.4
|
||||
pkgrel=0
|
||||
pkgdesc="Simple file share"
|
||||
arch=('x86_64' 'aarch64')
|
||||
|
29
handlers.go
29
handlers.go
@ -74,30 +74,37 @@ func HandleUpload(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
file, header, err := r.FormFile("file")
|
||||
var errors []string
|
||||
for _, header := range r.MultipartForm.File["file"] {
|
||||
file, err := header.Open()
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
errors = append(errors, err.Error())
|
||||
continue
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
filePath := filepath.Join(config.StoragePath, header.Filename)
|
||||
if _, err := os.Stat(filePath); !os.IsNotExist(err) {
|
||||
http.Error(w, "файл с таким именем уже существует", http.StatusBadRequest)
|
||||
return
|
||||
errors = append(errors, fmt.Sprintf("файл с именем %s уже существует", header.Filename))
|
||||
continue
|
||||
}
|
||||
|
||||
f, err := os.Create(filePath)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
errors = append(errors, err.Error())
|
||||
continue
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
_, err = io.Copy(f, file)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
errors = append(errors, err.Error())
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
if len(errors) > 0 {
|
||||
http.Error(w, strings.Join(errors, "\n"), http.StatusBadRequest)
|
||||
}
|
||||
}
|
||||
|
||||
@ -127,7 +134,7 @@ func HandleDownload(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Accept-Ranges", "none")
|
||||
w.Header().Set("Content-Length", strconv.Itoa(int(fileStat.Size())))
|
||||
|
||||
io.CopyBuffer(w, f, make([]byte, 4096))
|
||||
_, _ = io.CopyBuffer(w, f, make([]byte, 4096))
|
||||
}
|
||||
|
||||
func HandleStream(w http.ResponseWriter, r *http.Request) {
|
||||
@ -157,5 +164,5 @@ func HandleIcon(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "image/svg+xml")
|
||||
w.Header().Set("Cache-Control", "public, max-age=31557600")
|
||||
|
||||
io.Copy(w, f)
|
||||
_, _ = io.Copy(w, f)
|
||||
}
|
||||
|
2
main.go
2
main.go
@ -28,7 +28,7 @@ func init() {
|
||||
}
|
||||
|
||||
if config.RemoveFilePeriod > 0 {
|
||||
go removeOldFilesThread(config.StoragePath, time.Duration(config.RemoveFilePeriod)*time.Hour)
|
||||
go removeOldFilesThread(time.Duration(config.RemoveFilePeriod) * time.Hour)
|
||||
}
|
||||
|
||||
http.HandleFunc("/", HandleRoot)
|
||||
|
@ -7,33 +7,6 @@
|
||||
<title>File Storage</title>
|
||||
<link rel="stylesheet" href="/style.css">
|
||||
<link rel="icon" href="/favicon.svg" type="image/svg+xml">
|
||||
|
||||
<script>
|
||||
function myProgressHandler(event) {
|
||||
var p = Math.floor(event.loaded / event.total * 100);
|
||||
document.querySelector("label").innerHTML = 'Загрузка: ' + p + '%...';
|
||||
}
|
||||
|
||||
function myOnLoadHandler(event) {
|
||||
const response = event.currentTarget;
|
||||
if (response.status != 200) {
|
||||
alert('Ошибка при загрузке файла:\n' + response.responseText);
|
||||
}
|
||||
document.querySelector("label").innerHTML = 'Загрузка завершена.';
|
||||
location.reload();
|
||||
}
|
||||
|
||||
document.getElementById("file-uploader").addEventListener('change', (e) => {
|
||||
var file = document.getElementById("file-uploader").files[0];
|
||||
var formData = new FormData;
|
||||
formData.append('file', file);
|
||||
var ajax = new XMLHttpRequest;
|
||||
ajax.upload.addEventListener("progress", myProgressHandler, false);
|
||||
ajax.addEventListener('load', myOnLoadHandler, false);
|
||||
ajax.open('POST', '/upload', true);
|
||||
ajax.send(formData);
|
||||
});
|
||||
</script>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
@ -43,8 +16,8 @@
|
||||
File Storage
|
||||
</span>
|
||||
<label>
|
||||
<input id="file-uploader" type="file">
|
||||
Загрузить файл
|
||||
<input id="file-uploader" type="file" multiple>
|
||||
Загрузить файл(ы)
|
||||
</label>
|
||||
</header>
|
||||
<main>
|
||||
@ -70,6 +43,34 @@
|
||||
<footer>
|
||||
Файлы хранятся как минимум {{.StorageDuration}} ч.
|
||||
</footer>
|
||||
<script>
|
||||
function myProgressHandler(event) {
|
||||
var p = Math.floor(event.loaded / event.total * 100);
|
||||
document.querySelector("label").innerHTML = 'Загрузка: ' + p + '%...';
|
||||
}
|
||||
|
||||
function myOnLoadHandler(event) {
|
||||
const response = event.currentTarget;
|
||||
if (response.status != 200) {
|
||||
alert('Ошибка при загрузке:\n' + response.responseText);
|
||||
}
|
||||
document.querySelector("label").innerHTML = 'Загрузка завершена.';
|
||||
window.location.reload();
|
||||
}
|
||||
|
||||
document.getElementById("file-uploader").addEventListener('change', (e) => {
|
||||
var formData = new FormData;
|
||||
var ajax = new XMLHttpRequest;
|
||||
for (var i = 0; i < document.getElementById("file-uploader").files.length; i++) {
|
||||
file = document.getElementById("file-uploader").files[i];
|
||||
formData.append('file', file);
|
||||
}
|
||||
ajax.upload.addEventListener("progress", myProgressHandler, false);
|
||||
ajax.addEventListener('load', myOnLoadHandler, false);
|
||||
ajax.open('POST', '/upload', true);
|
||||
ajax.send(formData);
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
|
||||
</html>
|
Loading…
Reference in New Issue
Block a user