1
0
Fork 0

Made use of cmd arguments.

This commit is contained in:
Alexander Andreev 2023-05-24 22:19:41 +04:00
parent 11dfbfbc23
commit 931f98d12e
Signed by: Arav
GPG Key ID: D22A817D95815393
2 changed files with 33 additions and 17 deletions

View File

@ -19,7 +19,7 @@ import (
var configPath *string = flag.String("conf", "config.yaml", "path to configuration file")
var listenAddress *string = flag.String("listen", "/var/run/dwelling-upload/sock", "listen address (ip:port|unix_path)")
var uploadDir *string = flag.String("upload-dir", "/srv/uploads", "path to a directory where uploaded files are stored")
var keepFileForHours *int64 = flag.Int64("keep-for", 36, "keep files for this much hours")
var keepFileForHours *int = flag.Int("keep-for", 36, "keep files for this much hours")
var limitStorage *int64 = flag.Int64("storage", 102400, "storage size in MiB for uploads")
var limitFileSize *int64 = flag.Int64("file-size", 128, "max. size in MiB for uploaded files")
var showVersion *bool = flag.Bool("v", false, "show version")
@ -54,6 +54,11 @@ func main() {
}
}
hashSalt, err := os.ReadFile(path.Join(os.Getenv("CREDENTIALS_DIRECTORY"), "salt"))
if err != nil {
log.Fatalln("failed to read hash salt file:", err)
}
logFilePath := path.Join(os.Getenv("LOGS_DIRECTORY"), "file.log")
logFile, err := logging.New(logFilePath)
if err != nil {
@ -79,7 +84,8 @@ func main() {
watcha.WatchForMask(uploadDirNotify, watcher.CrDelMask)
hand := http.NewUploadHandlers(config, logFile, &uploadDirSize)
hand := http.NewUploadHandlers(logFile, *uploadDir, &uploadDirSize, string(hashSalt),
*keepFileForHours, *limitStorage, *limitFileSize)
srv := http.NewHttpServer()
srv.SetNotFoundHandler(http.NotFound)

View File

@ -2,7 +2,6 @@ package http
import (
"crypto/sha256"
"dwelling-upload/internal/configuration"
"dwelling-upload/pkg/logging"
"dwelling-upload/pkg/utils"
"dwelling-upload/web"
@ -20,17 +19,28 @@ import (
)
type UploadHandlers struct {
conf *configuration.Configuration
logFile *logging.Logger
uploadDir string
uploadDirSize *int64
hashSalt string
keepForHours int
limitStorage int64
limitFileSize int64
}
func NewUploadHandlers(conf *configuration.Configuration, lFile *logging.Logger, uploadDirSize *int64) *UploadHandlers {
func NewUploadHandlers(lFile *logging.Logger, uploadDir string, uploadDirSize *int64,
hashSalt string, keepForHours int, limStorage, limFileSz int64) *UploadHandlers {
return &UploadHandlers{
conf: conf,
logFile: lFile,
uploadDirSize: uploadDirSize}
uploadDir: uploadDir,
uploadDirSize: uploadDirSize,
hashSalt: hashSalt,
keepForHours: keepForHours,
limitStorage: limStorage,
limitFileSize: limFileSz}
}
func (*UploadHandlers) AssetsFS() http.FileSystem {
@ -38,20 +48,20 @@ func (*UploadHandlers) AssetsFS() http.FileSystem {
}
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
var storCapacity int64 = h.limitStorage << 20
var fMaxSize int64 = h.limitFileSize << 20
_, _, capStr := utils.ConvertFileSize(storCapacity)
_, _, usedStr := utils.ConvertFileSize(*h.uploadDirSize)
_, _, availStr := utils.ConvertFileSize(storCapacity - *h.uploadDirSize)
_, _, fMaxSzStr := utils.ConvertFileSize(fMaxSize)
web.Index(utils.MainSite(r.Host), storCapacity, *h.uploadDirSize, h.conf.Uploads.Limits.KeepForHours, fMaxSzStr, usedStr, capStr, availStr, w)
web.Index(utils.MainSite(r.Host), storCapacity, *h.uploadDirSize, h.keepForHours, fMaxSzStr, usedStr, capStr, availStr, w)
}
func (h *UploadHandlers) Upload(w http.ResponseWriter, r *http.Request) {
var fMaxSizeBytes int64 = h.conf.Uploads.Limits.FileSize << 20
var storCapacity int64 = h.conf.Uploads.Limits.Storage << 20
var fMaxSizeBytes int64 = h.limitFileSize << 20
var storCapacity int64 = h.limitStorage << 20
r.Body = http.MaxBytesReader(w, r.Body, fMaxSizeBytes)
@ -89,12 +99,12 @@ func (h *UploadHandlers) Upload(w http.ResponseWriter, r *http.Request) {
}
fHash := hex.EncodeToString(s256.Sum(nil))
s256.Write([]byte(h.conf.HashSalt))
s256.Write([]byte(h.hashSalt))
fSaltedHash := base64.RawURLEncoding.EncodeToString(s256.Sum(nil))
f.Seek(0, io.SeekStart)
fPath := path.Join(h.conf.Uploads.Directory, fSaltedHash)
fPath := path.Join(h.uploadDir, fSaltedHash)
_, err = os.Stat(fPath)
if os.IsNotExist(err) {
@ -143,13 +153,13 @@ func (h *UploadHandlers) Upload(w http.ResponseWriter, r *http.Request) {
return
}
web.Uploaded(utils.MainSite(r.Host), site, downloadURLParsed.String(), h.conf.Uploads.Limits.KeepForHours, w)
web.Uploaded(utils.MainSite(r.Host), site, downloadURLParsed.String(), h.keepForHours, w)
}
func (h *UploadHandlers) Download(w http.ResponseWriter, r *http.Request) {
saltedHash := GetURLParam(r, "hash")
path := path.Join(h.conf.Uploads.Directory, saltedHash)
path := path.Join(h.uploadDir, saltedHash)
stat, err := os.Stat(path)
if os.IsNotExist(err) {
@ -185,7 +195,7 @@ func (h *UploadHandlers) Delete(w http.ResponseWriter, r *http.Request) {
saltedHash = r.FormValue("hash")
}
path := path.Join(h.conf.Uploads.Directory, saltedHash)
path := path.Join(h.uploadDir, saltedHash)
_, err := os.Stat(path)
if os.IsNotExist(err) {