99 lines
2.1 KiB
Go
99 lines
2.1 KiB
Go
package main
|
|
|
|
import (
|
|
"dwelling-upload/internal/configuration"
|
|
"dwelling-upload/pkg/logging"
|
|
"dwelling-upload/pkg/utils"
|
|
"flag"
|
|
"io/ioutil"
|
|
"log"
|
|
"os"
|
|
"os/user"
|
|
"path"
|
|
"strconv"
|
|
"syscall"
|
|
"time"
|
|
)
|
|
|
|
var configPath *string = flag.String("conf", "config.yaml", "path to configuration file")
|
|
var logToStdout *bool = flag.Bool("log-stdout", false, "write logs to stdout")
|
|
|
|
func substituteUser(username string) error {
|
|
u, err := user.Lookup(username)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
uid, err := strconv.Atoi(u.Uid)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if os.Getuid() != uid {
|
|
if err := syscall.Setuid(uid); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func main() {
|
|
flag.Parse()
|
|
|
|
config, err := configuration.LoadConfiguration(*configPath)
|
|
if err != nil {
|
|
log.Fatalln(err)
|
|
}
|
|
|
|
if config.Chroot != "" {
|
|
if err := syscall.Chroot(config.Chroot); err != nil {
|
|
log.Fatalln("failed to chroot:", err)
|
|
}
|
|
}
|
|
|
|
if err := substituteUser(config.User); err != nil {
|
|
log.Fatalln(err)
|
|
}
|
|
|
|
if *logToStdout {
|
|
config.Log.ToStdout = true
|
|
}
|
|
|
|
logErr, err := logging.NewLogger(config.Log.ClearError, config.Log.ToStdout)
|
|
if err != nil {
|
|
log.Fatalln("failed to open error logger:", err)
|
|
}
|
|
defer logErr.Close()
|
|
|
|
logClean, err := logging.NewLogger(config.Log.Clear, config.Log.ToStdout)
|
|
if err != nil {
|
|
log.Fatalln("failed to open error logger:", err)
|
|
}
|
|
defer logClean.Close()
|
|
|
|
uploadsDir, err := ioutil.ReadDir(config.Uploads.Directory)
|
|
if err != nil {
|
|
logErr.Fatalf("failed to open directory %s: %s\n", config.Uploads.Directory, err)
|
|
}
|
|
|
|
var deletedCount int64 = 0
|
|
var deletedSize int64 = 0
|
|
|
|
for _, entry := range uploadsDir {
|
|
if time.Now().UTC().Sub(entry.ModTime().UTC()) >= time.Duration(config.Uploads.Limits.KeepForHours)*time.Hour {
|
|
if err := os.Remove(path.Join(config.Uploads.Directory, entry.Name())); err != nil {
|
|
logErr.Println("failed to remove file ", entry.Name(), ": ", err)
|
|
}
|
|
deletedSize += entry.Size()
|
|
deletedCount++
|
|
}
|
|
}
|
|
|
|
_, _, cFSz := utils.ConvertFileSize(deletedSize)
|
|
|
|
if deletedCount > 0 {
|
|
logClean.Printf("%d %s", deletedCount, cFSz)
|
|
}
|
|
}
|