Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Svg Helper function with Read Write mutex w/signoff #581

Closed
wants to merge 4 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions models/registration/svg_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,22 @@ import (
)

var hashCheckSVG = make(map[string]string)
var mx sync.Mutex
var mx sync.RWMutex // Changed to RWMutex
var UISVGPaths = make([]string, 1)

func writeHashCheckSVG(key string, val string) {
mx.Lock()
mx.Lock() // Lock for writing
hashCheckSVG[key] = val
mx.Unlock()
}

func readHashCheckSVG(key string) (val string, ok bool) {
mx.RLock() // Lock for reading
val, ok = hashCheckSVG[key]
mx.RUnlock()
return
}

func WriteAndReplaceSVGWithFileSystemPath(svgColor, svgWhite, svgComplete string, baseDir, dirname, filename string) (svgColorPath, svgWhitePath, svgCompletePath string) {
filename = strings.ToLower(filename)
successCreatingDirectory := false
Expand All @@ -39,8 +46,7 @@ func WriteAndReplaceSVGWithFileSystemPath(svgColor, svgWhite, svgComplete string

hash := md5.Sum([]byte(svgColor))
hashString := hex.EncodeToString(hash[:])
pathsvg := hashCheckSVG[hashString]
if pathsvg != "" { // the image has already been loaded, point the component to that path
if pathsvg, exists := readHashCheckSVG(hashString); exists { // the image has already been loaded, point the component to that path
svgColorPath = pathsvg
goto White
}
Expand All @@ -54,9 +60,8 @@ func WriteAndReplaceSVGWithFileSystemPath(svgColor, svgWhite, svgComplete string
fmt.Println(err)
return
}
svgColorPath = getRelativePathForAPI(baseDir, filepath.Join(dirname, "color", filename+"-color.svg")) //Replace the actual SVG with path to SVG
svgColorPath = getRelativePathForAPI(baseDir, filepath.Join(dirname, "color", filename+"-color.svg"))
writeHashCheckSVG(hashString, svgColorPath)

}
White:
if svgWhite != "" {
Expand All @@ -70,8 +75,7 @@ White:

hash := md5.Sum([]byte(svgWhite))
hashString := hex.EncodeToString(hash[:])
pathsvg := hashCheckSVG[hashString]
if pathsvg != "" { // the image has already been loaded, point the component to that path
if pathsvg, exists := readHashCheckSVG(hashString); exists { // the image has already been loaded, point the component to that path
svgWhitePath = pathsvg
goto Complete
}
Expand All @@ -85,9 +89,8 @@ White:
fmt.Println(err)
return
}
svgWhitePath = getRelativePathForAPI(baseDir, filepath.Join(dirname, "white", filename+"-white.svg")) //Replace the actual SVG with path to SVG
svgWhitePath = getRelativePathForAPI(baseDir, filepath.Join(dirname, "white", filename+"-white.svg"))
writeHashCheckSVG(hashString, svgWhitePath)

}
Complete:
if svgComplete != "" {
Expand All @@ -101,8 +104,7 @@ Complete:

hash := md5.Sum([]byte(svgComplete))
hashString := hex.EncodeToString(hash[:])
pathsvg := hashCheckSVG[hashString]
if pathsvg != "" { // the image has already been loaded, point the component to that path
if pathsvg, exists := readHashCheckSVG(hashString); exists { // the image has already been loaded, point the component to that path
svgCompletePath = pathsvg
return
}
Expand All @@ -116,9 +118,8 @@ Complete:
fmt.Println(err)
return
}
svgCompletePath = getRelativePathForAPI(baseDir, filepath.Join(dirname, "complete", filename+"-complete.svg")) //Replace the actual SVG with path to SVG
svgCompletePath = getRelativePathForAPI(baseDir, filepath.Join(dirname, "complete", filename+"-complete.svg"))
writeHashCheckSVG(hashString, svgCompletePath)

}
return
}
Expand Down
Loading