use read file helper function everywhere
This commit is contained in:
17
src/main/helpers.go
Normal file
17
src/main/helpers.go
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"astraltech.xyz/accountmanager/src/logging"
|
||||||
|
)
|
||||||
|
|
||||||
|
func ReadFile(path string) ([]byte, error) {
|
||||||
|
logging.Event(logging.ReadFile, "static/blank_profile.jpg")
|
||||||
|
data, err := os.ReadFile(path)
|
||||||
|
if err != nil {
|
||||||
|
logging.Infof("Could not read file at %s", path)
|
||||||
|
}
|
||||||
|
logging.Infof("Successfully read file at %s", path)
|
||||||
|
return data, err
|
||||||
|
}
|
||||||
@@ -172,7 +172,7 @@ func avatarHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
filePath := fmt.Sprintf("./avatars/%s.jpeg", username)
|
filePath := fmt.Sprintf("./avatars/%s.jpeg", username)
|
||||||
cleaned := filepath.Clean(filePath)
|
cleaned := filepath.Clean(filePath)
|
||||||
value, err := os.ReadFile(cleaned)
|
value, err := ReadFile(cleaned)
|
||||||
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
photoCreatedMutex.Lock()
|
photoCreatedMutex.Lock()
|
||||||
@@ -307,18 +307,10 @@ func cleanupSessions() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func ReadFile(path string) ([]byte, error) {
|
|
||||||
logging.Event(logging.ReadFile, "static/blank_profile.jpg")
|
|
||||||
data, err := os.ReadFile(path)
|
|
||||||
logging.Infof("Successfully read file at %s", path)
|
|
||||||
return data, err
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
logging.Info("Starting the server")
|
logging.Info("Starting the server")
|
||||||
|
|
||||||
var err error = nil
|
var err error = nil
|
||||||
|
|
||||||
blankPhotoData, err = ReadFile("static/blank_profile.jpg")
|
blankPhotoData, err = ReadFile("static/blank_profile.jpg")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Could not load blank profile image")
|
log.Fatal("Could not load blank profile image")
|
||||||
|
|||||||
Reference in New Issue
Block a user