autorun has been made more compact

This commit is contained in:
Данил 2022-10-02 12:22:05 +03:00
parent 7ba118f9b6
commit e23824367a

View file

@ -1,16 +1,32 @@
package controllers
import (
"github.com/Redume/EveryNasa/api/utils"
"github.com/Redume/EveryNasa/functions"
"github.com/go-ole/go-ole"
"github.com/go-ole/go-ole/oleutil"
"net/http"
"os"
"os/user"
"strings"
"github.com/Redume/EveryNasa/api/utils"
"github.com/Redume/EveryNasa/functions"
)
var Startup = func(w http.ResponseWriter, r *http.Request) {
startup := r.FormValue("startup")
if startup == "" {
utils.Respond(w, utils.Message(false, "All fields are required."))
return
}
if startup == "1" {
SetStartup(w, r)
} else if startup == "0" {
RemoveStartup(w, r)
} else {
utils.Respond(w, utils.Message(false, "Invalid field."))
return
}
}
var SetStartup = func(w http.ResponseWriter, r *http.Request) {
u, err := user.Current()
if err != nil {
@ -24,7 +40,7 @@ var SetStartup = func(w http.ResponseWriter, r *http.Request) {
dir = strings.Replace(dir, "\\", "\\\\", -1) + "\\EveryNasa.exe"
err = makeLnk(dir, strings.Replace(u.HomeDir, "\\", "\\\\", -1)+"\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\EveryNasa.lnk")
err = functions.CreateLnk(dir, strings.Replace(u.HomeDir, "\\", "\\\\", -1)+"\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\EveryNasa.lnk")
if err != nil {
functions.Logger(err.Error())
}
@ -45,50 +61,3 @@ var RemoveStartup = func(w http.ResponseWriter, r *http.Request) {
utils.Respond(w, utils.Message(true, "The settings have been applied successfully."))
}
func makeLnk(src, dst string) error {
err := ole.CoInitializeEx(0, ole.COINIT_APARTMENTTHREADED|ole.COINIT_SPEED_OVER_MEMORY)
if err != nil {
functions.Logger(err.Error())
}
oleShellObject, err := oleutil.CreateObject("WScript.Shell")
if err != nil {
functions.Logger(err.Error())
}
defer oleShellObject.Release()
wshell, err := oleShellObject.QueryInterface(ole.IID_IDispatch)
if err != nil {
functions.Logger(err.Error())
}
defer wshell.Release()
cs, err := oleutil.CallMethod(wshell, "CreateShortcut", dst)
if err != nil {
functions.Logger(err.Error())
}
idispatch := cs.ToIDispatch()
_, err = oleutil.PutProperty(idispatch, "TargetPath", src)
if err != nil {
functions.Logger(err.Error())
}
dir, err := os.Getwd()
if err != nil {
functions.Logger(err.Error())
}
_, err = oleutil.PutProperty(idispatch, "WorkingDirectory", dir)
if err != nil {
functions.Logger(err.Error())
}
_, err = oleutil.CallMethod(idispatch, "Save")
if err != nil {
functions.Logger(err.Error())
}
return nil
}