2020-10-27 17:17:41 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
2022-08-16 00:23:29 +02:00
|
|
|
|
2023-07-14 14:11:38 +02:00
|
|
|
"git.sr.ht/~noury/yaf/fileexpiration"
|
|
|
|
"github.com/julienschmidt/httprouter"
|
2022-08-16 00:23:29 +02:00
|
|
|
"github.com/leon-richardt/jaf/exifscrubber"
|
2020-10-27 17:17:41 +01:00
|
|
|
)
|
|
|
|
|
2023-07-14 14:11:38 +02:00
|
|
|
const (
|
|
|
|
allowedChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
|
|
|
|
maxAge = time.Hour * 24 * 7 // 7 days
|
|
|
|
)
|
2020-10-27 17:17:41 +01:00
|
|
|
|
|
|
|
type parameters struct {
|
|
|
|
configFile string
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseParams() *parameters {
|
2023-07-14 14:11:38 +02:00
|
|
|
configFile := flag.String("configFile", "yaf.conf", "path to config file")
|
2020-10-27 17:17:41 +01:00
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
retval := ¶meters{}
|
|
|
|
retval.configFile = *configFile
|
|
|
|
return retval
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
params := parseParams()
|
|
|
|
|
|
|
|
// Read config
|
|
|
|
config, err := ConfigFromFile(params.configFile)
|
|
|
|
if err != nil {
|
2022-08-23 12:04:56 +02:00
|
|
|
log.Fatalf("could not parse config file: %s\n", err.Error())
|
2020-10-27 17:17:41 +01:00
|
|
|
}
|
2023-07-14 14:11:38 +02:00
|
|
|
fd := config.FileDir
|
|
|
|
fmt.Println(fd)
|
2020-10-27 17:17:41 +01:00
|
|
|
|
2022-08-16 00:23:29 +02:00
|
|
|
handler := uploadHandler{
|
|
|
|
config: config,
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.ScrubExif {
|
|
|
|
scrubber := exifscrubber.NewExifScrubber(config.ExifAllowedIds, config.ExifAllowedPaths)
|
|
|
|
handler.exifScrubber = &scrubber
|
|
|
|
}
|
|
|
|
|
2023-07-14 14:11:38 +02:00
|
|
|
if config.FileExpiration {
|
|
|
|
fmt.Println("FILE EXPIRATION ENABLED")
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
<-time.After(time.Hour * 2)
|
|
|
|
fileexpiration.DeleteExpired(fd, maxAge)
|
|
|
|
}
|
|
|
|
}()
|
2020-10-27 17:17:41 +01:00
|
|
|
}
|
|
|
|
|
2023-07-14 14:11:38 +02:00
|
|
|
router := httprouter.New()
|
2020-10-27 17:17:41 +01:00
|
|
|
log.Printf("starting jaf on port %d\n", config.Port)
|
2023-07-14 14:11:38 +02:00
|
|
|
router.HandlerFunc(http.MethodPost, "/upload", handler.PostUpload)
|
|
|
|
router.HandlerFunc(http.MethodPost, "/uploadweb", handler.PostUploadRedirect)
|
|
|
|
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%v", config.Port), router))
|
2020-10-27 17:17:41 +01:00
|
|
|
}
|