From e430bfa01fd3a4caa9a7087522b51d13b7a7d30d Mon Sep 17 00:00:00 2001 From: benny Date: Sun, 30 Jun 2024 15:36:33 +0300 Subject: [PATCH] workign on the yamls --- YamlTemplates/template.installer.yaml | 13 +++++ YamlTemplates/template.locale.en-US.yaml | 13 +++++ YamlTemplates/template.yaml | 9 +++ fileOrginize.go | 30 ++++++++++ html/up.html | 2 +- html/uploadfile.html | 4 +- main.go | 28 ++++++++- packages.go | 74 ++++++++++++++---------- public/script.js | 5 ++ 9 files changed, 143 insertions(+), 35 deletions(-) create mode 100644 YamlTemplates/template.installer.yaml create mode 100644 YamlTemplates/template.locale.en-US.yaml create mode 100644 YamlTemplates/template.yaml create mode 100644 fileOrginize.go diff --git a/YamlTemplates/template.installer.yaml b/YamlTemplates/template.installer.yaml new file mode 100644 index 0000000..fc29bdc --- /dev/null +++ b/YamlTemplates/template.installer.yaml @@ -0,0 +1,13 @@ +# Created using wingetcreate 1.5.7.0 +# yaml-language-server: $schema=https://aka.ms/winget-manifest.installer.1.5.0.schema.json +{{define "installer"}} +PackageIdentifier: {{.PID}} +PackageVersion: {{.VER}} +InstallerType: {{.TYPE}} +Installers: +- Architecture: x64 + InstallerUrl: {{.URL}} + InstallerSha256: {{.HASH}} +ManifestType: installer +ManifestVersion: 1.6.0 +{{end}} \ No newline at end of file diff --git a/YamlTemplates/template.locale.en-US.yaml b/YamlTemplates/template.locale.en-US.yaml new file mode 100644 index 0000000..a6bc929 --- /dev/null +++ b/YamlTemplates/template.locale.en-US.yaml @@ -0,0 +1,13 @@ +# Created using wingetcreate 1.5.7.0 +# yaml-language-server: $schema=https://aka.ms/winget-manifest.defaultLocale.1.5.0.schema.json +{{define "locale"}} +PackageIdentifier: {{.PID}} +PackageVersion: {{.VER}} +PackageLocale: en-US +Publisher: {{.PUB}} +PackageName: {{.NAME}} +License: MIT +ShortDescription: {{.NAME}} +ManifestType: defaultLocale +ManifestVersion: 1.6.0 +{{end}} \ No newline at end of file diff --git a/YamlTemplates/template.yaml b/YamlTemplates/template.yaml new file mode 100644 index 0000000..9f99dc8 --- /dev/null +++ b/YamlTemplates/template.yaml @@ -0,0 +1,9 @@ +# Created using wingetcreate 1.5.7.0 +# yaml-language-server: $schema=https://aka.ms/winget-manifest.version.1.5.0.schema.json +{{define "yaml"}} +PackageIdentifier: {{.PID}} +PackageVersion: {{.VER}} +DefaultLocale: en-US +ManifestType: version +ManifestVersion: 1.6.0 +{{end}} \ No newline at end of file diff --git a/fileOrginize.go b/fileOrginize.go new file mode 100644 index 0000000..6377f26 --- /dev/null +++ b/fileOrginize.go @@ -0,0 +1,30 @@ +package main + +import ( + "html/template" + "log/slog" + "os" +) + +func CreateDirectoryInPackageDir(doc yaml) { + location := doc.LOCATION + slog.Debug("Creating the Directory: ", location) + direrr := os.MkdirAll(location, 0764) + if direrr != nil { + slog.Error(direrr.Error()) + } +} + +func CreateYaml(doc yaml) { + template, err := template.ParseFiles("YamlTemplates/template.yaml") + yamlFile, _:= os.OpenFile(doc.LOCATION+"/"+doc.PUB+"."+doc.NAME+".yaml", os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644) + slog.Debug("Create the basic yaml") + if err != nil { + slog.Error(err.Error()) + } + err = template.ExecuteTemplate(yamlFile, "yaml", doc) + if err != nil { + slog.Error(err.Error()) + + } +} diff --git a/html/up.html b/html/up.html index 60dbcdb..582e3ea 100644 --- a/html/up.html +++ b/html/up.html @@ -34,7 +34,7 @@ -
+ diff --git a/html/uploadfile.html b/html/uploadfile.html index d72c8b7..9292efc 100644 --- a/html/uploadfile.html +++ b/html/uploadfile.html @@ -2,7 +2,7 @@

Upload a File

- +
@@ -12,7 +12,7 @@
- +
diff --git a/main.go b/main.go index 216713c..fe76d1e 100644 --- a/main.go +++ b/main.go @@ -10,6 +10,7 @@ import ( "log/slog" "net/http" "os" + "regexp" "strings" "time" @@ -297,7 +298,7 @@ func upload() http.Handler { func createPackageAndRename() http.Handler{ return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request){ - + slog.Debug(req.Host) }) } // index creates an HTTP handler that serves the main page with data from the database. @@ -342,7 +343,31 @@ func index(sqldb *sql.DB, lang string) http.Handler { }) } +func renameForFile() http.Handler{ + return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) { + init:=req.FormValue("Vendor") + init=string(init[0]) + regexext:= regexp.MustCompile(`\.[\u\l]{1,5}$`) + extention:=regexext.FindString(req.FormValue("InstallerUrl")) + yamlmodel:=yaml{ + PUB: req.FormValue("Vendor"), + HASH: req.FormValue("InstallerSha256"), + URL: req.FormValue("InstallerUrl"), + NAME: req.FormValue("Package"), + PID: req.FormValue("Vendor")+"."+req.FormValue("Package"), + VER: req.FormValue("PackageVersion"), + INIT: init, + LOCATION: os.Getenv("packagesDirectory")+"/"+init+"/"+req.FormValue("Vendor")+"/"+req.FormValue("Package")+"/"+req.FormValue("PackageVersion"), + FILELOCATION: os.Getenv("installationDirectory")+"/"+req.FormValue("Vendor")+"."+req.FormValue("Package")+".v."+req.FormValue("PackageVersion")+extention , + } + CreateDirectoryInPackageDir(yamlmodel) + CreateYaml(yamlmodel) + // fur:= + + // slog.Debug(fur) + }) +} // public serves static files from the "public" directory. // Returns: An http.Handler that serves static files. func public() http.Handler { @@ -384,6 +409,7 @@ func main() { mux.Handle("/up", uploadFileForServer(db,"eng")) mux.Handle("/upload",upload()) mux.Handle("/pkg",getJson(db)) + mux.Handle("/ren",renameForFile()) addr := fmt.Sprintf(":%s", "12312") server := http.Server{ Addr: addr, diff --git a/packages.go b/packages.go index 0e4ec3e..ca2c307 100644 --- a/packages.go +++ b/packages.go @@ -3,29 +3,29 @@ package main import ( "crypto/sha256" "database/sql" + "encoding/hex" "fmt" + "html/template" "log/slog" "net/http" "os" "os/exec" - "encoding/hex" "regexp" "runtime" "strconv" "strings" - "html/template" _ "modernc.org/sqlite" ) type jsonStruct struct { - Vendor string - Package string - Version string - Type string - Location string - Checksum string - Url string + Vendor string + Package string + Version string + Type string + Location string + Checksum string + Url string } // packages struct represents a package with its details. @@ -38,6 +38,18 @@ type packages struct { Warning bool Element *elements } +type yaml struct { + PID string + VER string + TYPE string + URL string + HASH string + PUB string + NAME string + INIT string + LOCATION string + FILELOCATION string +} // ConvertStringsToPackage converts a slice of strings to a packages struct. // It returns an error if the input slice does not have exactly 6 elements or if the Warning value cannot be parsed as a boolean. @@ -145,14 +157,14 @@ func getVendor(data map[string]string) string { for key, val := range data { switch key { case "Author": - return strings.ToLower(strings.Replace(val," ","",-1)) + return strings.ToLower(strings.Replace(val, " ", "", -1)) case "Company Name": - return strings.ToLower(strings.Replace(val," ","",-1)) + return strings.ToLower(strings.Replace(val, " ", "", -1)) case "Manufacturer": - return strings.ToLower(strings.Replace(val," ","",-1)) + return strings.ToLower(strings.Replace(val, " ", "", -1)) case "Publisher": - return strings.ToLower(strings.Replace(val," ","",-1)) + return strings.ToLower(strings.Replace(val, " ", "", -1)) } } @@ -162,32 +174,32 @@ func getProgram(data map[string]string) string { for key, val := range data { switch key { case "Title": - return strings.ToLower(strings.Replace(val," ","",-1)) + return strings.ToLower(strings.Replace(val, " ", "", -1)) case "Product Name": - return strings.ToLower(strings.Replace(val," ","",-1)) + return strings.ToLower(strings.Replace(val, " ", "", -1)) case "ProductName": - return strings.ToLower(strings.Replace(val," ","",-1)) + return strings.ToLower(strings.Replace(val, " ", "", -1)) case "Subject": - return strings.ToLower(strings.Replace(val," ","",-1)) + return strings.ToLower(strings.Replace(val, " ", "", -1)) } } return "" } func getVersion(data map[string]string) string { - replacer:=regexp.MustCompile(`[, ]+`) + replacer := regexp.MustCompile(`[, ]+`) for key, val := range data { switch key { case "Version": - return replacer.ReplaceAllString(val,".") + return replacer.ReplaceAllString(val, ".") case "Product Version": - return replacer.ReplaceAllString(val,".") + return replacer.ReplaceAllString(val, ".") case "ProductVersion": - return replacer.ReplaceAllString(val,".") + return replacer.ReplaceAllString(val, ".") case "File Version": - return replacer.ReplaceAllString(val,".") + return replacer.ReplaceAllString(val, ".") case "FileVersion": - return replacer.ReplaceAllString(val,".") + return replacer.ReplaceAllString(val, ".") } } return "" @@ -198,7 +210,7 @@ func detectProblems(data map[string]string) bool { if !err || (exe == "exe" && !(mime == "application/octet-stream")) { return false } - if (exe != "msi" && exe != "exe" && exe != "zip") { + if exe != "msi" && exe != "exe" && exe != "zip" { return false } return true @@ -232,14 +244,14 @@ func moveToNewLocationAndName(data map[string]string) string { func createObject(object map[string]string) jsonStruct { if detectProblems(object) { prop := jsonStruct{ - Vendor: getVendor(object) , - Package: getProgram(object), - Version: getVersion(object), - Type: getInstallerType(object), + Vendor: getVendor(object), + Package: getProgram(object), + Version: getVersion(object), + Type: getInstallerType(object), } loc, _ := os.ReadFile(object["Directory"] + "/" + object["File Name"]) - chksum:=sha256.Sum256(loc) + chksum := sha256.Sum256(loc) prop.Checksum = hex.EncodeToString(chksum[:]) lc := moveToNewLocationAndName(object) prop.Url = os.Getenv("uri") + lc @@ -267,11 +279,11 @@ func getJson(sqldb *sql.DB) http.Handler { if err != nil { slog.Error(err.Error()) } - temp,err:=template.ParseFiles("html/lastob.json") + temp, err := template.ParseFiles("html/lastob.json") if err != nil { slog.Error(err.Error()) } - err=temp.ExecuteTemplate(res, "main", jsonelement) + err = temp.ExecuteTemplate(res, "main", jsonelement) if err != nil { slog.Error(err.Error()) } diff --git a/public/script.js b/public/script.js index a1ddf26..d3ff223 100644 --- a/public/script.js +++ b/public/script.js @@ -159,4 +159,9 @@ function updateForm(uri, eid){ } console.log(content) }) +} +function updateFormFields(...args){ + args.forEach(element => { + updateForm('/pkg', element) + }); } \ No newline at end of file