Added creation of a shortcut on the desktop

This commit is contained in:
Данил 2022-10-02 12:19:00 +03:00
parent d0496c5fdf
commit 3f51438c24
3 changed files with 107 additions and 0 deletions

32
api/controllers/label.go Normal file
View file

@ -0,0 +1,32 @@
package controllers
import (
"net/http"
"os"
"os/user"
"strings"
"github.com/Redume/EveryNasa/api/utils"
"github.com/Redume/EveryNasa/functions"
)
var CreateLabel = func(w http.ResponseWriter, r *http.Request) {
u, err := user.Current()
if err != nil {
functions.Logger(err.Error())
}
dir, err := os.Getwd()
if err != nil {
functions.Logger(err.Error())
}
dir = strings.Replace(dir, "\\", "\\\\", -1) + "\\EveryNasa.exe"
err = functions.CreateLnk(dir, strings.Replace(u.HomeDir, "\\", "\\\\", -1)+"\\Desktop\\EveryNasa.lnk")
if err != nil {
functions.Logger(err.Error())
}
utils.Respond(w, utils.Message(true, "The shortcut was successfully created"))
}

55
functions/createLnk.go Normal file
View file

@ -0,0 +1,55 @@
package functions
import (
"os"
"github.com/go-ole/go-ole"
"github.com/go-ole/go-ole/oleutil"
)
func CreateLnk(src, dst string) error {
err := ole.CoInitializeEx(0, ole.COINIT_APARTMENTTHREADED|ole.COINIT_SPEED_OVER_MEMORY)
if err != nil {
Logger(err.Error())
}
oleShellObject, err := oleutil.CreateObject("WScript.Shell")
if err != nil {
Logger(err.Error())
}
defer oleShellObject.Release()
wshell, err := oleShellObject.QueryInterface(ole.IID_IDispatch)
if err != nil {
Logger(err.Error())
}
defer wshell.Release()
cs, err := oleutil.CallMethod(wshell, "CreateShortcut", dst)
if err != nil {
Logger(err.Error())
}
idispatch := cs.ToIDispatch()
_, err = oleutil.PutProperty(idispatch, "TargetPath", src)
if err != nil {
Logger(err.Error())
}
dir, err := os.Getwd()
if err != nil {
Logger(err.Error())
}
_, err = oleutil.PutProperty(idispatch, "WorkingDirectory", dir)
if err != nil {
Logger(err.Error())
}
_, err = oleutil.CallMethod(idispatch, "Save")
if err != nil {
Logger(err.Error())
}
return nil
}

View file

@ -0,0 +1,20 @@
let button = document.querySelector("#createLabelButton");
button.addEventListener("click", function() {
$.ajax({
url: "http://localhost:8080/api/create/label",
type: "POST",
success: function(data) {
if(data.status) {
$(".toast-body").text(data.message);
let toastLiveExample = document.getElementById('liveToast');
let toast = new bootstrap.Toast(toastLiveExample);
toast.show();
} else {
$(".toast-body").text("An error occurred while creating the label.");
let toastLiveExample = document.getElementById('liveToast');
let toast = new bootstrap.Toast(toastLiveExample);
toast.show();
}
}
});
});