Skip to content

Commit

Permalink
fixing creating cache dir
Browse files Browse the repository at this point in the history
  • Loading branch information
revengel committed Jan 2, 2023
1 parent 83115da commit 00675e0
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions application.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,35 @@ func getAppInfoFromServer(id uint) (app Application, err error) {
return app, nil
}

// getImagePath -
func getImagePath(id uint, imagePath string) string {
func getCacheDirPath() string {
var cacheDir = os.Getenv("XDG_CACHE_HOME")
if cacheDir == "" {
cacheDir = filepath.Join(os.Getenv("HOME"), ".cache")
}
cacheDir = filepath.Join(cacheDir, "gotify-notify", "apps-images")
cacheDir = filepath.Join(cacheDir, "gotify-notify")
return cacheDir
}

func getAppImagesDirPath() string {
out := getCacheDirPath()
return filepath.Join(out, "apps-images")
}

func createAppImagesDir() error {
var dir = getAppImagesDirPath()
_, err := os.Stat(dir)
switch {
case err == nil:
return nil
case err != nil && !os.IsNotExist(err):
return err
}
return os.MkdirAll(dir, 0777)
}

// getImagePath -
func getImagePath(id uint, imagePath string) string {
var cacheDir = getAppImagesDirPath()
var ext = filepath.Ext(imagePath)
var fileName = fmt.Sprintf("app-%d%s", id, ext)
return filepath.Join(cacheDir, fileName)
Expand Down Expand Up @@ -189,7 +211,7 @@ func saveAppImage(id uint, appImagePath string) (filePath string, err error) {
return
}

err = os.MkdirAll(filepath.Dir(filePath), 0644)
err = createAppImagesDir()
if err != nil {
return
}
Expand Down

0 comments on commit 00675e0

Please sign in to comment.