Compare commits

...

8 Commits

Author SHA1 Message Date
c41ff56ffa Implement multiple file upload 2024-07-10 16:16:51 +05:00
09463c3b24 Remove unused variable 2024-07-10 16:16:15 +05:00
c2cbf59793 Remove unused code 2024-07-10 16:16:02 +05:00
456880bbe7 Add alt for image 2024-07-10 11:33:16 +05:00
14c1e89025 Remove type attribute 2024-07-10 11:30:45 +05:00
a2a16af605 Add lang attribute 2024-07-10 11:29:39 +05:00
e7a7baa0a0 Move script to head tag 2024-07-10 11:15:35 +05:00
48e439da10
Demove duplicated attribute 2024-07-10 11:10:54 +05:00
5 changed files with 75 additions and 63 deletions

View File

@ -1,5 +1,5 @@
pkgname=simplefileshare
pkgver=0.1.3
pkgver=0.1.4
pkgrel=0
pkgdesc="Simple file share"
arch=('x86_64' 'aarch64')

View File

@ -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)
}

View File

@ -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)

View File

@ -1,5 +1,5 @@
<!DOCTYPE html>
<html>
<html lang="ru">
<head>
<meta charset="utf-8">
@ -12,12 +12,12 @@
<body>
<header>
<span>
<img src="/favicon.svg">
<img src="/favicon.svg" alt="icon">
File Storage
</span>
<label>
<input id="file-uploader" type="file" id="upload-button">
Загрузить файл
<input id="file-uploader" type="file" multiple>
Загрузить файл(ы)
</label>
</header>
<main>
@ -32,18 +32,18 @@
</tr>
{{range .Files}} <tr>
<td><img src="/icon?ext={{.Ext}}"> <a href="/download?filename={{.Name}}">{{.Name}}</a> <a class="right" href="/stream?filename={{.Name}}">просмотр</a></td>
<td><pre>{{.Size}}</pre></td>
<td>
<pre>{{.Size}}</pre>
</td>
<td>{{.Date}}</td>
</tr>
{{end}} </table>
{{end}}
</table>
</main>
<footer>
Файлы хранятся как минимум {{.StorageDuration}} ч.
</footer>
</body>
</html>
<script type="text/javascript">
<script>
function myProgressHandler(event) {
var p = Math.floor(event.loaded / event.total * 100);
document.querySelector("label").innerHTML = 'Загрузка: ' + p + '%...';
@ -52,20 +52,25 @@ function myProgressHandler(event) {
function myOnLoadHandler(event) {
const response = event.currentTarget;
if (response.status != 200) {
alert('Ошибка при загрузке файла:\n' + response.responseText);
alert('Ошибка при загрузке:\n' + response.responseText);
}
document.querySelector("label").innerHTML = 'Загрузка завершена.';
location.reload();
window.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;
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>

View File

@ -8,12 +8,12 @@ import (
log "github.com/sirupsen/logrus"
)
func removeOldFilesThread(path string, olderThan time.Duration) {
func removeOldFilesThread(olderThan time.Duration) {
ticker := time.NewTicker(time.Hour)
for _ = range ticker.C {
for range ticker.C {
log.Debugln("Removing old files...")
err := removeOldFiles(path, olderThan)
err := removeOldFiles(olderThan)
if err != nil {
log.Println(err)
}
@ -21,7 +21,7 @@ func removeOldFilesThread(path string, olderThan time.Duration) {
}
}
func removeOldFiles(path string, olderThan time.Duration) error {
func removeOldFiles(olderThan time.Duration) error {
return filepath.Walk(config.StoragePath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err