github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/setting/picture.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  	"net/url"
    10  
    11  	"github.com/gitbundle/modules/log"
    12  
    13  	"strk.kbt.io/projects/go/libravatar"
    14  )
    15  
    16  // settings
    17  var (
    18  	// Picture settings
    19  	Avatar = struct {
    20  		Storage
    21  
    22  		MaxWidth           int
    23  		MaxHeight          int
    24  		MaxFileSize        int64
    25  		RenderedSizeFactor int
    26  	}{
    27  		MaxWidth:           4096,
    28  		MaxHeight:          3072,
    29  		MaxFileSize:        1048576,
    30  		RenderedSizeFactor: 3,
    31  	}
    32  
    33  	GravatarSource        string
    34  	GravatarSourceURL     *url.URL
    35  	DisableGravatar       bool
    36  	EnableFederatedAvatar bool
    37  	LibravatarService     *libravatar.Libravatar
    38  
    39  	RepoAvatar = struct {
    40  		Storage
    41  
    42  		Fallback      string
    43  		FallbackImage string
    44  	}{}
    45  )
    46  
    47  func newPictureService() {
    48  	sec := Cfg.Section("picture")
    49  
    50  	avatarSec := Cfg.Section("avatar")
    51  	storageType := sec.Key("AVATAR_STORAGE_TYPE").MustString("")
    52  	// Specifically default PATH to AVATAR_UPLOAD_PATH
    53  	avatarSec.Key("PATH").MustString(
    54  		sec.Key("AVATAR_UPLOAD_PATH").String())
    55  
    56  	Avatar.Storage = getStorage("avatars", storageType, avatarSec)
    57  
    58  	Avatar.MaxWidth = sec.Key("AVATAR_MAX_WIDTH").MustInt(4096)
    59  	Avatar.MaxHeight = sec.Key("AVATAR_MAX_HEIGHT").MustInt(3072)
    60  	Avatar.MaxFileSize = sec.Key("AVATAR_MAX_FILE_SIZE").MustInt64(1048576)
    61  	Avatar.RenderedSizeFactor = sec.Key("AVATAR_RENDERED_SIZE_FACTOR").MustInt(3)
    62  
    63  	switch source := sec.Key("GRAVATAR_SOURCE").MustString("gravatar"); source {
    64  	case "duoshuo":
    65  		GravatarSource = "http://gravatar.duoshuo.com/avatar/"
    66  	case "gravatar":
    67  		GravatarSource = "https://secure.gravatar.com/avatar/"
    68  	case "libravatar":
    69  		GravatarSource = "https://seccdn.libravatar.org/avatar/"
    70  	default:
    71  		GravatarSource = source
    72  	}
    73  	DisableGravatar = sec.Key("DISABLE_GRAVATAR").MustBool()
    74  	EnableFederatedAvatar = sec.Key("ENABLE_FEDERATED_AVATAR").MustBool(!InstallLock)
    75  	if OfflineMode {
    76  		DisableGravatar = true
    77  		EnableFederatedAvatar = false
    78  	}
    79  	if DisableGravatar {
    80  		EnableFederatedAvatar = false
    81  	}
    82  	if EnableFederatedAvatar || !DisableGravatar {
    83  		var err error
    84  		GravatarSourceURL, err = url.Parse(GravatarSource)
    85  		if err != nil {
    86  			log.Fatal("Failed to parse Gravatar URL(%s): %v",
    87  				GravatarSource, err)
    88  		}
    89  	}
    90  
    91  	if EnableFederatedAvatar {
    92  		LibravatarService = libravatar.New()
    93  		if GravatarSourceURL.Scheme == "https" {
    94  			LibravatarService.SetUseHTTPS(true)
    95  			LibravatarService.SetSecureFallbackHost(GravatarSourceURL.Host)
    96  		} else {
    97  			LibravatarService.SetUseHTTPS(false)
    98  			LibravatarService.SetFallbackHost(GravatarSourceURL.Host)
    99  		}
   100  	}
   101  
   102  	newRepoAvatarService()
   103  }
   104  
   105  func newRepoAvatarService() {
   106  	sec := Cfg.Section("picture")
   107  
   108  	repoAvatarSec := Cfg.Section("repo-avatar")
   109  	storageType := sec.Key("REPOSITORY_AVATAR_STORAGE_TYPE").MustString("")
   110  	// Specifically default PATH to AVATAR_UPLOAD_PATH
   111  	repoAvatarSec.Key("PATH").MustString(
   112  		sec.Key("REPOSITORY_AVATAR_UPLOAD_PATH").String())
   113  
   114  	RepoAvatar.Storage = getStorage("repo-avatars", storageType, repoAvatarSec)
   115  
   116  	RepoAvatar.Fallback = sec.Key("REPOSITORY_AVATAR_FALLBACK").MustString("none")
   117  	RepoAvatar.FallbackImage = sec.Key("REPOSITORY_AVATAR_FALLBACK_IMAGE").MustString("/assets/img/repo_default.png")
   118  }