code.gitea.io/gitea@v1.19.3/modules/setting/lfs.go (about)

     1  // Copyright 2019 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package setting
     5  
     6  import (
     7  	"encoding/base64"
     8  	"time"
     9  
    10  	"code.gitea.io/gitea/modules/generate"
    11  	"code.gitea.io/gitea/modules/log"
    12  
    13  	ini "gopkg.in/ini.v1"
    14  )
    15  
    16  // LFS represents the configuration for Git LFS
    17  var LFS = struct {
    18  	StartServer     bool          `ini:"LFS_START_SERVER"`
    19  	JWTSecretBase64 string        `ini:"LFS_JWT_SECRET"`
    20  	JWTSecretBytes  []byte        `ini:"-"`
    21  	HTTPAuthExpiry  time.Duration `ini:"LFS_HTTP_AUTH_EXPIRY"`
    22  	MaxFileSize     int64         `ini:"LFS_MAX_FILE_SIZE"`
    23  	LocksPagingNum  int           `ini:"LFS_LOCKS_PAGING_NUM"`
    24  
    25  	Storage
    26  }{}
    27  
    28  func loadLFSFrom(rootCfg ConfigProvider) {
    29  	sec := rootCfg.Section("server")
    30  	if err := sec.MapTo(&LFS); err != nil {
    31  		log.Fatal("Failed to map LFS settings: %v", err)
    32  	}
    33  
    34  	lfsSec := rootCfg.Section("lfs")
    35  	storageType := lfsSec.Key("STORAGE_TYPE").MustString("")
    36  
    37  	// Specifically default PATH to LFS_CONTENT_PATH
    38  	// DEPRECATED should not be removed because users maybe upgrade from lower version to the latest version
    39  	// if these are removed, the warning will not be shown
    40  	deprecatedSetting(rootCfg, "server", "LFS_CONTENT_PATH", "lfs", "PATH", "v1.19.0")
    41  	lfsSec.Key("PATH").MustString(
    42  		sec.Key("LFS_CONTENT_PATH").String())
    43  
    44  	LFS.Storage = getStorage(rootCfg, "lfs", storageType, lfsSec)
    45  
    46  	// Rest of LFS service settings
    47  	if LFS.LocksPagingNum == 0 {
    48  		LFS.LocksPagingNum = 50
    49  	}
    50  
    51  	LFS.HTTPAuthExpiry = sec.Key("LFS_HTTP_AUTH_EXPIRY").MustDuration(20 * time.Minute)
    52  
    53  	if LFS.StartServer {
    54  		LFS.JWTSecretBytes = make([]byte, 32)
    55  		n, err := base64.RawURLEncoding.Decode(LFS.JWTSecretBytes, []byte(LFS.JWTSecretBase64))
    56  
    57  		if err != nil || n != 32 {
    58  			LFS.JWTSecretBase64, err = generate.NewJwtSecretBase64()
    59  			if err != nil {
    60  				log.Fatal("Error generating JWT Secret for custom config: %v", err)
    61  				return
    62  			}
    63  
    64  			// Save secret
    65  			CreateOrAppendToCustomConf("server.LFS_JWT_SECRET", func(cfg *ini.File) {
    66  				cfg.Section("server").Key("LFS_JWT_SECRET").SetValue(LFS.JWTSecretBase64)
    67  			})
    68  		}
    69  	}
    70  }