2019-05-24 02:58:46 +00:00
|
|
|
package metadata
|
2019-05-20 04:12:40 +00:00
|
|
|
|
|
|
|
import (
|
2019-08-04 04:24:05 +00:00
|
|
|
"github.com/mitchellh/go-homedir"
|
2019-05-20 04:12:40 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
macOSDir = "/Library/Application Support/Sampler"
|
|
|
|
linuxDir = "/.config/Sampler"
|
2019-07-29 00:32:15 +00:00
|
|
|
windowsDir = "Sampler"
|
2019-05-20 04:12:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func fileExists(filename string) bool {
|
|
|
|
_, err := os.Stat(getPlatformStoragePath(filename))
|
|
|
|
return !os.IsNotExist(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getPlatformStoragePath(filename string) string {
|
|
|
|
switch runtime.GOOS {
|
|
|
|
case "darwin":
|
2019-08-04 04:48:46 +00:00
|
|
|
home, _ := homedir.Dir()
|
2019-05-20 04:12:40 +00:00
|
|
|
return filepath.Join(home, macOSDir, filename)
|
|
|
|
case "windows":
|
2019-07-29 00:32:15 +00:00
|
|
|
cache, _ := os.UserCacheDir()
|
|
|
|
return filepath.Join(cache, windowsDir, filename)
|
2019-05-20 04:12:40 +00:00
|
|
|
default:
|
2019-08-04 04:24:05 +00:00
|
|
|
home, _ := homedir.Dir()
|
2019-08-03 01:45:46 +00:00
|
|
|
return filepath.Join(home, linuxDir, filename)
|
2019-05-20 04:12:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func initStorage() {
|
|
|
|
err := os.MkdirAll(getPlatformStoragePath(""), os.ModePerm)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to init storage: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func readStorageFile(path string) []byte {
|
|
|
|
|
|
|
|
file, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to the read storage file: %s", path)
|
|
|
|
}
|
|
|
|
|
|
|
|
return file
|
|
|
|
}
|
|
|
|
|
|
|
|
func saveStorageFile(file []byte, fileName string) {
|
2019-05-21 04:41:03 +00:00
|
|
|
err := ioutil.WriteFile(getPlatformStoragePath(fileName), file, os.ModePerm)
|
2019-05-20 04:12:40 +00:00
|
|
|
if err != nil {
|
2019-05-28 01:44:06 +00:00
|
|
|
log.Fatalf("Failed to save the storage file: %s %v", fileName, err)
|
2019-05-20 04:12:40 +00:00
|
|
|
}
|
|
|
|
}
|