Skip to content

Commit

Permalink
Fixed crash after running setup and changing encryption #197, changed…
Browse files Browse the repository at this point in the history
… versioning of css/js files to prevent caching #195
  • Loading branch information
Forceu committed Jul 26, 2024
1 parent 673f4b3 commit f08490e
Show file tree
Hide file tree
Showing 16 changed files with 191 additions and 95 deletions.
70 changes: 50 additions & 20 deletions build/go-generate/minifyStaticContent.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@ import (
"github.com/tdewolff/minify/v2/js"
"os"
"path/filepath"
"strconv"
)

const pathPrefix = "../../internal/webserver/web/static/"

type converter struct {
InputPath string
OutputPath string
Type string
Name string
InputPath string
OutputPath string
PreviousVersion string
Type string
Name string
}

func main() {
Expand All @@ -29,28 +31,32 @@ func main() {
func getPaths() []converter {
var result []converter
result = append(result, converter{
InputPath: pathPrefix + "css/*.css",
OutputPath: pathPrefix + "css/min/gokapi.min.css",
Type: "text/css",
Name: "Main CSS",
InputPath: pathPrefix + "css/*.css",
OutputPath: pathPrefix + "css/min/gokapi.min." + strconv.Itoa(cssMainVersion) + ".css",
PreviousVersion: pathPrefix + "css/min/gokapi.min." + strconv.Itoa(cssMainVersion-1) + ".css",
Type: "text/css",
Name: "Main CSS",
})
result = append(result, converter{
InputPath: pathPrefix + "js/admin.js",
OutputPath: pathPrefix + "js/min/admin.min.js",
Type: "text/javascript",
Name: "Admin JS",
InputPath: pathPrefix + "js/admin.js",
OutputPath: pathPrefix + "js/min/admin.min." + strconv.Itoa(jsAdminVersion) + ".js",
PreviousVersion: pathPrefix + "js/min/admin.min." + strconv.Itoa(jsAdminVersion-1) + ".js",
Type: "text/javascript",
Name: "Admin JS",
})
result = append(result, converter{
InputPath: pathPrefix + "js/end2end_admin.js",
OutputPath: pathPrefix + "js/min/end2end_admin.min.js",
Type: "text/javascript",
Name: "Admin E2E JS",
InputPath: pathPrefix + "js/end2end_admin.js",
OutputPath: pathPrefix + "js/min/end2end_admin.min." + strconv.Itoa(jsE2EVersion) + ".js",
PreviousVersion: pathPrefix + "js/min/end2end_admin.min." + strconv.Itoa(jsE2EVersion-1) + ".js",
Type: "text/javascript",
Name: "Admin E2E JS",
})
result = append(result, converter{
InputPath: pathPrefix + "js/end2end_download.js",
OutputPath: pathPrefix + "js/min/end2end_download.min.js",
Type: "text/javascript",
Name: "Download E2E JS",
InputPath: pathPrefix + "js/end2end_download.js",
OutputPath: pathPrefix + "js/min/end2end_download.min." + strconv.Itoa(jsE2EVersion) + ".js",
PreviousVersion: pathPrefix + "js/min/end2end_download.min." + strconv.Itoa(jsE2EVersion-1) + ".js",
Type: "text/javascript",
Name: "Download E2E JS",
})
result = append(result, converter{
InputPath: pathPrefix + "js/streamsaver.js",
Expand Down Expand Up @@ -84,6 +90,15 @@ func minifyContent(conv converter) {
os.Exit(5)
}
fmt.Println("Minified " + conv.Name)
if conv.PreviousVersion != "" && fileExists(conv.PreviousVersion) {
fmt.Println("Removing old version of " + conv.Name)
err = os.Remove(conv.PreviousVersion)
if err != nil {
fmt.Println("Could not remove old " + conv.Name + " file")
fmt.Println(err)
os.Exit(6)
}
}
}

func getAllFiles(pattern string) []byte {
Expand All @@ -110,3 +125,18 @@ func getAllFiles(pattern string) []byte {
}
return result
}

func fileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}

// Auto-generated content below, do not modify
// Version codes can be changed in updateVersionNumbers.go

const jsAdminVersion = 5
const jsE2EVersion = 3
const cssMainVersion = 2
85 changes: 72 additions & 13 deletions build/go-generate/updateVersionNumbers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,79 @@ import (
"fmt"
"os"
"regexp"
"strconv"
"strings"
)

const versionJsAdmin = "5"
const versionJsDropzone = "4"
const versionJsE2EAdmin = "3"
const versionCssMain = "2"
const versionJsAdmin = 5
const versionJsDropzone = 4
const versionJsE2EAdmin = 3
const versionCssMain = 2

const fileMain = "../../cmd/gokapi/Main.go"
const fileMinify = "../../build/go-generate/minifyStaticContent.go"
const fileVersionConstants = "../../internal/webserver/web/templates/string_constants.tmpl"

func main() {
checkFileExists(fileMain)
checkFileExists(fileMinify)
checkFileExists(fileVersionConstants)
template := getTemplate()
writeVersionTemplates()
writeMinify()
}

func writeVersionTemplates() {
template := insertVersionNumbers(templateVersions)
err := os.WriteFile(fileVersionConstants, []byte(template), 0664)
if err != nil {
fmt.Println("FAIL: Updating version numbers")
fmt.Println("FAIL: Updating version template")
fmt.Println(err)
os.Exit(1)
}
fmt.Println("Updated version template")
}

func writeMinify() {
file, err := os.Open(fileMinify)
if err != nil {
fmt.Println("FAIL: Opening minify go file")
fmt.Println(err)
os.Exit(1)
}
defer file.Close()

var lines []string
scanner := bufio.NewScanner(file)
foundAutoGencomment := false
for scanner.Scan() {
line := scanner.Text()
lines = append(lines, line)
if strings.Contains(line, autoGenComment) {
foundAutoGencomment = true
break
}
}
err = scanner.Err()
if err != nil {
fmt.Println("FAIL: Reading minify go file")
fmt.Println(err)
os.Exit(1)
}
fmt.Println("Updated version numbers")

if !foundAutoGencomment {
fmt.Println("FAIL: Minify go file did not contain auto-gen comment")
fmt.Println(err)
os.Exit(2)
}
lines = append(lines, insertVersionNumbers(templateMinify))

err = os.WriteFile(fileMinify, []byte(strings.Join(lines, "\n")), 0664)
if err != nil {
fmt.Println("FAIL: Wrining minify go file")
fmt.Println(err)
os.Exit(3)
}
fmt.Println("Updated minify go file")
}

func checkFileExists(filename string) {
Expand All @@ -43,13 +94,13 @@ func checkFileExists(filename string) {
}
}

func getTemplate() string {
func insertVersionNumbers(input string) string {
versionGokapi := parseGokapiVersion()
result := strings.ReplaceAll(templateVersions, "%gokapiversion%", versionGokapi)
result = strings.ReplaceAll(result, "%jsadmin%", versionJsAdmin)
result = strings.ReplaceAll(result, "%jsdropzone%", versionJsDropzone)
result = strings.ReplaceAll(result, "%jse2e%", versionJsE2EAdmin)
result = strings.ReplaceAll(result, "%css_main%", versionCssMain)
result := strings.ReplaceAll(input, "%gokapiversion%", versionGokapi)
result = strings.ReplaceAll(result, "%jsadmin%", strconv.Itoa(versionJsAdmin))
result = strings.ReplaceAll(result, "%jsdropzone%", strconv.Itoa(versionJsDropzone))
result = strings.ReplaceAll(result, "%jse2e%", strconv.Itoa(versionJsE2EAdmin))
result = strings.ReplaceAll(result, "%css_main%", strconv.Itoa(versionCssMain))
return result
}

Expand Down Expand Up @@ -85,3 +136,11 @@ const templateVersions = `// File contains auto-generated values. Do not change
{{define "js_dropzone_version"}}%jsdropzone%{{end}}
{{define "js_e2eversion"}}%jse2e%{{end}}
{{define "css_main"}}%css_main%{{end}}`

const autoGenComment = "// Auto-generated content below, do not modify"
const templateMinify = `// Version codes can be changed in updateVersionNumbers.go
const jsAdminVersion = %jsadmin%
const jsE2EVersion = %jse2e%
const cssMainVersion = %css_main%
`
24 changes: 23 additions & 1 deletion internal/configuration/Configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func save() {

// LoadFromSetup creates a new configuration file after a user completed the setup. If cloudConfig is not nil, a new
// cloud config file is created. If it is nil an existing cloud config file will be deleted.
func LoadFromSetup(config models.Configuration, cloudConfig *cloudconfig.CloudConfig, isInitialSetup bool) {
func LoadFromSetup(config models.Configuration, cloudConfig *cloudconfig.CloudConfig, e2eConfig End2EndReconfigParameters) {
Environment = environment.New()
helper.CreateDir(Environment.ConfigDir)

Expand All @@ -154,6 +154,23 @@ func LoadFromSetup(config models.Configuration, cloudConfig *cloudconfig.CloudCo
Load()
ConnectDatabase()
database.DeleteAllSessions()
if e2eConfig.DeleteEnd2EndEncryption {
database.DeleteEnd2EndInfo()
}
if e2eConfig.DeleteEncryptedStorage {
deleteAllEncryptedStorage()
}
}

func deleteAllEncryptedStorage() {
files := database.GetAllMetadata()
for _, file := range files {
if file.Encryption.IsEncrypted {
file.UnlimitedTime = false
file.ExpireAt = 0
database.SaveMetaData(file)
}
}
}

// SetDeploymentPassword sets a new password. This should only be used for non-interactive deployment, but is not enforced
Expand Down Expand Up @@ -199,3 +216,8 @@ func HashPasswordCustomSalt(password, salt string) string {
hash.Write(pwBytes)
return hex.EncodeToString(hash.Sum(nil))
}

type End2EndReconfigParameters struct {
DeleteEnd2EndEncryption bool
DeleteEncryptedStorage bool
}
4 changes: 2 additions & 2 deletions internal/configuration/Configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ func TestLoadFromSetup(t *testing.T) {
}}

testconfiguration.WriteCloudConfigFile(true)
LoadFromSetup(newConfig, nil, false)
LoadFromSetup(newConfig, nil, End2EndReconfigParameters{})
test.FileDoesNotExist(t, "test/cloudconfig.yml")
test.IsEqualString(t, serverSettings.RedirectUrl, "redirect")

LoadFromSetup(newConfig, &newCloudConfig, false)
LoadFromSetup(newConfig, &newCloudConfig, End2EndReconfigParameters{})
test.FileExists(t, "test/cloudconfig.yml")
config, ok := cloudconfig.Load()
test.IsEqualBool(t, ok, true)
Expand Down
Loading

0 comments on commit f08490e

Please sign in to comment.