github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/setting/lfs.go (about)

     1  // Copyright 2023 The GitBundle Inc. All rights reserved.
     2  // Copyright 2017 The Gitea Authors. All rights reserved.
     3  // Use of this source code is governed by a MIT-style
     4  // license that can be found in the LICENSE file.
     5  
     6  package setting
     7  
     8  import (
     9  	"encoding/base64"
    10  	"time"
    11  
    12  	"github.com/gitbundle/modules/generate"
    13  	"github.com/gitbundle/modules/log"
    14  
    15  	ini "gopkg.in/ini.v1"
    16  )
    17  
    18  // LFS represents the configuration for Git LFS
    19  var LFS = struct {
    20  	StartServer     bool          `ini:"LFS_START_SERVER"`
    21  	JWTSecretBase64 string        `ini:"LFS_JWT_SECRET"`
    22  	JWTSecretBytes  []byte        `ini:"-"`
    23  	HTTPAuthExpiry  time.Duration `ini:"LFS_HTTP_AUTH_EXPIRY"`
    24  	MaxFileSize     int64         `ini:"LFS_MAX_FILE_SIZE"`
    25  	LocksPagingNum  int           `ini:"LFS_LOCKS_PAGING_NUM"`
    26  
    27  	Storage
    28  }{}
    29  
    30  func newLFSService() {
    31  	sec := Cfg.Section("server")
    32  	if err := sec.MapTo(&LFS); err != nil {
    33  		log.Fatal("Failed to map LFS settings: %v", err)
    34  	}
    35  
    36  	lfsSec := Cfg.Section("lfs")
    37  	storageType := lfsSec.Key("STORAGE_TYPE").MustString("")
    38  
    39  	// Specifically default PATH to LFS_CONTENT_PATH
    40  	// FIXME: DEPRECATED to be removed in v1.18.0
    41  	deprecatedSetting("server", "LFS_CONTENT_PATH", "lfs", "PATH")
    42  	lfsSec.Key("PATH").MustString(
    43  		sec.Key("LFS_CONTENT_PATH").String())
    44  
    45  	LFS.Storage = getStorage("lfs", storageType, lfsSec)
    46  
    47  	// Rest of LFS service settings
    48  	if LFS.LocksPagingNum == 0 {
    49  		LFS.LocksPagingNum = 50
    50  	}
    51  
    52  	LFS.HTTPAuthExpiry = sec.Key("LFS_HTTP_AUTH_EXPIRY").MustDuration(20 * time.Minute)
    53  
    54  	if LFS.StartServer {
    55  		LFS.JWTSecretBytes = make([]byte, 32)
    56  		n, err := base64.RawURLEncoding.Decode(LFS.JWTSecretBytes, []byte(LFS.JWTSecretBase64))
    57  
    58  		if err != nil || n != 32 {
    59  			LFS.JWTSecretBase64, err = generate.NewJwtSecretBase64()
    60  			if err != nil {
    61  				log.Fatal("Error generating JWT Secret for custom config: %v", err)
    62  				return
    63  			}
    64  
    65  			// Save secret
    66  			CreateOrAppendToCustomConf(func(cfg *ini.File) {
    67  				cfg.Section("server").Key("LFS_JWT_SECRET").SetValue(LFS.JWTSecretBase64)
    68  			})
    69  		}
    70  	}
    71  }