StarlioX/api/controllers/startup.go

67 lines
1.4 KiB
Go
Raw Permalink Normal View History

2022-09-25 11:05:14 +03:00
package controllers
import (
"github.com/gofiber/fiber/v2"
2022-09-25 11:05:14 +03:00
"os"
"os/user"
"strings"
2022-10-02 12:22:05 +03:00
2023-06-05 18:54:14 +03:00
"github.com/Redume/Starlio/utils"
2022-09-25 11:05:14 +03:00
)
var Startup = func(c *fiber.Ctx) error {
startup := c.FormValue("startup")
2022-10-02 12:22:05 +03:00
if startup == "" {
utils.Respond(c, utils.Message(false, "All fields are required."))
return nil
2022-10-02 12:22:05 +03:00
}
if startup == "1" {
SetStartup(c)
2022-10-02 12:22:05 +03:00
} else if startup == "0" {
RemoveStartup(c)
2022-10-02 12:22:05 +03:00
} else {
utils.Respond(c, utils.Message(false, "Invalid field."))
return nil
2022-10-02 12:22:05 +03:00
}
return nil
2022-10-02 12:22:05 +03:00
}
var SetStartup = func(c *fiber.Ctx) error {
2022-09-25 11:05:14 +03:00
u, err := user.Current()
if err != nil {
utils.Logger(err.Error())
2022-09-25 11:05:14 +03:00
}
dir, err := os.Getwd()
if err != nil {
utils.Logger(err.Error())
2022-09-25 11:05:14 +03:00
}
2023-06-05 18:54:14 +03:00
dir = strings.Replace(dir, "\\", "\\\\", -1) + "\\Starlio.exe"
2022-09-25 11:05:14 +03:00
2023-06-05 18:54:14 +03:00
err = utils.CreateLnk(dir, strings.Replace(u.HomeDir, "\\", "\\\\", -1)+"\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\Starlio.lnk")
2022-09-25 11:05:14 +03:00
if err != nil {
utils.Logger(err.Error())
2022-09-25 11:05:14 +03:00
}
utils.Respond(c, utils.Message(true, "The settings have been applied successfully."))
return nil
2022-09-25 11:05:14 +03:00
}
var RemoveStartup = func(c *fiber.Ctx) error {
2022-09-25 11:05:14 +03:00
u, err := user.Current()
if err != nil {
utils.Logger(err.Error())
2022-09-25 11:05:14 +03:00
}
2023-06-05 18:54:14 +03:00
err = os.Remove(strings.Replace(u.HomeDir, "\\", "\\\\", -1) + "\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\Starlio.lnk")
2022-09-25 11:05:14 +03:00
if err != nil {
utils.Logger(err.Error())
2022-09-25 11:05:14 +03:00
}
utils.Respond(c, utils.Message(true, "The settings have been applied successfully."))
return nil
2022-09-25 11:05:14 +03:00
}