workign on the yamls

This commit is contained in:
2024-06-30 15:36:33 +03:00
parent 9a716d8b54
commit e430bfa01f
9 changed files with 143 additions and 35 deletions

View File

@@ -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}}

View File

@@ -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}}

View File

@@ -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}}

30
fileOrginize.go Normal file
View File

@@ -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())
}
}

View File

@@ -34,7 +34,7 @@
</div> </div>
<!-- <div> --> <!-- <div> -->
<form action="/upload" method="post" enctype="multipart/form-data"> <form action="/upload" method="post" enctype="multipart/form-data" onsubmit="">
<input type="text" name="filename" placeholder="Selected file" readonly> <input type="text" name="filename" placeholder="Selected file" readonly>
<input type="file" name="file" id="file" onchange="document.forms[0].filename.value = this.files[0].name"> <input type="file" name="file" id="file" onchange="document.forms[0].filename.value = this.files[0].name">
<button class="btn" type="submit">Upload</button> <button class="btn" type="submit">Upload</button>

View File

@@ -2,7 +2,7 @@
<div class="container"> <div class="container">
<h1>Upload a File</h1> <h1>Upload a File</h1>
<form action="/upload" method="post" enctype="multipart/form-data" target="noframe"> <form action="/upload" method="post" enctype="multipart/form-data" target="noframe" onsubmit="showContainer('form-container')">
<div class="upload-btn-wrapper"> <div class="upload-btn-wrapper">
<button class="btn">Upload a file</button> <button class="btn">Upload a file</button>
<input type="file" name="myFile" accept=".zip, .exe, .msi" /> <input type="file" name="myFile" accept=".zip, .exe, .msi" />
@@ -12,7 +12,7 @@
<button type="button" onclick="showContainer('form-container')" >fubarus</button> <button type="button" onclick="showContainer('form-container')" >fubarus</button>
</div> </div>
<div class="form-container" oncontextmenu="showContainer('form-container')" onclick="updateForm('/pkg')"> <div class="form-container" oncontextmenu="showContainer('form-container')" onclick="updateForm('/pkg')">
<form> <form action="/ren", method="put" enctype="multipart/form-data" id="packageProperties">
<div class="form-group"> <div class="form-group">
<label for="Vendor">Vendor</label> <label for="Vendor">Vendor</label>
<input type="text" id="Vendor" name="Vendor" onclick="updateForm('/pkg','Vendor')"> <input type="text" id="Vendor" name="Vendor" onclick="updateForm('/pkg','Vendor')">

28
main.go
View File

@@ -10,6 +10,7 @@ import (
"log/slog" "log/slog"
"net/http" "net/http"
"os" "os"
"regexp"
"strings" "strings"
"time" "time"
@@ -297,7 +298,7 @@ func upload() http.Handler {
func createPackageAndRename() http.Handler{ func createPackageAndRename() http.Handler{
return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request){ 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. // 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. // public serves static files from the "public" directory.
// Returns: An http.Handler that serves static files. // Returns: An http.Handler that serves static files.
func public() http.Handler { func public() http.Handler {
@@ -384,6 +409,7 @@ func main() {
mux.Handle("/up", uploadFileForServer(db,"eng")) mux.Handle("/up", uploadFileForServer(db,"eng"))
mux.Handle("/upload",upload()) mux.Handle("/upload",upload())
mux.Handle("/pkg",getJson(db)) mux.Handle("/pkg",getJson(db))
mux.Handle("/ren",renameForFile())
addr := fmt.Sprintf(":%s", "12312") addr := fmt.Sprintf(":%s", "12312")
server := http.Server{ server := http.Server{
Addr: addr, Addr: addr,

View File

@@ -3,17 +3,17 @@ package main
import ( import (
"crypto/sha256" "crypto/sha256"
"database/sql" "database/sql"
"encoding/hex"
"fmt" "fmt"
"html/template"
"log/slog" "log/slog"
"net/http" "net/http"
"os" "os"
"os/exec" "os/exec"
"encoding/hex"
"regexp" "regexp"
"runtime" "runtime"
"strconv" "strconv"
"strings" "strings"
"html/template"
_ "modernc.org/sqlite" _ "modernc.org/sqlite"
) )
@@ -38,6 +38,18 @@ type packages struct {
Warning bool Warning bool
Element *elements 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. // 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. // 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 { for key, val := range data {
switch key { switch key {
case "Author": case "Author":
return strings.ToLower(strings.Replace(val," ","",-1)) return strings.ToLower(strings.Replace(val, " ", "", -1))
case "Company Name": case "Company Name":
return strings.ToLower(strings.Replace(val," ","",-1)) return strings.ToLower(strings.Replace(val, " ", "", -1))
case "Manufacturer": case "Manufacturer":
return strings.ToLower(strings.Replace(val," ","",-1)) return strings.ToLower(strings.Replace(val, " ", "", -1))
case "Publisher": 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 { for key, val := range data {
switch key { switch key {
case "Title": case "Title":
return strings.ToLower(strings.Replace(val," ","",-1)) return strings.ToLower(strings.Replace(val, " ", "", -1))
case "Product Name": case "Product Name":
return strings.ToLower(strings.Replace(val," ","",-1)) return strings.ToLower(strings.Replace(val, " ", "", -1))
case "ProductName": case "ProductName":
return strings.ToLower(strings.Replace(val," ","",-1)) return strings.ToLower(strings.Replace(val, " ", "", -1))
case "Subject": case "Subject":
return strings.ToLower(strings.Replace(val," ","",-1)) return strings.ToLower(strings.Replace(val, " ", "", -1))
} }
} }
return "" return ""
} }
func getVersion(data map[string]string) string { func getVersion(data map[string]string) string {
replacer:=regexp.MustCompile(`[, ]+`) replacer := regexp.MustCompile(`[, ]+`)
for key, val := range data { for key, val := range data {
switch key { switch key {
case "Version": case "Version":
return replacer.ReplaceAllString(val,".") return replacer.ReplaceAllString(val, ".")
case "Product Version": case "Product Version":
return replacer.ReplaceAllString(val,".") return replacer.ReplaceAllString(val, ".")
case "ProductVersion": case "ProductVersion":
return replacer.ReplaceAllString(val,".") return replacer.ReplaceAllString(val, ".")
case "File Version": case "File Version":
return replacer.ReplaceAllString(val,".") return replacer.ReplaceAllString(val, ".")
case "FileVersion": case "FileVersion":
return replacer.ReplaceAllString(val,".") return replacer.ReplaceAllString(val, ".")
} }
} }
return "" return ""
@@ -198,7 +210,7 @@ func detectProblems(data map[string]string) bool {
if !err || (exe == "exe" && !(mime == "application/octet-stream")) { if !err || (exe == "exe" && !(mime == "application/octet-stream")) {
return false return false
} }
if (exe != "msi" && exe != "exe" && exe != "zip") { if exe != "msi" && exe != "exe" && exe != "zip" {
return false return false
} }
return true return true
@@ -232,14 +244,14 @@ func moveToNewLocationAndName(data map[string]string) string {
func createObject(object map[string]string) jsonStruct { func createObject(object map[string]string) jsonStruct {
if detectProblems(object) { if detectProblems(object) {
prop := jsonStruct{ prop := jsonStruct{
Vendor: getVendor(object) , Vendor: getVendor(object),
Package: getProgram(object), Package: getProgram(object),
Version: getVersion(object), Version: getVersion(object),
Type: getInstallerType(object), Type: getInstallerType(object),
} }
loc, _ := os.ReadFile(object["Directory"] + "/" + object["File Name"]) loc, _ := os.ReadFile(object["Directory"] + "/" + object["File Name"])
chksum:=sha256.Sum256(loc) chksum := sha256.Sum256(loc)
prop.Checksum = hex.EncodeToString(chksum[:]) prop.Checksum = hex.EncodeToString(chksum[:])
lc := moveToNewLocationAndName(object) lc := moveToNewLocationAndName(object)
prop.Url = os.Getenv("uri") + lc prop.Url = os.Getenv("uri") + lc
@@ -267,11 +279,11 @@ func getJson(sqldb *sql.DB) http.Handler {
if err != nil { if err != nil {
slog.Error(err.Error()) slog.Error(err.Error())
} }
temp,err:=template.ParseFiles("html/lastob.json") temp, err := template.ParseFiles("html/lastob.json")
if err != nil { if err != nil {
slog.Error(err.Error()) slog.Error(err.Error())
} }
err=temp.ExecuteTemplate(res, "main", jsonelement) err = temp.ExecuteTemplate(res, "main", jsonelement)
if err != nil { if err != nil {
slog.Error(err.Error()) slog.Error(err.Error())
} }

View File

@@ -160,3 +160,8 @@ function updateForm(uri, eid){
console.log(content) console.log(content)
}) })
} }
function updateFormFields(...args){
args.forEach(element => {
updateForm('/pkg', element)
});
}