<commit_before>package main

import (
	"flag"
	"fmt"
	"log"
	"math/rand"
	"net/http"
	"os"
	"strconv"
	"time"

	"github.com/GeertJohan/go.rice"
	"github.com/gorilla/mux"

	"github.com/espebra/filebin/app/api"
	"github.com/espebra/filebin/app/config"
	"github.com/espebra/filebin/app/model"
)

var cfg = config.Global
var githash = "No githash provided"
var buildstamp = "No buildstamp provided"

var staticBox *rice.Box
var templateBox *rice.Box

// Initiate buffered channel for batch processing
var WorkQueue = make(chan model.File, 1000)

func isDir(path string) bool {
	fi, err := os.Stat(path)
	if err != nil {
		return false
	}
	if fi.IsDir() {
		return true
	} else {
		return false
	}
}

func generateReqId(n int) string {
	var letters = []rune("abcdefghijklmnopqrstuvwxyz0123456789")
	b := make([]rune, n)
	for i := range b {
		b[i] = letters[rand.Intn(len(letters))]
	}
	return string(b)
}

func init() {
	rand.Seed(time.Now().UTC().UnixNano())

	flag.StringVar(&cfg.Baseurl, "baseurl",
		cfg.Baseurl, "Baseurl used when generating links.")

	flag.StringVar(&cfg.Filedir, "filedir",
		cfg.Filedir, "Directory to store uploaded files.")

	flag.StringVar(&cfg.Tempdir, "tempdir",
		cfg.Tempdir, "Directory to temporarily store files during upload.")

	//flag.StringVar(&cfg.Logdir, "logdir",
	//	cfg.Logdir, "Directory to write log files to.")

	//flag.StringVar(&cfg.Thumbdir, "thumbdir",
	//	cfg.Thumbdir, "Path to thumbnail directory")

	//flag.StringVar(&cfg.Database, "database",
	//	cfg.Database, "Path to database file")

	flag.StringVar(&cfg.Host, "host",
		cfg.Host, "Listen host.")

	flag.IntVar(&cfg.Port, "port",
		cfg.Port, "Listen port.")

	flag.IntVar(&cfg.Readtimeout, "readtimeout",
		cfg.Readtimeout, "Request read timeout in seconds.")

	flag.IntVar(&cfg.Writetimeout, "writetimeout",
		cfg.Writetimeout, "Response write timeout in seconds.")

	flag.IntVar(&cfg.Maxheaderbytes, "maxheaderbytes",
		cfg.Maxheaderbytes, "Max header size in bytes.")

	flag.BoolVar(&cfg.CacheInvalidation, "cache-invalidation",
		cfg.CacheInvalidation,
		"HTTP PURGE requests will be sent on every change if enabled.")

	flag.IntVar(&cfg.Workers, "workers",
		cfg.Workers, "Number of workers for background processing.")

	//flag.IntVar(&cfg.Pagination, "pagination",
	//	cfg.Pagination,
	//	"Files to show per page for pagination.")

	//flag.StringVar(&cfg.GeoIP2, "geoip2",
	//	cfg.GeoIP2, "Path to the GeoIP2 database file.")

	flag.Int64Var(&cfg.Expiration, "expiration",
		cfg.Expiration, "Tag expiration time in seconds after the last modification.")

	//flag.BoolVar(&cfg.Verbose, "verbose",
	//	cfg.Verbose, "Verbose output.")

	flag.StringVar(&cfg.TriggerNewTag, "trigger-new-tag",
		cfg.TriggerNewTag,
		"Command to execute when a tag is created.")

	flag.StringVar(&cfg.TriggerUploadFile,
		"trigger-upload-file",
		cfg.TriggerUploadFile,
		"Command to execute when a file is uploaded.")

	flag.StringVar(&cfg.TriggerDownloadTag,
		"trigger-download-tag",
		cfg.TriggerDownloadTag,
		"Command to execute when a tag archive is downloaded.")

	flag.StringVar(&cfg.TriggerDownloadFile,
		"trigger-download-file",
		cfg.TriggerDownloadFile,
		"Command to execute when a file is downloaded.")

	flag.StringVar(&cfg.TriggerDeleteTag,
		"trigger-delete-tag",
		cfg.TriggerDeleteTag,
		"Command to execute when a tag is deleted.")

	flag.StringVar(&cfg.TriggerDeleteFile,
		"trigger-delete-file",
		cfg.TriggerDeleteFile,
		"Command to execute when a file is deleted.")

	//	flag.StringVar(&cfg.TriggerExpiredTag, "trigger-expired-tag",
	//		cfg.TriggerExpiredTag,
	//		"Trigger to execute when a tag expires.")

	flag.BoolVar(&cfg.Version, "version",
		cfg.Version, "Show the version information.")

	flag.Parse()

	if cfg.Version {
		fmt.Println("Git Commit Hash: " + githash)
		fmt.Println("UTC Build Time: " + buildstamp)
		os.Exit(0)
	}

	//if (!IsDir(cfg.Logdir)) {
	//    fmt.Println("The specified log directory is not a directory: ",
	//        cfg.Logdir)
	//    os.Exit(2)
	//}

	if cfg.Port < 1 || cfg.Port > 65535 {
		log.Fatalln("Invalid port number, aborting.")
	}

	if cfg.Readtimeout < 1 || cfg.Readtimeout > 3600 {
		log.Fatalln("Invalid read timeout, aborting.")
	}

	if cfg.Writetimeout < 1 || cfg.Writetimeout > 3600 {
		log.Fatalln("Invalid write timeout, aborting.")
	}

	if cfg.Maxheaderbytes < 1 ||
		cfg.Maxheaderbytes > 2<<40 {
		log.Fatalln("Invalid max header bytes, aborting.")
	}<commit_msg>Increase the max timeout limits since 1h might be too short for mobile/slow clients and large files<commit_after>package main

import (
	"flag"
	"fmt"
	"log"
	"math/rand"
	"net/http"
	"os"
	"strconv"
	"time"

	"github.com/GeertJohan/go.rice"
	"github.com/gorilla/mux"

	"github.com/espebra/filebin/app/api"
	"github.com/espebra/filebin/app/config"
	"github.com/espebra/filebin/app/model"
)

var cfg = config.Global
var githash = "No githash provided"
var buildstamp = "No buildstamp provided"

var staticBox *rice.Box
var templateBox *rice.Box

// Initiate buffered channel for batch processing
var WorkQueue = make(chan model.File, 1000)

func isDir(path string) bool {
	fi, err := os.Stat(path)
	if err != nil {
		return false
	}
	if fi.IsDir() {
		return true
	} else {
		return false
	}
}

func generateReqId(n int) string {
	var letters = []rune("abcdefghijklmnopqrstuvwxyz0123456789")
	b := make([]rune, n)
	for i := range b {
		b[i] = letters[rand.Intn(len(letters))]
	}
	return string(b)
}

func init() {
	rand.Seed(time.Now().UTC().UnixNano())

	flag.StringVar(&cfg.Baseurl, "baseurl",
		cfg.Baseurl, "Baseurl used when generating links.")

	flag.StringVar(&cfg.Filedir, "filedir",
		cfg.Filedir, "Directory to store uploaded files.")

	flag.StringVar(&cfg.Tempdir, "tempdir",
		cfg.Tempdir, "Directory to temporarily store files during upload.")

	//flag.StringVar(&cfg.Logdir, "logdir",
	//	cfg.Logdir, "Directory to write log files to.")

	//flag.StringVar(&cfg.Thumbdir, "thumbdir",
	//	cfg.Thumbdir, "Path to thumbnail directory")

	//flag.StringVar(&cfg.Database, "database",
	//	cfg.Database, "Path to database file")

	flag.StringVar(&cfg.Host, "host",
		cfg.Host, "Listen host.")

	flag.IntVar(&cfg.Port, "port",
		cfg.Port, "Listen port.")

	flag.IntVar(&cfg.Readtimeout, "readtimeout",
		cfg.Readtimeout, "Request read timeout in seconds.")

	flag.IntVar(&cfg.Writetimeout, "writetimeout",
		cfg.Writetimeout, "Response write timeout in seconds.")

	flag.IntVar(&cfg.Maxheaderbytes, "maxheaderbytes",
		cfg.Maxheaderbytes, "Max header size in bytes.")

	flag.BoolVar(&cfg.CacheInvalidation, "cache-invalidation",
		cfg.CacheInvalidation,
		"HTTP PURGE requests will be sent on every change if enabled.")

	flag.IntVar(&cfg.Workers, "workers",
		cfg.Workers, "Number of workers for background processing.")

	//flag.IntVar(&cfg.Pagination, "pagination",
	//	cfg.Pagination,
	//	"Files to show per page for pagination.")

	//flag.StringVar(&cfg.GeoIP2, "geoip2",
	//	cfg.GeoIP2, "Path to the GeoIP2 database file.")

	flag.Int64Var(&cfg.Expiration, "expiration",
		cfg.Expiration, "Tag expiration time in seconds after the last modification.")

	//flag.BoolVar(&cfg.Verbose, "verbose",
	//	cfg.Verbose, "Verbose output.")

	flag.StringVar(&cfg.TriggerNewTag, "trigger-new-tag",
		cfg.TriggerNewTag,
		"Command to execute when a tag is created.")

	flag.StringVar(&cfg.TriggerUploadFile,
		"trigger-upload-file",
		cfg.TriggerUploadFile,
		"Command to execute when a file is uploaded.")

	flag.StringVar(&cfg.TriggerDownloadTag,
		"trigger-download-tag",
		cfg.TriggerDownloadTag,
		"Command to execute when a tag archive is downloaded.")

	flag.StringVar(&cfg.TriggerDownloadFile,
		"trigger-download-file",
		cfg.TriggerDownloadFile,
		"Command to execute when a file is downloaded.")

	flag.StringVar(&cfg.TriggerDeleteTag,
		"trigger-delete-tag",
		cfg.TriggerDeleteTag,
		"Command to execute when a tag is deleted.")

	flag.StringVar(&cfg.TriggerDeleteFile,
		"trigger-delete-file",
		cfg.TriggerDeleteFile,
		"Command to execute when a file is deleted.")

	//	flag.StringVar(&cfg.TriggerExpiredTag, "trigger-expired-tag",
	//		cfg.TriggerExpiredTag,
	//		"Trigger to execute when a tag expires.")

	flag.BoolVar(&cfg.Version, "version",
		cfg.Version, "Show the version information.")

	flag.Parse()

	if cfg.Version {
		fmt.Println("Git Commit Hash: " + githash)
		fmt.Println("UTC Build Time: " + buildstamp)
		os.Exit(0)
	}

	//if (!IsDir(cfg.Logdir)) {
	//    fmt.Println("The specified log directory is not a directory: ",
	//        cfg.Logdir)
	//    os.Exit(2)
	//}

	if cfg.Port < 1 || cfg.Port > 65535 {
		log.Fatalln("Invalid port number, aborting.")
	}

	if cfg.Readtimeout < 1 || cfg.Readtimeout > 86400 {
		log.Fatalln("Invalid read timeout, aborting.")
	}

	if cfg.Writetimeout < 1 || cfg.Writetimeout > 86400 {
		log.Fatalln("Invalid write timeout, aborting.")
	}

	if cfg.Maxheaderbytes < 1 ||
		cfg.Maxheaderbytes > 2<<40 {
		log.Fatalln("Invalid max header bytes, aborting.")
	}
{'fraction_non_alphanumeric': 0.14600089365504915, 'fraction_numerical': 0.009718498659517426, 'mean_word_length': 4.081157775255392, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7667263', 'n_tokens_mistral': 3348, 'n_tokens_neox': 3111, 'n_words': 762}