Skip to content
This repository has been archived by the owner on Feb 21, 2024. It is now read-only.

Commit

Permalink
Create getDefaultTempDir and getValidTempDir functions. Closes xteve-…
Browse files Browse the repository at this point in the history
  • Loading branch information
SCP002 committed Apr 4, 2022
1 parent ba7bbe3 commit ac269c1
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 25 deletions.
5 changes: 1 addition & 4 deletions src/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,6 @@ func Init() (err error) {
System.Update.Name = "xteve_2"

// Define folder paths
var tempFolder = os.TempDir() + string(os.PathSeparator) + System.AppName + string(os.PathSeparator)
tempFolder = getPlatformPath(strings.Replace(tempFolder, "//", "/", -1))

if len(System.Folder.Config) == 0 {
System.Folder.Config = GetUserHomeDirectory() + string(os.PathSeparator) + "." + System.AppName + string(os.PathSeparator)
} else {
Expand All @@ -79,7 +76,7 @@ func Init() (err error) {
System.Folder.Cache = System.Folder.Config + "cache" + string(os.PathSeparator)
System.Folder.ImagesCache = System.Folder.Cache + "images" + string(os.PathSeparator)
System.Folder.ImagesUpload = System.Folder.Data + "images" + string(os.PathSeparator)
System.Folder.Temp = tempFolder
System.Folder.Temp = getDefaultTempDir()

// Dev Info
showDevInfo()
Expand Down
20 changes: 1 addition & 19 deletions src/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,25 +86,7 @@ func updateServerSettings(request RequestStruct) (settings SettingsStruct, err e
}

case "temp.path":
value = strings.TrimRight(value.(string), string(os.PathSeparator)) + string(os.PathSeparator)
err = checkFolder(value.(string))
if err == nil {

err = checkFilePermission(value.(string))
if err != nil {
return
}

}

if err != nil {
ShowError(err, 1015)
value = os.TempDir() + string(os.PathSeparator)
err = checkFilePermission(value.(string))
if err != nil {
return
}
}
value = getValidTempDir(value.(string))

case "ffmpeg.path", "vlc.path":
var path = value.(string)
Expand Down
5 changes: 3 additions & 2 deletions src/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"errors"
"fmt"
"os"
"reflect"
"strings"
"time"
Expand Down Expand Up @@ -170,6 +169,8 @@ func loadSettings() (settings SettingsStruct, err error) {
settings.VLCPath = searchFileInOS("cvlc")
}

settings.TempPath = getValidTempDir(settings.TempPath)

err = saveSettings(settings)

// Warning if FFmpeg was not found
Expand Down Expand Up @@ -203,7 +204,7 @@ func saveSettings(settings SettingsStruct) (err error) {
Settings.UUID = "2019-01-DEV-xTeVe!"
}

System.Folder.Temp = settings.TempPath + settings.UUID + string(os.PathSeparator)
System.Folder.Temp = getValidTempDir(settings.TempPath + settings.UUID)

err = writeByteToFile(System.File.Settings, []byte(mapToJSON(settings)))
if err != nil {
Expand Down
34 changes: 34 additions & 0 deletions src/toolchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,40 @@ func getPlatformPath(path string) string {
return filepath.Dir(path) + string(os.PathSeparator)
}

// getDefaultTempDir returns default temporary folder path + application name, e.g.: "/tmp/xteve/" or %Tmp%\xteve.
//
// Function assumes default OS temporary folder exists and writable.
func getDefaultTempDir() string {
return os.TempDir() + string(os.PathSeparator) + System.AppName + string(os.PathSeparator)
}

// getValidTempDir returns standartized temporary folder <path> with trailing path separator:
//
// Slashes will be replaced with OS specific ones and duplicated slashes removed.
//
// On Windows, "/tmp" will be replaced with expanded system environment variable %Tmp%.
func getValidTempDir(path string) string {
if runtime.GOOS == "windows" {
if strings.HasPrefix(path, "/tmp") {
path = strings.Replace(path, "/tmp", os.TempDir(), 1)
}
}
path = filepath.Clean(path)
path = path + string(os.PathSeparator)

err := checkFolder(path)
if err == nil {
err = checkFilePermission(path)
}

if err != nil {
ShowError(err, 1015)
path = getDefaultTempDir()
}

return path
}

// Generate File Path for the running OS
func getPlatformFile(filename string) (osFilePath string) {

Expand Down

0 comments on commit ac269c1

Please sign in to comment.