2023-04-24 00:49:53 +04:00
|
|
|
package http
|
2022-02-07 04:49:21 +04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/sha256"
|
|
|
|
"dwelling-upload/internal/configuration"
|
2022-02-07 19:42:09 +04:00
|
|
|
"dwelling-upload/pkg/logging"
|
2022-02-07 04:49:21 +04:00
|
|
|
"dwelling-upload/pkg/utils"
|
2022-05-25 00:27:30 +04:00
|
|
|
"dwelling-upload/web"
|
2022-02-07 04:49:21 +04:00
|
|
|
"encoding/base64"
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
2022-02-08 19:21:44 +04:00
|
|
|
"net/url"
|
2022-02-07 04:49:21 +04:00
|
|
|
"os"
|
|
|
|
"path"
|
2022-02-08 19:02:37 +04:00
|
|
|
"strings"
|
2022-02-07 04:49:21 +04:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type UploadHandlers struct {
|
2022-02-07 19:42:09 +04:00
|
|
|
conf *configuration.Configuration
|
|
|
|
logErr *logging.Logger
|
|
|
|
logUpload *logging.Logger
|
|
|
|
logDownload *logging.Logger
|
2022-07-01 03:30:40 +04:00
|
|
|
logDelete *logging.Logger
|
2022-02-08 00:25:50 +04:00
|
|
|
|
|
|
|
uploadDirSize *int64
|
2022-02-07 04:49:21 +04:00
|
|
|
}
|
|
|
|
|
2022-07-01 03:30:40 +04:00
|
|
|
func NewUploadHandlers(conf *configuration.Configuration, lErr, lUp, lDown, lDel *logging.Logger, uploadDirSize *int64) *UploadHandlers {
|
2022-02-07 04:49:21 +04:00
|
|
|
return &UploadHandlers{
|
2022-02-08 00:25:50 +04:00
|
|
|
conf: conf,
|
|
|
|
logErr: lErr,
|
|
|
|
logUpload: lUp,
|
|
|
|
logDownload: lDown,
|
2022-07-01 03:30:40 +04:00
|
|
|
logDelete: lDel,
|
2022-02-08 00:25:50 +04:00
|
|
|
uploadDirSize: uploadDirSize}
|
2022-02-07 04:49:21 +04:00
|
|
|
}
|
|
|
|
|
2022-03-06 22:34:34 +04:00
|
|
|
func (*UploadHandlers) AssetsFS() http.FileSystem {
|
2022-05-25 00:27:30 +04:00
|
|
|
return web.Assets()
|
2022-02-08 17:46:44 +04:00
|
|
|
}
|
|
|
|
|
2022-02-07 04:49:21 +04:00
|
|
|
func (h *UploadHandlers) Index(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var storCapacity int64 = h.conf.Uploads.Limits.Storage << 20
|
|
|
|
var fMaxSize int64 = h.conf.Uploads.Limits.FileSize << 20
|
|
|
|
|
|
|
|
_, _, capStr := utils.ConvertFileSize(storCapacity)
|
2022-02-08 00:25:50 +04:00
|
|
|
_, _, usedStr := utils.ConvertFileSize(*h.uploadDirSize)
|
|
|
|
_, _, availStr := utils.ConvertFileSize(storCapacity - *h.uploadDirSize)
|
2022-02-07 04:49:21 +04:00
|
|
|
_, _, fMaxSzStr := utils.ConvertFileSize(fMaxSize)
|
|
|
|
|
2022-06-13 21:07:33 +04:00
|
|
|
web.Index(utils.MainSite(r.Host), storCapacity, *h.uploadDirSize, h.conf.Uploads.Limits.KeepForHours, fMaxSzStr, usedStr, capStr, availStr, w)
|
2022-02-07 04:49:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *UploadHandlers) Upload(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var fMaxSizeBytes int64 = h.conf.Uploads.Limits.FileSize << 20
|
2022-02-09 01:48:19 +04:00
|
|
|
var storCapacity int64 = h.conf.Uploads.Limits.Storage << 20
|
2022-02-07 04:49:21 +04:00
|
|
|
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, fMaxSizeBytes)
|
|
|
|
|
|
|
|
if err := r.ParseMultipartForm(fMaxSizeBytes); err != nil {
|
2022-02-07 19:42:09 +04:00
|
|
|
h.logErr.Println("failed to parse form:", err)
|
2022-03-29 18:58:35 +04:00
|
|
|
http.Error(w, err.Error(), http.StatusExpectationFailed)
|
2022-02-07 04:49:21 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
f, fHandler, err := r.FormFile("file")
|
|
|
|
if err != nil {
|
2022-02-07 19:42:09 +04:00
|
|
|
h.logErr.Println("failed to open incoming file:", err)
|
2022-02-07 04:49:21 +04:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
2022-03-29 18:58:35 +04:00
|
|
|
http.Error(w, "cannot read incoming file", http.StatusInternalServerError)
|
2022-02-07 04:49:21 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
os.Remove(fHandler.Filename)
|
|
|
|
f.Close()
|
|
|
|
}()
|
|
|
|
|
2022-02-09 01:48:19 +04:00
|
|
|
var leftSpace int64 = storCapacity - *h.uploadDirSize
|
|
|
|
|
|
|
|
if leftSpace < fHandler.Size {
|
|
|
|
h.logErr.Println("not enough space left in storage, only", leftSpace>>20, "MiB left")
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
2022-06-13 21:07:33 +04:00
|
|
|
web.ErrorNoSpace(utils.MainSite(r.Host), w)
|
2022-02-09 01:48:19 +04:00
|
|
|
}
|
|
|
|
|
2022-02-07 04:49:21 +04:00
|
|
|
s256 := sha256.New()
|
|
|
|
if _, err := io.Copy(s256, f); err != nil {
|
2022-02-07 19:42:09 +04:00
|
|
|
h.logErr.Println("failed to compute SHA-256 hash:", err)
|
2022-03-29 18:58:35 +04:00
|
|
|
http.Error(w, "cannot compute hash for a file", http.StatusInternalServerError)
|
2022-02-07 04:49:21 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-07 23:40:41 +04:00
|
|
|
fHash := hex.EncodeToString(s256.Sum(nil))
|
2022-02-07 04:49:21 +04:00
|
|
|
s256.Write([]byte(h.conf.HashSalt))
|
|
|
|
fSaltedHash := base64.RawURLEncoding.EncodeToString(s256.Sum(nil))
|
|
|
|
|
|
|
|
f.Seek(0, io.SeekStart)
|
|
|
|
|
|
|
|
fPath := path.Join(h.conf.Uploads.Directory, fSaltedHash)
|
|
|
|
|
|
|
|
_, err = os.Stat(fPath)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
fDst, err := os.Create(fPath)
|
|
|
|
if err != nil {
|
2022-02-07 19:42:09 +04:00
|
|
|
h.logErr.Println("failed to open file for writing", err)
|
2022-03-29 18:58:35 +04:00
|
|
|
http.Error(w, "cannot create your file", http.StatusInternalServerError)
|
2022-02-07 04:49:21 +04:00
|
|
|
return
|
|
|
|
}
|
2022-02-07 22:27:34 +04:00
|
|
|
defer fDst.Close()
|
2022-02-07 04:49:21 +04:00
|
|
|
|
2022-02-08 02:12:01 +04:00
|
|
|
// We initialy set a dst file size to occupy space equal to uploaded's size.
|
|
|
|
// This is called a sparse file, if you need to know.
|
|
|
|
// It allows us to have a relatively small buffer size for inotify watcher.
|
|
|
|
// And it really affects that. I tested it.
|
|
|
|
fDst.Seek(fHandler.Size-1, io.SeekStart)
|
|
|
|
fDst.Write([]byte{0})
|
|
|
|
fDst.Seek(0, io.SeekStart)
|
|
|
|
|
2022-02-07 22:27:34 +04:00
|
|
|
_, err = io.Copy(fDst, f)
|
2022-02-07 04:49:21 +04:00
|
|
|
if err != nil {
|
2022-02-07 19:42:09 +04:00
|
|
|
h.logErr.Println("failed to copy uploaded file to destination:", err)
|
2022-03-29 18:58:35 +04:00
|
|
|
http.Error(w, "cannot copy file's content", http.StatusInternalServerError)
|
2022-02-07 04:49:21 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-07 23:40:41 +04:00
|
|
|
typ, _ := utils.NetworkType(r.Host)
|
2022-02-08 19:02:37 +04:00
|
|
|
|
2022-03-07 23:40:41 +04:00
|
|
|
h.logUpload.Printf("| %s | %s | %s | SHA256 %s | %s | %d | %s", r.Header.Get("X-Real-IP"), typ,
|
|
|
|
fHandler.Filename, fHash, fSaltedHash, fHandler.Size, r.UserAgent())
|
2022-02-07 04:49:21 +04:00
|
|
|
|
|
|
|
w.WriteHeader(http.StatusCreated)
|
|
|
|
} else {
|
|
|
|
os.Chtimes(fPath, time.Now(), time.Now())
|
2022-02-07 22:27:34 +04:00
|
|
|
w.WriteHeader(http.StatusFound)
|
2022-02-07 04:49:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
downloadURL := path.Join("/f", fSaltedHash, fHandler.Filename)
|
2022-02-08 19:21:44 +04:00
|
|
|
downloadURLParsed, _ := url.Parse(downloadURL)
|
2022-02-07 04:49:21 +04:00
|
|
|
|
2022-08-01 01:09:11 +04:00
|
|
|
_, scheme := utils.NetworkType(r.Host)
|
|
|
|
site := scheme + "://" + r.Host
|
|
|
|
|
2022-02-08 19:02:37 +04:00
|
|
|
if strings.Contains(r.UserAgent(), "curl") {
|
2022-08-01 01:09:11 +04:00
|
|
|
w.Write([]byte(site + downloadURLParsed.String() + "\r\n"))
|
2022-02-08 19:02:37 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-01 01:09:11 +04:00
|
|
|
web.Uploaded(utils.MainSite(r.Host), site, downloadURLParsed.String(), h.conf.Uploads.Limits.KeepForHours, w)
|
2022-02-07 04:49:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *UploadHandlers) Download(w http.ResponseWriter, r *http.Request) {
|
2023-04-24 00:49:53 +04:00
|
|
|
saltedHash := GetURLParam(r, "hash")
|
2022-02-07 04:49:21 +04:00
|
|
|
|
|
|
|
path := path.Join(h.conf.Uploads.Directory, saltedHash)
|
|
|
|
|
|
|
|
stat, err := os.Stat(path)
|
|
|
|
if os.IsNotExist(err) {
|
2023-05-13 16:29:32 +04:00
|
|
|
NotFound(w, r)
|
2022-02-07 04:49:21 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-04-24 00:49:53 +04:00
|
|
|
name := GetURLParam(r, "name")
|
2022-03-29 18:41:05 +04:00
|
|
|
|
2022-02-07 04:49:21 +04:00
|
|
|
w.Header().Add("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", name))
|
|
|
|
|
|
|
|
fd, err := os.Open(path)
|
|
|
|
if err != nil {
|
2022-02-07 19:42:09 +04:00
|
|
|
h.logErr.Println("failed to open file to read:", err)
|
2022-02-07 04:49:21 +04:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer fd.Close()
|
|
|
|
|
2022-02-08 19:02:37 +04:00
|
|
|
netTyp, _ := utils.NetworkType(r.Host)
|
|
|
|
|
2022-02-11 04:30:37 +04:00
|
|
|
h.logDownload.Printf("| %s | %s | %s | %s | %s", r.Header.Get("X-Real-IP"), netTyp, name, saltedHash, r.UserAgent())
|
2022-02-07 19:42:09 +04:00
|
|
|
|
2022-02-07 04:49:21 +04:00
|
|
|
http.ServeContent(w, r, path, stat.ModTime(), fd)
|
|
|
|
}
|
2022-02-07 22:51:47 +04:00
|
|
|
|
2022-07-01 03:31:33 +04:00
|
|
|
func (h *UploadHandlers) Delete(w http.ResponseWriter, r *http.Request) {
|
2022-07-01 03:43:52 +04:00
|
|
|
var saltedHash string
|
|
|
|
if r.Method == "DELETE" {
|
2023-04-24 00:49:53 +04:00
|
|
|
saltedHash = GetURLParam(r, "hash")
|
2022-07-01 03:43:52 +04:00
|
|
|
} else {
|
|
|
|
r.ParseForm()
|
|
|
|
saltedHash = r.FormValue("hash")
|
|
|
|
}
|
2022-07-01 03:31:33 +04:00
|
|
|
|
|
|
|
path := path.Join(h.conf.Uploads.Directory, saltedHash)
|
|
|
|
|
|
|
|
_, err := os.Stat(path)
|
|
|
|
if os.IsNotExist(err) {
|
2023-05-13 16:29:32 +04:00
|
|
|
NotFound(w, r)
|
2022-07-01 03:31:33 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = os.Remove(path)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
fmt.Fprint(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
netTyp, _ := utils.NetworkType(r.Host)
|
|
|
|
|
|
|
|
h.logDelete.Printf("| %s | %s | %s | %s", r.Header.Get("X-Real-IP"), netTyp, saltedHash, r.UserAgent())
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
}
|
|
|
|
|
2023-05-13 16:29:32 +04:00
|
|
|
func NotFound(w http.ResponseWriter, r *http.Request) {
|
2022-02-07 22:51:47 +04:00
|
|
|
w.WriteHeader(http.StatusNotFound)
|
2022-07-01 03:31:08 +04:00
|
|
|
if !strings.Contains(r.UserAgent(), "curl") {
|
2022-07-01 03:31:33 +04:00
|
|
|
web.Error404(utils.MainSite(r.Host), w)
|
2022-07-01 03:31:08 +04:00
|
|
|
} else {
|
2023-05-13 16:30:13 +04:00
|
|
|
fmt.Fprintln(w, "file not found")
|
2022-07-01 03:31:08 +04:00
|
|
|
}
|
2022-02-07 22:51:47 +04:00
|
|
|
}
|
2023-05-13 16:00:38 +04:00
|
|
|
|
2023-05-13 16:29:32 +04:00
|
|
|
func RobotsTxt(w http.ResponseWriter, r *http.Request) {
|
2023-05-13 16:00:38 +04:00
|
|
|
data, _ := web.AssetsGetFile("robots.txt")
|
|
|
|
w.Write(data)
|
|
|
|
}
|