add diskFilesWorldReadable option to control 0644/0600 choice for new files

This commit is contained in:
mappu 2017-11-18 14:15:31 +13:00
parent ee72f188a2
commit cd60e4c855
3 changed files with 23 additions and 11 deletions

View File

@ -5,6 +5,7 @@ import (
"encoding/json" "encoding/json"
"log" "log"
"net/http" "net/http"
"os"
"regexp" "regexp"
"strings" "strings"
"time" "time"
@ -21,14 +22,23 @@ type ServerPublicProperties struct {
} }
type ServerOptions struct { type ServerOptions struct {
DataDirectory string DataDirectory string
DBPath string DBPath string
BandwidthLimit int64 DiskFilesWorldReadable bool
TrustXForwardedFor bool BandwidthLimit int64
EnableHomepage bool TrustXForwardedFor bool
EnableHomepage bool
ServerPublicProperties ServerPublicProperties
} }
func (this *ServerOptions) FileMode() os.FileMode {
if this.DiskFilesWorldReadable {
return 0644
} else {
return 0600
}
}
type Server struct { type Server struct {
opts ServerOptions opts ServerOptions
db *bolt.DB db *bolt.DB

View File

@ -20,15 +20,17 @@ func main() {
maxUploadSpeed := flag.Int("speed", 0, "Maximum upload speed in bytes/sec (set zero for unlimited)") maxUploadSpeed := flag.Int("speed", 0, "Maximum upload speed in bytes/sec (set zero for unlimited)")
trustXForwardedFor := flag.Bool("trustXForwardedFor", false, "Trust X-Forwarded-For reverse proxy headers") trustXForwardedFor := flag.Bool("trustXForwardedFor", false, "Trust X-Forwarded-For reverse proxy headers")
enableHomepage := flag.Bool("enableHomepage", true, "Enable homepage (disable for embedded use only)") enableHomepage := flag.Bool("enableHomepage", true, "Enable homepage (disable for embedded use only)")
diskFilesWorldReadable := flag.Bool("diskFilesWorldReadable", false, "Save files as 0644 instead of 0600")
flag.Parse() flag.Parse()
svr, err := contented.NewServer(&contented.ServerOptions{ svr, err := contented.NewServer(&contented.ServerOptions{
DataDirectory: *dataDir, DataDirectory: *dataDir,
DBPath: *dbPath, DBPath: *dbPath,
BandwidthLimit: int64(*maxUploadSpeed), BandwidthLimit: int64(*maxUploadSpeed),
TrustXForwardedFor: *trustXForwardedFor, TrustXForwardedFor: *trustXForwardedFor,
EnableHomepage: *enableHomepage, EnableHomepage: *enableHomepage,
DiskFilesWorldReadable: *diskFilesWorldReadable,
ServerPublicProperties: contented.ServerPublicProperties{ ServerPublicProperties: contented.ServerPublicProperties{
AppTitle: *appTitle, AppTitle: *appTitle,
MaxUploadBytes: int64(*maxUploadMb) * 1024 * 1024, MaxUploadBytes: int64(*maxUploadMb) * 1024 * 1024,

View File

@ -91,7 +91,7 @@ func (this *Server) handleUploadFile(src multipart.File, hdr *multipart.FileHead
// Save file to disk // Save file to disk
fileHash := hex.EncodeToString(hasher.Sum(nil)) fileHash := hex.EncodeToString(hasher.Sum(nil))
dest, err := os.OpenFile(filepath.Join(this.opts.DataDirectory, fileHash), os.O_CREATE|os.O_WRONLY, 0600) dest, err := os.OpenFile(filepath.Join(this.opts.DataDirectory, fileHash), os.O_CREATE|os.O_WRONLY, this.opts.FileMode())
shouldSave := true shouldSave := true
if err != nil && os.IsExist(err) { if err != nil && os.IsExist(err) {
// hash matches existing upload // hash matches existing upload