mirror of
https://github.com/Forceu/Gokapi.git
synced 2026-02-21 08:08:40 -06:00
Changed Mutex to RWMutex
This commit is contained in:
@@ -36,7 +36,7 @@ var serverSettings Configuration
|
||||
const currentConfigVersion = 7
|
||||
|
||||
// For locking this object to prevent race conditions
|
||||
var mutex sync.Mutex
|
||||
var mutex sync.RWMutex
|
||||
|
||||
// Configuration is a struct that contains the global configuration
|
||||
type Configuration struct {
|
||||
@@ -98,13 +98,25 @@ func Release() {
|
||||
mutex.Unlock()
|
||||
}
|
||||
|
||||
// GetServerSettings locks the settings returns a pointer to the configuration
|
||||
// GetServerSettings locks the settings returns a pointer to the configuration for Read/Write access
|
||||
// Release needs to be called when finished with the operation!
|
||||
func GetServerSettings() *Configuration {
|
||||
mutex.Lock()
|
||||
return &serverSettings
|
||||
}
|
||||
|
||||
// GetServerSettingsReadOnly locks the settings for read-only access and returns a copy of the configuration
|
||||
// ReleaseReadOnly needs to be called when finished with the operation!
|
||||
func GetServerSettingsReadOnly() *Configuration {
|
||||
mutex.RLock()
|
||||
return &serverSettings
|
||||
}
|
||||
|
||||
// ReleaseReadOnly unlocks the configuration opened for read-only access
|
||||
func ReleaseReadOnly() {
|
||||
mutex.RUnlock()
|
||||
}
|
||||
|
||||
// Upgrades the ServerSettings if saved with a previous version
|
||||
func updateConfig() {
|
||||
// < v1.1.2
|
||||
|
||||
@@ -139,9 +139,9 @@ func GetFile(id string) (models.File, bool) {
|
||||
if id == "" {
|
||||
return emptyResult, false
|
||||
}
|
||||
settings := configuration.GetServerSettings()
|
||||
settings := configuration.GetServerSettingsReadOnly()
|
||||
defer configuration.ReleaseReadOnly()
|
||||
file := settings.Files[id]
|
||||
configuration.Release()
|
||||
if file.ExpireAt < time.Now().Unix() || file.DownloadsRemaining < 1 {
|
||||
return emptyResult, false
|
||||
}
|
||||
@@ -158,9 +158,9 @@ func GetFileByHotlink(id string) (models.File, bool) {
|
||||
if id == "" {
|
||||
return emptyResult, false
|
||||
}
|
||||
settings := configuration.GetServerSettings()
|
||||
settings := configuration.GetServerSettingsReadOnly()
|
||||
hotlink := settings.Hotlinks[id]
|
||||
configuration.Release()
|
||||
configuration.ReleaseReadOnly()
|
||||
return GetFile(hotlink.FileId)
|
||||
}
|
||||
|
||||
|
||||
@@ -113,7 +113,7 @@ func Start() {
|
||||
}
|
||||
|
||||
func initLocalVariables() {
|
||||
settings := configuration.GetServerSettings()
|
||||
settings := configuration.GetServerSettingsReadOnly()
|
||||
webserverPort = settings.Port
|
||||
webserverExtUrl = settings.ServerUrl
|
||||
webserverRedirectUrl = settings.RedirectUrl
|
||||
@@ -121,7 +121,7 @@ func initLocalVariables() {
|
||||
webserverAdminPassword = settings.AdminPassword
|
||||
webserverMaxMemory = settings.MaxMemory
|
||||
webserverUseSsl = settings.UseSsl
|
||||
configuration.Release()
|
||||
configuration.ReleaseReadOnly()
|
||||
}
|
||||
|
||||
// Initialises the templateFolder variable by scanning through all the templates.
|
||||
@@ -364,7 +364,7 @@ type UploadView struct {
|
||||
func (u *UploadView) convertGlobalConfig(isMainView bool) *UploadView {
|
||||
var result []models.File
|
||||
var resultApi []models.ApiKey
|
||||
settings := configuration.GetServerSettings()
|
||||
settings := configuration.GetServerSettingsReadOnly()
|
||||
if isMainView {
|
||||
for _, element := range settings.Files {
|
||||
result = append(result, element)
|
||||
@@ -401,7 +401,7 @@ func (u *UploadView) convertGlobalConfig(isMainView bool) *UploadView {
|
||||
u.TimeNow = time.Now().Unix()
|
||||
u.IsAdminView = true
|
||||
u.IsMainView = isMainView
|
||||
configuration.Release()
|
||||
configuration.ReleaseReadOnly()
|
||||
return u
|
||||
}
|
||||
|
||||
|
||||
@@ -94,13 +94,13 @@ func deleteFile(w http.ResponseWriter, request apiRequest) {
|
||||
func list(w http.ResponseWriter) {
|
||||
var validFiles []models.File
|
||||
sendOk(w)
|
||||
settings := configuration.GetServerSettings()
|
||||
settings := configuration.GetServerSettingsReadOnly()
|
||||
for _, element := range settings.Files {
|
||||
if element.ExpireAt > time.Now().Unix() && element.DownloadsRemaining > 0 {
|
||||
validFiles = append(validFiles, element)
|
||||
}
|
||||
}
|
||||
configuration.Release()
|
||||
configuration.ReleaseReadOnly()
|
||||
result, err := json.Marshal(validFiles)
|
||||
helper.Check(err)
|
||||
_, _ = w.Write(result)
|
||||
|
||||
@@ -24,7 +24,7 @@ func IsValidSession(w http.ResponseWriter, r *http.Request) bool {
|
||||
sessionString := cookie.Value
|
||||
if sessionString != "" {
|
||||
settings := configuration.GetServerSettings()
|
||||
defer configuration.ReleaseAndSave()
|
||||
defer configuration.Release()
|
||||
_, ok := (settings.Sessions)[sessionString]
|
||||
if ok {
|
||||
return useSession(w, sessionString, &settings.Sessions)
|
||||
|
||||
Reference in New Issue
Block a user