2021-08-31 19:07:19 +05:00
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"io"
|
|
|
|
|
"mime"
|
|
|
|
|
"net/http"
|
|
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"strconv"
|
2021-09-22 19:09:26 +05:00
|
|
|
|
"strings"
|
2021-08-31 19:07:19 +05:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func HandleRoot(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
if r.RequestURI != "/" {
|
2021-09-24 23:35:55 +05:00
|
|
|
|
http.FileServer(http.FS(stripSiteFS)).ServeHTTP(w, r)
|
2021-08-31 19:07:19 +05:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type FileInfo struct {
|
2021-09-22 19:09:26 +05:00
|
|
|
|
Ext string
|
2021-08-31 19:07:19 +05:00
|
|
|
|
Name string
|
|
|
|
|
Size string
|
|
|
|
|
Date string
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-22 19:09:26 +05:00
|
|
|
|
var data struct {
|
|
|
|
|
Files []FileInfo
|
|
|
|
|
StorageDuration uint
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
data.StorageDuration = config.RemoveFilePeriod
|
2021-08-31 19:07:19 +05:00
|
|
|
|
|
|
|
|
|
err := filepath.Walk(config.StoragePath, func(path string, info os.FileInfo, err error) error {
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if info.IsDir() {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2021-09-22 19:09:26 +05:00
|
|
|
|
|
|
|
|
|
fileInfo := FileInfo{
|
|
|
|
|
Ext: nvl(strings.ToLower(strings.TrimPrefix(filepath.Ext(path), ".")), "dat"),
|
|
|
|
|
Name: filepath.Base(path),
|
|
|
|
|
Size: sizeToApproxHuman(info.Size()),
|
|
|
|
|
Date: info.ModTime().Format("02.01.2006")}
|
|
|
|
|
|
|
|
|
|
data.Files = append(data.Files, fileInfo)
|
2021-08-31 19:07:19 +05:00
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = templates.ExecuteTemplate(w, "index.htm", data)
|
|
|
|
|
if err != nil {
|
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func HandleUpload(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
if r.Method != "POST" {
|
|
|
|
|
http.Error(w, "wrong method", http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err := r.ParseMultipartForm(32 << 20)
|
|
|
|
|
if err != nil {
|
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-10 16:16:51 +05:00
|
|
|
|
var errors []string
|
|
|
|
|
for _, header := range r.MultipartForm.File["file"] {
|
|
|
|
|
file, err := header.Open()
|
|
|
|
|
if err != nil {
|
|
|
|
|
errors = append(errors, err.Error())
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
defer file.Close()
|
2021-08-31 19:07:19 +05:00
|
|
|
|
|
2024-07-10 16:16:51 +05:00
|
|
|
|
filePath := filepath.Join(config.StoragePath, header.Filename)
|
|
|
|
|
if _, err := os.Stat(filePath); !os.IsNotExist(err) {
|
|
|
|
|
errors = append(errors, fmt.Sprintf("файл с именем %s уже существует", header.Filename))
|
|
|
|
|
continue
|
|
|
|
|
}
|
2021-08-31 19:07:19 +05:00
|
|
|
|
|
2024-07-10 16:16:51 +05:00
|
|
|
|
f, err := os.Create(filePath)
|
|
|
|
|
if err != nil {
|
|
|
|
|
errors = append(errors, err.Error())
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
|
|
_, err = io.Copy(f, file)
|
|
|
|
|
if err != nil {
|
|
|
|
|
errors = append(errors, err.Error())
|
|
|
|
|
continue
|
|
|
|
|
}
|
2021-08-31 19:07:19 +05:00
|
|
|
|
}
|
|
|
|
|
|
2024-07-10 16:16:51 +05:00
|
|
|
|
if len(errors) > 0 {
|
|
|
|
|
http.Error(w, strings.Join(errors, "\n"), http.StatusBadRequest)
|
2021-08-31 19:07:19 +05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func HandleDownload(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
filename := filepath.Base(r.FormValue("filename"))
|
|
|
|
|
|
|
|
|
|
if filename == "" {
|
2021-09-22 19:09:26 +05:00
|
|
|
|
http.Error(w, `"filename" field can't be empty`, http.StatusBadRequest)
|
2021-08-31 19:07:19 +05:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
f, err := os.Open(filepath.Join(config.StoragePath, filename))
|
|
|
|
|
if err != nil {
|
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
|
|
fileStat, err := f.Stat()
|
|
|
|
|
if err != nil {
|
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
w.Header().Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, filename))
|
|
|
|
|
w.Header().Set("Content-Type", mime.TypeByExtension(filepath.Ext(filename)))
|
|
|
|
|
w.Header().Set("Accept-Ranges", "none")
|
|
|
|
|
w.Header().Set("Content-Length", strconv.Itoa(int(fileStat.Size())))
|
|
|
|
|
|
2024-07-10 16:16:51 +05:00
|
|
|
|
_, _ = io.CopyBuffer(w, f, make([]byte, 4096))
|
2021-08-31 19:07:19 +05:00
|
|
|
|
}
|
2021-09-22 19:09:26 +05:00
|
|
|
|
|
2021-10-10 12:16:37 +05:00
|
|
|
|
func HandleStream(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
filename := filepath.Base(r.FormValue("filename"))
|
|
|
|
|
|
|
|
|
|
if filename == "" {
|
|
|
|
|
http.Error(w, `"filename" field can't be empty`, http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-27 19:40:40 +05:00
|
|
|
|
http.ServeFile(w, r, filepath.Join(config.StoragePath, filename))
|
2021-10-10 12:16:37 +05:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-22 19:09:26 +05:00
|
|
|
|
func HandleIcon(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
ext := r.FormValue("ext")
|
|
|
|
|
|
|
|
|
|
if ext == "" {
|
|
|
|
|
http.Error(w, `"ext" field can't be empty`, http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
f, err := iconsFS.Open(filepath.ToSlash(filepath.Join("icons", ext+".svg")))
|
|
|
|
|
if err != nil {
|
|
|
|
|
f, _ = iconsFS.Open(filepath.ToSlash(filepath.Join("icons", "bin.svg")))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "image/svg+xml")
|
|
|
|
|
w.Header().Set("Cache-Control", "public, max-age=31557600")
|
|
|
|
|
|
2024-07-10 16:16:51 +05:00
|
|
|
|
_, _ = io.Copy(w, f)
|
2021-09-22 19:09:26 +05:00
|
|
|
|
}
|