mirror of
https://github.com/Forceu/Gokapi.git
synced 2026-02-14 12:38:45 -06:00
Fixed setup if no config directory exists
Remove temp files after upload Set temp directory for docker to data directory by default #37 Increase default memory to 40 Added TMPDIR env Update doc
This commit is contained in:
@@ -57,19 +57,26 @@ Available environment variables
|
||||
==================================
|
||||
|
||||
|
||||
+---------------------+------------------------------------------------------------------+-------------+----------------+
|
||||
| Name | Action | Persistent* | Default |
|
||||
+=====================+==================================================================+=============+================+
|
||||
| GOKAPI_CONFIG_DIR | Sets the directory for the config file | No | config |
|
||||
+---------------------+------------------------------------------------------------------+-------------+----------------+
|
||||
| GOKAPI_CONFIG_FILE | Sets the name of the config file | No | config.json |
|
||||
+---------------------+------------------------------------------------------------------+-------------+----------------+
|
||||
| GOKAPI_DATA_DIR | Sets the directory for the data | Yes | data |
|
||||
+---------------------+------------------------------------------------------------------+-------------+----------------+
|
||||
| GOKAPI_LENGTH_ID | Sets the length of the download IDs. Value needs to be 5 or more | Yes | 15 |
|
||||
+---------------------+------------------------------------------------------------------+-------------+----------------+
|
||||
| GOKAPI_MAX_FILESIZE | Sets the maximum allowed file size in MB | Yes | 102400 (100GB) |
|
||||
+---------------------+------------------------------------------------------------------+-------------+----------------+
|
||||
+--------------------------+------------------------------------------------------------------------------+-------------+-----------------------------+
|
||||
| Name | Action | Persistent* | Default |
|
||||
+==========================+==============================================================================+=============+=============================+
|
||||
| GOKAPI_CONFIG_DIR | Sets the directory for the config file | No | config |
|
||||
+--------------------------+------------------------------------------------------------------------------+-------------+-----------------------------+
|
||||
| GOKAPI_CONFIG_FILE | Sets the name of the config file | No | config.json |
|
||||
+--------------------------+------------------------------------------------------------------------------+-------------+-----------------------------+
|
||||
| GOKAPI_DATA_DIR | Sets the directory for the data | Yes | data |
|
||||
+--------------------------+------------------------------------------------------------------------------+-------------+-----------------------------+
|
||||
| GOKAPI_LENGTH_ID | Sets the length of the download IDs. Value needs to be 5 or more | Yes | 15 |
|
||||
+--------------------------+------------------------------------------------------------------------------+-------------+-----------------------------+
|
||||
| GOKAPI_MAX_FILESIZE | Sets the maximum allowed file size in MB | Yes | 102400 (100GB) |
|
||||
+--------------------------+------------------------------------------------------------------------------+-------------+-----------------------------+
|
||||
| GOKAPI_MAX_MEMORY_UPLOAD | Sets the amount of RAM in MB that can be allocated for an upload. | Yes | 20 |
|
||||
| | Any upload with a size greater than that will be written to a temporary file | | |
|
||||
+--------------------------+------------------------------------------------------------------------------+-------------+-----------------------------+
|
||||
| TMPDIR | Sets the path which contains temporary files | No | Non-Docker: Default OS path |
|
||||
| | | | Docker: [DATA_DIR] |
|
||||
+--------------------------+------------------------------------------------------------------------------+-------------+-----------------------------+
|
||||
|
||||
|
||||
\* Variables that are persistent must be submitted during the first start when Gokapi creates a new config file. They can be omitted afterwards. Non-persistent variables need to be set on every start.
|
||||
|
||||
|
||||
@@ -40,7 +40,6 @@ func Exists() bool {
|
||||
// Load loads the configuration or creates the folder structure and a default configuration
|
||||
func Load() {
|
||||
Environment = environment.New()
|
||||
helper.CreateDir(Environment.ConfigDir)
|
||||
// No check if file exists, as this was checked earlier
|
||||
file, err := os.Open(Environment.ConfigPath)
|
||||
helper.Check(err)
|
||||
@@ -110,6 +109,7 @@ func save() {
|
||||
|
||||
func LoadFromSetup(config models.Configuration, cloudConfig *cloudconfig.CloudConfig, isInitialConfig bool) {
|
||||
Environment = environment.New()
|
||||
helper.CreateDir(Environment.ConfigDir)
|
||||
if !isInitialConfig {
|
||||
Load()
|
||||
config.DefaultDownloads = serverSettings.DefaultDownloads
|
||||
|
||||
@@ -27,18 +27,21 @@ type Environment struct {
|
||||
}
|
||||
|
||||
var defaultValues = defaultsEnvironment{
|
||||
CONFIG_DIR: "config",
|
||||
CONFIG_FILE: "config.json",
|
||||
DATA_DIR: "data",
|
||||
PORT: strconv.Itoa(DefaultPort),
|
||||
LENGTH_ID: 15,
|
||||
MAX_MEMORY_UPLOAD_MB: 20,
|
||||
MAX_FILESIZE: 102400, // 100GB
|
||||
CONFIG_DIR: "config",
|
||||
CONFIG_FILE: "config.json",
|
||||
DATA_DIR: "data",
|
||||
PORT: strconv.Itoa(DefaultPort),
|
||||
LENGTH_ID: 15,
|
||||
MAX_MEMORY_UPLOAD: 40,
|
||||
MAX_FILESIZE: 102400, // 100GB
|
||||
}
|
||||
|
||||
// New parses the env variables
|
||||
func New() Environment {
|
||||
configPath, configDir, configFile, _ := GetConfigPaths()
|
||||
if IsDocker == "true" && os.Getenv("TMPDIR") == "" {
|
||||
os.Setenv("TMPDIR", envString("DATA_DIR"))
|
||||
}
|
||||
return Environment{
|
||||
ConfigDir: configDir,
|
||||
ConfigFile: configFile,
|
||||
@@ -46,7 +49,7 @@ func New() Environment {
|
||||
WebserverPort: GetPort(),
|
||||
DataDir: envString("DATA_DIR"),
|
||||
LengthId: envInt("LENGTH_ID", 5),
|
||||
MaxMemory: envInt("MAX_MEMORY_UPLOAD_MB", 5),
|
||||
MaxMemory: envInt("MAX_MEMORY_UPLOAD", 5),
|
||||
MaxFileSize: envInt("MAX_FILESIZE", 1),
|
||||
AwsBucket: envString("AWS_BUCKET"),
|
||||
AwsRegion: envString("AWS_REGION"),
|
||||
@@ -121,11 +124,11 @@ func (structPointer *defaultsEnvironment) getInt(name string) int {
|
||||
}
|
||||
|
||||
type defaultsEnvironment struct {
|
||||
CONFIG_DIR string
|
||||
CONFIG_FILE string
|
||||
DATA_DIR string
|
||||
PORT string
|
||||
LENGTH_ID int
|
||||
MAX_MEMORY_UPLOAD_MB int
|
||||
MAX_FILESIZE int
|
||||
CONFIG_DIR string
|
||||
CONFIG_FILE string
|
||||
DATA_DIR string
|
||||
PORT string
|
||||
LENGTH_ID int
|
||||
MAX_MEMORY_UPLOAD int
|
||||
MAX_FILESIZE int
|
||||
}
|
||||
|
||||
@@ -9,6 +9,22 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
|
||||
func TestTempDir(t *testing.T) {
|
||||
test.IsEqualString(t,os.Getenv("TMPDIR"),"")
|
||||
New()
|
||||
test.IsEqualString(t,os.Getenv("TMPDIR"),"")
|
||||
IsDocker = "true"
|
||||
New()
|
||||
test.IsEqualString(t,os.Getenv("TMPDIR"),"data")
|
||||
os.Setenv("TMPDIR","test")
|
||||
New()
|
||||
test.IsEqualString(t,os.Getenv("TMPDIR"),"test")
|
||||
os.Unsetenv("TMPDIR")
|
||||
IsDocker="false"
|
||||
}
|
||||
|
||||
|
||||
func TestEnvLoad(t *testing.T) {
|
||||
os.Setenv("GOKAPI_CONFIG_DIR", "test")
|
||||
os.Setenv("GOKAPI_CONFIG_FILE", "test2")
|
||||
@@ -29,6 +45,7 @@ func TestEnvLoad(t *testing.T) {
|
||||
test.IsEqualInt(t, env.LengthId, -1)
|
||||
}
|
||||
|
||||
|
||||
func TestIsAwsProvided(t *testing.T) {
|
||||
os.Unsetenv("GOKAPI_AWS_BUCKET")
|
||||
os.Unsetenv("GOKAPI_AWS_REGION")
|
||||
|
||||
@@ -117,7 +117,7 @@ func generateHash(fileContent io.Reader, fileHeader *multipart.FileHeader, uploa
|
||||
helper.Check(err)
|
||||
_, err = tempFile.Seek(0, io.SeekStart)
|
||||
helper.Check(err)
|
||||
// Instead of returning a reference to the file as the 3rd result, one could use reflections. However that would be more expensive.
|
||||
// Instead of returning a reference to the file as the 3rd result, one could use reflections. However, that would be more expensive.
|
||||
return tempFile, hash.Sum(nil), tempFile
|
||||
}
|
||||
|
||||
|
||||
@@ -34,9 +34,9 @@ func Process(w http.ResponseWriter, r *http.Request, isWeb bool, maxMemory int)
|
||||
}
|
||||
defer file.Close()
|
||||
_, err = io.WriteString(w, result.ToJsonResult(config.ExternalUrl))
|
||||
if err != nil {
|
||||
helper.Check(err)
|
||||
}
|
||||
helper.Check(err)
|
||||
err = r.MultipartForm.RemoveAll()
|
||||
helper.Check(err)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user